' Matrix Operations — add, multiply, transpose, determinant (3x3)
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    DIM a(1 TO 9) AS DOUBLE
    DIM b(1 TO 9) AS DOUBLE
    DIM c(1 TO 9) AS DOUBLE
    DIM m(1 TO 9) AS DOUBLE
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL idx AS LONG
    LOCAL sum AS DOUBLE
    LOCAL det AS DOUBLE

    PRINT "========================="
    PRINT " Matrix Operations"
    PRINT "========================="
    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
    b(1) = 9
    b(2) = 8
    b(3) = 7
    b(4) = 6
    b(5) = 5
    b(6) = 4
    b(7) = 3
    b(8) = 2
    b(9) = 1

    PRINT "=== MATRIX ADDITION ==="
    PRINT "Matrix A:"
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            idx = (i - 1) * 3 + j
            PRINT FORMAT$(a(idx), "####.##")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "Matrix B:"
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            idx = (i - 1) * 3 + j
            PRINT FORMAT$(b(idx), "####.##")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "A + B:"
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            idx = (i - 1) * 3 + j
            c(idx) = a(idx) + b(idx)
            PRINT FORMAT$(c(idx), "####.##")
        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 "=== MATRIX MULTIPLICATION ==="
    PRINT "Matrix A (2x3):"
    FOR i = 1 TO 2
        FOR j = 1 TO 3
            idx = (i - 1) * 3 + j
            PRINT FORMAT$(a(idx), "####.##")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "Matrix B (3x2):"
    FOR i = 1 TO 3
        FOR j = 1 TO 2
            idx = (i - 1) * 2 + j
            PRINT FORMAT$(b(idx), "####.##")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "A * B (2x2):"
    FOR i = 1 TO 2
        FOR j = 1 TO 2
            sum = 0.0
            FOR k = 1 TO 3
                sum = sum + a((i - 1) * 3 + k) * b((k - 1) * 2 + j)
            NEXT k
            c((i - 1) * 2 + j) = 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 "=== MATRIX TRANSPOSE ==="
    PRINT "Original (3x2):"
    FOR i = 1 TO 3
        FOR j = 1 TO 2
            idx = (i - 1) * 2 + j
            PRINT FORMAT$(a(idx), "####.##")
        NEXT j
        PRINT
    NEXT i
    PRINT
    PRINT "Transposed (2x3):"
    FOR i = 1 TO 3
        FOR j = 1 TO 2
            b((j - 1) * 3 + i) = a((i - 1) * 2 + j)
        NEXT j
    NEXT i
    FOR i = 1 TO 2
        FOR j = 1 TO 3
            idx = (i - 1) * 3 + j
            PRINT FORMAT$(b(idx), "####.##")
        NEXT j
        PRINT
    NEXT i
    PRINT

    m(1) = 2
    m(2) = -1
    m(3) = 0
    m(4) = 3
    m(5) = 1
    m(6) = 4
    m(7) = -2
    m(8) = 0
    m(9) = 1

    PRINT "=== 3x3 DETERMINANT ==="
    PRINT "Matrix M:"
    FOR i = 1 TO 3
        FOR j = 1 TO 3
            idx = (i - 1) * 3 + j
            PRINT FORMAT$(m(idx), "####.##")
        NEXT j
        PRINT
    NEXT i
    PRINT
    det = m(1) * (m(5) * m(9) - m(6) * m(8)) - m(2) * (m(4) * m(9) - m(6) * m(7)) + m(3) * (m(4) * m(8) - m(5) * m(7))
    PRINT "det(M) = " & FORMAT$(det, "####.##")
    PRINT

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