Ejemplo n.º 1
0
def test_vector_zero():
    """
    全ての緯度の値が0のvectorを生成する
    :return:
    """
    zero2 = Vector.zero(2)
    assert str(zero2) == str((0, 0))
Ejemplo n.º 2
0
def init_vector2():
    """
    (3, 1)のvectorオブジェクトを初期化
    :return:
    """
    vec = Vector([3, 1])
    return vec
Ejemplo n.º 3
0
def init_vector():
    """
    (5,2)のvectorオブジェクトを初期化
    :return:
    """
    vec = Vector([5, 2])
    return vec
Ejemplo n.º 4
0
 def col_vector(self, index):
     """
     マトリクスのindex個の列vector
     :param index:
     :return:
     """
     return Vector([row[index] for row in self._value])
Ejemplo n.º 5
0
 def row_vector(self, index):
     """
     第何行の内容
     :param index:
     :return:
     """
     return Vector(self._value[index])
Ejemplo n.º 6
0
 def dot(self, another):
     """
     マトリクスのドット掛け算
     :param another:
     :return: 掛け算の結果
     """
     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())])
Ejemplo n.º 7
0
    print("len(matrix)={}".format(len(matrix)))  # 2
    print("matrix[0][0]={}".format(matrix[0, 0]))  # matrix[0][0]=1
    print("matrix")

    matrix2 = Matrix([[5, 6], [7, 8]])
    print("add:{}".format(matrix + matrix2))  # Matrix([[[6, 8]], [[10, 12]]])
    print("sub:{}".format(matrix -
                          matrix2))  # Matrix([[[-4, -4]], [[-4, -4]]])
    print("scalar-mul:{}".format(matrix *
                                 2))  # scalar-mul:Matrix([[2, 4], [6, 8]])
    print("scalar-mul:{}".format(
        2 * matrix))  # scalar-mul:Matrix([[2, 4], [6, 8]])
    print(Matrix.zero(2, 3))  # Matrix([[0, 0, 0], [0, 0, 0]])

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

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

    print("A.dot(B) = {}".format(
        matrix.dot(matrix2)))  # A.dot(B) = Matrix([[19, 22], [43, 50]])
    print("B.dot(A) = {}".format(
        matrix2.dot(matrix)))  # B.dot(A) = Matrix([[23, 34], [31, 46]])

    print("P.T = {}".format(P.T()))  # P.T = Matrix([[0, 0], [4, 0], [5, 3]])
    I = Matrix.identity(2)
    print('I=', I)  # I= Matrix([[1, 0], [0, 1]])
    print("A.dot(I) = {}".format(matrix.dot(I)))
Ejemplo n.º 8
0
__author__ = "ハリネズミ"
from useVector.Matrix import Matrix
from useVector.Vector import Vector
from useVector.LinearSystem import LinearSystem
from useVector.LinearSystem import inv


if __name__ == "__main__":
    A = Matrix([[1, 2, 3], [3, 7, 2], [2, 3, 3]])
    b = Vector([7, -11, 1])
    ls = LinearSystem(A, b)
    ls.gauss_jordan_elimination()
    ls.fancy_print()

    A7 = Matrix([[1, -1, 2, 0, 3],
                 [-1, 1, 0, 2, -5],
                 [1, -1, 4, 2, 4],
                 [-2, 2, -5, -1, -3]])
    b7 = Vector([1, 5, 13, -1])
    ls7 = LinearSystem(A7, b7)
    ls7.gauss_jordan_elimination()
    ls7.fancy_print()

    A = Matrix([[1, 2], [3, 4]])
    invA = inv(A)
    print(invA)
    print(A.dot(invA))
    print(invA.dot(A))
Ejemplo n.º 9
0
def test_vector_norm(init_vector):
    """
    vectorの長さ(ノルム)
    """
    vec = math.sqrt(sum(e**2 for e in Vector([5, 2])))
    assert vec == init_vector.norm()
Ejemplo n.º 10
0
from useVector.Vector import Vector

if __name__ == "__main__":

    vec = Vector([5, 2])
    print(vec)  # (5, 2)
    print(len(vec))  # 2
    print(vec[0])  # 5

    vec2 = Vector([3, 1])
    print(vec + vec2)  # (8, 6)
    print(vec - vec2)  # (2, 1)
    print(vec * 2)  # (10, 4)
    print(2 * vec)  # (10, 4)

    print(-vec)  # (-5, -2)
    print(+vec)  # (5, 2)

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

    print(vec.norm())  # 5.385164807134504

    print(vec.normalize().norm())  # 1.0

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