Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Vector inner (dot) product and vector outer product using MAT

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Vector inner (dot) product and vector outer product using MAT

    ' Vector inner (dot) product and vector outer product using MAT.
    '
    ' (Vector arrays need to be defined as two-dimensional for calculations to be correct. )
    '
    ' May increase speed in complex calculations.
    Code:
    #COMPILE EXE
    #REGISTER NONE
    #DIM ALL
    '
    FUNCTION PBMAIN
        LOCAL i&,j&,k&
        LOCAL t$
        LOCAL L&, M&, N&
    
        L = 4 ' Rows matrix A,      rows matrix B
        M = 1 ' Columns matrix B,   rows matrix C
        N = 4 ' Columns matrix A,   columns matrix C
    
        '     Rows    Columns
        DIM A(1 TO L, 1 TO N) AS LOCAL SINGLE           ' Resulting matrix
        DIM B(1 TO L, 1 TO M) AS LOCAL SINGLE           ' First matrix
        DIM C(1 TO M, 1 TO N) AS LOCAL SINGLE           ' Second matrix
        ' matrices B and C should be conformable,
        ' i.e. the number of columns of B should be equal to the number of rows of C.
        '
        DATA 1  : ' matrix B
        DATA 2
        DATA 3
        DATA 5
    
        DATA 2,4,7,9    : ' matrix C
    
        ' Read arrays
        t = "vector B() = " + $CRLF
        FOR i = 1 TO L      ' Rows
            FOR j = 1 TO M  ' Columns
                INCR k
                B(i,j) = VAL(READ$(k))
                t = t + STR$(B(i,j)) + "  "
            NEXT
            t = t + $CRLF
        NEXT
    
        t= t +$CRLF + "Vector C() = " + $CRLF
    
        FOR i = 1 TO M     ' Rows
            FOR j = 1 TO N ' columns
                INCR k
                C(i,j) = VAL(READ$(k))
                t = t + STR$(C(i,j)) + "  "
            NEXT
            t = t + $CRLF
        NEXT
        '
        t = t + $CRLF + "A() = B() * C() by direct calculation:" +$CRLF
    
        ' Do direct calculation
        FOR i = 1 TO L              ' Rows
            FOR j = 1 TO N          ' Column
                a(i,j) = 0!
                FOR k = 1 TO M
                    a(i,j) = a(i,j) + b(i,k) * c(k,j)
                NEXT k
                t=t+STR$(A(i,j))+"  "
            NEXT j
            t = t + $CRLF
        NEXT i
        '
        ' Do calculation using MAT
        RESET A()
        MAT A() = B() * C()
        t = t + $CRLF + "A() = B() * C() by MAT calculation:" +$CRLF
        FOR i = 1 TO L
            FOR j = 1 TO N
                t = t + STR$(A(i,j))+"  "
            NEXT
            t= t + $CRLF
        NEXT
        '
        MSGBOX t,,"Column Vector times Row Vector (Outer Product) using MAT"
        '
        L = 1 ' Rows matrix A,      rows matrix B
        M = 4 ' Columns matrix B,   rows matrix C
        N = 1 ' Columns matrix A,   columns matrix C
    
        '     Rows    Columns
        REDIM A(1 TO L, 1 TO N)           ' Resulting matrix
        REDIM B(1 TO L, 1 TO M)           ' First matrix
        REDIM C(1 TO M, 1 TO N)           ' Second matrix
        ' matrices B and C should be conformable:
        '   the number of columns of B = the number of rows of C.
        '
        DATA 1,2,3,5    : ' matrix B
        '
        DATA 2          : ' matrix C
        DATA 4
        DATA 7
        DATA 9
        '
        ' Read arrays
        t = "vector B() = " + $CRLF
        FOR i = 1 TO L      ' Rows
            FOR j = 1 TO M  ' Columns
                INCR k
                B(i,j) = VAL(READ$(k))
                t = t + STR$(B(i,j)) + "  "
            NEXT
            t = t + $CRLF
        NEXT
        '
        t= t +$CRLF + "Vector C() = " + $CRLF
        '
        FOR i = 1 TO M     ' Rows
            FOR j = 1 TO N ' columns
                INCR k
                C(i,j) = VAL(READ$(k))
                t = t + STR$(C(i,j)) + "  "
            NEXT
            t = t + $CRLF
        NEXT
        '
        t = t + $CRLF + "A() = B() * C() by direct calculation:" +$CRLF
        '
        ' Do direct calculation
        FOR i = 1 TO L              ' Rows
            FOR j = 1 TO N          ' Columns
                a(i,j) = 0!
                FOR k = 1 TO M
                    a(i,j) = a(i,j) + b(i,k) * c(k,j)
                NEXT k
                t=t+STR$(A(i,j))+"  "
            NEXT j
            t=t+$CRLF
        NEXT i
        '
        ' Do calculation using MAT
        RESET A()
        MAT A() = B() * C()
        t = t + $CRLF + "A() = B() * C() by MAT calculation:" +$CRLF
        FOR i = 1 TO L
            FOR j = 1 TO N
                t=t+STR$(A(i,j))+"  "
            NEXT
            t= t +$CRLF
        NEXT
        '
        MSGBOX t,,"Row Vector times Column Vector (Inner Product or Dot Product) using MAT"
        '
    END FUNCTION
    ------------------


    [This message has been edited by Erik Christensen (edited July 09, 2004).]
Working...
X