From e849593530b58202f7b60b7dd2143abee0ca3027 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 13:06:56 +0100 Subject: [PATCH 01/15] addded a custom sqrt function to the code. --- example.py | 5 +-- pyproject.toml | 2 +- setup.py | 18 ++++++++-- .../simple_functions_bindings.cpp} | 10 +++--- .../cpp/functions/bindings/sqrt_bindings.cpp | 34 +++++++++++++++++++ src/fabelous_math/cpp/functions/sqrt.cpp | 21 ++++++++++++ src/fabelous_math/include/sqrt.hpp | 5 +++ 7 files changed, 84 insertions(+), 11 deletions(-) rename src/fabelous_math/cpp/functions/{bindings.cpp => bindings/simple_functions_bindings.cpp} (82%) create mode 100644 src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp create mode 100644 src/fabelous_math/cpp/functions/sqrt.cpp create mode 100644 src/fabelous_math/include/sqrt.hpp diff --git a/example.py b/example.py index c9b0ffc..32dbf17 100644 --- a/example.py +++ b/example.py @@ -1,4 +1,5 @@ -from fabelous_math import is_even, is_odd +from fabelous_math import is_even, is_odd, fabelous_sqrt +print(fabelous_sqrt(50000000000000)) print(is_even(5)) -print(is_odd(19)) \ No newline at end of file +print(is_odd(19)) diff --git a/pyproject.toml b/pyproject.toml index 91c3f0e..ea5ae0b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "fabelous_math" -version = "0.0.1" +version = "0.2.0" description = "Math functions written in C++ for faster code" authors = [ {name = "Falko Habel", email = "falko.habel@fabelous.app"} diff --git a/setup.py b/setup.py index c59254f..6f6d988 100644 --- a/setup.py +++ b/setup.py @@ -10,11 +10,23 @@ if platform.system() == "Windows": else: extra_compile_args.append('-std=c++17') -module = Extension( +simple_functions_module = Extension( 'fabelous_math.simple_functions', sources=[ 'src/fabelous_math/cpp/functions/simple_functions.cpp', - 'src/fabelous_math/cpp/functions/bindings.cpp' + 'src/fabelous_math/cpp/functions/bindings/simple_functions_bindings.cpp' + ], + include_dirs=['src/fabelous_math/include'], + extra_compile_args=extra_compile_args, + extra_link_args=extra_link_args, +) + + +sqrt_module = Extension( + 'fabelous_math.sqrt', + sources=[ + 'src/fabelous_math/cpp/functions/sqrt.cpp', + 'src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp' ], include_dirs=['src/fabelous_math/include'], extra_compile_args=extra_compile_args, @@ -24,7 +36,7 @@ module = Extension( setup( name='fabelous_math', description='Math functions written in C++ for faster code', - ext_modules=[module], + ext_modules=[simple_functions_module, sqrt_module], author="Falko Habel", author_email="falko.habel@fabelous.app" ) \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/bindings.cpp b/src/fabelous_math/cpp/functions/bindings/simple_functions_bindings.cpp similarity index 82% rename from src/fabelous_math/cpp/functions/bindings.cpp rename to src/fabelous_math/cpp/functions/bindings/simple_functions_bindings.cpp index aef16dc..0eac404 100644 --- a/src/fabelous_math/cpp/functions/bindings.cpp +++ b/src/fabelous_math/cpp/functions/bindings/simple_functions_bindings.cpp @@ -17,20 +17,20 @@ static PyObject* is_odd_wrapper(PyObject* self, PyObject* args) { return PyBool_FromLong(simple_functions::is_odd(number)); } -static PyMethodDef NumberUtilsMethods[] = { +static PyMethodDef SimpleFunctionsMethods[] = { {"is_even", is_even_wrapper, METH_VARARGS, "Check if a number is even"}, {"is_odd", is_odd_wrapper, METH_VARARGS, "Check if a number is odd"}, {NULL, NULL, 0, NULL} }; -static struct PyModuleDef number_utils_module = { +static struct PyModuleDef simple_functions_module = { PyModuleDef_HEAD_INIT, "simple_functions", "Module for checking if numbers are even or odd", -1, - NumberUtilsMethods + SimpleFunctionsMethods }; PyMODINIT_FUNC PyInit_simple_functions(void) { - return PyModule_Create(&number_utils_module); -} \ No newline at end of file + return PyModule_Create(&simple_functions_module); +} diff --git a/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp b/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp new file mode 100644 index 0000000..adf5436 --- /dev/null +++ b/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp @@ -0,0 +1,34 @@ +#include +#include "sqrt.hpp" +#include + +static PyObject* fabelous_sqrt_wrapper(PyObject* self, PyObject* args) { + long long number; + if (!PyArg_ParseTuple(args, "L", &number)) { // Ensure 'L' is correct for long long + return NULL; + } + try { + double result = fabelous_sqrt::sqrt(number); // Adjust to match the actual return type + return PyFloat_FromDouble(result); + } catch (const std::invalid_argument& e) { // Ensure this is the correct exception + PyErr_SetString(PyExc_ValueError, e.what()); + return NULL; + } +} + +static PyMethodDef SqrtMethods[] = { + {"fabelous_sqrt", fabelous_sqrt_wrapper, METH_VARARGS, "Compute the square root of a long number"}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef sqrt_module = { + PyModuleDef_HEAD_INIT, + "sqrt", + "Module for computing the square root of a number", + -1, + SqrtMethods +}; + +PyMODINIT_FUNC PyInit_sqrt(void) { + return PyModule_Create(&sqrt_module); +} \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/sqrt.cpp b/src/fabelous_math/cpp/functions/sqrt.cpp new file mode 100644 index 0000000..d0d4899 --- /dev/null +++ b/src/fabelous_math/cpp/functions/sqrt.cpp @@ -0,0 +1,21 @@ +#include "sqrt.hpp" +#include +#include + +double fabelous_sqrt::sqrt(long long number) { + if (number < 0) { + throw std::invalid_argument("Cannot compute square root of a negative number"); + } + // Special case for 0 and 1 + if (number == 0 || number == 1) { + return static_cast(number); + } + + long long x = number; + double y = static_cast((x + 1) / 2); + while (y < x) { + x = static_cast(y); + y = (static_cast(x) + static_cast(number) / static_cast(x)) / 2; + } + return y; +} \ No newline at end of file diff --git a/src/fabelous_math/include/sqrt.hpp b/src/fabelous_math/include/sqrt.hpp new file mode 100644 index 0000000..bdd2588 --- /dev/null +++ b/src/fabelous_math/include/sqrt.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace fabelous_sqrt { + double sqrt(long long number); +} \ No newline at end of file From d008db30baf2a1f96ac841df8ebe10cbbf4b9d1b Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 13:07:09 +0100 Subject: [PATCH 02/15] updated the embedding process to not do a full reload --- .gitea/workflows/embed.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/embed.yaml b/.gitea/workflows/embed.yaml index 2a0d240..8407f8b 100644 --- a/.gitea/workflows/embed.yaml +++ b/.gitea/workflows/embed.yaml @@ -34,4 +34,4 @@ jobs: VECTORDB_TOKEN: ${{ secrets.VECTORDB_TOKEN }} run: | cd VectorLoader - python -m src.run --full + python -m src.run From 2bcb4f3d3dae180e3c08ad14d36903dc941a8c47 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:08:10 +0100 Subject: [PATCH 03/15] covnerted from sqrt to nrooting --- example.py | 4 +- setup.py | 6 +- .../functions/bindings/rooting_bindings.cpp | 37 ++++++++++++ src/fabelous_math/cpp/functions/rooting.cpp | 58 +++++++++++++++++++ src/fabelous_math/cpp/functions/sqrt.cpp | 21 ------- src/fabelous_math/include/rooting.hpp | 5 ++ src/fabelous_math/include/sqrt.hpp | 5 -- 7 files changed, 105 insertions(+), 31 deletions(-) create mode 100644 src/fabelous_math/cpp/functions/bindings/rooting_bindings.cpp create mode 100644 src/fabelous_math/cpp/functions/rooting.cpp delete mode 100644 src/fabelous_math/cpp/functions/sqrt.cpp create mode 100644 src/fabelous_math/include/rooting.hpp delete mode 100644 src/fabelous_math/include/sqrt.hpp diff --git a/example.py b/example.py index 32dbf17..3d51763 100644 --- a/example.py +++ b/example.py @@ -1,5 +1,5 @@ -from fabelous_math import is_even, is_odd, fabelous_sqrt +from fabelous_math import is_even, is_odd, rooting -print(fabelous_sqrt(50000000000000)) +print(rooting(0.5)) print(is_even(5)) print(is_odd(19)) diff --git a/setup.py b/setup.py index 6f6d988..3661352 100644 --- a/setup.py +++ b/setup.py @@ -23,10 +23,10 @@ simple_functions_module = Extension( sqrt_module = Extension( - 'fabelous_math.sqrt', + 'fabelous_math.rooting', sources=[ - 'src/fabelous_math/cpp/functions/sqrt.cpp', - 'src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp' + 'src/fabelous_math/cpp/functions/rooting.cpp', + 'src/fabelous_math/cpp/functions/bindings/rooting_bindings.cpp' ], include_dirs=['src/fabelous_math/include'], extra_compile_args=extra_compile_args, diff --git a/src/fabelous_math/cpp/functions/bindings/rooting_bindings.cpp b/src/fabelous_math/cpp/functions/bindings/rooting_bindings.cpp new file mode 100644 index 0000000..5f6c1b8 --- /dev/null +++ b/src/fabelous_math/cpp/functions/bindings/rooting_bindings.cpp @@ -0,0 +1,37 @@ +#include +#include "rooting.hpp" +#include + +static PyObject* rooting_wrapper(PyObject* self, PyObject* args) { + double number; // Changed to double to accept floating-point numbers + double n = 2.0; // Default value for the second argument + + if (!PyArg_ParseTuple(args, "d|d", &number, &n)) { // First arg required, second optional + return NULL; + } + + try { + double result = rooting::nth_root(number, n); // Pass number directly as double + return PyFloat_FromDouble(result); + } catch (const std::invalid_argument& e) { + PyErr_SetString(PyExc_ValueError, e.what()); + return NULL; + } +} + +static PyMethodDef NthRootMethods[] = { + {"rooting", rooting_wrapper, METH_VARARGS, "Compute the nth root of a number"}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef sqrt_module = { + PyModuleDef_HEAD_INIT, + "fabelous_math.rooting", + "Module for computing the nth root of a number", + -1, + NthRootMethods +}; + +PyMODINIT_FUNC PyInit_rooting(void) { + return PyModule_Create(&sqrt_module); +} \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/rooting.cpp b/src/fabelous_math/cpp/functions/rooting.cpp new file mode 100644 index 0000000..4db587e --- /dev/null +++ b/src/fabelous_math/cpp/functions/rooting.cpp @@ -0,0 +1,58 @@ +#include "rooting.hpp" +#include +#include +#include + +double rooting::nth_root(long double number, double n) { + // Special case handling + if (n <= 0) { + throw std::invalid_argument("The root must be positive and non-zero"); + } + + if (number < 0 && std::fmod(n, 2.0) == 0) { + throw std::invalid_argument("Cannot compute even root of a negative number"); + } + + if (number == 0) { + return 0.0; + } + + if (number == 1 || n == 1.0) { + return number; + } + + // Handle negative numbers for odd roots + bool negative = number < 0; + double abs_number = negative ? -number : number; + + // Use logarithm method for initial guess + double log_result = std::log(abs_number) / n; + double x = std::exp(log_result); + + // Constants + const double epsilon = 1e-15; + const int max_iterations = 100; + + // Newton-Raphson method + for (int i = 0; i < max_iterations; i++) { + double x_prev = x; + + // Calculate new approximation, handling potential overflow + double x_pow = std::pow(x, n - 1); + + // Prevent division by zero + if (x_pow < std::numeric_limits::min()) { + break; + } + + x = ((n - 1) * x + abs_number / x_pow) / n; + + // Check for convergence using relative error + if (std::fabs(x - x_prev) <= epsilon * std::fabs(x)) { + break; + } + } + + // Return correct sign for odd roots of negative numbers + return negative ? -x : x; +} \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/sqrt.cpp b/src/fabelous_math/cpp/functions/sqrt.cpp deleted file mode 100644 index d0d4899..0000000 --- a/src/fabelous_math/cpp/functions/sqrt.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "sqrt.hpp" -#include -#include - -double fabelous_sqrt::sqrt(long long number) { - if (number < 0) { - throw std::invalid_argument("Cannot compute square root of a negative number"); - } - // Special case for 0 and 1 - if (number == 0 || number == 1) { - return static_cast(number); - } - - long long x = number; - double y = static_cast((x + 1) / 2); - while (y < x) { - x = static_cast(y); - y = (static_cast(x) + static_cast(number) / static_cast(x)) / 2; - } - return y; -} \ No newline at end of file diff --git a/src/fabelous_math/include/rooting.hpp b/src/fabelous_math/include/rooting.hpp new file mode 100644 index 0000000..f62d9dc --- /dev/null +++ b/src/fabelous_math/include/rooting.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace rooting { + double nth_root(long double number, double n); +} \ No newline at end of file diff --git a/src/fabelous_math/include/sqrt.hpp b/src/fabelous_math/include/sqrt.hpp deleted file mode 100644 index bdd2588..0000000 --- a/src/fabelous_math/include/sqrt.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -namespace fabelous_sqrt { - double sqrt(long long number); -} \ No newline at end of file From a0283ddf64d54ef80330e1b88096d59450d145e2 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:08:28 +0100 Subject: [PATCH 04/15] added tests for rooting --- tests/functions/test_rooting.py | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/functions/test_rooting.py diff --git a/tests/functions/test_rooting.py b/tests/functions/test_rooting.py new file mode 100644 index 0000000..76ab423 --- /dev/null +++ b/tests/functions/test_rooting.py @@ -0,0 +1,44 @@ +import pytest +from fabelous_math import rooting +import math + +def test_fabelous_sqrt(): + # Test with a perfect square + assert rooting(4) == 2 + + # Test with a non-perfect square + expected_value = math.sqrt(5) + assert abs(rooting(5) - expected_value) < 1e-8 + + # Test with zero + assert rooting(0) == 0 + + # Test with a large number + assert abs(rooting(1000000) - 1000) < 1e-8 + + # Test with a negative number (should raise ValueError) + with pytest.raises(ValueError): + rooting(-1) + +def test_fabelous_sqrt_edge_cases(): + # Test with very small positive number + assert abs(rooting(1e-10, 2) - 1e-5) < 1e-8 # For square root + + # Test with very large positive number + assert abs(rooting(1e+308) - 1e+154) < 1e-8 + +def test_fabelous_sqrt_invalid_input(): + # Test with non-numeric input (should raise TypeError) + with pytest.raises(TypeError): + rooting("string") + + # Test with None input (should raise TypeError) + with pytest.raises(TypeError): + rooting(None) + +def test_fabelous_sqrt_consistency(): + # Test consistency with known values + known_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + for value in known_values: + expected_value = math.sqrt(value) + assert abs(rooting(value) - expected_value) < 1e-8 From fc840bb42b2a14e4e136596cf1337035f9cbc651 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:08:57 +0100 Subject: [PATCH 05/15] updated init for new rooting --- src/fabelous_math/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/fabelous_math/__init__.py b/src/fabelous_math/__init__.py index 40c1e47..1660b52 100644 --- a/src/fabelous_math/__init__.py +++ b/src/fabelous_math/__init__.py @@ -1,3 +1,4 @@ from fabelous_math.simple_functions import is_even, is_odd +from fabelous_math.rooting import rooting -__all__ = ["is_even", "is_odd"] \ No newline at end of file +__all__ = ["is_even", "is_odd", "rooting"] \ No newline at end of file From 0a25cdf3336d346596809e1827749917435ae905 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:09:11 +0100 Subject: [PATCH 06/15] removed outdated file --- .../cpp/functions/bindings/sqrt_bindings.cpp | 34 ------------------- 1 file changed, 34 deletions(-) delete mode 100644 src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp diff --git a/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp b/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp deleted file mode 100644 index adf5436..0000000 --- a/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include "sqrt.hpp" -#include - -static PyObject* fabelous_sqrt_wrapper(PyObject* self, PyObject* args) { - long long number; - if (!PyArg_ParseTuple(args, "L", &number)) { // Ensure 'L' is correct for long long - return NULL; - } - try { - double result = fabelous_sqrt::sqrt(number); // Adjust to match the actual return type - return PyFloat_FromDouble(result); - } catch (const std::invalid_argument& e) { // Ensure this is the correct exception - PyErr_SetString(PyExc_ValueError, e.what()); - return NULL; - } -} - -static PyMethodDef SqrtMethods[] = { - {"fabelous_sqrt", fabelous_sqrt_wrapper, METH_VARARGS, "Compute the square root of a long number"}, - {NULL, NULL, 0, NULL} -}; - -static struct PyModuleDef sqrt_module = { - PyModuleDef_HEAD_INIT, - "sqrt", - "Module for computing the square root of a number", - -1, - SqrtMethods -}; - -PyMODINIT_FUNC PyInit_sqrt(void) { - return PyModule_Create(&sqrt_module); -} \ No newline at end of file From 7345d295158cf528ba726960bdc188c0fad93682 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:11:28 +0100 Subject: [PATCH 07/15] updated version number --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ea5ae0b..d8448a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "fabelous_math" -version = "0.2.0" +version = "0.2.1" description = "Math functions written in C++ for faster code" authors = [ {name = "Falko Habel", email = "falko.habel@fabelous.app"} From 8e6f8dd178a982b06326eb9dd5c4459f2a8421a4 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:11:53 +0100 Subject: [PATCH 08/15] removed extra namespace from simple functions --- .../cpp/functions/simple_functions.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/fabelous_math/cpp/functions/simple_functions.cpp b/src/fabelous_math/cpp/functions/simple_functions.cpp index 4d5ecab..01f6cce 100644 --- a/src/fabelous_math/cpp/functions/simple_functions.cpp +++ b/src/fabelous_math/cpp/functions/simple_functions.cpp @@ -1,11 +1,9 @@ #include "simple_functions.hpp" -namespace simple_functions { - bool is_even(long long number) { - return (number & 1) == 0; - } +bool simple_functions::is_even(long long number) { +return (number & 1) == 0; +} - bool is_odd(long long number) { - return (number & 1); - } -} \ No newline at end of file +bool simple_functions::is_odd(long long number) { + return (number & 1); +} From 31ad0b27cbcd25adbfa8272c46df570b157f8246 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:22:06 +0100 Subject: [PATCH 09/15] added readme information for pi --- docs/a.png | Bin 0 -> 930 bytes docs/b.png | Bin 0 -> 949 bytes docs/base.png | Bin 0 -> 492 bytes docs/pi.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 docs/a.png create mode 100644 docs/b.png create mode 100644 docs/base.png create mode 100644 docs/pi.md diff --git a/docs/a.png b/docs/a.png new file mode 100644 index 0000000000000000000000000000000000000000..2f56c63d7470c4a64e2db1c4cd9feda36bc8c927 GIT binary patch literal 930 zcmeAS@N?(olHy`uVBq!ia0vp^Q-Ro&g&9chny_UOkTMAH332`Z|38pPJkYnn4v7hNNIQ4>a(5iT?A&{mAX7R2`I%`;1OBOz`%C|gc+x5^GP!>F#Yv(aSZX` zeH-STAsi@Td(vAyGwsqfuB>v+pu%9D6$@G~2|C8kRM6_^cKZ|Jn7Q;(dh%wi5^-&% z;MeJ@E4Z7z#CI8Vu!`!qI>u_~mOMPv)VeCe_1O2B<}*EeFYhb4xx3=~z2f(lGb=wo z^J`FuF1KBgeDJG>v&sGk&l=n&T{dFWbxd9S@}T?j<=bw(zVs@%x$jzp;fwa>SFU_Rhu8?>CY1-{? z`K6)beZ$y!grsKMyN1L&Q=dAUPtiti=36C>`2~WJ z2kX_B<@ujFmMHl~^q^VH1dh*3Hg|LhPks`4`mBAno+?_4?eg9911Aa55FB?7DH-GK_c{7&G%}zY*FV9?|{W-(S z?auj4Q~l2bJXVWmjdIFRJ=NAbk#Uy4+~Oru1@)chE4C}BJ*%6OKK1+a6(65ZF5U8B zYsn)|K93~=l@-#u7q}JIhvlXvOaC~sBwpvfFq^06+AlGIMQ$%n%Ca81r4pu``+t(f z=Z&XZ)02{pZQk*GtwZybOK;@17YXigo9rUDoORjz_YrTzA z9DYs8Te|qbHtvs1r{?R;%S#naljTU|x-$J6x9Q_OOgt9zc17q-S)$il&{tahmTRBD z#N!{8=6{{`Aj`GdYloe5^2x%g={JQ=Ew1xvWnHpA^7pzIt0yhAR%5?b@$h`FPBg1g z#Fs-R^W2Iz@ywduR=a$+wfXu)L5JBdbC;cuzN_?a@uQsev*PAfD^{8^^1p8A*;**M z@|Vl8L@8m`P~MZpd`CV{JZH(dvTE;_m!I~Y(5hPKzwYG!wOU4}Wv0qX#ov;Ea<$;oSP sAGLiKcE7mY^V9mHjF;Q{bVA zmjw9*GbpV#O50ub-X%n!^X}Q1TNA#q0Hrt!JR*x382FBWFymBhK4}I9W&uwZ$B+ol zx8dirF9*o{SM$BBlI#BUL79M=8i!U!;fB)_#BS;dZyGKLgtG>RgxDmU*@;>QUnE+UE~MY*8@#U6OjR#Pw@L;g$tErmhoc{iYTrIotO| zQrx=huT=#;Z+w)|7~{3GN!w|Qk(u_h{O^eqlTH8y9fjn?H!iw=uQ%*n`v3E?8vo}M zuF1K*?ckk6#-r6Y)fOi|e8MlpnVviMVZZT?&>360*Ok1S9j_c=mnWH-y8dueyvj2J zKFRhOLVgqX#5Y{LBk#4ux9fp|qvGQU0j&Nnn*@(9&ppj6`F-MpWra7mTe4$dD&9>n8iMi=z~wn`@_wP z7$QZ!UD=bot6K0TtNWtAYKKw-%sCIuzcl^V?dd9Fe~&wQM=~t#a1;L)$GpgC{wt}* zc1N9!)vN`bDhjW2=4Ko1DqwzC7a+~1y7K)-j#CGiy>`yqCY4f=E4@Ui>-=I>E4zf5 zT%1#{1~s%#oGCtI!ra{--e}*`+QHQsdRaMk!<{1qUG9vI2UiqT+V6RPIAF>Qz6+7D z{6}&GzHeT6J>lwtyz5LG<#pr|=Go6ryP(~5nPrNvS3%sI6NNnDYI|qTP?-68&VsKW zgs1BLU9`gay&Y%N{Ta4b6S5)Ly#N>~C-B`7>tT-=E%n<1k5t|NURF?YCo( zC$GA@%3!b2gbRm%lv#B0RNV9``Mu@vk3DVwreB#K?R0(Zne!FA{~xV;V4Js;Ml+bIz6qh(=A?Bj)8ksh&~!)WuW1lb)o2a-OHF KpUXO@geCwGp`3mI literal 0 HcmV?d00001 diff --git a/docs/base.png b/docs/base.png new file mode 100644 index 0000000000000000000000000000000000000000..7ac02a0695d545df28e7b7d387a2c5bd8c699e2f GIT binary patch literal 492 zcmeAS@N?(olHy`uVBq!ia0vp^K0vI-!VDzmTuV>^QU(D&A+G=b{|7RO2l{sxR0CD> zmjw9*Gn9oGDeX>MeYW$xi@?mgQkN$u0i`$#JR*x382FBWFymBhK4}I9Mt4sa#}Es_ zqZ19i4+n^}`d?aR%DM58{)!*$GBHb!o;=CM{#<95g{7c z8M=E-qw1WSu5U}%{xrP4_0y>}$?n>**>fLvt=yXQ=2Db3^L8_jV-{YQ51X8AP&lq` zoO>a^=fxJqJEx+b%FVHm7yi2~bW8f0CNsu&n_I3dD?GhL!0!Bhu_a=1g@;f0?w!IA zXZPVp{0pzhY3Y~JzJ^~p8S>}Rj@Bzidm2;YElociH(Z)uDs(fhV52$<*X->(Bi+}{ zOwoF;BcZ;^y<(??S7Y%mHP Date: Fri, 14 Mar 2025 14:22:21 +0100 Subject: [PATCH 10/15] basic python files also added. --- .../cpp/functions/bindings/pi_bindings.cpp | 28 +++++++++++++++++++ src/fabelous_math/cpp/functions/pi.cpp | 27 ++++++++++++++++++ src/fabelous_math/include/pi.hpp | 5 ++++ 3 files changed, 60 insertions(+) create mode 100644 src/fabelous_math/cpp/functions/bindings/pi_bindings.cpp create mode 100644 src/fabelous_math/cpp/functions/pi.cpp create mode 100644 src/fabelous_math/include/pi.hpp diff --git a/src/fabelous_math/cpp/functions/bindings/pi_bindings.cpp b/src/fabelous_math/cpp/functions/bindings/pi_bindings.cpp new file mode 100644 index 0000000..c6ddb61 --- /dev/null +++ b/src/fabelous_math/cpp/functions/bindings/pi_bindings.cpp @@ -0,0 +1,28 @@ +#include +#include "pi.hpp" + +static PyObject* approximate_pi_wrapper(PyObject* self, PyObject* args) { + long long number; + if (!PyArg_ParseTuple(args, "L", &number)) { // Changed to 'L' for long long + return NULL; + } + double result = pi::approximate_pi(number); // Change to double + return PyFloat_FromDouble(result); // Return the actual result as a float +} + +static PyMethodDef PiMethods[] = { + {"approximate_pi", approximate_pi_wrapper, METH_VARARGS, "Calculate the approximation of pi"}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef pi_module = { + PyModuleDef_HEAD_INIT, + "pi", + "Module for calculating the approximation of pi", + -1, + PiMethods +}; + +PyMODINIT_FUNC PyInit_pi(void) { + return PyModule_Create(&pi_module); +} \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/pi.cpp b/src/fabelous_math/cpp/functions/pi.cpp new file mode 100644 index 0000000..fa87882 --- /dev/null +++ b/src/fabelous_math/cpp/functions/pi.cpp @@ -0,0 +1,27 @@ +#include "pi.hpp" +#include +#include // Include iostream for debugging + +long double pi::approximate_pi(long long number) { + long long lange = number - 2; + long double a = 0, b = 0; + + // Compute summation for 'a' + for (long long k = 1; k <= lange; ++k) { // Changed to include lange + long double value = std::sqrt(number * number - k * k); + a += value; + } + + // Compute summation for 'b' + for (long long j = 0; j <= lange; ++j) { // Changed to include lange + long double value = std::sqrt(number * number - j * j); + b += value; + } + + // Normalize the results + b = 4.0 / (number * number) * b; + a = 4.0 / (number * number) * a; + + long double result = (a + b) / 2; + return result; +} \ No newline at end of file diff --git a/src/fabelous_math/include/pi.hpp b/src/fabelous_math/include/pi.hpp new file mode 100644 index 0000000..b5f5f31 --- /dev/null +++ b/src/fabelous_math/include/pi.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace pi { + long double approximate_pi(long long number); +} \ No newline at end of file From 70b40cdeeb10cb69f607c85d2b372ef03fe10875 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:22:39 +0100 Subject: [PATCH 11/15] updated python files included to handle cpp bindings --- example.py | 4 +++- pyproject.toml | 2 +- setup.py | 12 +++++++++++- src/fabelous_math/__init__.py | 3 ++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/example.py b/example.py index 3d51763..da2aaab 100644 --- a/example.py +++ b/example.py @@ -1,4 +1,6 @@ -from fabelous_math import is_even, is_odd, rooting +from fabelous_math import is_even, is_odd, rooting, approximate_pi + +print(approximate_pi(10000000)) print(rooting(0.5)) print(is_even(5)) diff --git a/pyproject.toml b/pyproject.toml index d8448a0..8ba9880 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "fabelous_math" -version = "0.2.1" +version = "0.3.14" description = "Math functions written in C++ for faster code" authors = [ {name = "Falko Habel", email = "falko.habel@fabelous.app"} diff --git a/setup.py b/setup.py index 3661352..bcd9e9f 100644 --- a/setup.py +++ b/setup.py @@ -21,6 +21,16 @@ simple_functions_module = Extension( extra_link_args=extra_link_args, ) +pi_module = Extension( + 'fabelous_math.pi', + sources=[ + 'src/fabelous_math/cpp/functions/pi.cpp', + 'src/fabelous_math/cpp/functions/bindings/pi_bindings.cpp' + ], + include_dirs=['src/fabelous_math/include'], + extra_compile_args=extra_compile_args, + extra_link_args=extra_link_args, +) sqrt_module = Extension( 'fabelous_math.rooting', @@ -36,7 +46,7 @@ sqrt_module = Extension( setup( name='fabelous_math', description='Math functions written in C++ for faster code', - ext_modules=[simple_functions_module, sqrt_module], + ext_modules=[simple_functions_module, sqrt_module, pi_module], author="Falko Habel", author_email="falko.habel@fabelous.app" ) \ No newline at end of file diff --git a/src/fabelous_math/__init__.py b/src/fabelous_math/__init__.py index 1660b52..4d829a3 100644 --- a/src/fabelous_math/__init__.py +++ b/src/fabelous_math/__init__.py @@ -1,4 +1,5 @@ from fabelous_math.simple_functions import is_even, is_odd +from fabelous_math.pi import approximate_pi from fabelous_math.rooting import rooting -__all__ = ["is_even", "is_odd", "rooting"] \ No newline at end of file +__all__ = ["is_even", "is_odd", "approximate_pi", "rooting"] \ No newline at end of file From d80c0d55f62dad066ebbe2ffe26da4b31ff42b2c Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:25:59 +0100 Subject: [PATCH 12/15] improved example file --- example.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/example.py b/example.py index da2aaab..dbd6df1 100644 --- a/example.py +++ b/example.py @@ -1,7 +1,14 @@ from fabelous_math import is_even, is_odd, rooting, approximate_pi -print(approximate_pi(10000000)) +number = 42 +print(f"Is {number} even? {is_even(number)}") +print(f"Is {number} odd? {is_odd(number)}") -print(rooting(0.5)) -print(is_even(5)) -print(is_odd(19)) +# Extended feature for rooting with a specified root +root = 4 +number = 16 +print(f"Rooting {number} to the power of {root}: {rooting(number, root)}") + +# Extended feature for approximate_pi with additional parameters if needed +precision = 10000000 +print(f"Approximate Pi with precision {precision}: {approximate_pi(precision)}") \ No newline at end of file From 7881302e6c15005bd808c46aa229820b83c21223 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:26:13 +0100 Subject: [PATCH 13/15] updated readme to give better explanation --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cf79f62..5a624fd 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,21 @@ pip install git+https://gitea.fabelous.app/Fabel/fabelous-math.git To use the functions provided by Fabelous Math in your Python code, you can import them as follows: ```python -from fabelous_math import is_even, is_odd +from fabelous_math import is_even, is_odd, rooting, approximate_pi -# Example usage: number = 42 print(f"Is {number} even? {is_even(number)}") print(f"Is {number} odd? {is_odd(number)}") -``` +# Extended feature for rooting with a specified root +root = 4 +number = 16 +print(f"Rooting {number} to the power of {root}: {rooting(number, root)}") + +# Extended feature for approximate_pi with additional parameters if needed +precision = 10000000 +print(f"Approximate Pi with precision {precision}: {approximate_pi(precision)}") +``` ## Performance Comparison To understand the performance of `fabelous-math` functions, I conducted a series of tests comparing my methods with traditional modulo operations. Below are the results: From 044cfcd0fce8a40793cfc68e7db028736a37547e Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:26:32 +0100 Subject: [PATCH 14/15] updated version number to meet current date --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 8ba9880..cdae9e8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "fabelous_math" -version = "0.3.14" +version = "0.3.141" description = "Math functions written in C++ for faster code" authors = [ {name = "Falko Habel", email = "falko.habel@fabelous.app"} From a9e4667d7dce0bedb16c06bb1787911dc168460b Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 14 Mar 2025 14:34:02 +0100 Subject: [PATCH 15/15] added testing for pi --- tests/functions/test_pi.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/functions/test_pi.py diff --git a/tests/functions/test_pi.py b/tests/functions/test_pi.py new file mode 100644 index 0000000..cb00897 --- /dev/null +++ b/tests/functions/test_pi.py @@ -0,0 +1,25 @@ +import pytest +from fabelous_math import approximate_pi + +def test_approximate_pi(): + # Test with a known precision value + precision = 10000 + pi_approx = approximate_pi(precision) + assert isinstance(pi_approx, float), "The result should be a float" + assert pi_approx > 3.14, "The approximation of Pi should be greater than 3.14" + assert pi_approx < 3.15, "The approximation of Pi should be less than 3.15" + +def test_approximate_pi_with_low_precision(): + # Test with a low precision value + precision = 100 + pi_approx = approximate_pi(precision) + assert isinstance(pi_approx, float), "The result should be a float" + assert pi_approx > 3.0, "The approximation of Pi should be greater than 3.0" + assert pi_approx < 4.0, "The approximation of Pi should be less than 4.0" + +def test_approximate_pi_with_high_precision(): + # Test with a high precision value + precision = 10000000 + pi_approx = approximate_pi(precision) + assert isinstance(pi_approx, float), "The result should be a float" + assert pi_approx > 3.1415926533, "The approximation of Pi should be greater than the known value of Pi" \ No newline at end of file