Compare commits
3 Commits
150d450965
...
2bcb4f3d3d
Author | SHA1 | Date |
---|---|---|
|
2bcb4f3d3d | |
|
d008db30ba | |
|
e849593530 |
|
@ -34,4 +34,4 @@ jobs:
|
|||
VECTORDB_TOKEN: ${{ secrets.VECTORDB_TOKEN }}
|
||||
run: |
|
||||
cd VectorLoader
|
||||
python -m src.run --full
|
||||
python -m src.run
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from fabelous_math import is_even, is_odd
|
||||
from fabelous_math import is_even, is_odd, rooting
|
||||
|
||||
print(rooting(0.5))
|
||||
print(is_even(5))
|
||||
print(is_odd(19))
|
||||
print(is_odd(19))
|
||||
|
|
|
@ -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"}
|
||||
|
|
18
setup.py
18
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.rooting',
|
||||
sources=[
|
||||
'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,
|
||||
|
@ -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"
|
||||
)
|
|
@ -0,0 +1,37 @@
|
|||
#include <Python.h>
|
||||
#include "rooting.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
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);
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
return PyModule_Create(&simple_functions_module);
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#include <Python.h>
|
||||
#include "sqrt.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
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);
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#include "rooting.hpp"
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
#include <limits>
|
||||
|
||||
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<double>::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;
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
namespace rooting {
|
||||
double nth_root(long double number, double n);
|
||||
}
|
Loading…
Reference in New Issue