' _________________________________________________________________________________________ dim GlobalArray( 100, 100) ' Accessible from anywhere, ie global.. ' This line needed at the start of your LB program. ' _________________________________________________________________________________________ ' Used to interchange my Matrix format and an array format ' NB I ( at least) am unlikely to need bigger matrices. ' Matrices are stored as a csv string- "rows, cols, data_1 . . . . data_c*r" mainwin 90 70 MatrixA$ ="4, 4, 1, 1, 1, 1, 2, 4, 8, 16, 3, 9, 27, 81, 4, 16, 64, 256" MatrixB$ ="4, 4, 4, -3, 1.333333333, -0.25 , -4.3333333333, 4.75, -2.333333333, 0.45833333, 1.5, -2, 1.166666666, -0.25, -0.166666666, 0.25, -0.166666666, 0.041666667" print print "Transpose of matrix" MatrixC$ ="4, 3, 0, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 1.00, 1.10" call DisplayMatrix MatrixC$ print " =" MatrixT$ =MatrixTranspose$( MatrixC$) call DisplayMatrix MatrixT$ print "Product of two matrices" call DisplayMatrix MatrixA$ print " *" call DisplayMatrix MatrixB$ print " =" MatrixP$ =MatrixMultiply$( MatrixA$, MatrixB$) call DisplayMatrix MatrixP$ print print "Exponentiation of a matrix" MatrixD$ ="3, 3, 0.86603, 0.50000, 0.00000, -0.50000, 0.86603, 0.00000, 0.00000, 0.00000, 1.00000" call DisplayMatrix MatrixD$ print " Raised to power 5 =" MatrixE$ =MatrixToPower$( MatrixD$, 5) call DisplayMatrix MatrixE$ print " Raised to power 9 =" MatrixE$ =MatrixToPower$( MatrixD$, 9) call DisplayMatrix MatrixE$ print "Done." wait ' ______________________________________________________________________ ' Copy the functions below here onto the end of your LB program. function determinant( in$) determinant =determinant2( in$) if instr( "3579", word$( in$, 1, ",")) then determinant =0 -determinant end function function determinant2( Mat$) if not( instr( MatrixProperties$( Mat$), "Square")) then notice "Has to be square.": end dims =val( word$( Mat$, 1, ",")) determinant2 =0 select case dims case 1 determinant2 =val( word$( Mat$, 3, ",")) case 2 determinant2 =GetTerm( Mat$, 1, 1) *GetTerm( Mat$, 2, 2) -GetTerm( Mat$, 1, 2) *GetTerm( Mat$, 2, 1) case else for i =1 to dims determinant2 =determinant2 +GetTerm( Mat$, i, 1) *determinant2( ReducedMatrix$( Mat$, i)) *( 0 -1)^i next i end select end function function ReducedMatrix$( Mat$, i) dims =val( word$( Mat$, 1, ",")) ReducedMatrix$ =str$( dims -1); ","; str$( dims -1); "," for col =2 to dims for row =1 to dims if not( ( row =i)) then ReducedMatrix$ =ReducedMatrix$ +str$( GetTerm( Mat$, row, col)) +"," next row next col end function function GetTerm( in$, i, j) ' Return element( i along row, j down column) w =val( word$( in$, 1, ",")) h =val( word$( in$, 2, ",")) if (w int( n)) then notice "Not a square matrix.": end o$ =str$( n); ","; str$( n); "," for i =1 to n for j =1 to n if i =j then o$ =o$ +str$( 1) +"," else o$ =o$ +str$( 0) +"," next j next i UnityMatrix$ =o$ end function function MatrixProperties$( in$) ' Detect vector ( row, column) or square forms ( TBA determinant etc) r$ ="" w =val( word$( in$, 1, ",")) h =val( word$( in$, 2, ",")) if w =h then r$ =" Square" else r$ =" Matrix" r$ =r$ +" size "; str$( w); " columns by "; str$( h); " rows." if w =1 then r$ =r$ +" Column vector size "; str$( w); " by "; str$( h) if h =1 then r$ =r$ +" Row vector size "; str$( w); " by "; str$( h) ' add detection of diagonal, determinant, etc... MatrixProperties$ =r$ end function function ScaleMatrix$( scalar, in$) ' Scalar product w =val( word$( in$, 1, ",")) h =val( word$( in$, 2, ",")) prod$ =str$( w); ","; str$( h); "," for i =1 to h for j =1 to w term$ =word$( in$, 2 +j +( i -1) *w, ",") tv =scalar *val( term$) res$ =str$( tv) prod$ =prod$ +res$ +"," next j next i ScaleMatrix$ =left$( prod$, len( prod$) -1) ' ############## trailing ',' to remove. end function function MatrixTranspose$( in$) w =val( word$( in$, 1, ",")) ' swap w and h parameters h =val( word$( in$, 2, ",")) t$ =str$( h); ","; str$( w); "," for i =1 to w for j =1 to h t$ =t$ +word$( in$, 2 +i +( j -1) *w, ",") +"," next j next i MatrixTranspose$ =left$( t$, len( t$) -1) ' ############## trailing ',' to remove end function sub DisplayMatrix in$ ' Display looking like a matrix! w =val( word$( in$, 1, ",")) h =val( word$( in$, 2, ",")) 'print "Width = "; w; " & height ="; h for i =0 to h -1 print " |"; for j =1 to w term$ =word$( in$, j +2 +i *w, ",") print using( "#####.#####", val( term$)), ' This formatting may not be a good general idea, but aids readability next j print "|" next i 'print in$ print end sub function MatrixMultiply$( inA$, inB$) AColumn =val( word$( inA$, 1, ",")) ' This holds number of COLUMNs ARow =val( word$( inA$, 2, ",")) ' ROWs ' eg "1, 4, 1,2,3,4" is a column vector BColumn =val( word$( inB$, 1, ",")) BRow =val( word$( inB$, 2, ",")) if ( AColumn <>BRow) or ( ARow <>BColumn) then notice "Matrix dimensions unsuitable. Not conformable": end n$ =str$( ARow); ","; str$( BRow); "," for AnsRow =1 to AColumn for AnsColumn =1 to BColumn trm =0 for i =1 to BColumn j1 =GetTerm( inB$, i, AnsRow) j2 =GetTerm( inA$, AnsColumn, i ) trm =trm + j1 *j2 next i 'if abs( trm) <1e-4 then trm =0 ' fiddled so acceptable in LB 4.03 n$ =n$ +str$( trm) +"," next AnsColumn next AnsRow MatrixMultiply$ =left$( n$, len( n$) -1) ' ############## trailing ',' to remove end function function MatrixToPower$( Mat$, n) if ( n <>int( n)) or ( n <0) then notice "Integer pos've powers only.": end if not( instr( MatrixProperties$( Mat$), "Square")) then notice "Has to be square.": end dims =val( word$( Mat$, 1, ",")) o$ =UnityMatrix$( dims) if n <>0 then for i =1 to n o$ =MatrixMultiply$( Mat$, o$) next i end if MatrixToPower$ =o$ end function