Open main menu

Changes

Numpy linalg

1,809 bytes added, 09:22, 25 June 2014
<source lang="python">
from numpy.lib.stride_tricks import as_strided
from numpy import arange, asarray, int16, zerosfrom numpy as np.linalg import matrix_power NE, NS, NM, NO, ND, Row, Col = 1, 3, 2, 1, 4, 2, 2
NE, NS, NM, NO, ND, Row, Col = 1, 2, 2, 1, 2, 2, 2 mat = np.arange(NE*NS*NM*NO*ND*Row*Col).reshape(NE, NS, NM, NO, ND, Row, Col)power_arr = zeros([NE, NS, NM, NO, ND])
print "mat is:"
# Preform the power array, to be outside profiling test.
for ei in range(NE):
for si in range(NS):
for mi in range(NM):
for oi in range(NO):
power_arr[ei, si, mi, oi, :] = (mi+1)*arange(1, ND+1).astype(int)
 
# Convert to int
power_arr = power_arr.astype(int)
 
# Make array to store results
collected_power = zeros([NE, NS, NM, NO, ND, Row, Col])
 
# Normal way, by loop of loops.
for ei in range(NE):
for si in range(NS):
for oi in range(NO):
for di in range(ND):
power_i = power_arr[ei, si, mi, oi, di] print("ei:%i, si:%i, mi:%i, oi:%i, di:%i, power:%i"%(ei, si, mi, oi, di, power_i)) mat_i = mat[ei, si, mi, oi, di] #print mat_i  mat_power_i = matrix_power(mat_i, power_i) print mat_power_i # Store power collected_power[ei, si, mi, oi, di] = mat_power_i print(mat"Collected power shape is: %s")print collected_power.shapeprint collected_power[ei, si, mi, oi, di]  #### Make a stride function, that will make a view which is the chain of the outer Row x Col matrices. # Shape should end out in tuple.nr_of_mat = NE*NS*NM*NO*NDshape = tuple([nr_of_mat, Row, Col]) print "View of matrix has shape:", shape # Get itemsize, Length of one array element in bytes. Depends on dtype. float64=8, complex128=16.size mat_itz = mat.itemsizeprint "itemsize is:", mat_itz  # Get strides of original matricmat_str = list(mat.strides)print size"strides is", mat_str # Now we need to calculate how we move with the bytes in the original matrix, when we are creating a view with the# new matrix. It is easiest to start from the back of the list of strides. # bytes_between_elementsbbe = 1*mat_itz # bytes between row. The distance in bytes to next row is number of Columns elements multiplied with itemsize.bbr = Col * mat_itz # bytes bet 
strides a = matzeros([10, 2, 2, 2])print aprint a.stridesitemsizeprint "a.strides is"print asarray(a.strides)/a.itemsize
</source>