Finding a string in files and removing it

Removing #include<malloc.h> in all c files in a structure


In [18]:
%%bash
find . -name "*.c" | xargs sed -i bck "/#include<malloc\.h>/d"

In [20]:
%%bash 

cat ./isis/abs/allocate.cbck


#include<stdio.h>
#include<math.h>
#include<malloc.h>
#include<stdlib.h>


#include "types.h"

void allocate()
{
 int i,nc;


 i=deg_bg+2;

 nc=((i+1)*(i+2))/2;


 printf("nc: %i\n", nc);

 mat = (double **)malloc(nc*sizeof(double *));
 for(i=0;i<nc;i++)
  mat[i] = (double *)malloc(nc*sizeof(double));

 vec = (double *)malloc(nc*sizeof(double));
 lsvec = (double *)malloc(nc*sizeof(double));
 indx  = (int *)malloc(nc*sizeof(int));

}

Quick introduction to chocolate bars and classes


In [29]:
from astropy import units as u, constants as const

class SnickersBar(object):
    def __init__(self, w, h, l, weight, energy_density=2460 * u.kJ/ (100 * u.g)):
        self.w = u.Quantity(w, u.cm)
        self.h = u.Quantity(h, u.cm)
        self.l = u.Quantity(l, u.cm)
        self.weight = u.Quantity(weight, u.g)
        self.energy_density = u.Quantity(energy_density, u.kJ / u.g)
    
    def calculate_volume(self):
        return self.w * self.h * self.l
    
    @property
    def volume(self):
        return self.w * self.h * self.l

In [33]:
my_snickers_bar = SnickersBar(0.5, 0.5, 4, 0.01 * u.kg)
my_snickers_bar.w = 1 * u.cm

In [34]:
my_snickers_bar.volume


Out[34]:
$2 \; \mathrm{cm^{3}}$

Using cython


In [37]:
%load_ext Cython
import numpy as np


The Cython extension is already loaded. To reload it, use:
  %reload_ext Cython

In [67]:
import numexpr as ne
x1, y1 = np.random.random((2, 1000000))
x2, y2 = np.random.random((2, 1000000))

distance = []

def calculate_distances(x1, y1, x2, y2):
    distances = []
    for i in xrange(len(x1)):
        distances.append(np.sqrt((x1[i] - x2[i])**2 + (y1[i] - y2[i]**2)))
    return distances

def numpy_calculate_distances(x1, y1, x2, y2):
    return np.sqrt((x1 - x2)**2 + (y1-y2)**2)


def ne_calculate_distances(x1, y1, x2, y2):
    return ne.evaluate('sqrt((x1 - x2)**2 + (y1-y2)**2)')

#%timeit calculate_distances(x1, y1, x2, y2)
%timeit ne_calculate_distances(x1, y1, x2, y2)


100 loops, best of 3: 3.03 ms per loop

In [64]:
%%cython -a

import numpy as np
cimport numpy as np
import cython

cdef extern from "math.h":
    cpdef double sqrt(double x)

@cython.boundscheck(False)
def cython_calculate_distances(double [:] x1, double [:] y1, double [:] x2, double [:] y2):
    distances = np.empty(len(x1))
    cdef double [:] distances_view = distances
    cdef int i
    cdef int len_x1=len(x1)
    for i in xrange(len_x1):
        distances_view[i] = sqrt((x1[i] - x2[i])**2 + (y1[i] - y2[i]**2))
    return distances


Out[64]:
Cython: _cython_magic_e9229b1c69a6b31a2f06567772d7ecda.pyx

Generated by Cython 0.23.2

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

 01: 
+02: import numpy as np
  __pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
/* … */
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 2; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 03: cimport numpy as np
 04: import cython
 05: 
 06: cdef extern from "math.h":
 07:     cpdef double sqrt(double x)
 08: 
 09: @cython.boundscheck(False)
+10: def cython_calculate_distances(double [:] x1, double [:] y1, double [:] x2, double [:] y2):
/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_e9229b1c69a6b31a2f06567772d7ecda_1cython_calculate_distances(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyMethodDef __pyx_mdef_46_cython_magic_e9229b1c69a6b31a2f06567772d7ecda_1cython_calculate_distances = {"cython_calculate_distances", (PyCFunction)__pyx_pw_46_cython_magic_e9229b1c69a6b31a2f06567772d7ecda_1cython_calculate_distances, METH_VARARGS|METH_KEYWORDS, 0};
static PyObject *__pyx_pw_46_cython_magic_e9229b1c69a6b31a2f06567772d7ecda_1cython_calculate_distances(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  __Pyx_memviewslice __pyx_v_x1 = { 0, 0, { 0 }, { 0 }, { 0 } };
  __Pyx_memviewslice __pyx_v_y1 = { 0, 0, { 0 }, { 0 }, { 0 } };
  __Pyx_memviewslice __pyx_v_x2 = { 0, 0, { 0 }, { 0 }, { 0 } };
  __Pyx_memviewslice __pyx_v_y2 = { 0, 0, { 0 }, { 0 }, { 0 } };
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("cython_calculate_distances (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_x1,&__pyx_n_s_y1,&__pyx_n_s_x2,&__pyx_n_s_y2,0};
    PyObject* values[4] = {0,0,0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_x1)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_y1)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("cython_calculate_distances", 1, 4, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  2:
        if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_x2)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("cython_calculate_distances", 1, 4, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
        case  3:
        if (likely((values[3] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_y2)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("cython_calculate_distances", 1, 4, 4, 3); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "cython_calculate_distances") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 4) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
      values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
    }
    __pyx_v_x1 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[0]); if (unlikely(!__pyx_v_x1.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    __pyx_v_y1 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[1]); if (unlikely(!__pyx_v_y1.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    __pyx_v_x2 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[2]); if (unlikely(!__pyx_v_x2.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    __pyx_v_y2 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(values[3]); if (unlikely(!__pyx_v_y2.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("cython_calculate_distances", 1, 4, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("_cython_magic_e9229b1c69a6b31a2f06567772d7ecda.cython_calculate_distances", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  __pyx_r = __pyx_pf_46_cython_magic_e9229b1c69a6b31a2f06567772d7ecda_cython_calculate_distances(__pyx_self, __pyx_v_x1, __pyx_v_y1, __pyx_v_x2, __pyx_v_y2);
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;

  /* function exit code */
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_e9229b1c69a6b31a2f06567772d7ecda_cython_calculate_distances(CYTHON_UNUSED PyObject *__pyx_self, __Pyx_memviewslice __pyx_v_x1, __Pyx_memviewslice __pyx_v_y1, __Pyx_memviewslice __pyx_v_x2, __Pyx_memviewslice __pyx_v_y2) {
  PyObject *__pyx_v_distances = NULL;
  __Pyx_memviewslice __pyx_v_distances_view = { 0, 0, { 0 }, { 0 }, { 0 } };
  int __pyx_v_i;
  int __pyx_v_len_x1;
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("cython_calculate_distances", 0);
/* … */
  /* function exit code */
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_XDECREF(__pyx_t_2);
  __Pyx_XDECREF(__pyx_t_3);
  __Pyx_XDECREF(__pyx_t_5);
  __Pyx_XDECREF(__pyx_t_6);
  __PYX_XDEC_MEMVIEW(&__pyx_t_7, 1);
  __Pyx_AddTraceback("_cython_magic_e9229b1c69a6b31a2f06567772d7ecda.cython_calculate_distances", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XDECREF(__pyx_v_distances);
  __PYX_XDEC_MEMVIEW(&__pyx_v_distances_view, 1);
  __PYX_XDEC_MEMVIEW(&__pyx_v_x1, 1);
  __PYX_XDEC_MEMVIEW(&__pyx_v_y1, 1);
  __PYX_XDEC_MEMVIEW(&__pyx_v_x2, 1);
  __PYX_XDEC_MEMVIEW(&__pyx_v_y2, 1);
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}
/* … */
  __pyx_tuple__22 = PyTuple_Pack(8, __pyx_n_s_x1, __pyx_n_s_y1, __pyx_n_s_x2, __pyx_n_s_y2, __pyx_n_s_distances, __pyx_n_s_distances_view, __pyx_n_s_i, __pyx_n_s_len_x1); if (unlikely(!__pyx_tuple__22)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_tuple__22);
  __Pyx_GIVEREF(__pyx_tuple__22);
/* … */
  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_e9229b1c69a6b31a2f06567772d7ecda_1cython_calculate_distances, NULL, __pyx_n_s_cython_magic_e9229b1c69a6b31a2f); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_cython_calculate_distances, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_codeobj__23 = (PyObject*)__Pyx_PyCode_New(4, 0, 8, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__22, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_wkerzend_ipython_cython, __pyx_n_s_cython_calculate_distances, 10, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__23)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+11:     distances = np.empty(len(x1))
  __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_empty); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_3);
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = __pyx_memoryview_fromslice(__pyx_v_x1, 1, (PyObject *(*)(char *)) __pyx_memview_get_double, (int (*)(char *, PyObject *)) __pyx_memview_set_double, 0);; if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_4 = PyObject_Length(__pyx_t_2); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __pyx_t_2 = PyInt_FromSsize_t(__pyx_t_4); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_2);
  __pyx_t_5 = NULL;
  if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_3))) {
    __pyx_t_5 = PyMethod_GET_SELF(__pyx_t_3);
    if (likely(__pyx_t_5)) {
      PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);
      __Pyx_INCREF(__pyx_t_5);
      __Pyx_INCREF(function);
      __Pyx_DECREF_SET(__pyx_t_3, function);
    }
  }
  if (!__pyx_t_5) {
    __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_t_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
    __Pyx_GOTREF(__pyx_t_1);
  } else {
    __pyx_t_6 = PyTuple_New(1+1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_6);
    __Pyx_GIVEREF(__pyx_t_5); PyTuple_SET_ITEM(__pyx_t_6, 0, __pyx_t_5); __pyx_t_5 = NULL;
    __Pyx_GIVEREF(__pyx_t_2);
    PyTuple_SET_ITEM(__pyx_t_6, 0+1, __pyx_t_2);
    __pyx_t_2 = 0;
    __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_6, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
    __Pyx_GOTREF(__pyx_t_1);
    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
  }
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __pyx_v_distances = __pyx_t_1;
  __pyx_t_1 = 0;
+12:     cdef double [:] distances_view = distances
  __pyx_t_7 = __Pyx_PyObject_to_MemoryviewSlice_ds_double(__pyx_v_distances);
  if (unlikely(!__pyx_t_7.memview)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_v_distances_view = __pyx_t_7;
  __pyx_t_7.memview = NULL;
  __pyx_t_7.data = NULL;
 13:     cdef int i
+14:     cdef int len_x1=len(x1)
  __pyx_t_1 = __pyx_memoryview_fromslice(__pyx_v_x1, 1, (PyObject *(*)(char *)) __pyx_memview_get_double, (int (*)(char *, PyObject *)) __pyx_memview_set_double, 0);; if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_t_4 = PyObject_Length(__pyx_t_1); if (unlikely(__pyx_t_4 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 14; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
  __pyx_v_len_x1 = __pyx_t_4;
+15:     for i in xrange(len_x1):
  __pyx_t_8 = __pyx_v_len_x1;
  for (__pyx_t_9 = 0; __pyx_t_9 < __pyx_t_8; __pyx_t_9+=1) {
    __pyx_v_i = __pyx_t_9;
+16:         distances_view[i] = sqrt((x1[i] - x2[i])**2 + (y1[i] - y2[i]**2))
    __pyx_t_10 = __pyx_v_i;
    if (__pyx_t_10 < 0) __pyx_t_10 += __pyx_v_x1.shape[0];
    __pyx_t_11 = __pyx_v_i;
    if (__pyx_t_11 < 0) __pyx_t_11 += __pyx_v_x2.shape[0];
    __pyx_t_12 = __pyx_v_i;
    if (__pyx_t_12 < 0) __pyx_t_12 += __pyx_v_y1.shape[0];
    __pyx_t_13 = __pyx_v_i;
    if (__pyx_t_13 < 0) __pyx_t_13 += __pyx_v_y2.shape[0];
    __pyx_t_14 = __pyx_v_i;
    if (__pyx_t_14 < 0) __pyx_t_14 += __pyx_v_distances_view.shape[0];
    *((double *) ( /* dim=0 */ (__pyx_v_distances_view.data + __pyx_t_14 * __pyx_v_distances_view.strides[0]) )) = sqrt((pow(((*((double *) ( /* dim=0 */ (__pyx_v_x1.data + __pyx_t_10 * __pyx_v_x1.strides[0]) ))) - (*((double *) ( /* dim=0 */ (__pyx_v_x2.data + __pyx_t_11 * __pyx_v_x2.strides[0]) )))), 2.0) + ((*((double *) ( /* dim=0 */ (__pyx_v_y1.data + __pyx_t_12 * __pyx_v_y1.strides[0]) ))) - pow((*((double *) ( /* dim=0 */ (__pyx_v_y2.data + __pyx_t_13 * __pyx_v_y2.strides[0]) ))), 2.0))));
  }
+17:     return distances
  __Pyx_XDECREF(__pyx_r);
  __Pyx_INCREF(__pyx_v_distances);
  __pyx_r = __pyx_v_distances;
  goto __pyx_L0;

In [ ]: