Add class matrix.py (wip)
This commit is contained in:
@ -1,2 +1,35 @@
|
||||
import numpy
|
||||
|
||||
|
||||
class Matrix:
|
||||
|
||||
__data__ = []
|
||||
__shape__ = ()
|
||||
|
||||
def __init__(self, data=None, shape=None, structure=None, model=None, size=None):
|
||||
if isinstance(data, numpy.ndarray):
|
||||
try:
|
||||
data.shape[1]
|
||||
except IndexError:
|
||||
self.__shape__ = (data.shape[0], 1)
|
||||
else:
|
||||
self.__shape__ = data.shape
|
||||
self.__data__ = data.tolist()
|
||||
elif isinstance(data, list) and isinstance(shape, tuple):
|
||||
self.__shape__ = shape
|
||||
self.__data__ = numpy.array(data).reshape(shape).tolist()
|
||||
elif isinstance(data, list) and isinstance(structure, str) and isinstance(size, int):
|
||||
...
|
||||
elif isinstance(model, str) and isinstance(size, int):
|
||||
...
|
||||
else:
|
||||
raise ValueError("Only following signatures are allowed: "
|
||||
"(numpy.ndarray), (list, tuple), (list, str, int), (str, int)")
|
||||
|
||||
def get_data(self):
|
||||
return self.__data__
|
||||
|
||||
def shape(self):
|
||||
return self.__shape__
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.__data__ == other.__data__
|
||||
|
@ -1,4 +1,7 @@
|
||||
class Vector:
|
||||
from matrix import Matrix
|
||||
|
||||
|
||||
class Vector(Matrix):
|
||||
__data__ = []
|
||||
|
||||
def __init__(self, data):
|
||||
@ -24,21 +27,33 @@ class Vector:
|
||||
def __str__(self):
|
||||
return f"{self.__data__}"
|
||||
|
||||
def __neg__(self):
|
||||
return Vector([-x for x in self.__data__])
|
||||
|
||||
def __add__(self, other):
|
||||
if self.get_dimension() != other.get_dimension():
|
||||
raise ValueError("The vectors to be added must have the same dimension")
|
||||
if isinstance(other, Vector):
|
||||
if self.get_dimension() != other.get_dimension():
|
||||
raise ValueError("The vectors to be added must have the same dimension")
|
||||
return Vector([(x + y) for (x, y) in zip(self, other)])
|
||||
elif isinstance(other, int) or isinstance(other, float):
|
||||
return Vector([(x + other) for x in self.__data__])
|
||||
else:
|
||||
raise ValueError("A vector can only be multiplied with an vector (dot product) or a scalar")
|
||||
|
||||
data = []
|
||||
for (i, j) in zip(self, other):
|
||||
data.append(i + j)
|
||||
def __radd__(self, other):
|
||||
return self + other
|
||||
|
||||
return Vector(data)
|
||||
def __sub__(self, other):
|
||||
return self + (-other)
|
||||
|
||||
def __rsub__(self, other):
|
||||
return -self + other
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Vector):
|
||||
...
|
||||
return sum([(x * y) for (x, y) in zip(self, other)])
|
||||
elif isinstance(other, int) or isinstance(other, float):
|
||||
...
|
||||
return Vector([(other * x) for x in self.__data__])
|
||||
else:
|
||||
raise ValueError("A vector can only be multiplied with an vector (dot product) or a scalar")
|
||||
|
||||
|
Reference in New Issue
Block a user