Compare commits
3 Commits
8e6f8dd178
...
70b40cdeeb
Author | SHA1 | Date |
---|---|---|
|
70b40cdeeb | |
|
0ef21826db | |
|
31ad0b27cb |
Binary file not shown.
After Width: | Height: | Size: 930 B |
Binary file not shown.
After Width: | Height: | Size: 949 B |
Binary file not shown.
After Width: | Height: | Size: 492 B |
|
@ -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:
|
||||
|
||||

|
||||
|
||||
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:
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
### 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 \).
|
|
@ -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))
|
||||
|
|
|
@ -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"}
|
||||
|
|
12
setup.py
12
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"
|
||||
)
|
|
@ -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"]
|
|
@ -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);
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
namespace pi {
|
||||
long double approximate_pi(long long number);
|
||||
}
|
Loading…
Reference in New Issue