|
Post by bencooper on Oct 12, 2019 10:18:44 GMT -5
I use matrix algebra, finding it incredibly useful for many math and 3D graphics applications
I have written various matrix routines in LB such as were normal on early BASIC implementations (MatInput, MatAdd,MatMult, MatInv, MatDet etc), using linear and rectangular Arrays offered by LB.
I wish to make them more flexible in dealing with any selected (and suitable) arrays which have been declared/initialized in main program but am non-plussed on how to pass the array names as parameters?
Typically, I have:
CALL MatAdd END
SUB MatAdd
…. code for A(10,10)=B(10,10)+C(10,10) 'but I would like alternatives as P(10,10)=Q(10,10)+R(10,10) for other CALLs
END SUB
How do I pass Array names to enable this ?
|
|
|
Post by Brandon Parker on Oct 12, 2019 11:57:40 GMT -5
You can use the ability to "Eval$()" a string that forms an Array/ Array Element to get LB to evaluate it. Below is how you would want to do such a thing although arrays are Global in Liberty BASIC so any manipulation is acting on the actual array and not a copy of that array. Just something to be aware of. The code below creates three arrays, stores a string of words in the first, an iterated number in the second, and then calls the concatArrayElements$() function to concatenate the string values held in both arrays returning the result which is then stored into the third array. Dim myArray1$(9) Dim myArray2$(9) Dim myArray3$(9)
For i = 0 To 9 myArray1$(i) = "Hello, I am element " myArray2$(i) = str$(i) Next i
For i = 0 To 9 myArray3$(i) = concatArrayElements$("myArray1", i, "myArray2", i) Next i
For i = 0 To 9 Print myArray3$(i) Next i
Wait
Function concatArrayElements$(arrayName1$, elem1, arrayName2$, elem2) concatArrayElements$ = Eval$(arrayName1$;"$(";elem1;")") + Eval$(arrayName2$;"$(";elem2;")") End Function You could then send another array into the function as well and program the function to add or concatenate (or whatever) the elements of the two arrays. You would have to perform the store into the array to receive the data with the return from the function. Unfortunately, Liberty BASIC does not provide the functionality to Eval$() on the left side of the "=" operator which means one cannot just "evaluate" which array to store the value into during runtime. This is how I do stuff in my Dynamic Array Library, but there's much more checking for errors as well as many more functions to make my life easier. The only problem with with going down this road is that you have to be super careful with code because once you starting using error trapping inside LB functions to catch errors (such as array index out of bounds errors) you will not be able to use the LB Debugger to debug your code. The LB Debugger halts on such errors where the LB Runtime Engine correctly deals with errors that are purposely trapped and dealt with. I hope that helps point you in the direction you want to be. Feel free to continue asking questions about the subject. I love arrays ... {:0) Brandon Parker
|
|
|
Post by Rod on Oct 12, 2019 12:21:30 GMT -5
Ok Brandon's solution is polished and uses a powerful Liberty command. So, you are both going to think this solution very clunky.
'create array index variables (array names) A=1 B=2 C=3 P=4 Q=5 R=6
'create a single array 10,10,10 dim a(10,100)
'fill A and P with 1 and 5 respectively for n=0 to 10 for m=0 to 10 a(n,A*10+m)=1 a(n,P*10+m)=5 next next
'call our sub to add P matrix to A matrix CALL MatAdd A,P
'print the resultant A matrix for n=0 to 10 for m=0 to 10 print a(n,A*10+m);" "; next print next
END
SUB MatAdd x,y for n=0 to 10 for m= 0 to 10 a(n,x*10+m)=a(n,x*10+m)+a(n,y*10+m) next next END SUB
|
|
|
Post by tenochtitlanuk on Oct 12, 2019 12:35:58 GMT -5
Anyone else picking up this thread might look at the methods used in the LB Rosetta entries for multiplying, transposing and exponentiating matrices. eg Rosetta Code LB entriesI think Anatoly ( tsh3) posted a related version somewhere??
|
|
|
Post by bencooper on Oct 12, 2019 15:03:28 GMT -5
Thanks Gents for quick responses - glad it wasn't an obvious trick I'd missed? I will try the suggested routes. Yes, it is a valid point that the global arrays will be altered, so perhaps I can build some "dummies" to maintain the originals?
|
|
|
Post by Brandon Parker on Oct 13, 2019 16:44:59 GMT -5
Yeah, not really sure if you would need "dummy" arrays. I just mentioned that because some people come from other languages where arrays can be passed into a Sub/ Function by value and thus they would be working with a copy inside the Sub/ Function. As long as you understand that arrays are global in LB, then I am sure you have nothing to worry about.
{:0)
Brandon Parker
|
|