From e36a6488d536b7aee6c77d594d6d23c51d4a5e94 Mon Sep 17 00:00:00 2001 From: Aryansh Bhargavan Date: Sat, 28 Oct 2023 14:39:18 +0530 Subject: [PATCH 1/7] Feat: Added Fast inverse square root --- maths/fast_inverse_sqrt.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 maths/fast_inverse_sqrt.py diff --git a/maths/fast_inverse_sqrt.py b/maths/fast_inverse_sqrt.py new file mode 100644 index 000000000000..87f28ca43b31 --- /dev/null +++ b/maths/fast_inverse_sqrt.py @@ -0,0 +1,34 @@ +""" +Fast inverse square root. +Reference: https://en.wikipedia.org/wiki/Fast_inverse_square_root +""" + +import struct + + +def fastInvSqrt(x): + """ + Calculate the fast inverse square root of a number. + + This function computes the fast inverse square root of a floating-point number + using the famous Quake III algorithm, originally developed by id Software. + + :param float x: The input number for which to calculate the inverse square root. + :return float: The fast inverse square root of the input number. + + Example: + >>> fastInvSqrt(10) + 0.3156857923527257 + + >>> fastInvSqrt(4) + 0.49915357479239103 + """ + i = struct.unpack('>i', struct.pack('>f', x))[0] + i = 0x5f3759df - (i >> 1) + y = struct.unpack('>f', struct.pack('>i', i))[0] + return y * (1.5 - 0.5 * x * y * y) + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) \ No newline at end of file From dc74572497d5f4072b1e67f3d1c0a71c11b122a5 Mon Sep 17 00:00:00 2001 From: Aryansh Bhargavan Date: Sat, 28 Oct 2023 14:41:51 +0530 Subject: [PATCH 2/7] Fix: Added typehint --- maths/fast_inverse_sqrt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/fast_inverse_sqrt.py b/maths/fast_inverse_sqrt.py index 87f28ca43b31..fe72423b53e3 100644 --- a/maths/fast_inverse_sqrt.py +++ b/maths/fast_inverse_sqrt.py @@ -6,7 +6,7 @@ import struct -def fastInvSqrt(x): +def fastInvSqrt(x: float) -> float: """ Calculate the fast inverse square root of a number. From 2a8fda4d226dea668ac371e9d3e2afcebb160b17 Mon Sep 17 00:00:00 2001 From: Aryansh Bhargavan Date: Sun, 29 Oct 2023 19:51:23 +0530 Subject: [PATCH 3/7] Fix: Added doctests that break the code, changed var name --- maths/fast_inverse_sqrt.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/maths/fast_inverse_sqrt.py b/maths/fast_inverse_sqrt.py index fe72423b53e3..b2aab3eb00ec 100644 --- a/maths/fast_inverse_sqrt.py +++ b/maths/fast_inverse_sqrt.py @@ -6,29 +6,41 @@ import struct -def fastInvSqrt(x: float) -> float: +def fast_inverse_sqrt(number: float) -> float: """ Calculate the fast inverse square root of a number. This function computes the fast inverse square root of a floating-point number using the famous Quake III algorithm, originally developed by id Software. - :param float x: The input number for which to calculate the inverse square root. + :param float number: The input number for which to calculate the inverse square root. :return float: The fast inverse square root of the input number. Example: - >>> fastInvSqrt(10) + >>> fast_inverse_sqrt(10) 0.3156857923527257 - >>> fastInvSqrt(4) + >>> fast_inverse_sqrt(4) 0.49915357479239103 + + >>> fast_inverse_sqrt(0) + Traceback (most recent call last): + ... + ValueError: Input must be a positive number. + + >>> fast_inverse_sqrt(-1) + Traceback (most recent call last): + ... + ValueError: Input must be a positive number. """ - i = struct.unpack('>i', struct.pack('>f', x))[0] + if number<=0: + raise ValueError("Input must be a positive number.") + i = struct.unpack('>i', struct.pack('>f', number))[0] i = 0x5f3759df - (i >> 1) y = struct.unpack('>f', struct.pack('>i', i))[0] - return y * (1.5 - 0.5 * x * y * y) + return y * (1.5 - 0.5 * number * y * y) if __name__ == "__main__": import doctest - doctest.testmod(verbose=True) \ No newline at end of file + doctest.testmod(verbose=True) From a1ec88e9be2a46f4f96566f7b0dae95b0c90f272 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 29 Oct 2023 14:21:43 +0000 Subject: [PATCH 4/7] updating DIRECTORY.md --- DIRECTORY.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d108acf8dcfb..9b2c8ce735c3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -34,6 +34,7 @@ * [Bitwise Addition Recursive](bit_manipulation/bitwise_addition_recursive.py) * [Count 1S Brian Kernighan Method](bit_manipulation/count_1s_brian_kernighan_method.py) * [Count Number Of One Bits](bit_manipulation/count_number_of_one_bits.py) + * [Excess 3 Code](bit_manipulation/excess_3_code.py) * [Gray Code Sequence](bit_manipulation/gray_code_sequence.py) * [Highest Set Bit](bit_manipulation/highest_set_bit.py) * [Index Of Rightmost Set Bit](bit_manipulation/index_of_rightmost_set_bit.py) @@ -170,7 +171,10 @@ * Arrays * [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py) * [Find Triplets With 0 Sum](data_structures/arrays/find_triplets_with_0_sum.py) + * [Index 2D Array In 1D](data_structures/arrays/index_2d_array_in_1d.py) + * [Kth Largest Element](data_structures/arrays/kth_largest_element.py) * [Median Two Array](data_structures/arrays/median_two_array.py) + * [Monotonic Array](data_structures/arrays/monotonic_array.py) * [Pairs With Given Sum](data_structures/arrays/pairs_with_given_sum.py) * [Permutations](data_structures/arrays/permutations.py) * [Prefix Sum](data_structures/arrays/prefix_sum.py) @@ -368,6 +372,7 @@ ## Electronics * [Apparent Power](electronics/apparent_power.py) * [Builtin Voltage](electronics/builtin_voltage.py) + * [Capacitor Equivalence](electronics/capacitor_equivalence.py) * [Carrier Concentration](electronics/carrier_concentration.py) * [Charging Capacitor](electronics/charging_capacitor.py) * [Charging Inductor](electronics/charging_inductor.py) @@ -531,12 +536,14 @@ ## Machine Learning * [Apriori Algorithm](machine_learning/apriori_algorithm.py) * [Astar](machine_learning/astar.py) + * [Automatic Differentiation](machine_learning/automatic_differentiation.py) * [Data Transformations](machine_learning/data_transformations.py) * [Decision Tree](machine_learning/decision_tree.py) * [Dimensionality Reduction](machine_learning/dimensionality_reduction.py) * Forecasting * [Run](machine_learning/forecasting/run.py) * [Frequent Pattern Growth](machine_learning/frequent_pattern_growth.py) + * [Gradient Boosting Classifier](machine_learning/gradient_boosting_classifier.py) * [Gradient Descent](machine_learning/gradient_descent.py) * [K Means Clust](machine_learning/k_means_clust.py) * [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py) @@ -598,6 +605,7 @@ * [Extended Euclidean Algorithm](maths/extended_euclidean_algorithm.py) * [Factorial](maths/factorial.py) * [Factors](maths/factors.py) + * [Fast Inverse Sqrt](maths/fast_inverse_sqrt.py) * [Fermat Little Theorem](maths/fermat_little_theorem.py) * [Fibonacci](maths/fibonacci.py) * [Find Max](maths/find_max.py) @@ -648,6 +656,7 @@ * [Numerical Integration](maths/numerical_analysis/numerical_integration.py) * [Runge Kutta](maths/numerical_analysis/runge_kutta.py) * [Runge Kutta Fehlberg 45](maths/numerical_analysis/runge_kutta_fehlberg_45.py) + * [Runge Kutta Gills](maths/numerical_analysis/runge_kutta_gills.py) * [Secant Method](maths/numerical_analysis/secant_method.py) * [Simpson Rule](maths/numerical_analysis/simpson_rule.py) * [Square Root](maths/numerical_analysis/square_root.py) @@ -814,6 +823,7 @@ * [Ideal Gas Law](physics/ideal_gas_law.py) * [In Static Equilibrium](physics/in_static_equilibrium.py) * [Kinetic Energy](physics/kinetic_energy.py) + * [Lens Formulae](physics/lens_formulae.py) * [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py) * [Malus Law](physics/malus_law.py) * [Mass Energy Equivalence](physics/mass_energy_equivalence.py) From a0b81b1a6f34d39dc77fe11810a1253ed07d1bdf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 29 Oct 2023 14:22:02 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/fast_inverse_sqrt.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/maths/fast_inverse_sqrt.py b/maths/fast_inverse_sqrt.py index b2aab3eb00ec..25fd892e5e26 100644 --- a/maths/fast_inverse_sqrt.py +++ b/maths/fast_inverse_sqrt.py @@ -33,13 +33,14 @@ def fast_inverse_sqrt(number: float) -> float: ... ValueError: Input must be a positive number. """ - if number<=0: + if number <= 0: raise ValueError("Input must be a positive number.") - i = struct.unpack('>i', struct.pack('>f', number))[0] - i = 0x5f3759df - (i >> 1) - y = struct.unpack('>f', struct.pack('>i', i))[0] + i = struct.unpack(">i", struct.pack(">f", number))[0] + i = 0x5F3759DF - (i >> 1) + y = struct.unpack(">f", struct.pack(">i", i))[0] return y * (1.5 - 0.5 * number * y * y) + if __name__ == "__main__": import doctest From 6a4702f418e33dd28eaaccc25fcc9982cba723b8 Mon Sep 17 00:00:00 2001 From: Aryansh Bhargavan Date: Sun, 29 Oct 2023 23:39:10 +0530 Subject: [PATCH 6/7] Fix: fixed length of docstring --- maths/fast_inverse_sqrt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/fast_inverse_sqrt.py b/maths/fast_inverse_sqrt.py index b2aab3eb00ec..1d92490a16c5 100644 --- a/maths/fast_inverse_sqrt.py +++ b/maths/fast_inverse_sqrt.py @@ -13,7 +13,7 @@ def fast_inverse_sqrt(number: float) -> float: This function computes the fast inverse square root of a floating-point number using the famous Quake III algorithm, originally developed by id Software. - :param float number: The input number for which to calculate the inverse square root. + :param float number: Input number for which to calculate the inverse square root. :return float: The fast inverse square root of the input number. Example: From aeae0a809aa5cc07321f1f0cdddaf77b3de17006 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 21:54:19 +0100 Subject: [PATCH 7/7] Update fast_inverse_sqrt.py --- maths/fast_inverse_sqrt.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/maths/fast_inverse_sqrt.py b/maths/fast_inverse_sqrt.py index 9513126853e6..79385bb84877 100644 --- a/maths/fast_inverse_sqrt.py +++ b/maths/fast_inverse_sqrt.py @@ -1,6 +1,7 @@ """ -Fast inverse square root. +Fast inverse square root (1/sqrt(x)) using the Quake III algorithm. Reference: https://en.wikipedia.org/wiki/Fast_inverse_square_root +Accuracy: https://en.wikipedia.org/wiki/Fast_inverse_square_root#Accuracy """ import struct @@ -8,10 +9,8 @@ def fast_inverse_sqrt(number: float) -> float: """ - Calculate the fast inverse square root of a number. - - This function computes the fast inverse square root of a floating-point number - using the famous Quake III algorithm, originally developed by id Software. + Compute the fast inverse square root of a floating-point number using the famous + Quake III algorithm. :param float number: Input number for which to calculate the inverse square root. :return float: The fast inverse square root of the input number. @@ -19,19 +18,22 @@ def fast_inverse_sqrt(number: float) -> float: Example: >>> fast_inverse_sqrt(10) 0.3156857923527257 - >>> fast_inverse_sqrt(4) 0.49915357479239103 - + >>> fast_inverse_sqrt(4.1) + 0.4932849504615651 >>> fast_inverse_sqrt(0) Traceback (most recent call last): ... ValueError: Input must be a positive number. - >>> fast_inverse_sqrt(-1) Traceback (most recent call last): ... ValueError: Input must be a positive number. + >>> from math import isclose, sqrt + >>> all(isclose(fast_inverse_sqrt(i), 1 / sqrt(i), rel_tol=0.00132) + ... for i in range(50, 60)) + True """ if number <= 0: raise ValueError("Input must be a positive number.") @@ -42,6 +44,11 @@ def fast_inverse_sqrt(number: float) -> float: if __name__ == "__main__": - import doctest + from doctest import testmod + + testmod() + # https://en.wikipedia.org/wiki/Fast_inverse_square_root#Accuracy + from math import sqrt - doctest.testmod(verbose=True) + for i in range(5, 101, 5): + print(f"{i:>3}: {(1 / sqrt(i)) - fast_inverse_sqrt(i):.5f}")