Skip to content

[pull] master from TheAlgorithms:master #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 28, 2024
Merged
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
1 change: 0 additions & 1 deletion computer_vision/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ Image processing and computer vision are a little different from each other. Ima
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).

* <https://en.wikipedia.org/wiki/Computer_vision>
* <https://www.datarobot.com/blog/introduction-to-computer-vision-what-it-is-and-how-it-works/>
39 changes: 16 additions & 23 deletions linear_algebra/src/gaussian_elimination_pivoting.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,33 @@ def solve_linear_system(matrix: np.ndarray) -> np.ndarray:
>>> solution = solve_linear_system(np.column_stack((A, B)))
>>> np.allclose(solution, np.array([2., 3., -1.]))
True
>>> solve_linear_system(np.array([[0, 0], [0, 0]], dtype=float))
array([nan, nan])
>>> solve_linear_system(np.array([[0, 0, 0]], dtype=float))
Traceback (most recent call last):
...
ValueError: Matrix is not square
>>> solve_linear_system(np.array([[0, 0, 0], [0, 0, 0]], dtype=float))
Traceback (most recent call last):
...
ValueError: Matrix is singular
"""
ab = np.copy(matrix)
num_of_rows = ab.shape[0]
num_of_columns = ab.shape[1] - 1
x_lst: list[float] = []

# Lead element search
for column_num in range(num_of_rows):
for i in range(column_num, num_of_columns):
if abs(ab[i][column_num]) > abs(ab[column_num][column_num]):
ab[[column_num, i]] = ab[[i, column_num]]
if ab[column_num, column_num] == 0.0:
raise ValueError("Matrix is not correct")
else:
pass
if column_num != 0:
for i in range(column_num, num_of_rows):
ab[i, :] -= (
ab[i, column_num - 1]
/ ab[column_num - 1, column_num - 1]
* ab[column_num - 1, :]
)
if num_of_rows != num_of_columns:
raise ValueError("Matrix is not square")

# Upper triangular matrix
for column_num in range(num_of_rows):
# Lead element search
for i in range(column_num, num_of_columns):
if abs(ab[i][column_num]) > abs(ab[column_num][column_num]):
ab[[column_num, i]] = ab[[i, column_num]]
if ab[column_num, column_num] == 0.0:
raise ValueError("Matrix is not correct")
else:
pass

# Upper triangular matrix
if abs(ab[column_num, column_num]) < 1e-8:
raise ValueError("Matrix is singular")

if column_num != 0:
for i in range(column_num, num_of_rows):
ab[i, :] -= (
Expand Down
19 changes: 14 additions & 5 deletions maths/perfect_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,27 @@ def perfect(number: int) -> bool:
False
>>> perfect(-1)
False
>>> perfect(33550336) # Large perfect number
True
>>> perfect(33550337) # Just above a large perfect number
False
>>> perfect(1) # Edge case: 1 is not a perfect number
False
>>> perfect("123") # String representation of a number
Traceback (most recent call last):
...
ValueError: number must be an integer
>>> perfect(12.34)
Traceback (most recent call last):
...
ValueError: number must an integer
ValueError: number must be an integer
>>> perfect("Hello")
Traceback (most recent call last):
...
ValueError: number must an integer
ValueError: number must be an integer
"""
if not isinstance(number, int):
raise ValueError("number must an integer")
raise ValueError("number must be an integer")
if number <= 0:
return False
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
Expand All @@ -70,8 +80,7 @@ def perfect(number: int) -> bool:
try:
number = int(input("Enter a positive integer: ").strip())
except ValueError:
msg = "number must an integer"
print(msg)
msg = "number must be an integer"
raise ValueError(msg)

print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
48 changes: 45 additions & 3 deletions maths/trapezoidal_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@

method 1:
"extended trapezoidal rule"
int(f) = dx/2 * (f1 + 2f2 + ... + fn)

"""


def method_1(boundary, steps):
# "extended trapezoidal rule"
# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
"""
Apply the extended trapezoidal rule to approximate the integral of function f(x)
over the interval defined by 'boundary' with the number of 'steps'.

Args:
boundary (list of floats): A list containing the start and end values [a, b].
steps (int): The number of steps or subintervals.
Returns:
float: Approximation of the integral of f(x) over [a, b].
Examples:
>>> method_1([0, 1], 10)
0.3349999999999999
"""
h = (boundary[1] - boundary[0]) / steps
a = boundary[0]
b = boundary[1]
Expand All @@ -26,13 +38,40 @@ def method_1(boundary, steps):


def make_points(a, b, h):
"""
Generates points between 'a' and 'b' with step size 'h', excluding the end points.
Args:
a (float): Start value
b (float): End value
h (float): Step size
Examples:
>>> list(make_points(0, 10, 2.5))
[2.5, 5.0, 7.5]

>>> list(make_points(0, 10, 2))
[2, 4, 6, 8]

>>> list(make_points(1, 21, 5))
[6, 11, 16]

>>> list(make_points(1, 5, 2))
[3]

>>> list(make_points(1, 4, 3))
[]
"""
x = a + h
while x < (b - h):
while x <= (b - h):
yield x
x = x + h


def f(x): # enter your function here
"""
Example:
>>> f(2)
4
"""
y = (x - 0) * (x - 0)
return y

Expand All @@ -47,4 +86,7 @@ def main():


if __name__ == "__main__":
import doctest

doctest.testmod()
main()