Add project related project
This commit is contained in:
parent
265dc44c1c
commit
6b968f3505
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
.idea/
|
||||
target/
|
||||
__pycache__
|
||||
*.out
|
||||
*.o
|
||||
|
0
pwr_project/src/main.py
Normal file
0
pwr_project/src/main.py
Normal file
2
pwr_project/src/matrix.py
Normal file
2
pwr_project/src/matrix.py
Normal file
@ -0,0 +1,2 @@
|
||||
class Matrix:
|
||||
|
46
pwr_project/src/vector.py
Normal file
46
pwr_project/src/vector.py
Normal file
@ -0,0 +1,46 @@
|
||||
class Vector:
|
||||
__data__ = []
|
||||
|
||||
def __init__(self, data):
|
||||
if isinstance(data, list):
|
||||
self.__data__ = data
|
||||
elif isinstance(data, int):
|
||||
self.__init__([0] * data)
|
||||
else:
|
||||
raise ValueError("data must be a list or an integer for dimension")
|
||||
|
||||
def get_data(self):
|
||||
return self.__data__
|
||||
|
||||
def get_dimension(self):
|
||||
return len(self.__data__)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.__data__)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.__data__ == other.__data__
|
||||
|
||||
def __str__(self):
|
||||
return f"{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")
|
||||
|
||||
data = []
|
||||
for (i, j) in zip(self, other):
|
||||
data.append(i + j)
|
||||
|
||||
return Vector(data)
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, Vector):
|
||||
...
|
||||
elif isinstance(other, int) or isinstance(other, float):
|
||||
...
|
||||
else:
|
||||
raise ValueError("A vector can only be multiplied with an vector (dot product) or a scalar")
|
||||
|
||||
def __rmul__(self, other):
|
||||
return self * other
|
47
pwr_project/test/test_vector.py
Normal file
47
pwr_project/test/test_vector.py
Normal file
@ -0,0 +1,47 @@
|
||||
from unittest import TestCase
|
||||
|
||||
from vector import Vector
|
||||
|
||||
|
||||
class TestVector(TestCase):
|
||||
def test_should_create_vector_dim_5(self):
|
||||
dim = 5
|
||||
vector = Vector(dim)
|
||||
|
||||
actual = vector.get_dimension()
|
||||
expected = dim
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_zero_vector(self):
|
||||
dim = 5
|
||||
vector = Vector(dim)
|
||||
|
||||
actual = vector.get_data()
|
||||
expected = [0, 0, 0, 0, 0]
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_vector(self):
|
||||
data = list(range(5))
|
||||
vector = Vector(data)
|
||||
|
||||
actual = vector.get_data()
|
||||
expected = [0, 1, 2, 3, 4]
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_add_vectors(self):
|
||||
v1 = Vector([1, 2])
|
||||
v2 = Vector([3, 4])
|
||||
|
||||
expected = Vector([4, 6])
|
||||
actual = v1 + v2
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_raise_error_while_adding_vectors(self):
|
||||
v1 = Vector(1)
|
||||
v2 = Vector(2)
|
||||
|
||||
self.assertRaises(ValueError, lambda: v1 + v2)
|
Loading…
Reference in New Issue
Block a user