def mul_matrix(m1:list, m2:list)->list:
    '''
    Input of this function is two 2d list.
    Output is a 2d list. If the result is a number,
    then return [[num]].
    Assume both matrixs are well structured (row & col)
    '''
    if len(m1[0]) != len(m2):                               # verify valid multiplication
        print("Invalid multiplication attempted.")
        return None
    nrow = len(m1)                                          # row number of product
    ncol = len(m2[0])                                       # col number of product
    product = []
    temp = []
    s = 0
    m2 = transpose(m2)                                      # transpose for simplicity
    for rowm1 in m1:
        for rowm2 in m2:
            for i in range(len(m1[0])):
                s += rowm1[i] * rowm2[i]
            temp.append(s)                                  # product's element
            s = 0                                           # reset to zero in next loop
        product.append(temp)                                # product's row
        temp = []                                           # reset to empty list
    return product
示例#2
0
def inverse(M):
    if (len(M) is not 4 or len(M[0]) is not 4):
        raise ValueError('Matrix not 4X4')

    MI = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    # extract rotational matrix
    R = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    for i in range(3):
        for j in range(3):
            R[i][j] = M[i][j]

    # extract translation matrix
    P = [[M[0][3]], [M[1][3]], [M[2][3]]]

    #calc inverse of the rotation
    RI = transpose(M)

    #negative RI
    P = negative(P)

    #calc positional component
    PI = multiply(RI, P)

    # putting values of R-transpose in new matrix
    for idx, i in enumerate(RI):
        for jdx, j in enumerate(i):
            MI[idx][jdx] = RI[idx][jdx]

    # putting values of -(R-transpose * P) in new matrix
    MI[0][3] = PI[0][0]
    MI[1][3] = PI[1][0]
    MI[2][3] = PI[2][0]
    MI[3][3] = 1

    return (MI)
示例#3
0
def validsolution(l):
    k = transpose(l)
    for i in l:
        if validrow(i) != True:
            return False
    for j in k:
        if validrow(j) != True:
            return False
    for k in [0,3,6]:
        if validrow(blocktorows(l[k:k+3])) != True:
            return False
    return True
示例#4
0
def multiply(A, B):
  if dimensionErr(A, B):
    print("ERROR: Matrices dimensions do not allow multiplication.")
    exit(0)
  BT = transpose(B)
  C = []
  for rowA in A:
    rowC = []
    for rowBT in BT:
      entry = 0
      for i in range(len(A[0])):
        entry += rowA[i] * rowBT[i]
      rowC.append(entry)
    C.append(rowC)
  return C
        print("Invalid multiplication attempted.")
        return None
    nrow = len(m1)                                          # row number of product
    ncol = len(m2[0])                                       # col number of product
    product = []
    temp = []
    s = 0
    m2 = transpose(m2)                                      # transpose for simplicity
    for rowm1 in m1:
        for rowm2 in m2:
            for i in range(len(m1[0])):
                s += rowm1[i] * rowm2[i]
            temp.append(s)                                  # product's element
            s = 0                                           # reset to zero in next loop
        product.append(temp)                                # product's row
        temp = []                                           # reset to empty list
    return product


if __name__ == '__main__':

    M1 = [[1,2,0,1],
          [4,3,1,4],
          [0,1,1,4],
          [0,0,1,1],
          [1,2,1,3]]
    M2 = transpose(M1)
    res = mul_matrix(M1, M2)
    print(res)