Compare commits

...

3 Commits

Author SHA1 Message Date
Falko Victor Habel 70b40cdeeb updated python files included to handle cpp bindings
Gitea Actions For Fabelous-Math / Explore-Gitea-Actions (push) Successful in 18s Details
2025-03-14 14:22:39 +01:00
Falko Victor Habel 0ef21826db basic python files also added. 2025-03-14 14:22:21 +01:00
Falko Victor Habel 31ad0b27cb added readme information for pi 2025-03-14 14:22:06 +01:00
11 changed files with 138 additions and 4 deletions

BIN
docs/a.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 930 B

BIN
docs/b.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 949 B

BIN
docs/base.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 492 B

61
docs/pi.md Normal file
View File

@ -0,0 +1,61 @@
## Approximating Pi Using Numerical Summation
### Explanation
This method approximates the value of \( \pi \) using a numerical summation technique. The approach is based on the concept of inscribing and circumscribing rectangles under a quarter-circle to estimate the area. By summing the areas of these rectangles, we can approximate the area of the quarter-circle, which in turn helps us estimate \( \pi \).
### Mathematical Representation
The equation used is:
![Pi Equation](.../.././base.png)
Where:
- \( a \) and \( b \) are summations that approximate the quarter-circle area.
- The sum iterates over a set of discrete steps to refine the approximation.
More specifically, \( a \) and \( b \) are defined as:
![A Equation](.../.././a.png)
![B Equation](.../.././b.png)
### Python Equivalent
```python file.py
import math
def approximate_pi(n=46325):
lange = n - 2
a, b = 0, 0
# Compute summation for 'a'
for k in range(1, lange + 1): # Changed to include lange
a += math.sqrt(n**2 - k**2)
# Compute summation for 'b'
for j in range(lange + 1): # Changed to include lange
b += math.sqrt(n**2 - j**2)
# Normalize the results
b = 4 / (n**2) * b
a = 4 / (n**2) * a
result = (a + b) / 2
return result
# Run the function
print(approximate_pi())
```
### How It Works
1. **Initialization**: The variable `lange` is set to \( n - 2 \), and two summation variables `a` and `b` are initialized to zero.
2. **Summation for 'a'**: Iterates from 1 to `lange`, calculating the square root of \( n^2 - k^2 \) for each \( k \).
3. **Summation for 'b'**: Iterates from 0 to `lange`, calculating the square root of \( n^2 - j^2 \) for each \( j \).
4. **Normalization**: Both summations are normalized by multiplying by \( \frac{4}{n^2} \).
5. **Result Calculation**: The final approximation of \( \pi \) is obtained by averaging the normalized values of `a` and `b`.
### Conclusion
This method provides an approximation of \( \pi \) using numerical summation. The accuracy increases as \( n \) grows larger, effectively refining the result. By adjusting the value of \( n \), you can control the trade-off between computational effort and precision.
### Notes
- The choice of `n = 46325` is arbitrary and can be adjusted for better performance or higher precision.
- This method is computationally intensive and may require optimization for very large values of \( n \).

View File

@ -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))

View File

@ -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"}

View File

@ -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"
)

View File

@ -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"]
__all__ = ["is_even", "is_odd", "approximate_pi", "rooting"]

View File

@ -0,0 +1,28 @@
#include <Python.h>
#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);
}

View File

@ -0,0 +1,27 @@
#include "pi.hpp"
#include <cmath>
#include <iostream> // 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;
}

View File

@ -0,0 +1,5 @@
#pragma once
namespace pi {
long double approximate_pi(long long number);
}