Skip to content

feat: Implemented Matrix Exponentiation Method #11636

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

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f5211cc
feat: add Matrix Exponentiation method
Acuspeedster Oct 1, 2024
95421d7
feat: added new function matrix exponetiation method
Acuspeedster Oct 1, 2024
adf10bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2024
ba1cbc6
feat: This function uses the tail-recursive form of the Euclidean alg…
Acuspeedster Oct 1, 2024
fd37108
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2024
50fd058
reduced the number of characters per line in the comments
Acuspeedster Oct 1, 2024
d6fec67
Merge branch 'branch1' of https://github.com/Acuspeedster/Python into…
Acuspeedster Oct 1, 2024
ede7215
removed unwanted code
Acuspeedster Oct 1, 2024
0f939a8
feat: Implemented a new function to swaap numbers without dummy variable
Acuspeedster Oct 1, 2024
5dc67f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2024
b581cf5
removed previos code
Acuspeedster Oct 1, 2024
3ad4b6a
removed the previous code
Acuspeedster Oct 1, 2024
31c5645
Done with the required changes
Acuspeedster Oct 2, 2024
6ee1a40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2024
e48a3f3
Done with the required changes
Acuspeedster Oct 2, 2024
8085e66
Merge branch 'branch1' of https://github.com/Acuspeedster/Python into…
Acuspeedster Oct 2, 2024
f82bd8c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2024
8bf1389
Done with the required changes
Acuspeedster Oct 2, 2024
7860646
Merge branch 'branch1' of https://github.com/Acuspeedster/Python into…
Acuspeedster Oct 2, 2024
9ffd634
Done with the required changes
Acuspeedster Oct 2, 2024
4e2456f
Done with the required changes
Acuspeedster Oct 2, 2024
07ed6b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 2, 2024
955c792
Update maths/fibonacci.py
Acuspeedster Oct 4, 2024
70dd92d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 4, 2024
1909143
Done with the required changes
Acuspeedster Oct 4, 2024
6cc39c0
Merge branch 'branch1' of https://github.com/Acuspeedster/Python into…
Acuspeedster Oct 4, 2024
b7db4ca
Done with the required changes
Acuspeedster Oct 4, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
def xor_swap(a: int, b: int) -> (int, int):

Check failure on line 1 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:1:1: N999 Invalid module name: 'swaping_two_variables_without_using _temporary_variables'
"""
Swap two integers using bitwise XOR
without using a temporary variable.

This algorithm utilizes the properties
of the bitwise XOR operation to swap the values

Check failure on line 7 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:7:52: W291 Trailing whitespace
of two integers `a` and `b` without
the use of a temporary variable. XOR swap is a

Check failure on line 9 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:9:51: W291 Trailing whitespace
rarely used trick but showcases the power
of bit manipulation for efficient operations

Check failure on line 11 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:11:49: W291 Trailing whitespace
at the hardware level.

The steps involved are:
1. `a = a ^ b`
2. `b = a ^ b` (Now `b` holds the original value of `a`)
3. `a = a ^ b` (Now `a` holds the original value of `b`)

Although this technique can swap variables
without extra space, it is generally not

Check failure on line 20 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:20:45: W291 Trailing whitespace
recommended in production code because it is
less readable than using a temporary variable.

Args:
a (int): The first integer to be swapped.
b (int): The second integer to be swapped.

Returns:
(int, int): The swapped values of `a` and `b`.

Raises:
None

Example:
>>> xor_swap(5, 10)
(10, 5)
>>> xor_swap(0, 100)
(100, 0)
>>> xor_swap(-3, 9)
(9, -3)

Notes:
- Swapping using XOR can lead to confusion and
should generally be avoided in

Check failure on line 44 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:44:39: W291 Trailing whitespace
favor of more readable methods.
- This algorithm does not work if both `a` and `b`
refer to the same variable.

"""
if a == b:
print("Both values are the same; no swap needed.")
return a, b

Check failure on line 53 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:53:1: W293 Blank line contains whitespace
# print(f"Original a = {a}, b = {b}")
a = a ^ b # Step 1
b = a ^ b # Step 2: Now b is the original value of a
a = a ^ b # Step 3: Now a is the original value of b
# print(f"Swapped a = {a}, b = {b}")
return a, b


if __name__ == "__main__":
import doctest
doctest.testmod()
print(xor_swap(5, 10)) # (10, 5)
print(xor_swap(0, 100)) # (100, 0)
print(xor_swap(-3, 9)) # (9, -3)

Check failure on line 68 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:68:1: W293 Blank line contains whitespace

Check failure on line 68 in bit_manipulation/swaping_two_variables_without_using _temporary_variables.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

bit_manipulation/swaping_two_variables_without_using _temporary_variables.py:68:5: W292 No newline at end of file
1 change: 1 addition & 0 deletions bit_manipulation/tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# print(f"Swapped a = {a}, b = {b}")

Check failure on line 1 in bit_manipulation/tempCodeRunnerFile.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

bit_manipulation/tempCodeRunnerFile.py:1:1: N999 Invalid module name: 'tempCodeRunnerFile'
37 changes: 37 additions & 0 deletions maths/fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

NOTE 2: the Binet's formula function is much more limited in the size of inputs
that it can handle due to the size limitations of Python floats
NOTE 3: the matrix function is the fastest and most memory efficient for large n


See benchmark numbers in __main__ for performance comparisons/
https://en.wikipedia.org/wiki/Fibonacci_number for more information
Expand Down Expand Up @@ -230,6 +232,40 @@ def fib_binet(n: int) -> list[int]:
return [round(phi**i / sqrt_5) for i in range(n + 1)]


def fib_matrix(n: int) -> int:
# https://www.nayuki.io/page/fast-fibonacci-algorithms#:~:text=
# Summary:%20The%20two%20fast%20Fibonacci%20algorithms%20are%20matrix
def matrix_mult(a, b):
return [
[
a[0][0] * b[0][0] + a[0][1] * b[1][0],
a[0][0] * b[0][1] + a[0][1] * b[1][1],
],
[
a[1][0] * b[0][0] + a[1][1] * b[1][0],
a[1][0] * b[0][1] + a[1][1] * b[1][1],
],
]

def matrix_pow(m, power):
result = [[1, 0], [0, 1]] # Identity matrix
base = m
while power:
if power % 2 == 1:
result = matrix_mult(result, base)
base = matrix_mult(base, base)
power //= 2
return result

if n < 0:
raise ValueError("n is negative")
if n == 0:
return 0
m = [[1, 1], [1, 0]]
result = matrix_pow(m, n - 1)
return result[0][0]


if __name__ == "__main__":
from doctest import testmod

Expand All @@ -242,3 +278,4 @@ def fib_binet(n: int) -> list[int]:
time_func(fib_memoization, num) # 0.0100 ms
time_func(fib_recursive_cached, num) # 0.0153 ms
time_func(fib_recursive, num) # 257.0910 ms
time_func(fib_matrix, num) # 0.0000 ms
Loading