Skip to content

Instantly share code, notes, and snippets.

@tpoisot
Created December 14, 2011 17:09
Show Gist options
  • Save tpoisot/1477488 to your computer and use it in GitHub Desktop.
Save tpoisot/1477488 to your computer and use it in GitHub Desktop.
Move along a 2D numpy array in weave/inline
import numpy as np
import scipy as sp
from scipy.weave import inline
def along2Darray(rows,cols):
arr = np.zeros((rows,cols))
code = """
int Pos;
for(int Row = 0; Row < rows; Row++)
{
for(int Col = 0; Col < cols; Col++)
{
Pos = Row*cols + Col;
arr[Pos] = Pos;
return_val = 1;
"""
res = inline(code, ['arr','cols','rows'], headers = ['<math.h>'], compiler = 'gcc')
return arr
newarr = along2Darray(10,10)
# Things to know
# 1 - Narr will have infos about the size of the array, so passing 'rows' and 'cols' is not necessary
# 2 - inline modifies the original array, so this is not a copy. Use np.copy if you want to keep the original array intact
# 3 - using the blitz library allows to of arr(Row,Col) instead of finding the position
@tpoisot
Copy link
Author

tpoisot commented Dec 14, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment