Esempio n. 1
0
 def dot(self, another):
     # 矩阵点乘
     if isinstance(another, Vector):
         assert self.col_num() == len(
             another), "Error in Matrix-Vector Multiplication"
         return Vector([
             self.row_vector(i).dot(another) for i in range(self.row_num())
         ])
     if isinstance(another, Matrix):
         assert self.col_num() == another.row_num(
         ), "Error in Matrix-Matrix Multiplication"
         return Matrix([[
             self.row_vector(i).dot(another.col_vector(j))
             for j in range(another.col_num())
         ] for i in range(self.row_num())])
from LA.Vector import Vector
from LA.GramSchmidtProcess import gram_schmidt_process
from itertools import product

if __name__ == "__main__":

    basis1 = [Vector([2, 1]), Vector([1, 1])]
    res1 = gram_schmidt_process(basis1)
    for row in res1:
        print(row)

    res1 = [row / row.norm() for row in res1]
    for row in res1:
        print(row)
    print(res1[0].dot(res1[1]))
    print()

    basis2 = [Vector([2, 3]), Vector([4, 5])]
    res2 = gram_schmidt_process(basis2)
    res2 = [row / row.norm() for row in res2]
    for row in res2:
        print(row)
    print(res2[0].dot(res2[1]))
    print()

    basis3 = [Vector([1, 0, 1]), Vector([3, 1, 1]), Vector([-1, -1, -1])]
    res3 = gram_schmidt_process(basis3)
    res3 = [row / row.norm() for row in res3]
    for row in res3:
        print(row)
    print(
from LA.Matrix import Matrix
from LA.Vector import Vector
from LA.LinearSystem import LinearSystem
from LA.LinearSystem import inv

if __name__ == "__main__":

    A = Matrix([[1, 2, 4], [3, 7, 2], [2, 3, 3]])
    b = Vector([7, -11, 1])
    ls = LinearSystem(A, b)
    ls.gauss_jordan_elimination()
    ls.fancy_print()
    print()
    # [-1, -2, 3]

    A2 = Matrix([[1, -3, 5], [2, -1, -3], [3, 1, 4]])
    b2 = Vector([-9, 19, -13])
    ls2 = LinearSystem(A2, b2)
    ls2.gauss_jordan_elimination()
    ls2.fancy_print()
    print()
    # [2, -3, -4]

    A3 = Matrix([[1, 2, -2], [2, -3, 1], [3, -1, 3]])
    b3 = Vector([6, -10, -16])
    ls3 = LinearSystem(A3, b3)
    ls3.gauss_jordan_elimination()
    ls3.fancy_print()
    print()
    # [-2, 1, -3]
Esempio n. 4
0
    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()))

    I = Matrix.identity(2)
    print(I)
    print("A.dot(I) = {}".format(matrix.dot(I)))
    print("I.dot(A) = {}".format(I.dot(matrix)))
Esempio n. 5
0
from LA.Vector import Vector

if __name__ == "__main__":

    vec = Vector([5, 2])
    print(vec)
    print("len(vec) = {}".format(len(vec)))
    print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1]))

    vec2 = Vector([3, 1])
    print("{} + {} = {}".format(vec, vec2, vec + vec2))
    print("{} - {} = {}".format(vec, vec2, vec - vec2))

    print("{} * {} = {}".format(vec, 3, vec * 3))
    print("{} * {} = {}".format(3, vec, 3 * vec))

    print("+{} = {}".format(vec, +vec))
    print("-{} = {}".format(vec, -vec))
Esempio n. 6
0
 def col_vector(self, index):
     # 获取第index列向量
     return Vector([row[index] for row in self._values])
Esempio n. 7
0
 def row_vector(self, index):
     # 获取第index行向量
     return Vector(self._values[index])
Esempio n. 8
0
from LA.Vector import Vector

if __name__ == "__main__":

    vec = Vector([5, 2])
    vec2 = Vector([2, 5])
    print(vec)
    print(len(vec))
    print(vec + vec2)
    print(vec - vec2)
    print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1]))
    print("{} + {} = {}".format(vec, vec2, vec + vec2))
    print("{} - {} = {}".format(vec, vec2, vec - vec2))
    print("{} * {} = {}".format(vec, 3, vec * 3))
    print("{} * {} = {}".format(3, vec, vec * 3))
    print("+{} = {}".format(vec, +vec))
    print("-{} = {}".format(vec, -vec))

    zero2 = Vector.zero(2)
    print(zero2)

    print("norm {} is {}".format(vec, vec.norm()))
    print("normalize {} is {}".format(vec, vec.normalize()))
    try:
        print("normalize {} is {}".format(zero2, zero2.normalize()))
    except ZeroDivisionError:
        print("Cannot normalize zero vector {}".format(zero2))

    print("{} dot {} = {}".format(vec, vec2, vec.dot(vec2)))
Esempio n. 9
0
from LA.Vector import Vector

if __name__ == '__main__':
    vec = Vector([5, 2])
    print(vec)
    print("len(vec) = {}".format(len(vec)))
    print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1]))
    print("vec = {}".format(str(vec)))
    print(repr(vec))
Esempio n. 10
0
from LA.Matrix import Matrix
from LA.Vector import Vector
from LA.LinearSystem import LinearSystem


if __name__ == "__main__":

    A = Matrix([[1, 2, 4], [3, 7, 2], [2, 3, 3]])
    b = Vector([7, -11, 1])
    ls = LinearSystem(A, b)
    ls.gauss_jordan_elimination()
    ls.fancy_print()
    print()

    A2 = Matrix([[1, -3, 5], [2, -1, -3], [3, 1, 4]])
    b2 = Vector([-9, 19, -13])
    ls2 = LinearSystem(A2, b2)
    ls2.gauss_jordan_elimination()
    ls2.fancy_print()
    print()

    A3 = Matrix([[1, 2, -2], [2, -3, 1], [3, -1, 3]])
    b3 = Vector([6, -10, -16])
    ls3 = LinearSystem(A3, b3)
    ls3.gauss_jordan_elimination()
    ls3.fancy_print()
    print()

    A4 = Matrix([[3, 1, -2], [5, -3, 10], [7, 4, 16]])
    b4 = Vector([4, 32, 13])
    ls4 = LinearSystem(A4, b4)
Esempio n. 11
0
from LA.Matrix import Matrix
from LA.Vector import Vector

if __name__ == "__main__":
    matrix = Matrix([[1, 2], [3, 4]])
    matrix2 = Matrix([[5, 6], [7, 8]])
    print(matrix)
    print("matrix.shape = {}".format(matrix.shape()))
    print("matrix.row_num = {}".format(matrix.row_num()))
    print("matrix.col_num = {}".format(matrix.col_num()))
    print("matrix.size = {}".format(matrix.size()))
    print("matrix.length = {}".format(len(matrix)))
    print("matrix[0][0] = {}".format(matrix[0, 0]))
    print("matrix.row_vector = {}".format(matrix.row_vector(0)))
    print("matrix.col_vector = {}".format(matrix.col_vector(0)))
    print("add: {}".format(matrix + matrix2))
    print("sub: {}".format(matrix - matrix2))
    print("matrix * 3 = {}".format(matrix * 3))
    print("3 * matrix = {}".format(matrix * 3))
    print("3 * matrix = {}".format(matrix * 3))
    print("zero_2_3 = {}".format(Matrix.zero(2, 3)))
    print("matrix.dot((1, 2)) = {}".format(matrix.dot(Vector([1, 2]))))
    print("matrix1.dot(matrix2) = {}".format(matrix.dot(matrix2)))
    P = Matrix([[1, 2, 3], [4, 5, 6]])
    print("P.T() = {}".format(P.T()))
Esempio n. 12
0
from LA.Vector import Vector

if __name__ == "__main__":

    vec = Vector([5, 2])
    print(vec)
    print("len(vec) = {}".format(len(vec)))
    print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1]))

    vec2 = Vector([3, 1])
    print("{} + {} = {}".format(vec, vec2, vec + vec2))
    print("{} - {} = {}".format(vec, vec2, vec - vec2))

    print("{} * {} = {}".format(vec, 3, vec * 3))
    print("{} * {} = {}".format(3, vec, 3 * vec))

    print("+{} = {}".format(vec, +vec))
    print("-{} = {}".format(vec, -vec))

    zero2 = Vector.zero(2)
    print(zero2)
    print("{} + {} = {}".format(vec, zero2, vec + zero2))
Esempio n. 13
0
from LA.Vector import Vector

if __name__ == "__main__":

    vec = Vector([5, 2])
    print(vec)
    print("len(vec) = {}".format(len(vec)))
    print("vec[0] = {}, vec[1] = {}".format(vec[0], vec[1]))

    vec2 = Vector([3, 1])
    print("{} + {} = {}".format(vec, vec2, vec + vec2))
    print("{} - {} = {}".format(vec, vec2, vec - vec2))

    print("{} * {} = {}".format(vec, 3, vec * 3))
    print("{} * {} = {}".format(3, vec, 3 * vec))

    print("+{} = {}".format(vec, +vec))
    print("-{} = {}".format(vec, -vec))

    zero2 = Vector.zero(2)
    print(zero2)
    print("{} + {} = {}".format(vec, zero2, vec + zero2))

    print("norm({}) = {}".format(vec, vec.norm()))
    print("norm({}) = {}".format(vec2, vec2.norm()))
    print("norm({}) = {}".format(zero2, zero2.norm()))

    print("normalize {} is {}".format(vec, vec.normalize()))
    print(vec.normalize().norm())

    print("normalize {} is {}".format(vec2, vec2.normalize()))