tips-computer-programming-scipy

Tips and gotchas for people moving from Octave (or MATLAB) to Python+Scipy. I already know Python well, so i'll probably leave out many things that Python beginners might find surprising, although i'll include the ones i can think of.

a = array([1,2,3])
b = a
a
b
b[0] = 4
b
a

->

In [1]: a = array([1,2,3])

In [2]: b = a

In [3]: a
Out[3]: array([1, 2, 3])

In [4]: b
Out[4]: array([1, 2, 3])

In [5]: b[0] = 4

In [6]: b
Out[6]: array([4, 2, 3])

In [7]: a
Out[7]: array([4, 2, 3])

If you need to copy a matrix, use the "copy" method, i.e. a.copy():

a = array([1,2,3])
b = a.copy()
a
b
b[0] = 4
b
a

->

In [1]: a = array([1,2,3])

In [2]: b = a.copy()

In [3]: a
Out[3]: array([1, 2, 3])

In [4]: b
Out[4]: array([1, 2, 3])

In [5]: b[0] = 4

In [6]: b
Out[6]: array([4, 2, 3])

In [7]: a
Out[7]: array([1, 2, 3])
a = zeros((3,3))
a[1,1] = 3
max(a)
a.max()

->

In [1]: a = zeros((3,3))

In [2]: a[1,1] = 3

In [3]: max(a)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/bshanks/sync/<ipython console> in <module>()

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [4]: a.max()
Out[4]: 3.0