Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroJJimenez committed Nov 24, 2024
1 parent 4288f06 commit 2acca9b
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run Tests

on: [push, pull_request]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
arch: [x64, arm64]
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: pytest tests
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Here you can find a math library.
Here you can find a math library.

![Test Status](https://github.com/ep2lab/github_examples/actions/workflows/run_tests.yml/badge.svg)
Empty file added requirements.txt
Empty file.
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from vectormath import scalar_product, vector_product, matrix_product

def test_scalar_product():
assert scalar_product([1, 2, 3], [4, 5, 6]) == 32
assert scalar_product([1, 2], [5, 6]) == 17

def test_vector_product():
assert vector_product([1, 2, 3], [4, 5, 6]) == [-3, 6, -3]
assert vector_product([0, 0, 1], [1, 0, 0]) == [0, 1, 0]
assert vector_product([1, 0, 0], [0, 1, 0]) == [0, 0, 1]
assert vector_product([1, 1, 1], [1, 1, 1]) == [0, 0, 0]

def test_matrix_product():
assert matrix_product([[1, 2], [3, 4]], [[5, 6], [7, 8]]) == [[19, 22], [43, 50]]
assert matrix_product([[2, 0], [0, 2]], [[1, 2], [3, 4]]) == [[2, 4], [6, 8]]
assert matrix_product([[1, 2, 3], [4, 5, 6]], [[7, 8], [9, 10], [11, 12]]) == [[58, 64], [139, 154]]
assert matrix_product([[1]], [[2]]) == [[2]]
assert matrix_product([[1, 2, 3]], [[4], [5], [6]]) == [[32]]
1 change: 1 addition & 0 deletions vectormath/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .math_library import scalar_product, vector_product, matrix_product
Binary file added vectormath/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file not shown.
26 changes: 26 additions & 0 deletions vectormath/math_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def scalar_product(vector1, vector2):
if len(vector1) != len(vector2):
raise ValueError("Vectors must be of the same length")
return sum(x * y for x, y in zip(vector1, vector2))

def vector_product(vector1, vector2):
if len(vector1) != 3 or len(vector2) != 3:
raise ValueError("Both vectors must be of length 3")
return [
vector1[1] * vector2[2] - vector1[2] * vector2[1],
vector1[2] * vector2[0] - vector1[0] * vector2[2],
vector1[0] * vector2[1] - vector1[1] * vector2[0]
]

def matrix_product(matrix1, matrix2):
if len(matrix1[0]) != len(matrix2):
raise ValueError("Number of columns in the first matrix must be equal to the number of rows in the second matrix")

result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]

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

return result

0 comments on commit 2acca9b

Please sign in to comment.