IonicWind Software

IWBasic => Object Oriented Programming => Topic started by: REDEBOLT on April 29, 2008, 10:35:44 PM

Title: Matrix Class
Post by: REDEBOLT on April 29, 2008, 10:35:44 PM
I am thinking about writing a matrix class.
I have noticed that some people have indicating
that they were doing the same.

Has anyone published such a class?

I have looked at the oop_array example, but it
is not the kind of thing I am looking for.
Title: Re: Matrix Class
Post by: John S on April 29, 2008, 11:52:44 PM
I worked on one in Aurora based on Paul's examples.
Title: Re: Matrix Class
Post by: REDEBOLT on May 02, 2008, 06:49:22 PM
Ok.  Here goes with my cut:

Quote
CLASS MAT
   PRIVATE
      int m_nr, m_nc      ' Number of rows, columns.
      pointer m_pa      ' Pointer to MAT.
      
   PUBLIC
      DECLARE MAT()   ' Constructor.
      DECLARE _MAT() ' Destructor.
      DECLARE MATINIT(int r, int c)                  'Defines this matrix a1()
      DECLARE MATCON(double d)                     'Set all elements of a1() to value of expr
      DECLARE MATCONONE()                           'Set all elements of a1() to one
      DECLARE MATIDN()                              'Establish a1() as an identity matrix
      DECLARE MATZER()                              'Set all elements of a1() to zero
      DECLARE MATADD(pointer pa2, pointer pa3)      'Addition             a1() = a2() + a3()
      DECLARE MATASG(pointer pa2)                  'Assignment          a1() = a2()
      DECLARE MATINV(pointer pa2)                  'Inversion             a1() = INV(a2())
      DECLARE MATScalarMult(double d, pointer pa2)   'Scalar Multiply       a1() = (expr) * a2()
      DECLARE MATSUBT(pointer pa2, pointer pa3)      'Subtraction          a1() = a2() + a3()
      DECLARE MATMULT(pointer pa2, pointer pa3)      'Multiplication       a1() = a2() + a3()
      DECLARE MATTRN(pointer pa2)                  'Transposition       a1() = TRN(a2())
      DECLARE GetAt(int row, int col),double               'Retrieve value at row, col
      DECLARE SetAt(int row, int col, value as double)   'Set value at row, col
ENDCLASS

SUB MAT::MAT()      ' Constructor.
ENDSUB

SUB MAT::_MAT()   ' Destructor.
   DELETE m_pa
ENDSUB

SUB MAT::MATINIT(int r, int c)
   m_nr = r
   m_nc = c
   m_pa = NEW(double, (r+1)*(c+1))
ENDSUB

SUB MAT::MATCON(double d)   'Set all elements of a1() to value of expr
   int i,j
   FOR i=1 TO m_nr+1
      FOR j=1 TO m_nc+1
         #m_pa[i,j]=d
      NEXT j
   NEXT i
ENDSUB

SUB MAT::MATCONONE()      'Set all elements of a1() to one
   MATCON(1.0)
ENDSUB

SUB MAT::MATZER()            'Set all elements of a1() to zero
   MATCON(0.0)
ENDSUB

SUB MAT::MATIDN()            'Establish a1() as an identity matrix
   int i
   MATZER()
   FOR i=1 TO m_nr+1
      #m_pa[i,i]=1.0
   NEXT i
ENDSUB

SUB MAT::MATADD(pointer pa2, pointer pa3)   'Addition a1() = a2() + a3()
   int i,j
   FOR i=1 TO m_nr+1
      FOR j=1 TO m_nc+1
         #<DOUBLE>m_pa[i,j]=#<DOUBLE>pa2[i,j]+#<DOUBLE>pa3[i,j]
      NEXT j
   NEXT i
ENDSUB

SUB MAT::MATASG(pointer pa2)   'Assignment   a1() = a2()
   int i,j
   FOR i=1 TO m_nr+1
      FOR j=1 TO m_nc+1
         #m_pa[i,j]=#<double>pa2[i,j]
      NEXT j
   NEXT i
ENDSUB

SUB MAT::MATINV(pointer pa2)   'Inversion a1() = INV(a2())
ENDSUB

SUB MAT::MATScalarMult(double d, pointer pa2)   'Scalar Multiply a1() = (expr) * a2()
   int i,j
   FOR i=1 TO m_nr+1
      FOR j=1 TO m_nc+1
'         #m_pa[i,j]=d*(pa2[i,j])
      NEXT j
   NEXT i
ENDSUB

SUB MAT::MATSUBT(pointer pa2, pointer pa3)      'Subtraction a1() = a2() + a3()
ENDSUB

SUB MAT::MATMULT(pointer pa2, pointer pa3)      'Multiplication a1() = a2() + a3()
ENDSUB

SUB MAT::MATTRN(pointer pa2)                     'Transposition   a1() = TRN(a2())
ENDSUB

SUB MAT::GetAt(int row, int col),double            'Retrieve value at row, col
   double value
   value=#<DOUBLE>m_pa[row,col]
   PRINT row,col,value
   return value
ENDSUB

SUB MAT::SetAt(int row, int col, value as double)   'Set value at row, col 
   PRINT row,col,value
   #<DOUBLE>m_pa[row,col]=value   
ENDSUB

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

OPENCONSOLE
   int i,j
   double value
   value=0.0
   MAT a1,a2,a3      ' Define three matrices.
   a1.MATINIT(2,3)
   a2.MATINIT(3,2)
   a3.MATINIT(2,2)
   FOR i=1 TO 2
      FOR j=1 TO 3
         value=value+1.0
         a1.SetAt(i,j,value)
      NEXT j
   NEXT i

PRINT

   FOR i=1 TO 2
      FOR j=1 TO 3
         PRINT a1.GetAt(i,j)
      NEXT j
      PRINT
   NEXT i

   DO:UNTIL INKEY$<>""
CLOSECONSOLE
END


My problem is that I don't know how to attach a pointer to a double subscripted array.