Shop OBEX P1 Docs P2 Docs Learn Events
Spin multidimensional arrays — Parallax Forums

Spin multidimensional arrays

bjdav5bjdav5 Posts: 1
edited 2009-03-02 05:00 in Propeller 1
Hi
I am trying to compile spin containing a multidimensional array like this:

var
long weightsIH[noparse][[/noparse]numInputs][noparse][[/noparse]numHiddens]····

numInputs and numHiddens are declared as constants, such as

con
numInputs···· =·············· 3·····
numHiddens······ = 4····

and the compiler returns with the error:· expected "," or end of line.

Any help would be appreciated!

Thanks

Comments

  • Mike HuseltonMike Huselton Posts: 746
    edited 2009-02-27 06:03
    Entire program or no help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    JMH
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-02-27 06:46
    Spin does not support multidimensional arrays.

    -Phil
  • virtuPICvirtuPIC Posts: 193
    edited 2009-02-27 07:03
    Right. You can only simulate multidimensional arrays by defining them one-dimensional and do the index arithmetic on your own:
    CON
      maxx = 4
      maxy = 3
    
    VAR
      long a[noparse][[/noparse]x*y]
    
    PUB doit
      a[noparse][[/noparse]3 * maxx + 2] = 42 ' access last element
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Airspace V - international hangar flying!
    www.airspace-v.com/ggadgets for tools & toys
  • BradCBradC Posts: 2,601
    edited 2009-02-27 11:42
    mpark has his homespun SPIN compiler doing multi-dimensional arrays in precisely this fashion. It's a very neat implementation.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cardinal Fang! Fetch the comfy chair.
  • Ken PetersonKen Peterson Posts: 806
    edited 2009-02-27 12:26
    Taking VirtuPIC's example just a bit further, if you want something a little more efficient, you can size your array in powers of 2

    For example, if you want a 16 x 16 array, you can generate indexes like this:

    VAR
      long a[noparse][[/noparse]16<<4 + 16]
    
    PUB doit
      a[noparse][[/noparse]3 <<4 + 2] = 42 
    
    



    shifting ( multiplying by powers of 2) is much faster because the Propeller makes use of a barrel shifter that can shift any number of places with one instruction.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup

    Post Edited (Ken Peterson) : 2/27/2009 12:33:28 PM GMT
  • ReiserificKReiserificK Posts: 24
    edited 2009-02-27 13:30
    Are there plans for multi dimensional arrays in the next prop/spin revision?
  • TJHJTJHJ Posts: 243
    edited 2009-02-27 17:03
    Other spin compiliers support it. Homespun does I think. But it's just as easy to make a routine to do the lookup.

    Pub array(x,y, maxX)
    Return yourarray[noparse][[/noparse]y*(maxX) + x]
    
    
    


    TJ
  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-27 17:19
    ReiserificK,
    Parallax does not have the resources to make significant changes to the Spin compiler like what would be required to add support for multiple dimensional arrays. They will be busy enough finishing the development of the Propeller II chip and making the basic changes to the Propeller Tool to support the changes to the instruction set, built-in register names, etc. Try the 3rd party (free) Spin compilers.
  • BradCBradC Posts: 2,601
    edited 2009-03-02 03:36
    To be honest, the best and most compatible way to do it is as shown by virtuPIC and Ken Peterson above.

    mpark's homespun compiler takes a format like ...

    foo[noparse][[/noparse]a][noparse][[/noparse]c]

    ..and converts it into the equivalent spin bytecode, and while its a more convenient terminology to use it's no more efficient than doing it longhand.

    In addition, homespun is the only compiler that supports this extension, so code written this way is not compatible with the Parallax compiler.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cardinal Fang! Fetch the comfy chair.

  • cessnapilotcessnapilot Posts: 182
    edited 2009-03-02 05:00
    Look at the Obex/FPU_Matrix_Driver_Demo object in OBEX

    http://obex.parallax.com/objects/317/

    to study a comprehensive working solution for vector and matrix operations in SPIN. This driver uses the simple coding form for matrices as shown by virtuPIC.·In the demo·you can find examples such as the triad algorithm for your robot to figure out its least squares attitude solution from acceleration and magnetometer data, or even singular value decomposition of any arbitrary (real) [noparse][[/noparse]n x m] matrices. This later operation can be found only in advanced math packages.

    When you setup your ANN application in matrix form, then the propagation from the input layer to the hidden neurons can be coded as simply as

    Matrix_Multiply(@HiddenInput, @WeightsI_2_H, @InputValues, 4, 3, 3, 1)

    Starting from a [noparse][[/noparse]3 by 1] column vector of the inputs for the neural network, you will get a [noparse][[/noparse]4 by 1] matrix, a column
    vector 'HiddenInput' that represents the weighted input values for the hidden nonlinear, e.g. tanh, processing neurons.
    After these processing elements did their job, i.e. create 'HiddenOutput' vector, the outcome values for the network can be obtained simply as

    Matrix_Multiply(@OutputValues, @WeigthsH_2_O, @HiddenOutput, 2, 4, 4, 1)

    where I supposed 2 output values. You can make simple Put and Get SPIN procedures to access individual i,j matrix elements, like

    Put(@Mat, i, j) and Get(@Mat, i, j)

    Similarly, the whole ANN backpropagation learning algorithm· can be programmed just·IN FEW LINES IN SPIN·ONLY using the FPU_Matrix_Driver.SPIN.
Sign In or Register to comment.