Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code_style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
matrix:

# Each option you define in the matrix has a key and value
python-version: [ 3.8 ]
python-version: [ 3.9 ]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Types check with myPy

on: [push, pull_request]

jobs:
mypy:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.9]

steps:
- name: Set up Git repository
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel setuptools
python -m pip install -r requirements.txt
python -m pip install mypy

- name: Run MyPy
run: |
mypy project/
17 changes: 17 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Run Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install pytest
run: pip install pytest
- name: Run tests using project script
run: |
python ./scripts/run_tests.py
Empty file added project/Task1/__init__.py
Empty file.
129 changes: 129 additions & 0 deletions project/Task1/matrix_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
def matrix_add(matrix1, matrix2):
"""
Add two matrices element-wise.

Args:
matrix1 (list): First matrix as list of lists.
matrix2 (list): Second matrix as list of lists.

Returns:
list: Resulting matrix after addition.

Raises:
ValueError: If matrices have different dimensions.
"""
if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
raise ValueError("Matrices must have the same dimensions")

return [
[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))]
for i in range(len(matrix1))
]


def matrix_multiply(matrix1, matrix2):
"""
Multiply two matrices.

Args:
matrix1 (list): First matrix.
matrix2 (list): Second matrix.

Returns:
list: Resulting matrix after multiplication.

Raises:
ValueError: If number of columns in first matrix doesn't match
number of rows in second matrix.
"""
if len(matrix1[0]) != len(matrix2):
raise ValueError(
"Number of columns in first matrix must equal "
"number of rows in second matrix"
)

result = [[0] * len(matrix2[0]) for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for k in range(len(matrix2)):
for j in range(len(matrix2[0])):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result


def matrix_transpose(matrix):
"""
Transpose a matrix (swap rows and columns).

Args:
matrix (list): Input matrix.

Returns:
list: Transposed matrix.
"""
return [list(row) for row in zip(*matrix)]


class Matrix:
"""
A class representing a mathematical matrix.

Attributes:
data (list): Matrix data as list of lists.
"""

def __init__(self, data):
"""
Initialize a matrix with given data.

Args:
data (list): Matrix data as list of lists.

Raises:
ValueError: If rows have inconsistent lengths.
"""
if not all(len(row) == len(data[0]) for row in data):
raise ValueError("All rows must have the same length")
self.data = data
self.rows = len(data)
self.cols = len(data[0])

def add(self, other):
"""
Add another matrix to this matrix.

Args:
other (Matrix): Another matrix.

Returns:
Matrix: Result of addition.
"""
return Matrix(matrix_add(self.data, other.data))

def multiply(self, other):
"""
Multiply with another matrix.

Args:
other (Matrix): Another matrix.

Returns:
Matrix: Result of multiplication.
"""
return Matrix(matrix_multiply(self.data, other.data))

def transpose(self):
"""
Transpose the matrix.

Returns:
Matrix: Transposed matrix.
"""
return Matrix(matrix_transpose(self.data))

def __repr__(self):
return f"Matrix({self.data})"

def __eq__(self, other):
if not isinstance(other, Matrix):
return False
return self.data == other.data
120 changes: 120 additions & 0 deletions project/Task1/vector_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import math


def dot_product(vector1, vector2):
"""
Calculate the dot product of two vectors.

Args:
vector1 (list): First vector as a list of numbers.
vector2 (list): Second vector as a list of numbers.

Returns:
float: Dot product of the two vectors.

Raises:
ValueError: If vectors have different lengths.
"""
if len(vector1) != len(vector2):
raise ValueError("Vectors must have the same length")
return sum(v1 * v2 for v1, v2 in zip(vector1, vector2))


def vector_length(vector):
"""
Calculate the Euclidean length (magnitude) of a vector.

Args:
vector (list): Vector as a list of numbers.

Returns:
float: Length of the vector.
"""
return math.sqrt(sum(x * x for x in vector))


def angle_between(vector1, vector2):
"""
Calculate the angle between two vectors in radians.

Args:
vector1 (list): First vector.
vector2 (list): Second vector.

Returns:
float: Angle between vectors in radians.

Raises:
ValueError: If either vector is a zero vector.
"""
dot = dot_product(vector1, vector2)
len1 = vector_length(vector1)
len2 = vector_length(vector2)

if len1 == 0 or len2 == 0:
raise ValueError("Vectors cannot be zero vectors")

# Ensure cosine value is within valid range to avoid floating point errors
cos_angle = dot / (len1 * len2)
cos_angle = max(min(cos_angle, 1.0), -1.0)

return math.acos(cos_angle)


class Vector:
"""
A class representing a mathematical vector.

Attributes:
components (list): The components of the vector.
"""

def __init__(self, components):
"""
Initialize a vector with given components.

Args:
components (list): List of numerical components.
"""
self.components = list(components)

def dot(self, other):
"""
Calculate dot product with another vector.

Args:
other (Vector): Another vector.

Returns:
float: Dot product.
"""
return dot_product(self.components, other.components)

def length(self):
"""
Calculate the length of the vector.

Returns:
float: Vector length.
"""
return vector_length(self.components)

def angle_with(self, other):
"""
Calculate angle with another vector.

Args:
other (Vector): Another vector.

Returns:
float: Angle in radians.
"""
return angle_between(self.components, other.components)

def __repr__(self):
return f"Vector({self.components})"

def __eq__(self, other):
if not isinstance(other, Vector):
return False
return self.components == other.components
Empty file added project/Task2/__init__.py
Empty file.
Loading