Skip to content

Commit 0157f89

Browse files
committed
Merge branch 'master' into fix-sphinx/build_docs-warning-for-maths/zellers_congruence
2 parents 84701cc + bfc804a commit 0157f89

File tree

21 files changed

+277
-117
lines changed

21 files changed

+277
-117
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v4
13-
- uses: astral-sh/setup-uv@v4
13+
- uses: astral-sh/setup-uv@v5
1414
with:
1515
enable-cache: true
1616
cache-dependency-glob: uv.lock

.github/workflows/project_euler.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v4
18-
- uses: astral-sh/setup-uv@v4
18+
- uses: astral-sh/setup-uv@v5
1919
- uses: actions/setup-python@v5
2020
with:
2121
python-version: 3.x
@@ -25,7 +25,7 @@ jobs:
2525
runs-on: ubuntu-latest
2626
steps:
2727
- uses: actions/checkout@v4
28-
- uses: astral-sh/setup-uv@v4
28+
- uses: astral-sh/setup-uv@v5
2929
- uses: actions/setup-python@v5
3030
with:
3131
python-version: 3.x

.github/workflows/ruff.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- uses: actions/checkout@v4
15-
- uses: astral-sh/setup-uv@v4
15+
- uses: astral-sh/setup-uv@v5
1616
- run: uvx ruff check --output-format=github .

.github/workflows/sphinx.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
runs-on: ubuntu-latest
2727
steps:
2828
- uses: actions/checkout@v4
29-
- uses: astral-sh/setup-uv@v4
29+
- uses: astral-sh/setup-uv@v5
3030
- uses: actions/setup-python@v5
3131
with:
3232
python-version: 3.13

computer_vision/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ Image processing and computer vision are a little different from each other. Ima
88
While computer vision comes from modelling image processing using the techniques of machine learning, computer vision applies machine learning to recognize patterns for interpretation of images (much like the process of visual reasoning of human vision).
99

1010
* <https://en.wikipedia.org/wiki/Computer_vision>
11-
* <https://www.datarobot.com/blog/introduction-to-computer-vision-what-it-is-and-how-it-works/>

data_structures/binary_tree/mirror_binary_tree.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def mirror(self) -> Node:
5656
def make_tree_seven() -> Node:
5757
r"""
5858
Return a binary tree with 7 nodes that looks like this:
59+
::
60+
5961
1
6062
/ \
6163
2 3
@@ -81,13 +83,15 @@ def make_tree_seven() -> Node:
8183
def make_tree_nine() -> Node:
8284
r"""
8385
Return a binary tree with 9 nodes that looks like this:
84-
1
85-
/ \
86-
2 3
87-
/ \ \
88-
4 5 6
89-
/ \ \
90-
7 8 9
86+
::
87+
88+
1
89+
/ \
90+
2 3
91+
/ \ \
92+
4 5 6
93+
/ \ \
94+
7 8 9
9195
9296
>>> tree_nine = make_tree_nine()
9397
>>> len(tree_nine)
@@ -117,23 +121,25 @@ def main() -> None:
117121
>>> tuple(tree.mirror())
118122
(6, 3, 1, 9, 5, 2, 8, 4, 7)
119123
120-
nine_tree:
121-
1
122-
/ \
123-
2 3
124-
/ \ \
125-
4 5 6
126-
/ \ \
127-
7 8 9
128-
129-
The mirrored tree looks like this:
124+
nine_tree::
125+
126+
1
127+
/ \
128+
2 3
129+
/ \ \
130+
4 5 6
131+
/ \ \
132+
7 8 9
133+
134+
The mirrored tree looks like this::
135+
130136
1
131-
/ \
132-
3 2
133-
/ / \
134-
6 5 4
135-
/ / \
136-
9 8 7
137+
/ \
138+
3 2
139+
/ / \
140+
6 5 4
141+
/ / \
142+
9 8 7
137143
"""
138144
trees = {"zero": Node(0), "seven": make_tree_seven(), "nine": make_tree_nine()}
139145
for name, tree in trees.items():

electronics/electric_power.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ def electric_power(voltage: float, current: float, power: float) -> tuple:
2323
>>> electric_power(voltage=2, current=4, power=2)
2424
Traceback (most recent call last):
2525
...
26-
ValueError: Only one argument must be 0
26+
ValueError: Exactly one argument must be 0
2727
>>> electric_power(voltage=0, current=0, power=2)
2828
Traceback (most recent call last):
2929
...
30-
ValueError: Only one argument must be 0
30+
ValueError: Exactly one argument must be 0
3131
>>> electric_power(voltage=0, current=2, power=-4)
3232
Traceback (most recent call last):
3333
...
3434
ValueError: Power cannot be negative in any electrical/electronics system
3535
>>> electric_power(voltage=2.2, current=2.2, power=0)
3636
Result(name='power', value=4.84)
37+
>>> electric_power(current=0, power=6, voltage=2)
38+
Result(name='current', value=3.0)
3739
"""
3840
if (voltage, current, power).count(0) != 1:
39-
raise ValueError("Only one argument must be 0")
41+
raise ValueError("Exactly one argument must be 0")
4042
elif power < 0:
4143
raise ValueError(
4244
"Power cannot be negative in any electrical/electronics system"
@@ -48,7 +50,7 @@ def electric_power(voltage: float, current: float, power: float) -> tuple:
4850
elif power == 0:
4951
return Result("power", float(round(abs(voltage * current), 2)))
5052
else:
51-
raise ValueError("Exactly one argument must be 0")
53+
raise AssertionError
5254

5355

5456
if __name__ == "__main__":

linear_algebra/src/gaussian_elimination_pivoting.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,33 @@ def solve_linear_system(matrix: np.ndarray) -> np.ndarray:
2222
>>> solution = solve_linear_system(np.column_stack((A, B)))
2323
>>> np.allclose(solution, np.array([2., 3., -1.]))
2424
True
25-
>>> solve_linear_system(np.array([[0, 0], [0, 0]], dtype=float))
26-
array([nan, nan])
25+
>>> solve_linear_system(np.array([[0, 0, 0]], dtype=float))
26+
Traceback (most recent call last):
27+
...
28+
ValueError: Matrix is not square
29+
>>> solve_linear_system(np.array([[0, 0, 0], [0, 0, 0]], dtype=float))
30+
Traceback (most recent call last):
31+
...
32+
ValueError: Matrix is singular
2733
"""
2834
ab = np.copy(matrix)
2935
num_of_rows = ab.shape[0]
3036
num_of_columns = ab.shape[1] - 1
3137
x_lst: list[float] = []
3238

33-
# Lead element search
34-
for column_num in range(num_of_rows):
35-
for i in range(column_num, num_of_columns):
36-
if abs(ab[i][column_num]) > abs(ab[column_num][column_num]):
37-
ab[[column_num, i]] = ab[[i, column_num]]
38-
if ab[column_num, column_num] == 0.0:
39-
raise ValueError("Matrix is not correct")
40-
else:
41-
pass
42-
if column_num != 0:
43-
for i in range(column_num, num_of_rows):
44-
ab[i, :] -= (
45-
ab[i, column_num - 1]
46-
/ ab[column_num - 1, column_num - 1]
47-
* ab[column_num - 1, :]
48-
)
39+
if num_of_rows != num_of_columns:
40+
raise ValueError("Matrix is not square")
4941

50-
# Upper triangular matrix
5142
for column_num in range(num_of_rows):
43+
# Lead element search
5244
for i in range(column_num, num_of_columns):
5345
if abs(ab[i][column_num]) > abs(ab[column_num][column_num]):
5446
ab[[column_num, i]] = ab[[i, column_num]]
55-
if ab[column_num, column_num] == 0.0:
56-
raise ValueError("Matrix is not correct")
57-
else:
58-
pass
47+
48+
# Upper triangular matrix
49+
if abs(ab[column_num, column_num]) < 1e-8:
50+
raise ValueError("Matrix is singular")
51+
5952
if column_num != 0:
6053
for i in range(column_num, num_of_rows):
6154
ab[i, :] -= (

linear_algebra/src/lib.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class Vector:
4646
change_component(pos: int, value: float): changes specified component
4747
euclidean_length(): returns the euclidean length of the vector
4848
angle(other: Vector, deg: bool): returns the angle between two vectors
49-
TODO: compare-operator
5049
"""
5150

5251
def __init__(self, components: Collection[float] | None = None) -> None:
@@ -96,6 +95,16 @@ def __sub__(self, other: Vector) -> Vector:
9695
else: # error case
9796
raise Exception("must have the same size")
9897

98+
def __eq__(self, other: object) -> bool:
99+
"""
100+
performs the comparison between two vectors
101+
"""
102+
if not isinstance(other, Vector):
103+
return NotImplemented
104+
if len(self) != len(other):
105+
return False
106+
return all(self.component(i) == other.component(i) for i in range(len(self)))
107+
99108
@overload
100109
def __mul__(self, other: float) -> Vector: ...
101110

machine_learning/linear_regression.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta):
4141
:param theta : Feature vector (weight's for our model)
4242
;param return : Updated Feature's, using
4343
curr_features - alpha_ * gradient(w.r.t. feature)
44+
>>> import numpy as np
45+
>>> data_x = np.array([[1, 2], [3, 4]])
46+
>>> data_y = np.array([5, 6])
47+
>>> len_data = len(data_x)
48+
>>> alpha = 0.01
49+
>>> theta = np.array([0.1, 0.2])
50+
>>> run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta)
51+
array([0.196, 0.343])
4452
"""
4553
n = len_data
4654

@@ -58,6 +66,12 @@ def sum_of_square_error(data_x, data_y, len_data, theta):
5866
:param len_data : len of the dataset
5967
:param theta : contains the feature vector
6068
:return : sum of square error computed from given feature's
69+
70+
Example:
71+
>>> vc_x = np.array([[1.1], [2.1], [3.1]])
72+
>>> vc_y = np.array([1.2, 2.2, 3.2])
73+
>>> round(sum_of_square_error(vc_x, vc_y, 3, np.array([1])),3)
74+
np.float64(0.005)
6175
"""
6276
prod = np.dot(theta, data_x.transpose())
6377
prod -= data_y.transpose()
@@ -93,6 +107,11 @@ def mean_absolute_error(predicted_y, original_y):
93107
:param predicted_y : contains the output of prediction (result vector)
94108
:param original_y : contains values of expected outcome
95109
:return : mean absolute error computed from given feature's
110+
111+
>>> predicted_y = [3, -0.5, 2, 7]
112+
>>> original_y = [2.5, 0.0, 2, 8]
113+
>>> mean_absolute_error(predicted_y, original_y)
114+
0.5
96115
"""
97116
total = sum(abs(y - predicted_y[i]) for i, y in enumerate(original_y))
98117
return total / len(original_y)
@@ -114,4 +133,7 @@ def main():
114133

115134

116135
if __name__ == "__main__":
136+
import doctest
137+
138+
doctest.testmod()
117139
main()

0 commit comments

Comments
 (0)