matrices/script.rst
changeset 457 68813d8d80fb
parent 404 3117f104c577
child 477 0406115dccd1
equal deleted inserted replaced
456:be96dc6c9743 457:68813d8d80fb
    28 .. Author              : Anoop Jacob Thomas <anoop@fossee.in>
    28 .. Author              : Anoop Jacob Thomas <anoop@fossee.in>
    29    Internal Reviewer   : 
    29    Internal Reviewer   : 
    30    External Reviewer   :
    30    External Reviewer   :
    31    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    31    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    32 
    32 
       
    33 .. #[punch: please mark the exercises, using the syntax we decided upon.]
    33 
    34 
    34 ========
    35 ========
    35 Matrices
    36 Matrices
    36 ========
    37 ========
    37 {{{ show the welcome slide }}}
    38 {{{ show the welcome slide }}}
    39 Welcome to the spoken tutorial on Matrices.
    40 Welcome to the spoken tutorial on Matrices.
    40 
    41 
    41 {{{ switch to next slide, outline slide }}}
    42 {{{ switch to next slide, outline slide }}}
    42 
    43 
    43 In this tutorial we will learn about matrices, creating matrices using
    44 In this tutorial we will learn about matrices, creating matrices using
    44 direct data, by converting a list, matrix operations. Finding inverse
    45 direct data, by converting a list and matrix operations. Finding
    45 of a matrix, determinant of a matrix, eigen values and eigen vectors
    46 inverse of a matrix, determinant of a matrix, eigen values and eigen
    46 of a matrix, norm and singular value decomposition of matrices.
    47 vectors of a matrix, norm and singular value decomposition of
       
    48 matrices.
    47 
    49 
    48 {{{ creating a matrix }}}
    50 {{{ creating a matrix }}}
    49 
    51 
    50 All matrix operations are done using arrays. Thus all the operations
    52 All matrix operations are done using arrays. Thus all the operations
    51 on arrays are valid on matrices also. A matrix may be created as,
    53 on arrays are valid on matrices also. A matrix may be created as,
    52 ::
    54 ::
    53 
    55 
    54     m1 = matrix([1,2,3,4])
    56     m1 = array([1,2,3,4])
    55 
    57 
    56 
    58 
    57 .. #[Puneeth: don't use ``matrix``. Use ``array``. The whole script will
    59 .. #[Puneeth: don't use ``matrix``. Use ``array``. The whole script will
    58 .. have to be fixed.]
    60 .. have to be fixed.]
    59 
    61 
    68 
    70 
    69 A list can be converted to a matrix as follows,
    71 A list can be converted to a matrix as follows,
    70 ::
    72 ::
    71 
    73 
    72     l1 = [[1,2,3,4],[5,6,7,8]]
    74     l1 = [[1,2,3,4],[5,6,7,8]]
    73     m2 = matrix(l1)
    75     m2 = array(l1)
    74 
    76 
    75 Note that all matrix operations are done using arrays, so a matrix may
    77 {{{ switch to next slide, exercise 1}}}
    76 also be created as
    78 
       
    79 Pause here and create a two dimensional matrix m3 of order 2 by 4 with
       
    80 elements 5, 6, 7, 8, 9, 10, 11, 12.
       
    81 
       
    82 {{{ switch to next slide, solution }}}
       
    83 
       
    84 m3 can be created as,
    77 ::
    85 ::
    78 
    86 
    79     m3 = array([[5,6,7,8],[9,10,11,12]])
    87     m3 = array([[5,6,7,8],[9,10,11,12]])
    80 
    88 
    81 {{{ switch to next slide, matrix operations }}}
    89 {{{ switch to next slide, matrix operations }}}
    98 {{{ Switch to next slide, Matrix multiplication }}}
   106 {{{ Switch to next slide, Matrix multiplication }}}
    99 ::
   107 ::
   100 
   108 
   101     m3 * m2
   109     m3 * m2
   102 
   110 
   103 Note that in arrays ``array(A) star array(B)`` does element wise
   111 Note that in arrays ``m3 * m2`` does element wise multiplication and not
   104 multiplication and not matrix multiplication, but unlike arrays, the
   112 matrix multiplication,
   105 operation ``matrix(A) star matrix(B)`` does matrix multiplication and
   113 
   106 not element wise multiplication. And in this case since the sizes are
   114 And matrix multiplication in matrices are done using the function ``dot()``
   107 not compatible for multiplication it returned an error.
   115 ::
   108 
   116 
   109 And element wise multiplication in matrices are done using the
   117     dot(m3, m2)
   110 function ``multiply()``
   118 
   111 ::
   119 but due to size mismatch the multiplication could not be done and it
   112 
   120 returned an error,
   113     multiply(m3,m2)
       
   114 
   121 
   115 {{{ switch to next slide, Matrix multiplication (cont'd) }}}
   122 {{{ switch to next slide, Matrix multiplication (cont'd) }}}
   116 
   123 
   117 Now let us see an example for matrix multiplication. For doing matrix
   124 Now let us see an example for matrix multiplication. For doing matrix
   118 multiplication we need to have two matrices of the order n by m and m
   125 multiplication we need to have two matrices of the order n by m and m
   124 
   131 
   125 matrix m1 is of the shape one by four, let us create another one of
   132 matrix m1 is of the shape one by four, let us create another one of
   126 the order four by two,
   133 the order four by two,
   127 ::
   134 ::
   128 
   135 
   129     m4 = matrix([[1,2],[3,4],[5,6],[7,8]])
   136     m4 = array([[1,2],[3,4],[5,6],[7,8]])
   130     m1 * m4
   137     dot(m1, m4)
   131 
   138 
   132 thus unlike in array object ``star`` can be used for matrix multiplication
   139 thus the function ``dot()`` can be used for matrix multiplication.
   133 in matrix object.
       
   134 
   140 
   135 {{{ switch to next slide, recall from arrays }}}
   141 {{{ switch to next slide, recall from arrays }}}
   136 
   142 
   137 As we already saw in arrays, the functions ``identity()`` which
   143 As we already saw in arrays, the functions ``identity()`` which
   138 creates an identity matrix of the order n by n, ``zeros()`` which
   144 creates an identity matrix of the order n by n, ``zeros()`` which
   152 
   158 
   153 Matrix name dot capital T will give the transpose of a matrix
   159 Matrix name dot capital T will give the transpose of a matrix
   154 
   160 
   155 {{{ switch to next slide, Frobenius norm of inverse of matrix }}}
   161 {{{ switch to next slide, Frobenius norm of inverse of matrix }}}
   156 
   162 
       
   163 .. #[punch: arange has not been introduced.]
       
   164 
   157 Now let us try to find out the Frobenius norm of inverse of a 4 by 4
   165 Now let us try to find out the Frobenius norm of inverse of a 4 by 4
   158 matrix, the matrix being,
   166 matrix, the matrix being,
   159 ::
   167 ::
   160 
   168 
   161     m5 = matrix(arange(1,17).reshape(4,4))
   169     m5 = arange(1,17).reshape(4,4)
   162     print m5
   170     print m5
   163 
   171 
   164 The inverse of a matrix A, A raise to minus one is also called the
   172 The inverse of a matrix A, A raise to minus one is also called the
   165 reciprocal matrix such that A multiplied by A inverse will give 1. The
   173 reciprocal matrix such that A multiplied by A inverse will give 1. The
   166 Frobenius norm of a matrix is defined as square root of sum of squares
   174 Frobenius norm of a matrix is defined as square root of sum of squares
   171 And here is the solution, first let us find the inverse of matrix m5.
   179 And here is the solution, first let us find the inverse of matrix m5.
   172 ::
   180 ::
   173 
   181 
   174     im5 = inv(m5)
   182     im5 = inv(m5)
   175 
   183 
       
   184 .. #[punch: we don't need to show this way of calculating the norm, do
       
   185 .. we? even if we do, we should show it in the "array style".
       
   186 .. something like:
       
   187 .. sqrt(sum(each * each))]
       
   188 
   176 And the Frobenius norm of the matrix ``im5`` can be found out as,
   189 And the Frobenius norm of the matrix ``im5`` can be found out as,
   177 ::
   190 ::
   178 
   191 
   179     sum = 0
   192     sum = 0
   180     for each in array(im5.flatten())[0]:
   193     for each in im5.flatten():
   181         sum += each * each
   194         sum += each * each
   182     print sqrt(sum)
   195     print sqrt(sum)
   183 
   196 
   184 {{{ switch to next slide, infinity norm }}}
   197 {{{ switch to next slide, infinity norm }}}
       
   198 .. #[punch: similarly for this section.]
   185 
   199 
   186 Now try to find out the infinity norm of the matrix im5. The infinity
   200 Now try to find out the infinity norm of the matrix im5. The infinity
   187 norm of a matrix is defined as the maximum value of sum of the
   201 norm of a matrix is defined as the maximum value of sum of the
   188 absolute of elements in each row. Pause here and try to solve the
   202 absolute of elements in each row. Pause here and try to solve the
   189 problem yourself.
   203 problem yourself.
   190 
   204 
       
   205 
   191 The solution for the problem is,
   206 The solution for the problem is,
   192 ::
   207 ::
   193 
   208 
   194     sum_rows = []
   209     sum_rows = []
   195     for i in im5:
   210     for i in im5:
   235 Let us find out the eigen values and eigen vectors of the matrix
   250 Let us find out the eigen values and eigen vectors of the matrix
   236 m5. We can do it as,
   251 m5. We can do it as,
   237 ::
   252 ::
   238 
   253 
   239     eig(m5)
   254     eig(m5)
       
   255 
       
   256 
       
   257 .. #[punch: has the tuple word been introduced?]
   240 
   258 
   241 Note that it returned a tuple of two matrices. The first element in
   259 Note that it returned a tuple of two matrices. The first element in
   242 the tuple are the eigen values and the second element in the tuple are
   260 the tuple are the eigen values and the second element in the tuple are
   243 the eigen vectors. Thus the eigen values are,
   261 the eigen vectors. Thus the eigen values are,
   244 ::
   262 ::
   294 .. 
   312 .. 
   295    Local Variables:
   313    Local Variables:
   296    mode: rst
   314    mode: rst
   297    indent-tabs-mode: nil
   315    indent-tabs-mode: nil
   298    sentence-end-double-space: nil
   316    sentence-end-double-space: nil
   299    fill-column: 75
   317    fill-column: 70
   300    End:
   318    End: