# Mintoris Forum

### Author Topic: matrix multiply  (Read 2372 times)

#### jclemens

• Sr. Member
•    • Posts: 146 ##### matrix multiply
« on: Jun 08, 2014, 02:35 PM »
This uses new array syntax:
'
'
' matrix multiply A*B giving C
' returns 0 on error, else 1
' the parameter symbolized by C should be passed by reference
Sub matmpy(A(),B(),C())
dim bdim(0)
dim tmp(0)
bdim() = getdim(B())
' must be 2 dims
if adim(0)<>2 or bdim(0)<>2 then return 0
' number of columns of A must equal  number of rows of B
for arow = 0 to adim(1)
for bcol = 0 to bdim(2)
'  A row vector multiplied pairwise by B column vector
tmp() = A(arow,*)*B(*,bcol)
sum=0
for k = 0 to adim(2)
sum = sum  + tmp(k)
next k
C(arow,bcol) = sum
next bcol
next arow
return 1
End Sub

#### Chuck

• Global Moderator
• Hero Member
•     • • Posts: 1899 ##### Re: matrix multiply
« Reply #1 on: Jun 08, 2014, 06:39 PM »
I think you could condense the inner loop:

tmp() = A(arow,*)*B(*,bcol)
sum=0
for k = 0 to adim(2)
sum = sum  + tmp(k)
next k
C(arow,bcol) = sum

to

C(arow,bcol) = ArrSum(A(arow,*)*B(*,bcol))

It's wild how much easier it is to write this routine in Basic.

-Chuck

« Last Edit: Jun 08, 2014, 07:07 PM by Chuck »

#### jclemens

• Sr. Member
•    • Posts: 146 ##### Re: matrix multiply
« Reply #2 on: Jun 09, 2014, 08:12 AM »
That is neat‼ Extremely compact code and probably runs much faster. I didn't know about the existence of ArrSum, possibly because it doesn't appear in the list of array functions although it is in the index.  Here it is now:
'
' matrix multiply A*B giving C
' returns 0 on error, else 1
' the parameter symbolized by C should be passed by reference
Sub matmpy(A(),B(),C())
dim bdim(0)
bdim() = getdim(B())
' must be 2 dims
if adim(0)<>2 or bdim(0)<>2 then return 0
' number of columns of A must equal  number of rows of B
for arow = 0 to adim(1)
for bcol = 0 to bdim(2)
'  A row vector multiplied pairwise by B column vector, then summed
C(arow,bcol) = ArrSum(A(arow,*)*B(*,bcol))
next bcol
next arow
return 1
End Sub

« Last Edit: Jun 09, 2014, 01:23 PM by jclemens »

#### Chuck

• Global Moderator
• Hero Member
•     • • Posts: 1899 ##### Re: matrix multiply
« Reply #3 on: Jun 09, 2014, 06:48 PM »
dim bdim(0)

These dims are no longer needed, those arrays will be initialized by the GetDim() function.

' must be 2 dims
if adim(0)<>2 or bdim(0)<>2 then return 0
' number of columns of A must equal  number of rows of B

After you check the dimensions, you might as well Dim C()

That way the calling program doesn't have to dim C().

'
' matrix multiply A*B giving C
' returns 0 on error, else 1
' the parameter symbolized by C should be passed by reference
Sub matmpy(A(),B(),C())
bdim() = getdim(B())
' must be 2 dims
if adim(0)<>2 or bdim(0)<>2 then return 0
' number of columns of A must equal  number of rows of B

for arow = 0 to adim(1)
for bcol = 0 to bdim(2)
'  A row vector multiplied pairwise by B column vector, then summed
C(arow,bcol) = ArrSum(A(arow,*)*B(*,bcol))
next bcol
next arow
return 1
End Sub

I have a java linear algebra library that has all the matrix functions. I will probably add built-in functions that use this library.

-Chuck
« Last Edit: Jun 09, 2014, 06:52 PM by Chuck »