' 2D Matrix Operations Using 1D Arrays — add, multiply, transpose
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    DIM a(1 TO 9) AS LONG
    DIM b(1 TO 9) AS LONG
    DIM c(1 TO 9) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL sum AS LONG
    LOCAL idxA AS LONG
    LOCAL idxB AS LONG
    LOCAL idxC AS LONG

    PRINT "==================================="
    PRINT " 1D Array Matrix Operations"
    PRINT "==================================="
    PRINT

    k = 1
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            a(k) = i * 10 + j
            b(k) = j * 10 + i
            k = k + 1
        NEXT j
    NEXT i

    PRINT "=== 1D ARRAY MATRIX ADDITION (3x3) ==="
    PRINT "Matrix A:"
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            PRINT FORMAT$(a((i - 1) * 3 + j), "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "Matrix B:"
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            PRINT FORMAT$(b((i - 1) * 3 + j), "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "A + B:"
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            idxC = (i - 1) * 3 + j
            c(idxC) = a(idxC) + b(idxC)
            PRINT FORMAT$(c(idxC), "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT

    a(1) = 1
    a(2) = 2
    a(3) = 3
    a(4) = 4
    a(5) = 5
    a(6) = 6
    b(1) = 7
    b(2) = 8
    b(3) = 9
    b(4) = 10
    b(5) = 11
    b(6) = 12

    PRINT "=== 1D ARRAY MATRIX MULTIPLY (2x3 * 3x2) ==="
    PRINT "Matrix A (2x3):"
    FOR i = 1 TO 2
        FOR j = 1 TO 3
            PRINT FORMAT$(a((i - 1) * 3 + j), "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "Matrix B (3x2):"
    FOR i = 1 TO 3
        FOR j = 1 TO 2
            PRINT FORMAT$(b((i - 1) * 2 + j), "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "A * B (2x2):"
    FOR i = 1 TO 2
        FOR j = 1 TO 2
            sum = 0
            FOR k = 1 TO 3
                idxA = (i - 1) * 3 + k
                idxB = (k - 1) * 2 + j
                sum = sum + a(idxA) * b(idxB)
            NEXT k
            idxC = (i - 1) * 2 + j
            c(idxC) = sum
            PRINT FORMAT$(sum, "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT

    a(1) = 1
    a(2) = 2
    a(3) = 3
    a(4) = 4
    a(5) = 5
    a(6) = 6

    PRINT "=== 1D ARRAY MATRIX TRANSPOSE (2x3 -> 3x2) ==="
    PRINT "Original (2x3):"
    FOR i = 1 TO 2
        FOR j = 1 TO 3
            PRINT FORMAT$(a((i - 1) * 3 + j), "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "Transposed (3x2):"
    FOR i = 1 TO 2
        FOR j = 1 TO 3
            b((j - 1) * 2 + i) = a((i - 1) * 3 + j)
        NEXT j
    NEXT i
    FOR i = 1 TO 3
        FOR j = 1 TO 2
            PRINT FORMAT$(b((i - 1) * 2 + j), "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT

    a(1) = 1
    a(2) = 2
    a(3) = 3
    a(4) = 4
    a(5) = 5
    a(6) = 6
    a(7) = 7
    a(8) = 8
    a(9) = 9

    PRINT "=== MATRIX TRACE (3x3) ==="
    PRINT "Matrix:"
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            PRINT FORMAT$(a((i - 1) * 3 + j), "#####")
        NEXT j
        PRINT
    NEXT i
    PRINT
    sum = 0
    FOR i = 1 TO 3
        sum = sum + a((i - 1) * 3 + i)
    NEXT i
    PRINT "Trace (sum of diagonal): " & STR$(sum)
    PRINT

    PRINT "Matrix operations complete."
    FUNCTION = 0
END FUNCTION
