Multi dimensional arrays in spin
John_B
Posts: 16
Hi,
Is it possible to have an array of arrays in spin?
Say 3 sensors with 5 parameters each.
I have tried creating a var:
long sensor[noparse][[/noparse]3][noparse][[/noparse]5]
and accessing with:
sensor[noparse][[/noparse]ch1][noparse][[/noparse]param1] := 40
with no luck.
If its possible what is the correct syntax; any examples?
If its not possible what is the best way of creating and accessing multi dimensional arrays in spin.
Cheers
John
Is it possible to have an array of arrays in spin?
Say 3 sensors with 5 parameters each.
I have tried creating a var:
long sensor[noparse][[/noparse]3][noparse][[/noparse]5]
and accessing with:
sensor[noparse][[/noparse]ch1][noparse][[/noparse]param1] := 40
with no luck.
If its possible what is the correct syntax; any examples?
If its not possible what is the best way of creating and accessing multi dimensional arrays in spin.
Cheers
John
Comments
see http://forums.parallax.com/showthread.php?p=754811
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.mikronauts.com - my site 6.250MHz custom Crystals for running Propellers at 100MHz
Las - Large model assembler for the Propeller Largos - a feature full nano operating system for the Propeller
Morpheus & Mem+ Advanced dual Propeller SBC with XMM and 256 Color VGA
Please use mikronauts _at_ gmail _dot_ com to contact me off-forum, my PM is almost totally full
address := (row * ARRAY_COLUMN_LENGTH) + column
Bascially each row is worth X number of columns.
Simple.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit some of my articles at Propeller Wiki:
MATH on the propeller propeller.wikispaces.com/MATH
pPropQL: propeller.wikispaces.com/pPropQL
pPropQL020: propeller.wikispaces.com/pPropQL020
OMU for the pPropQL/020 propeller.wikispaces.com/OMU
Your code trials are· very similar to my first lines in SPIN.
An array of arrays can be regarded as a matrix, or name it whatever you like. I·cut and paste·some·comment part of an obex object,
http://obex.parallax.com/objects/317/·
that might help you in the·indexing and addressing of 2-dimensional arrays.
'
"In the worlds of math, engineering and physics, it's the matrix that·
separates the men from the boys, the· women from the girls."··········
·············································· (Jack W. Crenshaw).····
······································································
·A matrix is an array of numbers organized in rows and columns. We····
usually give the row number first, then the column. So a [noparse][[/noparse]3-by-4]·····
matrix has twelve numbers arranged in three rows where each row has a·
length of four························································
······································································
························ +··············· ·+····························
························ | 1· 2· 3·· 4··· |····························
························ | 2· 3· 4·· 5··· |····························
························ | 3· 4· 5· 6.28 |····························
························ +················ +····························
······································································
·Since computer RAM is organized sequentially, as we access it with a·
single number that we call address, we have to find a way to map the··
two dimensions of the matrix onto the one-dimensional, sequential·····
memory. In Propeller SPIN that is rather easy since we can use arrays.
For the previous matrix we can declare an array of LONGs, e.g.········
······································································
························· VAR·· LONG mA[noparse][[/noparse]12]···························
······································································
that is large enough to contain the "three times four" 32-bit IEEE 754
float numbers of the· matrix. In SPIN language the indexing starts with
zero, so the first row, first column element of this matrix is placed·
in mA[noparse][[/noparse]0]. The second row, fourth column element is placed in mA[noparse][[/noparse]7]. The
general convention that I used with the "FPU_Matrix_Driver.spin" object
is that the ith row, jth column element is accessed at the index······
······································································
························ "mA[noparse][[/noparse]i,j]" = mA[noparse][[/noparse]index]························
······································································
where·································································
······································································
···················· index = (i-1)*(#col) + (j-1)·····················
······································································
and #col = 4 in this example. There are the 'Matrix_Put' and the······
'Matrix_Get' procedures in the driver to aid the access to the elements
of a matrix. In this example the second row, fourth column element of·
mA can be set to 5.0 using············································
······································································
·········· OBJNAME.Matrix_Put(@mA, 5.0, 2, 4, #row, #col)·············
Like in the previous example, the bunch of data in matrices is accessed
by the driver using the starting HUB memory address of the data. For··
example, after you declared mB and mC matrices to be the same [noparse][[/noparse]3-by-4]
size as mA····························································
······································································
························· VAR·· LONG mB[noparse][[/noparse]12]···························
························· VAR·· LONG mC[noparse][[/noparse]12]···························
······································································
you can add mB to mC and store the result in mA with the following····
single procedure call·················································
······································································
·OBJNAME.Matrix_Add(@mA, @mB, @mC, 3, 4)···· (meaning mA := mB + mC)··
······································································
You can't multiply mB with mC, of course, but you can multiply mB with
the transpose of mC. To obtain this transpose use·····················
······································································
·OBJNAME.Matrix_Transpose(@mCT, @mC, 3, 4)·· (meaning mCT := Tr. of mC)
······································································
mCT is a [noparse][[/noparse]4-by-3] matrix, which can be now multiplied from the left···
with mB as····························································
······································································
·OBJNAME.Matrix_Multiply(@mD,@mB,@mCT,3,4,3) (meaning mD := mB * mCT)·
······································································
where the result mD is a [noparse][[/noparse]3-by-3] matrix. This matrix algebra coding··
convention can yield compact and easy to debug code. The following 8··
lines of SPIN code (OBJNAME here is FPUMAT) were taken from the·······
'FPU_ExtendedKF.spin' application and calculate the Kalman gain matrix
from five other matrices (A, P, C, CT, Sz) at a snap··················
······································································
······ (··· Formula: K = A * P * CT * Inv[noparse][[/noparse]C * P * CT + Sz]·· )········
············································································
···· FPUMAT.Matrix_Transpose(@mCT, @mC, _R, _N)·······················
···· FPUMAT.Matrix_Multiply(@mAP, @mA, @mP, _N, _N, _N)···············
···· FPUMAT.Matrix_Multiply(@mAPCT, @mAP, @mCT, _N, _N, _R)···········
···· FPUMAT.Matrix_Multiply(@mCP, @mC, @mP, _R, _N, _N)···············
···· FPUMAT.Matrix_Multiply(@mCPCT, @mCP, @mCT, _R, _N, _R)···········
···· FPUMAT.Matrix_Add(@mCPCTSz, @mCPCT, @mSz, _R, _R)················
···· FPUMAT.Matrix_Invert(@mCPCTSzInv, @mCPCTSz, _R)···················
···· FPUMAT.Matrix_Multiply(@mK, @mAPCT, @mCPCTSzInv, _N, _R, _R)
'
····
·Never mind floating point, this indexing and addressing works with LONGs, too.
·
And a good advice. When your SPIN program uses 2-dimensional
arrays of the same size very intesively, prepare an index array vector, e.g.
VAR LONG row_base[noparse][[/noparse]#row]
REPEAT i FROM 1 TO #row
· row_base[noparse][[/noparse]i] := (i - 1) * #col
and in the numerous calculation of 2-dimensinal indices you can use this vector to
access data at matrix[noparse][[/noparse]i,j]
data_index := row_base[noparse][[/noparse]i] + (j-1)
avoiding a multiplication in each turn. That may speed up the code noticably.
Cheers,
Istvan·········
Post Edited (cessnapilot) : 7/29/2009 5:23:01 AM GMT
No it doesn't. mpark built multi-dimension arrays into homespun. I have not replicated this feature.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<This space for rent>
John
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.mikronauts.com - my site 6.250MHz custom Crystals for running Propellers at 100MHz
Las - Large model assembler for the Propeller Largos - a feature full nano operating system for the Propeller
Morpheus & Mem+ Advanced dual Propeller SBC with XMM and 256 Color VGA
Please use mikronauts _at_ gmail _dot_ com to contact me off-forum, my PM is almost totally full