matrices/script.rst
changeset 394 1a79f9ee7f5c
parent 340 3951809e75bd
child 404 3117f104c577
equal deleted inserted replaced
393:f99254fc7d70 394:1a79f9ee7f5c
    49 
    49 
    50 All matrix operations are done using arrays. Thus all the operations
    50 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,
    51 on arrays are valid on matrices also. A matrix may be created as,
    52 ::
    52 ::
    53 
    53 
    54     m1 = matrix([1,2,3,4])
    54     m1 = array([1,2,3,4])
    55 
    55 
    56 
    56 
    57 .. #[Puneeth: don't use ``matrix``. Use ``array``. The whole script will
    57 .. #[Puneeth: don't use ``matrix``. Use ``array``. The whole script will
    58 .. have to be fixed.]
    58 .. have to be fixed.]
    59 
    59 
    68 
    68 
    69 A list can be converted to a matrix as follows,
    69 A list can be converted to a matrix as follows,
    70 ::
    70 ::
    71 
    71 
    72     l1 = [[1,2,3,4],[5,6,7,8]]
    72     l1 = [[1,2,3,4],[5,6,7,8]]
    73     m2 = matrix(l1)
    73     m2 = array(l1)
    74 
    74 
    75 Note that all matrix operations are done using arrays, so a matrix may
    75 {{{ switch to next slide, exercise 1}}}
    76 also be created as
    76 
       
    77 Pause here and create a two dimensional matrix m3 of order 2 by 4 with
       
    78 elements 5, 6, 7, 8, 9, 10, 11, 12.
       
    79 
       
    80 {{{ switch to next slide, solution }}}
       
    81 
       
    82 m3 can be created as,
    77 ::
    83 ::
    78 
    84 
    79     m3 = array([[5,6,7,8],[9,10,11,12]])
    85     m3 = array([[5,6,7,8],[9,10,11,12]])
    80 
    86 
    81 {{{ switch to next slide, matrix operations }}}
    87 {{{ switch to next slide, matrix operations }}}
    98 {{{ Switch to next slide, Matrix multiplication }}}
   104 {{{ Switch to next slide, Matrix multiplication }}}
    99 ::
   105 ::
   100 
   106 
   101     m3 * m2
   107     m3 * m2
   102 
   108 
   103 Note that in arrays ``array(A) star array(B)`` does element wise
   109 Note that in arrays ``m3 * m2`` does element wise multiplication and not
   104 multiplication and not matrix multiplication, but unlike arrays, the
   110 matrix multiplication,
   105 operation ``matrix(A) star matrix(B)`` does matrix multiplication and
   111 
   106 not element wise multiplication. And in this case since the sizes are
   112 And matrix multiplication in matrices are done using the function ``dot()``
   107 not compatible for multiplication it returned an error.
   113 ::
   108 
   114 
   109 And element wise multiplication in matrices are done using the
   115     dot(m3, m2)
   110 function ``multiply()``
   116 
   111 ::
   117 but due to size mismatch the multiplication could not be done and it
   112 
   118 returned an error,
   113     multiply(m3,m2)
       
   114 
   119 
   115 {{{ switch to next slide, Matrix multiplication (cont'd) }}}
   120 {{{ switch to next slide, Matrix multiplication (cont'd) }}}
   116 
   121 
   117 Now let us see an example for matrix multiplication. For doing matrix
   122 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
   123 multiplication we need to have two matrices of the order n by m and m
   124 
   129 
   125 matrix m1 is of the shape one by four, let us create another one of
   130 matrix m1 is of the shape one by four, let us create another one of
   126 the order four by two,
   131 the order four by two,
   127 ::
   132 ::
   128 
   133 
   129     m4 = matrix([[1,2],[3,4],[5,6],[7,8]])
   134     m4 = array([[1,2],[3,4],[5,6],[7,8]])
   130     m1 * m4
   135     dot(m1, m4)
   131 
   136 
   132 thus unlike in array object ``star`` can be used for matrix multiplication
   137 thus the function ``dot()`` can be used for matrix multiplication.
   133 in matrix object.
       
   134 
   138 
   135 {{{ switch to next slide, recall from arrays }}}
   139 {{{ switch to next slide, recall from arrays }}}
   136 
   140 
   137 As we already saw in arrays, the functions ``identity()`` which
   141 As we already saw in arrays, the functions ``identity()`` which
   138 creates an identity matrix of the order n by n, ``zeros()`` which
   142 creates an identity matrix of the order n by n, ``zeros()`` which
   156 
   160 
   157 Now let us try to find out the Frobenius norm of inverse of a 4 by 4
   161 Now let us try to find out the Frobenius norm of inverse of a 4 by 4
   158 matrix, the matrix being,
   162 matrix, the matrix being,
   159 ::
   163 ::
   160 
   164 
   161     m5 = matrix(arange(1,17).reshape(4,4))
   165     m5 = arange(1,17).reshape(4,4)
   162     print m5
   166     print m5
   163 
   167 
   164 The inverse of a matrix A, A raise to minus one is also called the
   168 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
   169 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
   170 Frobenius norm of a matrix is defined as square root of sum of squares
   175 
   179 
   176 And the Frobenius norm of the matrix ``im5`` can be found out as,
   180 And the Frobenius norm of the matrix ``im5`` can be found out as,
   177 ::
   181 ::
   178 
   182 
   179     sum = 0
   183     sum = 0
   180     for each in array(im5.flatten())[0]:
   184     for each in im5.flatten():
   181         sum += each * each
   185         sum += each * each
   182     print sqrt(sum)
   186     print sqrt(sum)
   183 
   187 
   184 {{{ switch to next slide, infinity norm }}}
   188 {{{ switch to next slide, infinity norm }}}
   185 
   189