Tampilkan postingan dengan label determinants. Tampilkan semua postingan
Tampilkan postingan dengan label determinants. Tampilkan semua postingan

Matrices in Python without Numpy: Part 4

Matrices in Python without Numpy:  Part 4


We have a good set of tools through out the first three parts.  


For today's blog, let mat1 = [ [ 2, -3, 4 ], [ 1, 5, 7 ] [ 4, 8, -6 ] ]


Screenshots are made using an emulator on my.numworks.com. 



Row Multiply and Add


rowop(matrix, row1, scalar, row2)


Multiplies row1 by a scalar and adds the results to row2.  The matrix is permanently changed.


def rowop(M,r1,s,r2):

  c=len(M[0])

  MT=M

  for k in range (c):

    MT[r2][k]=MT[r1][k]*s+MT[r2][k]

  return MT





Upper Triangle Matrix


utri(matrix)


Changes the matrix to an upper triangular form.  All the elements below the diagonals are zero.  Due to the algorithm, row 0 cannot have a zero or an error occurs.  The algorithm is not perfect.  


def utri(M):

  MT=M

  c=len(M[0])

  for j in range(c-1):

    for k in range(j+1,c):

      rowop(MT,j,-MT[k][j]/MT[j][j],k)

  return MT





Determinant Using the Upper Triangle Matrix


det(matrix)


Calculates the determinant by first transforming the matrix into an upper triangle matrix.


def det(M):

  MT=utri(M)

  d=1

  for k in range(len(MT[0])):

    d*=MT[k][k]

  return d


Please be aware that floating point limitations of Python. 





The determinant should be -322. 



This concludes the series.  


Happy computing,


Eddie 



Source:


Ives, Thom.  "BASIC Linear Algebra Tools in Pure Python without Numpy or Scipy"  Integrated Machine Learning & AI  December 11, 2018.  https://integratedmlai.com/basic-linear-algebra-tools-in-pure-python-without-numpy-or-scipy/


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Matrices in Python without Numpy: Part 3

Matrices in Python without Numpy:  Part 3



Today's functions will focus on exact calculations for determinants and inverses for certain sizes:  2 x 2 and 3 x3.  


In the following examples:


mat2 = [ [ 11, 2 ] , [ -8, 4 ] ]

mat3 = [ [ 11, 2, 3 ], [ 4, 51, 6 ], [ 17, 8, 19 ] ]


Screenshots are made using an emulator on my.numworks.com. 



Determinants


det2(matrix):  determinant of a 2 x 2 matrix

det3(matrix):  determinant of a 3 x 3 matrix


def det2(M):

  if len(M)==2 and len(M[0])==2:

    return M[0][0]*M[1][1]-M[0][1]*M[1][0]

  else:

    return "not a 2 x 2 matrix"


def det3(M):

  if len(M)==3 and len(M[0])==3:

    m10=M[1][0]

    m20=M[2][0]

    m11=M[1][1]

    m21=M[2][1]

    m12=M[1][2]

    m22=M[2][2]

    t=M[0][0]*det2([[m11,m12],[m21,m22]])

    t-=M[0][1]*det2([[m10,m12],[m20,m22]])

    t+=M[0][2]*det2([[m10,m11],[m20,m21]])

    return t

  else:

    return "not a 3 x 3 matrix"



Inverses


inv2(matrix):  inverse of a 2 x 2 matrix

inv3(matrix):  inverse of a 3 x 3 matrix


def inv2(M):

  if len(M)==2 and len(M[0])==2:

    MT=newmatrix(2,2)

    t=det2(MT)

    MT[0][0]=M[0][0]*1/t

    MT[0][1]=-M[1][0]*1/t

    MT[1][0]=-M[0][1]*1/t

    MT[1][1]=M[1][1]*1/t

    return MT

  else:

    "not a 2 x 2 matrix"


def inv3(M):

  if len(M)==3 and len(M[0])==3:

    t=det3(M)

    A=newmatrix(3,3)

    A[0][0]=det2([[M[1][1],M[1][2]],[M[2][1],M[2][2]]])/t

    A[0][1]=det2([[M[0][2],M[0][1]],[M[2][2],M[2][1]]])/t

    A[0][2]=det2([[M[0][1],M[0][2]],[M[1][1],M[1][2]]])/t

    A[1][0]=det2([[M[1][2],M[1][0]],[M[2][2],M[2][0]]])/t

    A[1][1]=det2([[M[0][0],M[0][2]],[M[2][0],M[2][2]]])/t

    A[1][2]=det2([[M[0][2],M[0][0]],[M[1][2],M[1][0]]])/t

    A[2][0]=det2([[M[1][0],M[1][1]],[M[2][0],M[2][1]]])/t

    A[2][1]=det2([[M[0][1],M[0][0]],[M[2][1],M[2][0]]])/t

    A[2][2]=det2([[M[0][0],M[0][1]],[M[1][0],M[1][1]]])/t

    return A

  else:

    "not a 3 x 3 matrix"







The functions inv2, inv3, and det3 require det2.  I am using mprint to print matrices as they are supposed to appear.  Please see the blog post on October 3 for the code for mprint. 


Coming up in Part 4, row operations, upper triangular matrices, and determinants of matrices of any size. 



Happy computing,


Eddie 



Source:


Ives, Thom.  "BASIC Linear Algebra Tools in Pure Python without Numpy or Scipy"  Integrated Machine Learning & AI  December 11, 2018.  https://integratedmlai.com/basic-linear-algebra-tools-in-pure-python-without-numpy-or-scipy/


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 

 

Backlink 9999 Traffic Super

Order Now...!!!!