from playLA.Matrix import Matrix

if __name__ == "__main__":

    matrix = Matrix([[1, 2], [3, 4]])
    print(matrix)
    print("matrix.shape = {}".format(matrix.shape()))
    print("matrix.size = {}".format(matrix.size()))
    print("len(matrix) = {}".format(len(matrix)))
    print("matrix[0][0] = {}".format(matrix[0, 0]))

    matrix2 = Matrix([[5, 6], [7, 8]])
    print(matrix2)
    print("add: {}".format(matrix + matrix2))
    print("subtract: {}".format(matrix - matrix2))
    print("scalar-mul: {}".format(2 * matrix))
    print("scalar-mul: {}".format(matrix * 2))
    print("zero_2_3: {}".format(Matrix.zero(2, 3)))

    T = Matrix([[1.5, 0], [0, 2]])
    p = Vector([5, 3])
    print("T.dot(p) = {}".format(T.dot(p)))

    P = Matrix([[0, 4, 5], [0, 0, 3]])
    print("T.dot(P) = {}".format(T.dot(P)))

    print("A.dot(B) = {}".format(matrix.dot(matrix2)))
    print("B.dot(A) = {}".format(matrix2.dot(matrix)))

    print("P.T = {}".format(P.T()))
Exemplo n.º 2
0
from playLA.Matrix import Matrix
from playLA.Vector import Vector

if __name__ == "__main__":
    T = Matrix([[1.5, 0], [0, 2], [1, 9]])
    p = Vector([5, 3])
    print(T.dot(p))
    print(T.T())
Exemplo n.º 3
0
if __name__ == '__main__':
    matrix = Matrix([[1, 2], [3, 4]])
    print(matrix)
    print("矩阵的形状:{}".format(matrix.shape()))
    print("矩阵的行数:{},矩阵的列数:{}".format(matrix.row_num(), matrix.col_num()))
    print("矩阵的元素个数:{}".format(matrix.size()))
    print("矩阵元素:{}".format(matrix[1, 1]))
    print("矩阵行向量:{}".format(matrix.row_vector(1)))
    print("矩阵列向量:{}".format(matrix.col_vector(1)))
    m2 = Matrix([[2, 4], [4, 6]])
    print("add:{}".format(matrix + m2))
    print("sub:{}".format(matrix - m2))
    print("multi:{}".format(matrix * 2))
    print("rMulti:{}".format(2 * matrix))
    print("div:{}".format(m2 / 2))
    print("neg:{}".format(-m2))
    print("零矩阵:{}".format(Matrix.zero(3, 2)))

    T = Matrix([[1.5, 0], [0, 2]])
    p = Vector([5, 3])
    print("T.dot(p) = {}".format(T.dot(p)))

    T2 = Matrix([[0, 4, 5], [0, 0, 3]])
    print("T.dot(T2) = {}".format(T.dot(T2)))

    print("转置:{}".format(T2.T()))

    I = Matrix.identity(2)
    print("2行2列的单位矩阵:{}".format(I))
    print("matrix乘以单位矩阵:{}".format(matrix.dot(I)))
              [1, 2], [1, 0]]

    x = [point[0] for point in points]
    y = [point[1] for point in points]

    plt.figure(figsize=(5, 5))
    plt.xlim(-10, 10)
    plt.ylim(-10, 10)

    plt.plot(x, y)
    # plt.show()

    P = Matrix(points)
    # T = Matrix([[2, 0], [0, 1.5]])
    # T = Matrix([[1, 0], [0, -1]])
    # T = Matrix([[-1, 0], [0, 1]])
    # T = Matrix([[-1, 0], [0, -1]])
    # T = Matrix([[1, 0.5], [0, 1]])
    # T = Matrix([[1, 0], [0.5, 1]])

    # theta = math.pi / 3
    # T = Matrix([[math.cos(theta), math.sin(theta)],
    #             [-math.sin(theta), math.cos(theta)]])
    root2 = math.sqrt(2) / 2
    T = Matrix([[root2, -root2], [root2, root2]])

    P2 = T.dot(P.T())
    plt.plot([P2.col_vector(i)[0] for i in range(P2.col_num())],
             [P2.col_vector(i)[1] for i in range(P2.col_num())])
    plt.show()
Exemplo n.º 5
0
from playLA.Matrix import Matrix
from playLA.Vector import Vector
if __name__ == "__main__":
    matrix = Matrix([[1, 2], [3, 4]])
    print(matrix)
    matrix2 = Matrix([[5, 6], [7, 8]])
    print("matrix.shape={}".format(matrix.shape()))
    print(matrix.size())
    print(len(matrix))
    print(matrix.__getitem__((0, 1)))
    print(matrix[0, 0])
    print(matrix.row_vector(0))
    print(matrix.col_vector(1))
    print(matrix + matrix2)
    print(matrix - matrix2)
    print(matrix * 2)
    print(2 * matrix)
    print(Matrix.zero(2, 3))
    T = Matrix([[1.5, 0], [0, 2]])
    p = Vector([5, 3])
    print(T.dot(p))

    P = Matrix([[0, 4, 5], [0, 0, 3]])
    print(T.dot(P))
    print(matrix.dot(matrix2))
    print(matrix2.dot(matrix))
    print(P.T())

    I = Matrix.identity(2)
    print(I)
    print(matrix2.dot(I))
Exemplo n.º 6
0
    x = [point[0] for point in points]
    y = [point[1] for point in points]

    plt.figure(figsize=(5, 5))
    plt.xlim(-10, 10)
    plt.ylim(-10, 10)

    plt.plot(x, y)
    # plt.show()

    P = Matrix(points)
    # P 10*2
    # T 2*2 变换矩阵,可称为函数
    # T = Matrix([[2, 0], [0, 1.5]]) # 放大
    # T = Matrix([[1, 0], [0, -1]])  # x轴翻转
    # T = Matrix([[-1, 0], [0, 1]])  # y轴翻转
    # T = Matrix([[-1, 0], [0, -1]])  # 完全翻转
    # T = Matrix([[1, 0.5], [0, 1]])  # x轴错切
    # T = Matrix([[1, 0], [0.5, 1]])  # y轴错切
    # theta = math.pi / 3  # 1/3度
    # T = Matrix([[math.cos(theta), math.sin(theta)], [-math.sin(theta), math.cos(theta)]]) # 旋转theta度

    T = Matrix([[0, -1], [1, 0]])


    P2 = T.dot(P.T())  # 因为要用T作为函数乘以P,需满足T的列为2,P的行为2,但P是10*2,T是2*2,所以要将P转置成2*10再相乘
    plt.plot([P2.col_vector(i)[0] for i in range(P2.col_num())],
             [P2.col_vector(i)[1] for i in range(P2.col_num())])

    plt.show()
Exemplo n.º 7
0
from playLA.Matrix import Matrix

if __name__ == "__main__":
    matrix = Matrix([[1, 2], [3, 4]])
    matrix1 = Matrix([[1, 2], [3, 4]])

    print("matrix={}".format(matrix + matrix1))

    print("matrix={}".format(matrix - matrix1))

    print("matrix={}".format(matrix * 2))

    print(len(matrix))

    print(matrix[0, 1])

    print(matrix.col_vector(1))

    print(matrix.dot(matrix1))

    print("{}.t = {}".format(matrix1, matrix1.T()))

    print("{}".format(matrix[1, 1]))

    print("{}".format(Matrix.identity(3)))
Exemplo n.º 8
0
    x = [point[0] for point in points]
    y = [point[1] for point in points]

    plt.figure(figsize=(5, 5))
    plt.xlim(-10, 10)
    plt.ylim(-10, 10)

    plt.plot(x, y)
    # plt.show()

    P = Matrix(points)
    # print(P)

    # T = Matrix([[2, 0], [0, 1.5]])  # T: 2 * 2   P: 10 * 2
    # T = Matrix([[1, 0], [0, -1]])
    # T = Matrix([[-1, 0], [0, 1]])
    # T = Matrix([[-1, 0], [0, -1]])
    # T = Matrix([[1, 1], [0, 1]])
    # T = Matrix([[1, 0], [1, 1]])
    # T = Matrix([[1, 0.5], [1, 1]])
    # T = Matrix([[1, 0.5], [1, 1]])
    theta = math.pi / 3
    T = Matrix([[math.cos(theta), math.sin(theta)],
                [-math.sin(theta), math.cos(theta)]])

    P2 = T.dot(P.T())  # P2: 2 * 10
    # print(P2)
    plt.plot([P2.col_vector(i)[0] for i in range(P2.col_num())],
             [P2.col_vector(i)[1] for i in range(P2.col_num())])
    plt.show()