Grading Code: Check if the rows of a matrix form a basis of a 2 dimensional vector space


  • TA School

    #
    # Checks if input is a basis of some 2d vector space
    #
    # Takes:    mat        (matrix with rows as basis vectors)
    #           ans1, ans2 (some correct basis vectors)
    #
    mat  := Matrix([[1,1],[0,3]]):
    ans1 := Matrix([1,1]):
    ans2 := Matrix([0,3]):
    isMultiple := proc(vec1, vec2):
        matCheck := LinearAlgebra:-Transpose(Matrix([[vec1], [vec2]])):
        ans := 0:
        if evalb(LinearAlgebra:-Determinant(matCheck) = 0) then:
            ans := 1:
        end if:
        ans:
    end proc:
    result := "unknown":
    dim := LinearAlgebra:-Dimension(mat):
    if (evalb(dim[1] <> 2) or evalb(dim[2] <> 2)) then:
        result := 0:
    else:
        v1 := mat[1, 1..2]:
        v2 := mat[2, 1..2]:
        if evalb(isMultiple(v1,v2) = 0) then:
            if evalb(isMultiple(v1, ans1) = 1) then:
                if evalb(isMultiple(v2, ans2) = 1) then:
                    result := "1":
                else:
                    result := "0":
                end if:
            elif evalb(isMultiple(v2, ans1) = 1) then:
                if evalb(isMultiple(v1, ans2) = 1) then:
                    result := "1":
                else:
                    result := "0":
                end if:
            else
                result := "0":
            end if:
        else:
            result := "0":
        end if:
    end if:
    print(result);