This will raise an error if the matrix is singular or not square. To invert of a square matrix, we need the linear algebra numpy submodule numpy.linalg. Inversing a non-singular square matrix, the linear algebra submodule > print sp.transpose(n) # returns the transpose of n > print sp.trace(n) # returns the trace or the sum of the diagonal elements > sp.diag((1, 2, 3)) # returns a diagonal matrix with entries 1,2,3
> print sp.diagonal(n) # returns the elements on the diagonal
> print sp.multiply(m,n) # component-wise multiplication, equal to m*n > print sp.dot(m,a) # matrix vector multiplication > print sp.dot(m,n) # matrix multiplications > print sp.dot(a,b) # dot product = inner product=scalar product > import scipy as sp # all these operations are valid with numpy too > a > np.where(a> np.where(conditions, x, y) # returns x when condition is True and y otherwise > sp.sum(a) # return the sum of the elements of a > d = sp.exp(b) # element by element exponentiation > print a + b # vectors addition (vector must have same dimension) > print 2*a # component-wise multiplication Scipy (or numpy) provides element-wise operations on arrays called vectorization, in particular arithmetic operations +, -, *, /, //, ** are applied element by element to an array. > np.eyes(3) # also return the 3x3 identity matrix > np.identity(3) # return the 3x3 identity matrix > a = b # assigning b to the first column
> a #yields column at index 1 to column at index 3, increment 3 > a # yields row at index 0 to row at index 2, increment 2 > a # returns elements from index 2 to index 6, increment 2 > a = np.array(,], dtype = float) # a 3X3 matrix of floats > M # returns element at row i and column j > np.shape(M) # returns the dimension of the matrix > M = np.array(,]) # creates a 2x2 matrix > import numpy as np #import numpy and name it np Matrices are created in python using numpy (or scipy) arrays > for i in a: # traversing a numpy 1D arrayĬreating numpy (or scipy) multi-dimensional arrays > a = 'first' # replacing element at index 0 by 'first'Īrray() > a # returns elements from index 2 to index 6, taking only the 2nd elements > a # returns elements from index 2 to index 4 > import numpy as np # import numpy and name it np The elements of a numpy 1D array are accessible using the same operation we have seen with lists data type. > u = numpy.ones(5) # return a vector of dimension 5 filled with 1 > t = numpy.zeros(6) # return the null vector of dimension 6 > x = numpy.arange(5, 21, 4) # yields all the integers from 5 to 20, taking only the 4th > numpy.arange(3, 9) # yields all the integers from 3 to 8 > numpy.arange(7) # yields the first 7 integers Remember to use the floating point division instead of integer divisison. > x = numpy.linspace(1, 4, 10) # create 10 evenly spaced points between 1 and 4 > numpy.array(, dtype = float) # you can specify the data type > c = numpy.array() # numpy list comprehension > b = numpy.array() # a mixed numpy array > a = numpy.array() # a numpy array of integers To list all the functions available in number, typeĬreating numpy (or scipy) one dimensional (1D) arrays we will be using both of them indifferently. The scipy and the numpy (numerical python) modules are very similar although scipy is more powerful. To see the list of the function available in scipy, first import scipy. It provides a variety of functions and utilities for performing matrix operations, integration, optimizations, solving differential equations, plotting graphs and much more. Scipy (scientific python) is the python module for computational sciences.