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
…. 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
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
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 ...
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 entries I think Anatoly ( tsh3) posted a related version somewhere??
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.
Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM