Mintoris Forum

Author Topic: Simple Matrix functions  (Read 3217 times)

Laszlo

  • Full Member
  • ***
  • Posts: 34
Simple Matrix functions
« on: Feb 01, 2011, 07:44 PM »
With the current limitation that a function returning an array cannot be directly used as a parameter for another function, matrix functions have to be used with assignments to temporary arrays. As my learning exercise I figured out how a one dimensional initializing list sets up 2 dimensional arrays, and wrote 4 very simple functions (and test cases) for 2 dimensional numeric arrays (matrices): MATtranspose(Z()) to flip matrix Z() over its main diagonal; MATrow(Z(),r) to extract row r; MATcol(Z(),c) to extract column c; and MATprint(Z()) to Print matrix in tabular form. They would help with writing and testing more complex functions.

' Matrix functions
'   by Laszlo Hars, January 31, 2011

' test matrices -------------------------

Dim X(2,2) = 1,0,0, 1,1,0, 1,1,1        ' stored as row1(col1 col2…), row2, row3…
Dim Y(2,2) = 1,2,3, 4,5,6, 7,8,0
Dim Z(2,3) = 1,2,3, 4,5,6, 7,8,0, -1,1,-2
Dim V0(2)  = 1,2,3                      ' row vector
Dim V(2,0) = 1,2,3                      ' row vector as matrix
Dim W(0,3) = 1,2,3,4                    ' column vector matrix
Dim W3(2,2,2)
Dim Q(0)

' testing the functions -----------------

MATprint(V0())
MATprint(V())
MATprint(W())
MATprint(W3())
MATprint(Z())

Q() = MATrow(Z(),0)
MATprint(Q())
Q() = MATrow(Z(),3)
MATprint(Q())
Q() = MATcol(Z(),1)
MATprint(Q())
Q() = MATcol(Z(),2)
MATprint(Q())
Q() = MATcol(Z(),3)
MATprint(Q())
Q() = MATtranspose(Z())
MATprint(Q())

' The MATRIX functions ------------------

Sub MATtranspose(Z())                   ' <- y(), Y(i,j) = Z(j,i)
  Dim d(0)
  Dim x(0)
  d() = GetDim(Z())
  If d(0)<>2 Then return x()
  Dim Y(d(2),d(1))
  For j = 0 To d(1)
    For k = 0 To d(2)
      Y(k,j) = Z(j,k)
    Next k
  Next j
  return Y()
End Sub

Sub MATrow(Z(),r)                       ' <- Z(0‥dim1,row),
  • if row not in [0,dim2]

  Dim d(0)
  Dim x(0)
  d() = GetDim(Z())
  If d(0)<>2 OR r<0 OR r>d(2) Then return x()
  Dim Y(d(1))
  For j = 0 To d(1)
    Y(j) = Z(j,r)
  Next j
  return Y()
End Sub

Sub MATcol(Z(),c)                         ' <- Z(col,0‥dim2),
  • if col not in [0,dim1]

  Dim d(0)
  Dim x(0)
  d() = GetDim(Z())
  If d(0)<>2 OR c<0 OR c>d(1) Then return x()
  Dim Y(d(2))
  For j = 0 To d(2)
    Y(j) = Z(c,j)
  Next j
  return Y()
End Sub

Sub MATprint(Z())                       ' Print matrix
  Dim d(0)
  d() = GetDim(Z())                     ' #dimensions, dimension1 (#columns), dimension2 (#rows)…
  If d(0) > 2 Then
    Print "Error:\nMATRIX dimension =", d(0); "; [1,2] required"
  ElseIf d(0) = 1 Then
    For c = 0 To d(1)                   ' for all positions
      Print "\t",Z(c);                  ' Vector entries
    Next c
  Else ' d(0) = 2
    For r = 0 To d(2)                   ' for all rows
      For c = 0 To d(1)                 ' for all columns
        Print "\t",Z(c,r);              ' Matrix entries
      Next c
      Print
    Next r
  EndIf
  Print
End Sub

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Simple Matrix functions
« Reply #1 on: Feb 03, 2011, 12:08 AM »
With the current limitation that a function returning an array cannot be directly used as a parameter for another function, matrix functions have to be used with assignments to temporary arrays.

This is a symptom of the same parameter passing problem you have noted elsewhere on the forum.  I am currently working on correcting this and will release a fix in the next few days.

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Simple Matrix functions
« Reply #2 on: Feb 04, 2011, 03:17 AM »
Laszlo, Version 3.4.2 has been released.  I think all the array parameter problems have been corrected.  Please let me know if you have any problems after you upgrade.

Laszlo

  • Full Member
  • ***
  • Posts: 34
Re: Simple Matrix functions
« Reply #3 on: Feb 05, 2011, 06:22 PM »
Thanks, Chuck. Version 3.4.2 solved the array parameter problems. The test calls of the simple matrix functions can now be simplified:

MATprint(MATrow(Z(),0))
MATprint(MATcol(Z(),2))
MATprint(MATtranspose(Z()))