polysolve 0.2.1__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {polysolve-0.2.1/src/polysolve.egg-info → polysolve-0.3.0}/PKG-INFO +3 -2
- {polysolve-0.2.1 → polysolve-0.3.0}/README.md +2 -1
- {polysolve-0.2.1 → polysolve-0.3.0}/pyproject.toml +1 -1
- {polysolve-0.2.1 → polysolve-0.3.0}/src/polysolve/__init__.py +55 -14
- {polysolve-0.2.1 → polysolve-0.3.0/src/polysolve.egg-info}/PKG-INFO +3 -2
- {polysolve-0.2.1 → polysolve-0.3.0}/LICENSE +0 -0
- {polysolve-0.2.1 → polysolve-0.3.0}/setup.cfg +0 -0
- {polysolve-0.2.1 → polysolve-0.3.0}/src/polysolve.egg-info/SOURCES.txt +0 -0
- {polysolve-0.2.1 → polysolve-0.3.0}/src/polysolve.egg-info/dependency_links.txt +0 -0
- {polysolve-0.2.1 → polysolve-0.3.0}/src/polysolve.egg-info/requires.txt +0 -0
- {polysolve-0.2.1 → polysolve-0.3.0}/src/polysolve.egg-info/top_level.txt +0 -0
- {polysolve-0.2.1 → polysolve-0.3.0}/tests/test_polysolve.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: polysolve
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A Python library for representing, manipulating, and solving exponential functions using analytical methods and genetic algorithms, with optional CUDA acceleration.
|
|
5
5
|
Author-email: Jonathan Rampersad <jonathan@jono-rams.work>
|
|
6
6
|
License: MIT License
|
|
@@ -59,7 +59,7 @@ A Python library for representing, manipulating, and solving polynomial equation
|
|
|
59
59
|
|
|
60
60
|
## Key Features
|
|
61
61
|
|
|
62
|
-
* **Create and Manipulate Polynomials**: Easily define polynomials of any degree and perform arithmetic operations like addition, subtraction, and scaling.
|
|
62
|
+
* **Create and Manipulate Polynomials**: Easily define polynomials of any degree using integer or float coefficients, and perform arithmetic operations like addition, subtraction, multiplication, and scaling.
|
|
63
63
|
* **Genetic Algorithm Solver**: Find approximate real roots for complex polynomials where analytical solutions are difficult or impossible.
|
|
64
64
|
* **CUDA Accelerated**: Leverage NVIDIA GPUs for a massive performance boost when finding roots in large solution spaces.
|
|
65
65
|
* **Analytical Solvers**: Includes standard, exact solvers for simple cases (e.g., `quadratic_solve`).
|
|
@@ -94,6 +94,7 @@ Here is a simple example of how to define a quadratic function, find its propert
|
|
|
94
94
|
from polysolve import Function, GA_Options, quadratic_solve
|
|
95
95
|
|
|
96
96
|
# 1. Define the function f(x) = 2x^2 - 3x - 5
|
|
97
|
+
# Coefficients can be integers or floats.
|
|
97
98
|
f1 = Function(largest_exponent=2)
|
|
98
99
|
f1.set_constants([2, -3, -5])
|
|
99
100
|
|
|
@@ -9,7 +9,7 @@ A Python library for representing, manipulating, and solving polynomial equation
|
|
|
9
9
|
|
|
10
10
|
## Key Features
|
|
11
11
|
|
|
12
|
-
* **Create and Manipulate Polynomials**: Easily define polynomials of any degree and perform arithmetic operations like addition, subtraction, and scaling.
|
|
12
|
+
* **Create and Manipulate Polynomials**: Easily define polynomials of any degree using integer or float coefficients, and perform arithmetic operations like addition, subtraction, multiplication, and scaling.
|
|
13
13
|
* **Genetic Algorithm Solver**: Find approximate real roots for complex polynomials where analytical solutions are difficult or impossible.
|
|
14
14
|
* **CUDA Accelerated**: Leverage NVIDIA GPUs for a massive performance boost when finding roots in large solution spaces.
|
|
15
15
|
* **Analytical Solvers**: Includes standard, exact solvers for simple cases (e.g., `quadratic_solve`).
|
|
@@ -44,6 +44,7 @@ Here is a simple example of how to define a quadratic function, find its propert
|
|
|
44
44
|
from polysolve import Function, GA_Options, quadratic_solve
|
|
45
45
|
|
|
46
46
|
# 1. Define the function f(x) = 2x^2 - 3x - 5
|
|
47
|
+
# Coefficients can be integers or floats.
|
|
47
48
|
f1 = Function(largest_exponent=2)
|
|
48
49
|
f1.set_constants([2, -3, -5])
|
|
49
50
|
|
|
@@ -12,8 +12,32 @@ try:
|
|
|
12
12
|
except ImportError:
|
|
13
13
|
_CUPY_AVAILABLE = False
|
|
14
14
|
|
|
15
|
-
# The CUDA
|
|
16
|
-
|
|
15
|
+
# The CUDA kernels for the fitness function
|
|
16
|
+
_FITNESS_KERNEL_FLOAT = """
|
|
17
|
+
extern "C" __global__ void fitness_kernel(
|
|
18
|
+
const double* coefficients,
|
|
19
|
+
int num_coefficients,
|
|
20
|
+
const double* x_vals,
|
|
21
|
+
double* ranks,
|
|
22
|
+
int size,
|
|
23
|
+
double y_val)
|
|
24
|
+
{
|
|
25
|
+
int idx = threadIdx.x + blockIdx.x * blockDim.x;
|
|
26
|
+
if (idx < size)
|
|
27
|
+
{
|
|
28
|
+
double ans = 0;
|
|
29
|
+
int lrgst_expo = num_coefficients - 1;
|
|
30
|
+
for (int i = 0; i < num_coefficients; ++i)
|
|
31
|
+
{
|
|
32
|
+
ans += coefficients[i] * pow(x_vals[idx], (double)(lrgst_expo - i));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
ans -= y_val;
|
|
36
|
+
ranks[idx] = (ans == 0) ? 1.7976931348623157e+308 : fabs(1.0 / ans);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
"""
|
|
40
|
+
_FITNESS_KERNEL_INT = """
|
|
17
41
|
extern "C" __global__ void fitness_kernel(
|
|
18
42
|
const long long* coefficients,
|
|
19
43
|
int num_coefficients,
|
|
@@ -38,6 +62,7 @@ extern "C" __global__ void fitness_kernel(
|
|
|
38
62
|
}
|
|
39
63
|
"""
|
|
40
64
|
|
|
65
|
+
|
|
41
66
|
@dataclass
|
|
42
67
|
class GA_Options:
|
|
43
68
|
"""
|
|
@@ -76,13 +101,14 @@ class Function:
|
|
|
76
101
|
self.coefficients: Optional[np.ndarray] = None
|
|
77
102
|
self._initialized = False
|
|
78
103
|
|
|
79
|
-
def set_coeffs(self, coefficients: List[int]):
|
|
104
|
+
def set_coeffs(self, coefficients: List[Union[int, float]]):
|
|
80
105
|
"""
|
|
81
106
|
Sets the coefficients of the polynomial.
|
|
82
107
|
|
|
83
108
|
Args:
|
|
84
|
-
coefficients (List[int]): A list of integer
|
|
85
|
-
|
|
109
|
+
coefficients (List[Union[int, float]]): A list of integer or float
|
|
110
|
+
coefficients. The list size
|
|
111
|
+
must be largest_exponent + 1.
|
|
86
112
|
|
|
87
113
|
Raises:
|
|
88
114
|
ValueError: If the input is invalid.
|
|
@@ -95,8 +121,14 @@ class Function:
|
|
|
95
121
|
)
|
|
96
122
|
if coefficients[0] == 0 and self._largest_exponent > 0:
|
|
97
123
|
raise ValueError("The first constant (for the largest exponent) cannot be 0.")
|
|
124
|
+
|
|
125
|
+
# Check if any coefficient is a float
|
|
126
|
+
is_float = any(isinstance(c, float) for c in coefficients)
|
|
98
127
|
|
|
99
|
-
|
|
128
|
+
# Choose the dtype based on the input
|
|
129
|
+
target_dtype = np.float64 if is_float else np.int64
|
|
130
|
+
|
|
131
|
+
self.coefficients = np.array(coefficients, dtype=target_dtype)
|
|
100
132
|
self._initialized = True
|
|
101
133
|
|
|
102
134
|
def _check_initialized(self):
|
|
@@ -275,11 +307,16 @@ class Function:
|
|
|
275
307
|
|
|
276
308
|
def _solve_x_cuda(self, y_val: float, options: GA_Options) -> np.ndarray:
|
|
277
309
|
"""Genetic algorithm implementation using CuPy (GPU/CUDA)."""
|
|
278
|
-
# Load the raw CUDA kernel
|
|
279
|
-
fitness_gpu = cupy.RawKernel(_FITNESS_KERNEL, 'fitness_kernel')
|
|
280
310
|
|
|
281
|
-
#
|
|
282
|
-
|
|
311
|
+
# Check the dtype of our coefficients array
|
|
312
|
+
if self.coefficients.dtype == np.float64:
|
|
313
|
+
fitness_gpu = cupy.RawKernel(_FITNESS_KERNEL_FLOAT, 'fitness_kernel')
|
|
314
|
+
d_coefficients = cupy.array(self.coefficients, dtype=cupy.float64)
|
|
315
|
+
elif self.coefficients.dtype == np.int64:
|
|
316
|
+
fitness_gpu = cupy.RawKernel(_FITNESS_KERNEL_INT, 'fitness_kernel')
|
|
317
|
+
d_coefficients = cupy.array(self.coefficients, dtype=cupy.int64)
|
|
318
|
+
else:
|
|
319
|
+
raise TypeError(f"Unsupported dtype for CUDA solver: {self.coefficients.dtype}")
|
|
283
320
|
|
|
284
321
|
# Create initial random solutions on the GPU
|
|
285
322
|
d_solutions = cupy.random.uniform(
|
|
@@ -337,12 +374,16 @@ class Function:
|
|
|
337
374
|
power = self._largest_exponent - i
|
|
338
375
|
|
|
339
376
|
# Coefficient part
|
|
340
|
-
|
|
377
|
+
coeff_val = c
|
|
378
|
+
if c == int(c):
|
|
379
|
+
coeff_val = int(c)
|
|
380
|
+
|
|
381
|
+
if coeff_val == 1 and power != 0:
|
|
341
382
|
coeff = ""
|
|
342
|
-
elif
|
|
383
|
+
elif coeff_val == -1 and power != 0:
|
|
343
384
|
coeff = "-"
|
|
344
385
|
else:
|
|
345
|
-
coeff = str(
|
|
386
|
+
coeff = str(coeff_val)
|
|
346
387
|
|
|
347
388
|
# Variable part
|
|
348
389
|
if power == 0:
|
|
@@ -356,7 +397,7 @@ class Function:
|
|
|
356
397
|
sign = ""
|
|
357
398
|
if i > 0:
|
|
358
399
|
sign = " + " if c > 0 else " - "
|
|
359
|
-
coeff = str(abs(
|
|
400
|
+
coeff = str(abs(coeff_val))
|
|
360
401
|
if abs(c) == 1 and power != 0:
|
|
361
402
|
coeff = "" # Don't show 1 for non-constant terms
|
|
362
403
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: polysolve
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A Python library for representing, manipulating, and solving exponential functions using analytical methods and genetic algorithms, with optional CUDA acceleration.
|
|
5
5
|
Author-email: Jonathan Rampersad <jonathan@jono-rams.work>
|
|
6
6
|
License: MIT License
|
|
@@ -59,7 +59,7 @@ A Python library for representing, manipulating, and solving polynomial equation
|
|
|
59
59
|
|
|
60
60
|
## Key Features
|
|
61
61
|
|
|
62
|
-
* **Create and Manipulate Polynomials**: Easily define polynomials of any degree and perform arithmetic operations like addition, subtraction, and scaling.
|
|
62
|
+
* **Create and Manipulate Polynomials**: Easily define polynomials of any degree using integer or float coefficients, and perform arithmetic operations like addition, subtraction, multiplication, and scaling.
|
|
63
63
|
* **Genetic Algorithm Solver**: Find approximate real roots for complex polynomials where analytical solutions are difficult or impossible.
|
|
64
64
|
* **CUDA Accelerated**: Leverage NVIDIA GPUs for a massive performance boost when finding roots in large solution spaces.
|
|
65
65
|
* **Analytical Solvers**: Includes standard, exact solvers for simple cases (e.g., `quadratic_solve`).
|
|
@@ -94,6 +94,7 @@ Here is a simple example of how to define a quadratic function, find its propert
|
|
|
94
94
|
from polysolve import Function, GA_Options, quadratic_solve
|
|
95
95
|
|
|
96
96
|
# 1. Define the function f(x) = 2x^2 - 3x - 5
|
|
97
|
+
# Coefficients can be integers or floats.
|
|
97
98
|
f1 = Function(largest_exponent=2)
|
|
98
99
|
f1.set_constants([2, -3, -5])
|
|
99
100
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|