matrices/script.rst
changeset 261 c7f0069d698a
child 287 d9507624eb8f
equal deleted inserted replaced
260:25b4e962b55e 261:c7f0069d698a
       
     1 .. 4.3 LO: Matrices (3) [anoop] 
       
     2 .. -----------------------------
       
     3 .. * creating matrices 
       
     4 ..   + direct data 
       
     5 ..   + list conversion 
       
     6 ..   + builtins - identitiy, zeros, 
       
     7 .. * matrix operations 
       
     8 ..   + + - * / 
       
     9 ..   + dot 
       
    10 ..   + inv 
       
    11 ..   + det 
       
    12 ..   + eig 
       
    13 ..   + norm 
       
    14 ..   + svd 
       
    15 
       
    16 ========
       
    17 Matrices
       
    18 ========
       
    19 {{{ show the welcome slide }}}
       
    20 
       
    21 Welcome to the spoken tutorial on Matrices.
       
    22 
       
    23 {{{ switch to next slide, outline slide }}}
       
    24 
       
    25 In this tutorial we will learn about matrices, creating matrices and
       
    26 matrix operations.
       
    27 
       
    28 {{{ creating a matrix }}}
       
    29 
       
    30 All matrix operations are done using arrays. Thus all the operations
       
    31 on arrays are valid on matrices also. A matrix may be created as,
       
    32 ::
       
    33 
       
    34     m1 = matrix([1,2,3,4])
       
    35 
       
    36 Using the tuple ``m1.shape`` we can find out the shape or size of the
       
    37 matrix,
       
    38 ::
       
    39 
       
    40     m1.shape
       
    41 
       
    42 Since it is a one row four column matrix it returned a tuple, one by
       
    43 four.
       
    44 
       
    45 A list can be converted to a matrix as follows,
       
    46 ::
       
    47 
       
    48     l1 = [[1,2,3,4],[5,6,7,8]]
       
    49     m2 = matrix(l1)
       
    50 
       
    51 Note that all matrix operations are done using arrays, so a matrix may
       
    52 also be created as
       
    53 ::
       
    54 
       
    55     m3 = array([[5,6,7,8],[9,10,11,12]])
       
    56 
       
    57 {{{ switch to next slide, matrix operations }}}
       
    58 
       
    59 We can do matrix addition and subtraction as,
       
    60 ::
       
    61 
       
    62     m3 + m2
       
    63 
       
    64 does element by element addition, thus matrix addition.
       
    65 
       
    66 Similarly,
       
    67 ::
       
    68 
       
    69     m3 - m2
       
    70 
       
    71 it does matrix subtraction, that is element by element
       
    72 subtraction. Now let us try,
       
    73 ::
       
    74 
       
    75     m3 * m2
       
    76 
       
    77 Note that in arrays ``array(A) star array(B)`` does element wise
       
    78 multiplication and not matrix multiplication, but unlike arrays, the
       
    79 operation ``matrix(A) star matrix(B)`` does matrix multiplication and
       
    80 not element wise multiplication. And in this case since the sizes are
       
    81 not compatible for multiplication it returned an error.
       
    82 
       
    83 And element wise multiplication in matrices are done using the
       
    84 function ``multiply()``
       
    85 ::
       
    86 
       
    87     multiply(m3,m2)
       
    88 
       
    89 Now let us see an example for matrix multiplication. For doing matrix
       
    90 multiplication we need to have two matrices of the order n by m and m
       
    91 by r and the resulting matrix will be of the order n by r. Thus let us
       
    92 first create two matrices which are compatible for multiplication.
       
    93 ::
       
    94 
       
    95     m1.shape
       
    96 
       
    97 matrix m1 is of the shape one by four, let us create another one of
       
    98 the order four by two,
       
    99 ::
       
   100 
       
   101     m4 = matrix([[1,2],[3,4],[5,6],[7,8]])
       
   102     m1 * m4
       
   103 
       
   104 thus unlike in array object ``star`` can be used for matrix multiplication
       
   105 in matrix object.
       
   106 
       
   107 {{{ switch to next slide, recall from arrays }}}
       
   108 
       
   109 As we already saw in arrays, the functions ``identity()``,
       
   110 ``zeros()``, ``zeros_like()``, ``ones()``, ``ones_like()`` may also be
       
   111 used with matrices.
       
   112 
       
   113 {{{ switch to next slide, matrix operations }}}
       
   114 
       
   115 To find out the transpose of a matrix we can do,
       
   116 ::
       
   117 
       
   118     print m4
       
   119     m4.T
       
   120 
       
   121 Matrix name dot capital T will give the transpose of a matrix
       
   122 
       
   123 {{{ switch to next slide, Euclidean norm of inverse of matrix }}}
       
   124 
       
   125 Now let us try to find out the Euclidean norm of inverse of a 4 by 4
       
   126 matrix, the matrix being,
       
   127 ::
       
   128 
       
   129     m5 = matrix(arange(1,17).reshape(4,4))
       
   130     print m5
       
   131 
       
   132 The inverse of a matrix A, A raise to minus one is also called the
       
   133 reciprocal matrix such that A multiplied by A inverse will give 1. The
       
   134 Euclidean norm or the Frobenius norm of a matrix is defined as square
       
   135 root of sum of squares of elements in the matrix. Pause here and try
       
   136 to solve the problem yourself, the inverse of a matrix can be found
       
   137 using the function ``inv(A)``.
       
   138 
       
   139 And here is the solution, first let us find the inverse of matrix m5.
       
   140 ::
       
   141 
       
   142     im5 = inv(m5)
       
   143 
       
   144 And the euclidean norm of the matrix ``im5`` can be found out as,
       
   145 ::
       
   146 
       
   147     sum = 0
       
   148     for each in array(im5.flatten())[0]:
       
   149         sum += each * each
       
   150     print sqrt(sum)
       
   151 
       
   152 {{{ switch to next slide, infinity norm }}}
       
   153 
       
   154 Now try to find out the infinity norm of the matrix im5. The infinity
       
   155 norm of a matrix is defined as the maximum value of sum of the
       
   156 absolute of elements in each row. Pause here and try to solve the
       
   157 problem yourself.
       
   158 
       
   159 The solution for the problem is,
       
   160 ::
       
   161 
       
   162     sum_rows = []
       
   163     for i in im5:
       
   164         sum_rows.append(abs(i).sum())
       
   165     print max(sum_rows)
       
   166 
       
   167 {{{ switch to slide the ``norm()`` method }}}
       
   168 
       
   169 Well! to find the Euclidean norm and Infinity norm we have an even easier
       
   170 method, and let us see that now.
       
   171 
       
   172 The norm of a matrix can be found out using the method
       
   173 ``norm()``. Inorder to find out the Euclidean norm of the matrix im5,
       
   174 we do,
       
   175 ::
       
   176 
       
   177     norm(im5)
       
   178 
       
   179 And to find out the Infinity norm of the matrix im5, we do,
       
   180 ::
       
   181 
       
   182     norm(im5,ord=inf)
       
   183 
       
   184 This is easier when compared to the code we wrote. Do ``norm``
       
   185 question mark to read up more about ord and the possible type of norms
       
   186 the norm function produces.
       
   187 
       
   188 {{{ switch to next slide, determinant }}}
       
   189 
       
   190 Now let us find out the determinant of a the matrix m5. 
       
   191 
       
   192 The determinant of a square matrix can be obtained using the function
       
   193 ``det()`` and the determinant of m5 can be found out as,
       
   194 ::
       
   195 
       
   196     det(m5)
       
   197 
       
   198 {{{ switch to next slide, eigen vectors and eigen values }}}
       
   199 
       
   200 The eigen values and eigen vector of a square matrix can be computed
       
   201 using the function ``eig()`` and ``eigvals()``.
       
   202 
       
   203 Let us find out the eigen values and eigen vectors of the matrix
       
   204 m5. We can do it as,
       
   205 ::
       
   206 
       
   207     eig(m5)
       
   208 
       
   209 Note that it returned a tuple of two matrices. The first element in
       
   210 the tuple are the eigen values and the second element in the tuple are
       
   211 the eigen vectors. Thus the eigen values are,
       
   212 ::
       
   213 
       
   214     eig(m5)[0]
       
   215 
       
   216 and the eigen vectors are,
       
   217 ::
       
   218 
       
   219     eig(m5)[1]
       
   220 
       
   221 The eigen values can also be computed using the function ``eigvals()`` as,
       
   222 ::
       
   223 
       
   224     eigvals(m5)
       
   225 
       
   226 {{{ switch to next slide, singular value decomposition }}}
       
   227 
       
   228 Now let us learn how to do the singular value decomposition or S V D
       
   229 of a matrix.
       
   230 
       
   231 Suppose M is an m×n matrix whose entries come from the field K, which
       
   232 is either the field of real numbers or the field of complex
       
   233 numbers. Then there exists a factorization of the form
       
   234 
       
   235     M = U\Sigma V star
       
   236 
       
   237 where U is an (m by m) unitary matrix over K, the matrix \Sigma is an
       
   238 (m by n) diagonal matrix with nonnegative real numbers on the
       
   239 diagonal, and V*, an (n by n) unitary matrix over K, denotes the
       
   240 conjugate transpose of V. Such a factorization is called the
       
   241 singular-value decomposition of M.
       
   242 
       
   243 The SVD of matrix m5 can be found as
       
   244 ::
       
   245 
       
   246     svd(m5)
       
   247 
       
   248 Notice that it returned a tuple of 3 elements. The first one U the
       
   249 next one Sigma and the third one V star.
       
   250     
       
   251 {{{ switch to next slide, recap slide }}}
       
   252 
       
   253 So this brings us to the end of this tutorial. In this tutorial, we
       
   254 learned about matrices, creating matrices, matrix operations, inverse
       
   255 of matrices, determinant, norm, eigen values and vectors and singular
       
   256 value decomposition of matrices.
       
   257 
       
   258 {{{ switch to next slide, thank you }}}
       
   259 
       
   260 Thank you!
       
   261 
       
   262 ..  Author: Anoop Jacob Thomas <anoop@fossee.in>
       
   263     Reviewer 1:
       
   264     Reviewer 2:
       
   265     External reviewer: