Pages

Monday, May 14, 2012

Mathcalc8 Basis : matrix calculation

Mathcalc8 focus more in list arithmetic, so we only provide 'essential' support for matrix calculation. In this post, we are going to do a lot of matrix calculation and first of all, we give a definition of matrix :

A p x q matrix A is defined as :

A = (a11,.....,a1j,.....,a1q)
    (.         .          .)
    (.         .          .)
    (ai1,.....,aij         .)
    (.                    .)
    (.                    .)
    (ap1,...............,apq)

where aij is the element of the matrix A at row ith and column jth

Matrix is treated as a list of list on Mathcalc8. This means that matrix A defined above is viewed as a list of p elements with each elment is itself a list with q elements. Therefore, create a matrix is the same as create a list which we should be very familiar now. As an example, we will show how to create a matrix (Note : Free version of Mathcalc8 do not support matrix calculation) :

Enter a 2 x 3 matrix a = (1, 2, 3)
                         (4, 5, 6) 
 





  • Input 1 : a=((1,2,3),(4,5,6))
    Ans. set a = 1, 2, 3, 4, 5, 6
     

  • We can see that a is defined as a list of two elements. (1, 2, 3) is the first element and (4, 5, 6) is the second element. Both elements are also a list. To show the first row of a :






  • Input 2 : a(0)
    Ans. 1, 2, 3
     

  • Mathcalc8 do not have a way to show individual element of a (input a(0)(1) do not work). So when we want to change one element of the matrix, say, change the first row, second column element from 2 to -1, we need to click the 'Dec' button to go back to Input 1 and make the change or we can use the following trick :






  • Input 3 : b=a(0)
    Ans. set b = 1, 2, 3
     
  • Input 4 : b(1)=-1
    Ans. set b = 1, -1, 3
     
  • Input 5 : a(0)=b
    Ans. set a(0) = 1, -1, 3
     
  • Input 6 : a
    Ans. 1, -1, 3, 4, 5, 6
     

  • We can see that b is used as an intermediate variable to make the change. Now, the original value of 2 in matrix a was changed to -1.

    Next, we show how to get the transpose of matrix a through the function 'trans' :






  • Input 7 : b=trans(a)
    Ans. set b = 1, 4, -1, 5, 3, 6
     

  • The first row of b is :





  • Input 8 : b(0)
    Ans. 1, 4
     

  • The second row of b is :





  • Input 9 : b(1)
    Ans. -1, 5
     

  • The third row of b is :





  • Input 10 : b(2)
    Ans. 3, 6
     

  • So, we can see that b is a 3 x 2 matrix.

    One precaution about matrix definition is on the definition of n x 1 matrix. For example, if we want to define a 3 x 1 matrix as follow :

    a = (1)
        (2)
        (3) 
    

    We should not define it by setting a=((1),(2),(3))! Instead, we need to define the matrix using the function 'trans' as follow :






  • Input 11 : a=trans((1,2,3))
    Ans. set a = 1, 2, 3
     
  • Input 12 : a(0)
    Ans. 1
     

  • Next, we would show how to do matrix arithmetic. They are similar to list arithmetic except that there is two kind of multiplication for matrix :
    1. Addition and Subtraction uses the same symbol '+' and '-' :

    Add (1, 2) + (5, 6)
        (3, 4)   (7, 8) 
     





  • Input 13 : ((1,2),(3,4))+((5,6),(7,8))
    Ans. 6, 8, 10, 12
     

  • Subtract (1, 2) - (5, 6)
             (3, 4)   (7, 8) 
     





  • Input 14 : ((1,2),(3,4))-((5,6),(7,8))
    Ans. -4, -4, -4, -4
     

  • Add 1 + (5, 6)
            (7, 8) 
     





  • Input 15 : 1+((5,6),(7,8))
    Ans. 6, 7, 8, 9
     

  • Subtract 1 - (5, 6)
                 (7, 8) 
     





  • Input 16 : 1-((5,6),(7,8))
    Ans. -4, -5, -6, -7
     

  • 2. Scale multiplication and division also uses same symbol '*' and '/' :

    Multiply 2 x (1, 2)
                 (3, 4) 
     





  • Input 17 : 2*((1,2),(3,4))
    Ans. 2, 4, 6, 8
     

  • Division  (1, 2) / 2
              (3, 4) 
     





  • Input 18 : ((1,2),(3,4))/2
    Ans. 0.5, 1, 1.5, 2
     

  • 3. Matrix-matrix multiplication, we need to use function 'mmult' for standard matrix multiply :

    Matrix Multiply (1, 2) x (5, 6)
                    (3, 4)   (7, 8) 
     





  • Input 19 : mmult(((1,2),(3,4)),((5,6),(7,8)))
    Ans. 19, 22, 43, 50
     

  • If we use '*' to do matrix multiplication, the multiplication would be done on the same position of elements between the two matrix :

    Multiply (1, 2) x (5, 6)
             (3, 4)   (7, 8) 
     





  • Input 20 : ((1,2),(3,4))*((5,6),(7,8))
    Ans. 5, 12, 21, 32
     

  • Matrix Multiply (2,-6) x (5)
                             (3) 
     





  • Input 21 : mmult((2,-6),trans((5,3)))
    Ans. -8
     

  • Matrix Multiply (5) x (2,-6)
                    (3) 
     





  • Input 22 : mmult(trans((5,3)),(2,-6))
    Ans. 10, -30, 6, -18
     

  • Matrix Multiply (2, 1) x (-1, 6)
                    (4, 3)   ( 3, 2)
                    (1, 2) 
     





  • Input 23 : mmult(((2,1),(4,3),(1,2)),((-1,6),(3,2)))
    Ans. 1, 14, 5, 30, 5, 10
     

  • Next, we define two matrix a and b as follow :

    a = (-2 , 4, 3+2i)        b = (5, 1,  -4)
        (1  , 6,   -7)            (1, 3, -3i) 
     





  • Input 24 : a=((-2,4,3+2*sqrt(-1)),(1,6,-7))
     
  • Input 25 : b=((5,1,-4),(1,3,-3*sqrt(-1)))
     

  • 1. Evaluate 2a - 3b :






  • Input 26 : 2*a-3*b
    Ans. 19, 5, 18+4i, -1, 3, -14+9i
     

  • 2. Evaluate abT. The symbol 'T' means 'transpose' :

    (-2 , 4, 3+2i) x (5,   1)
    (1  , 6,   -7)   (1,   3)
                     (-4,-3i) 
     





  • Input 27 : mmult(a,trans(b))
    Ans. -18-8i, 16-9i, 39, 19+21i
     

  • 3. Evaluate abH. The symbol 'H' means 'Hermitian transpose', (i.e. the matrix is being transposed and then take complex conjugate for all its elements). To take complex conjugate for a complex number means that we change the sign of the complex number's imaginary part with the real part kept fixed. In Mathcalc8, this is done by using the function 'conj' :

    (-2 , 4, 3+2i) x (5,   1)
    (1  , 6,   -7)   (1,   3)
                     (-4, 3i) 
     





  • Input 28 : mmult(a,conj(trans(b)))
    Ans. -18-8i, 4+9i, 39, 19-21i
     

  • 4. Evaluate aTb :
    (-2,   1) x (5, 1,  -4)
    (4,    6)   (1, 3, -3i)
    (3+2i,-7)
    





  • Input 29 : mmult(trans(a),b)
    Ans. -9, 1, 8-3i, 26, 22, -16-18i, 8+10i, -18+2i, -12+13i
     

  • 5. Evaluate aHb :

    (-2,   1) x (5, 1,  -4)
    (4,    6)   (1, 3, -3i)
    (3-2i,-7) 
     





  • Input 30 : mmult(conj(trans(a)),b)
    Ans. -9, 1, 8-3i, 26, 22, -16-18i, 8+10i, -18-2i, -12+29i
     

  • 6. Evaluate trace(abH). The trace of a matrix a is equal to the summation of all of its diagonal elements aii :






  • Input 31 : c=mmult(a,conj(trans(b)))
     
  • Input 32 : tr=0
    Ans. set tr = 0
     
  • Input 33 : table('s=c(x)|tr=tr+s(x)',id(2))
     
  • Input 34 : tr
    Ans. 1-29i
     

  • 7. Evaluate (abH)5 :






  • Input 35 : y=c
     
  • Input 36 : table('y=mmult(c,y)',id(4))
     
  • Input 37 : y
    Ans. 13935162-1400900i, -603116-5405403i, -2052971-6510816i, -7712021-734589i
     
  • No comments:

    Post a Comment

    Note: Only a member of this blog may post a comment.