Major changes preparing to satisfy given test_serial.py
This commit is contained in:
@ -87,6 +87,14 @@ class Matrix:
|
||||
"""
|
||||
return Matrix(self.__data__.transpose())
|
||||
|
||||
def T(self):
|
||||
"""
|
||||
Same as ``matrix.transpose()``
|
||||
|
||||
:return: see ``matrix.transpose()``
|
||||
"""
|
||||
return self.transpose()
|
||||
|
||||
def __eq__(self, other):
|
||||
"""
|
||||
Return ``self==value``
|
||||
|
@ -1,13 +1,17 @@
|
||||
import numpy
|
||||
|
||||
from matrix import Matrix
|
||||
|
||||
|
||||
class Vector(Matrix):
|
||||
def __init__(self, data: list | int):
|
||||
def __init__(self, data):
|
||||
"""
|
||||
|
||||
:type data: list | int
|
||||
:type data: numpy.ndarray | list | int
|
||||
"""
|
||||
if isinstance(data, list):
|
||||
if isinstance(data, numpy.ndarray):
|
||||
super().__init__(data)
|
||||
elif isinstance(data, list):
|
||||
super().__init__(data, (len(data), 1))
|
||||
elif isinstance(data, int):
|
||||
self.__init__([0] * data)
|
||||
@ -17,11 +21,16 @@ class Vector(Matrix):
|
||||
def get_dimension(self):
|
||||
return super().shape()[0]
|
||||
|
||||
def transpose(self):
|
||||
return Vector(self.__data__.reshape(self.__shape__[1], self.__shape__[0]))
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Vector):
|
||||
return (super().transpose().__mul__(other))[0][0]
|
||||
if self.shape() == other.shape():
|
||||
return Vector(self.__data__ * other.__data__)
|
||||
return super().__mul__(other)[0][0]
|
||||
elif isinstance(other, int) or isinstance(other, float):
|
||||
return super().__mul__(other)
|
||||
return Vector(super().__mul__(other).__data__)
|
||||
else:
|
||||
raise ValueError("A vector can only be multiplied with an vector (dot product) or a scalar")
|
||||
|
||||
@ -29,7 +38,19 @@ class Vector(Matrix):
|
||||
return self * other
|
||||
|
||||
def norm(self, **kwargs):
|
||||
"""
|
||||
Computes the 2-norm of the vector which is the Frobenius-Norm of a nx1 matrix.
|
||||
|
||||
:param kwargs: ignored
|
||||
:return: the 2-norm of the vector
|
||||
"""
|
||||
return super().norm()
|
||||
|
||||
def normalize(self):
|
||||
"""
|
||||
A normalized vector has the length (norm) 1.
|
||||
To achieve that the vector is divided by the norm of itself.
|
||||
|
||||
:return: the normalized vector
|
||||
"""
|
||||
return self / self.norm()
|
||||
|
Reference in New Issue
Block a user