polysolve 0.2.2__tar.gz → 0.3.1__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polysolve
3
- Version: 0.2.2
3
+ Version: 0.3.1
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
@@ -48,7 +48,9 @@ Provides-Extra: dev
48
48
  Requires-Dist: pytest; extra == "dev"
49
49
  Dynamic: license-file
50
50
 
51
- # polysolve
51
+ <p align="center">
52
+ <img src="https://i.ibb.co/N22Gx6xq/Poly-Solve-Logo.png" alt="polysolve Logo" width="256">
53
+ </p>
52
54
 
53
55
  [![PyPI version](https://img.shields.io/pypi/v/polysolve.svg)](https://pypi.org/project/polysolve/)
54
56
  [![PyPI pyversions](https://img.shields.io/pypi/pyversions/polysolve.svg)](https://pypi.org/project/polysolve/)
@@ -59,7 +61,7 @@ A Python library for representing, manipulating, and solving polynomial equation
59
61
 
60
62
  ## Key Features
61
63
 
62
- * **Create and Manipulate Polynomials**: Easily define polynomials of any degree and perform arithmetic operations like addition, subtraction, multiplication, and scaling.
64
+ * **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
65
  * **Genetic Algorithm Solver**: Find approximate real roots for complex polynomials where analytical solutions are difficult or impossible.
64
66
  * **CUDA Accelerated**: Leverage NVIDIA GPUs for a massive performance boost when finding roots in large solution spaces.
65
67
  * **Analytical Solvers**: Includes standard, exact solvers for simple cases (e.g., `quadratic_solve`).
@@ -94,6 +96,7 @@ Here is a simple example of how to define a quadratic function, find its propert
94
96
  from polysolve import Function, GA_Options, quadratic_solve
95
97
 
96
98
  # 1. Define the function f(x) = 2x^2 - 3x - 5
99
+ # Coefficients can be integers or floats.
97
100
  f1 = Function(largest_exponent=2)
98
101
  f1.set_constants([2, -3, -5])
99
102
 
@@ -1,4 +1,6 @@
1
- # polysolve
1
+ <p align="center">
2
+ <img src="https://i.ibb.co/N22Gx6xq/Poly-Solve-Logo.png" alt="polysolve Logo" width="256">
3
+ </p>
2
4
 
3
5
  [![PyPI version](https://img.shields.io/pypi/v/polysolve.svg)](https://pypi.org/project/polysolve/)
4
6
  [![PyPI pyversions](https://img.shields.io/pypi/pyversions/polysolve.svg)](https://pypi.org/project/polysolve/)
@@ -9,7 +11,7 @@ A Python library for representing, manipulating, and solving polynomial equation
9
11
 
10
12
  ## Key Features
11
13
 
12
- * **Create and Manipulate Polynomials**: Easily define polynomials of any degree and perform arithmetic operations like addition, subtraction, multiplication, and scaling.
14
+ * **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
15
  * **Genetic Algorithm Solver**: Find approximate real roots for complex polynomials where analytical solutions are difficult or impossible.
14
16
  * **CUDA Accelerated**: Leverage NVIDIA GPUs for a massive performance boost when finding roots in large solution spaces.
15
17
  * **Analytical Solvers**: Includes standard, exact solvers for simple cases (e.g., `quadratic_solve`).
@@ -44,6 +46,7 @@ Here is a simple example of how to define a quadratic function, find its propert
44
46
  from polysolve import Function, GA_Options, quadratic_solve
45
47
 
46
48
  # 1. Define the function f(x) = 2x^2 - 3x - 5
49
+ # Coefficients can be integers or floats.
47
50
  f1 = Function(largest_exponent=2)
48
51
  f1.set_constants([2, -3, -5])
49
52
 
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
5
5
  [project]
6
6
  # --- Core Metadata ---
7
7
  name = "polysolve"
8
- version = "0.2.2"
8
+ version = "0.3.1"
9
9
  authors = [
10
10
  { name="Jonathan Rampersad", email="jonathan@jono-rams.work" },
11
11
  ]
@@ -12,8 +12,32 @@ try:
12
12
  except ImportError:
13
13
  _CUPY_AVAILABLE = False
14
14
 
15
- # The CUDA kernel for the fitness function
16
- _FITNESS_KERNEL = """
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 coefficients. The list size
85
- must be largest_exponent + 1.
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
- self.coefficients = np.array(coefficients, dtype=np.int64)
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
- # Move coefficients to GPU
282
- d_coefficients = cupy.array(self.coefficients, dtype=cupy.int64)
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
- if c == 1 and power != 0:
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 c == -1 and power != 0:
383
+ elif coeff_val == -1 and power != 0:
343
384
  coeff = "-"
344
385
  else:
345
- coeff = str(c)
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(c))
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.2.2
3
+ Version: 0.3.1
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
@@ -48,7 +48,9 @@ Provides-Extra: dev
48
48
  Requires-Dist: pytest; extra == "dev"
49
49
  Dynamic: license-file
50
50
 
51
- # polysolve
51
+ <p align="center">
52
+ <img src="https://i.ibb.co/N22Gx6xq/Poly-Solve-Logo.png" alt="polysolve Logo" width="256">
53
+ </p>
52
54
 
53
55
  [![PyPI version](https://img.shields.io/pypi/v/polysolve.svg)](https://pypi.org/project/polysolve/)
54
56
  [![PyPI pyversions](https://img.shields.io/pypi/pyversions/polysolve.svg)](https://pypi.org/project/polysolve/)
@@ -59,7 +61,7 @@ A Python library for representing, manipulating, and solving polynomial equation
59
61
 
60
62
  ## Key Features
61
63
 
62
- * **Create and Manipulate Polynomials**: Easily define polynomials of any degree and perform arithmetic operations like addition, subtraction, multiplication, and scaling.
64
+ * **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
65
  * **Genetic Algorithm Solver**: Find approximate real roots for complex polynomials where analytical solutions are difficult or impossible.
64
66
  * **CUDA Accelerated**: Leverage NVIDIA GPUs for a massive performance boost when finding roots in large solution spaces.
65
67
  * **Analytical Solvers**: Includes standard, exact solvers for simple cases (e.g., `quadratic_solve`).
@@ -94,6 +96,7 @@ Here is a simple example of how to define a quadratic function, find its propert
94
96
  from polysolve import Function, GA_Options, quadratic_solve
95
97
 
96
98
  # 1. Define the function f(x) = 2x^2 - 3x - 5
99
+ # Coefficients can be integers or floats.
97
100
  f1 = Function(largest_exponent=2)
98
101
  f1.set_constants([2, -3, -5])
99
102
 
File without changes
File without changes