From 6b968f350524f6a428ebe1a61086b4e5eb8b7749 Mon Sep 17 00:00:00 2001 From: niklas Date: Thu, 30 Nov 2023 15:58:09 +0100 Subject: [PATCH] Add project related project --- .gitignore | 1 + pwr_project/src/main.py | 0 pwr_project/src/matrix.py | 2 ++ pwr_project/src/vector.py | 46 ++++++++++++++++++++++++++++++++ pwr_project/test/test_vector.py | 47 +++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 pwr_project/src/main.py create mode 100644 pwr_project/src/matrix.py create mode 100644 pwr_project/src/vector.py create mode 100644 pwr_project/test/test_vector.py diff --git a/.gitignore b/.gitignore index fcaf82f..9f99302 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/ target/ +__pycache__ *.out *.o diff --git a/pwr_project/src/main.py b/pwr_project/src/main.py new file mode 100644 index 0000000..e69de29 diff --git a/pwr_project/src/matrix.py b/pwr_project/src/matrix.py new file mode 100644 index 0000000..73f67c2 --- /dev/null +++ b/pwr_project/src/matrix.py @@ -0,0 +1,2 @@ +class Matrix: + diff --git a/pwr_project/src/vector.py b/pwr_project/src/vector.py new file mode 100644 index 0000000..39fc575 --- /dev/null +++ b/pwr_project/src/vector.py @@ -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 diff --git a/pwr_project/test/test_vector.py b/pwr_project/test/test_vector.py new file mode 100644 index 0000000..80a051c --- /dev/null +++ b/pwr_project/test/test_vector.py @@ -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)