Python:Various

From GISAXS
Revision as of 16:06, 15 October 2014 by KevinYager (talk | contribs) (Created page with "This page collects some notes/hints about the use of Python. ==Matrix== ===Multiply matrix/array/grid by vector=== <source lang="python"> #!/usr/bin/python import numpy as np...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page collects some notes/hints about the use of Python.

Matrix

Multiply matrix/array/grid by vector

#!/usr/bin/python
import numpy as np

# 2D example
size = 11
extent = 1.0
#axis_x = np.linspace( -extent, +extent, size )
#axis_y = np.linspace( -extent, +extent, size )
X, Y = np.meshgrid( axis_x, axis_y ) # Example 2D arrays

v = np.asarray( [ np.linspace( 0, 1, size ) ] )

print X*v               # Multiplies across row (x-direction)
print X*v.transpose()   # Multiplies down columns (y-direction)


# 3D example
size = 3
extent = 1.0
X, Y, Z = np.mgrid[ -extent:+extent:size*1j , -extent:+extent:size*1j , -extent:+extent:size*1j ] # Example 3D arrays

# Example vectors we want to multiply with
u = np.linspace( 1, 2, size ).reshape(size,1,1) # Multiplies down layers (z-direction)
v = np.linspace( 1, 2, size ).reshape(1,size,1) # Multiplies down column (y-direction)
w = np.linspace( 1, 2, size ).reshape(1,1,size) # Multiplies across row (x-direction)


print X
print '--'
print u
print X*u
print '--'
print v
print X*v
print '--'
print w
print X*w    

See Also