#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL m1(15) AS DOUBLE
    LOCAL m2(15) AS DOUBLE
    LOCAL r(15) AS DOUBLE
    LOCAL ident(15) AS DOUBLE
    LOCAL det_val AS DOUBLE
    LOCAL inv_val AS DOUBLE
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL temp_str AS STRING

    ' Identity matrix
    i = 0
    WHILE i < 16
        ident(i) = 0
        i = i + 1
    WEND
    ident(0 * 4 + 0) = 1
    ident(1 * 4 + 1) = 1
    ident(2 * 4 + 2) = 1
    ident(3 * 4 + 3) = 1

    PRINT "4x4 Matrix Operations"
    PRINT "====================="
    PRINT ""

    PRINT "Identity matrix:"
    i = 0
    WHILE i < 4
        temp_str = ""
        j = 0
        WHILE j < 4
            temp_str = temp_str + STR$(ident(i * 4 + j))
            j = j + 1
        WEND
        PRINT temp_str
        i = i + 1
    WEND

    ' Matrix 1: translation-like
    m1(0 * 4 + 0) = 1
    m1(0 * 4 + 1) = 0
    m1(0 * 4 + 2) = 0
    m1(0 * 4 + 3) = 5
    m1(1 * 4 + 0) = 0
    m1(1 * 4 + 1) = 1
    m1(1 * 4 + 2) = 0
    m1(1 * 4 + 3) = 10
    m1(2 * 4 + 0) = 0
    m1(2 * 4 + 1) = 0
    m1(2 * 4 + 2) = 1
    m1(2 * 4 + 3) = 15
    m1(3 * 4 + 0) = 0
    m1(3 * 4 + 1) = 0
    m1(3 * 4 + 2) = 0
    m1(3 * 4 + 3) = 1

    ' Matrix 2: rotation-like
    m2(0 * 4 + 0) = 0
    m2(0 * 4 + 1) = 1
    m2(0 * 4 + 2) = 0
    m2(0 * 4 + 3) = 0
    m2(1 * 4 + 0) = -1
    m2(1 * 4 + 1) = 0
    m2(1 * 4 + 2) = 0
    m2(1 * 4 + 3) = 0
    m2(2 * 4 + 0) = 0
    m2(2 * 4 + 1) = 0
    m2(2 * 4 + 2) = 1
    m2(2 * 4 + 3) = 0
    m2(3 * 4 + 0) = 0
    m2(3 * 4 + 1) = 0
    m2(3 * 4 + 2) = 0
    m2(3 * 4 + 3) = 1

    ' Multiply: r = m1 * m2
    i = 0
    WHILE i < 4
        j = 0
        WHILE j < 4
            r(i * 4 + j) = 0
            k = 0
            WHILE k < 4
                r(i * 4 + j) = r(i * 4 + j) + m1(i * 4 + k) * m2(k * 4 + j)
                k = k + 1
            WEND
            j = j + 1
        WEND
        i = i + 1
    WEND

    PRINT ""
    PRINT "M1 * M2:"
    i = 0
    WHILE i < 4
        temp_str = ""
        j = 0
        WHILE j < 4
            temp_str = temp_str + STR$(r(i * 4 + j))
            j = j + 1
        WEND
        PRINT temp_str
        i = i + 1
    WEND

    ' Transpose of M1
    i = 0
    WHILE i < 4
        j = 0
        WHILE j < 4
            r(i * 4 + j) = m1(j * 4 + i)
            j = j + 1
        WEND
        i = i + 1
    WEND

    PRINT ""
    PRINT "Transpose(M1):"
    i = 0
    WHILE i < 4
        temp_str = ""
        j = 0
        WHILE j < 4
            temp_str = temp_str + STR$(r(i * 4 + j))
            j = j + 1
        WEND
        PRINT temp_str
        i = i + 1
    WEND

    ' Determinant of 4x4 (using 3x3 minors)
    ' Simple test with an easy matrix
    m1(0) = 1
    m1(1) = 0
    m1(2) = 0
    m1(3) = 0
    m1(4) = 0
    m1(5) = 2
    m1(6) = 0
    m1(7) = 0
    m1(8) = 0
    m1(9) = 0
    m1(10) = 3
    m1(11) = 0
    m1(12) = 0
    m1(13) = 0
    m1(14) = 0
    m1(15) = 4

    det_val = m1(0) * (m1(5) * (m1(10) * m1(15) - m1(11) * m1(14)) - m1(6) * (m1(9) * m1(15) - m1(11) * m1(13)) + m1(7) * (m1(9) * m1(14) - m1(10) * m1(13)))
    PRINT ""
    PRINT "Determinant: " + STR$(det_val)

    ' Multiply identity * vector (3,4,5,1)
    PRINT ""
    PRINT "Transform (3,4,5,1) by identity:"
    i = 0
    temp_str = ""
    WHILE i < 4
        inv_val = 0
        j = 0
        WHILE j < 4
            IF j = 0 THEN inv_val = inv_val + ident(i * 4 + j) * 3
            IF j = 1 THEN inv_val = inv_val + ident(i * 4 + j) * 4
            IF j = 2 THEN inv_val = inv_val + ident(i * 4 + j) * 5
            IF j = 3 THEN inv_val = inv_val + ident(i * 4 + j) * 1
            j = j + 1
        WEND
        temp_str = temp_str + STR$(inv_val)
        i = i + 1
    WEND
    PRINT "(" + temp_str + ")"

    PRINT ""
    PRINT "4x4 matrix operations complete."

    FUNCTION = 0
END FUNCTION
