It is hardly worth while (for matrix types anyway) creating a constructor or using a let operator to copy values into a matrix.

It would have been good to directly use my matrix=type<matrix>({{...),{...},{...}}).

But since that cannot be, using data is perhaps the simplest method.

Here is an alternative linear equation solver using the Cramer method.

The matrix determinant function is used for this method.

I notice in the function matrix.solve I am not allowed to use RETURN. ??

Apart from that it seems fair enough.

So I'll but out of this thread for a while.

Cheers!

Code: Select all

`Type matrix `

Dim As Double element(Any,Any)

Declare Operator Cast() As String

Declare Function determinant() As Double

Declare Function solve(As matrix) As matrix

End Type

'matrix multiply

Operator *(m1 As matrix,m2 As matrix) As matrix

Dim rows As Integer=Ubound(m1.element,1)

Dim columns As Integer=Ubound(m2.element,2)

If Ubound(m1.element,2)<> Ubound(m2.element,1)Then

Print "Can't do"

Exit Operator

End If

Dim As matrix ans

Redim ans.element(1 To rows,1 To columns)

Dim rxc As Double

For r As Integer=1 To rows

For c As Integer=1 To columns

rxc=0

For k As Integer = 1 To Ubound(m1.element,2)

rxc=rxc+m1.element(r,k)*m2.element(k,c)

Next k

ans.element(r,c)=rxc

Next c

Next r

Operator= ans

End Operator

'rounding function

Function round (a As Double,b As Integer) As Double

Var y = (Abs(a)-Int(Abs(a))) * (10 ^ b),i=Int(y):y-=i

If y >= .5 Then i+= 1

i /= (10 ^ b)

Var r = Int(Abs(a))+i

If a < 0 Then r = -r

Return r

End Function

'matrix printer

Operator matrix.cast() As String

Dim As String ans,comma

For a As Integer=1 To Ubound(this.element,1)

For b As Integer=1 To Ubound(this.element,2)

If b=Ubound(element,2) Then comma="" Else comma=" , "

ans=ans+Str(round(element(a,b),12))+comma

Next b

ans=ans+Chr(10)

Next a

Operator= ans

End Operator

Function matrix.determinant() As Double

Dim As Double det=1

Dim As Integer n=Ubound(this.element),sign=1

Dim As matrix b=(This)

#macro pivot(num)

For p1 As Integer = num To n - 1

For p2 As Integer = p1 + 1 To n

If Abs(b.element(p1,num))<Abs(b.element(p2,num)) Then

sign=-sign

For g As Integer=1 To n

Swap b.element(p1,g),b.element(p2,g)

Next g

End If

Next p2

Next p1

#endmacro

For k As Integer=1 To n-1

pivot(k)

For row As Integer =k To n-1

If b.element(row+1,k)=0 Then Exit For

Var f=b.element(k,k)/b.element(row+1,k)

For g As Integer=1 To n

b.element((row+1),g)=(b.element((row+1),g)*f-b.element(k,g))/f

Next g

Next row

det=det*b.element(k,k)

Next k

Return sign*det*b.element(n,n)

End Function

'column swapper for Cramer method

Function CRAMERswaps(m As matrix,c As matrix,column As Integer) As matrix

Var _new=m

For z As Integer=1 To Ubound(_new.element)

_new.element(z,column)=c.element(z,1)

Next z

Return _new

End Function

Function matrix.solve(rhs As matrix) As matrix

Var m=this.determinant

Dim As matrix result

Redim result.element(1 To Ubound(this.element),1 To 1)

Dim As matrix ans

If m Then

' do CRAMER column substitutions

For column As Integer=1 To Ubound(this.element)

ans= CRAMERswaps(This,RHS,column)

result.element(column,1)=ans.determinant/m

Next column

Else

Print "Main matrix is most likely singular"

Exit Function

End If

Function= result

'return result

End Function

'=====================================================

Screen 19

Windowtitle "Cramer method"

Dim As matrix m,rhs

'square matrices only for linear equation systems

Redim m.element(1 To 6,1 To 6)

Redim rhs.element(1 To 6,1 To 1)

Restore System1

For r As Integer=1 To 6

For c As Integer=1 To 6

Read M.element(r,c)

Next c

Next r

For r As Integer=1 To 6

Read rhs.element(r,1)

Next r

Print "Matrix ":Print m

Print "RHS ":Print rhs

Print "Determinant of matrix = ";:Print m.determinant

Print

Print "Solution of linear equations"

Print m.solve(rhs)

Print

Print "Check answer (matrix x solution = RHS) "

Print m*m.solve(rhs)

Print "Done"

Sleep

System1:

Data 10,2,3,9,0,0, _

-5,8,0,-3,-8,1, _

7,1,2,-1,9,2, _

-9,12,-2,-11,0,0, _

9,3,-2,.5,0,-.9, _

-9,-.03,-10,-1,0,0

Data -6,0,4.008,-1,3.33,3