locked
Matrix Operations _ Need a how to guide.for small basic. RRS feed

  • Question

  • I am wanting to do matrix math in small basic. Havig trouble.. I  would like to do 2 dim arrays but not supported in small basic. I would like to do straight look up tables and would be interested in the dot product, transform, cross product specifically. My attempts are lacking an understing of a 2x2 or 3x3. Any suggestions?

    Steven M. Cash



    • Edited by Steve Cash Saturday, April 28, 2012 2:24 AM
    Saturday, April 28, 2012 2:14 AM

Answers

  • The methods shown do work with SmallBasic, although they may ultimately be slow for intensive calculations.

    Simple example:

    vec1[1] = 4
    vec1[2] = 1
    vec1[3] = 6
    
    vec2[1] = 5
    vec2[2] = 3
    vec2[3] = 1
    
    dotProduct()
    crossProduct()
    
    TextWindow.WriteLine("The dot product is "+dot)
    TextWindow.WriteLine("The cross product is "+cross[1]+","+cross[2]+","+cross[3])
    
    Sub dotProduct
      dot = 0
      For i = 1 To 3
        dot = dot+vec1[i]*vec2[i]
      EndFor
    EndSub
    
    Sub crossProduct
      cross[1] = vec1[2]*vec2[3] - vec1[3]*vec2[2]
      cross[2] = vec1[3]*vec2[1] - vec1[1]*vec2[3]
      cross[3] = vec1[1]*vec2[2] - vec1[2]*vec2[1]
    EndSub
    Saturday, April 28, 2012 1:05 PM
  • This is another sample code of Cross Product.

    ' Cross Product of Matrix
    MAXDIGITS = 3
    SP = "   "
    n = 2
    
    CreateRandomMatrixAB()
    target = "A"
    WriteMatrix()
    target = "B"
    WriteMatrix()
    
    a = "A"
    b = "B"
    TextWindow.WriteLine("C = A x B")
    c = "C"
    CrossProduct()
    target = "C"
    WriteMatrix()
    ' program end
    
    Sub CreateRandomMatrixAB
      For i = 1 To n
        For j = 1 To n
          M["A"+Text.Append(i, j)] = Math.GetRandomNumber(9)
          M["B"+Text.Append(i, j)] = Math.GetRandomNumber(9)
        EndFor
      EndFor
    EndSub
    
    Sub WriteMatrix
      ' param target
      For i = 1 To n
        If i = 1 Then
          TextWindow.Write(target + " = ")
        Else
          TextWindow.Write("    ")
        EndIf
        For j = 1 To n
          mij = M[target + Text.Append(i, j)]
          l = Text.GetLength(Mij)
          ns = MAXDIGITS - l
          TextWindow.Write(Text.GetSubText(SP, 1, ns) + mij + " ")
        EndFor
        TextWindow.WriteLine("")
      EndFor
    EndSub
      
    Sub CrossProduct
      ' param a
      ' param b
      ' param c
      For i = 1 To n
        For j = 1 To n
          cij = 0
          For k = 1 To n
            aik = M[a + Text.Append(i, k)]
            bkj = M[b + Text.Append(k, j)]
            cij = cij + aik * bkj
          EndFor
          M[c + Text.Append(i, j)] = cij
        EndFor
      EndFor
    EndSub


    Nonki Takahashi

    Saturday, April 28, 2012 6:49 PM
  • Arrays with multiple dimensions are in fact supported in Small Basic. However, very large structures may experience a little bit of slowdown when elements are being accessed. Here's how to do a three dimensional array:

    variable[1]["A"]["B"] = 1

    This code would assign the value of 1 to the element "B" which is a substructure of element "A" which is a substructure of element "1" in the array "variable"

    For example, if you were working with an X-Y coordinate plane, you could store the coordinates of the points in a structure like this:

    point[1]["X"] = 1
    point[1]["Y"] = 2
    point[2]["X"] = -1
    point[2]["Y"] = -3

    This would create point on, with the X value of 1 and the Y value of 2, which could be formatted like (1,2) after reading the array. It would also create point 2, with an X value of -1 and a Y value of -3, or (-1,-3).


    Saturday, April 28, 2012 3:35 AM
    Answerer

All replies

  • Arrays with multiple dimensions are in fact supported in Small Basic. However, very large structures may experience a little bit of slowdown when elements are being accessed. Here's how to do a three dimensional array:

    variable[1]["A"]["B"] = 1

    This code would assign the value of 1 to the element "B" which is a substructure of element "A" which is a substructure of element "1" in the array "variable"

    For example, if you were working with an X-Y coordinate plane, you could store the coordinates of the points in a structure like this:

    point[1]["X"] = 1
    point[1]["Y"] = 2
    point[2]["X"] = -1
    point[2]["Y"] = -3

    This would create point on, with the X value of 1 and the Y value of 2, which could be formatted like (1,2) after reading the array. It would also create point 2, with an X value of -1 and a Y value of -3, or (-1,-3).


    Saturday, April 28, 2012 3:35 AM
    Answerer
  • Another way to do a 1D array with (x,y) is data[x] = y, using gungan37's data this would be:

    data[1] = 2

    data[-1] = 3

    A 2D array 3*3 array might be

    data[1][1] = 5

    data[1][2] = 4

    data[1][3] = 7

    data[2][1] = 8

    etc.

    You can write subroutines to then do matrix operations, show what code you have so far for further help with this.

    Saturday, April 28, 2012 8:53 AM
  • I tried your answer  but iI could not get it to work. Are you sure this is supported in Small Basic? I am not sure where the  problem is but it does not work in  Small Basic.


    Steven M. Cash

    Saturday, April 28, 2012 11:44 AM
  • I have looked at your proposed anawer but it does not work ,  I tried to use your method but it did not give me results that worked. I still cannot see the answer.


    Steven M. Cash

    Saturday, April 28, 2012 11:52 AM
  • The methods shown do work with SmallBasic, although they may ultimately be slow for intensive calculations.

    Simple example:

    vec1[1] = 4
    vec1[2] = 1
    vec1[3] = 6
    
    vec2[1] = 5
    vec2[2] = 3
    vec2[3] = 1
    
    dotProduct()
    crossProduct()
    
    TextWindow.WriteLine("The dot product is "+dot)
    TextWindow.WriteLine("The cross product is "+cross[1]+","+cross[2]+","+cross[3])
    
    Sub dotProduct
      dot = 0
      For i = 1 To 3
        dot = dot+vec1[i]*vec2[i]
      EndFor
    EndSub
    
    Sub crossProduct
      cross[1] = vec1[2]*vec2[3] - vec1[3]*vec2[2]
      cross[2] = vec1[3]*vec2[1] - vec1[1]*vec2[3]
      cross[3] = vec1[1]*vec2[2] - vec1[2]*vec2[1]
    EndSub
    Saturday, April 28, 2012 1:05 PM
  • Sorry, I forgot the quotation marks on the "X" and "Y" elements. See if it works with changes.
    Saturday, April 28, 2012 3:50 PM
    Answerer
  • This is another sample code of Cross Product.

    ' Cross Product of Matrix
    MAXDIGITS = 3
    SP = "   "
    n = 2
    
    CreateRandomMatrixAB()
    target = "A"
    WriteMatrix()
    target = "B"
    WriteMatrix()
    
    a = "A"
    b = "B"
    TextWindow.WriteLine("C = A x B")
    c = "C"
    CrossProduct()
    target = "C"
    WriteMatrix()
    ' program end
    
    Sub CreateRandomMatrixAB
      For i = 1 To n
        For j = 1 To n
          M["A"+Text.Append(i, j)] = Math.GetRandomNumber(9)
          M["B"+Text.Append(i, j)] = Math.GetRandomNumber(9)
        EndFor
      EndFor
    EndSub
    
    Sub WriteMatrix
      ' param target
      For i = 1 To n
        If i = 1 Then
          TextWindow.Write(target + " = ")
        Else
          TextWindow.Write("    ")
        EndIf
        For j = 1 To n
          mij = M[target + Text.Append(i, j)]
          l = Text.GetLength(Mij)
          ns = MAXDIGITS - l
          TextWindow.Write(Text.GetSubText(SP, 1, ns) + mij + " ")
        EndFor
        TextWindow.WriteLine("")
      EndFor
    EndSub
      
    Sub CrossProduct
      ' param a
      ' param b
      ' param c
      For i = 1 To n
        For j = 1 To n
          cij = 0
          For k = 1 To n
            aik = M[a + Text.Append(i, k)]
            bkj = M[b + Text.Append(k, j)]
            cij = cij + aik * bkj
          EndFor
          M[c + Text.Append(i, j)] = cij
        EndFor
      EndFor
    EndSub


    Nonki Takahashi

    Saturday, April 28, 2012 6:49 PM
  • This answered my quesyion. Thank you!

    Steven M. Cash

    Monday, May 7, 2012 12:17 AM