rgwfuncs 0.0.28__tar.gz → 0.0.30__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.
- {rgwfuncs-0.0.28/src/rgwfuncs.egg-info → rgwfuncs-0.0.30}/PKG-INFO +95 -67
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/README.md +94 -66
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/pyproject.toml +1 -1
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/setup.cfg +1 -1
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs/__init__.py +1 -1
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs/algebra_lib.py +91 -22
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30/src/rgwfuncs.egg-info}/PKG-INFO +95 -67
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/tests/test_algebra_lib.py +40 -23
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/LICENSE +0 -0
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs/df_lib.py +0 -0
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs/docs_lib.py +0 -0
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs/str_lib.py +0 -0
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs.egg-info/SOURCES.txt +0 -0
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs.egg-info/dependency_links.txt +0 -0
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs.egg-info/entry_points.txt +0 -0
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs.egg-info/requires.txt +0 -0
- {rgwfuncs-0.0.28 → rgwfuncs-0.0.30}/src/rgwfuncs.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: rgwfuncs
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.30
|
4
4
|
Summary: A functional programming paradigm for mathematical modelling and data science
|
5
5
|
Home-page: https://github.com/ryangerardwilson/rgwfunc
|
6
6
|
Author: Ryan Gerard Wilson
|
@@ -154,105 +154,137 @@ Print a list of available function names in alphabetical order. If a filter is p
|
|
154
154
|
|
155
155
|
This section provides comprehensive functions for handling algebraic expressions, performing tasks such as computation, simplification, solving equations, and prime factorization, all outputted in LaTeX format.
|
156
156
|
|
157
|
-
### 1. `
|
157
|
+
### 1. `python_polynomial_expression_to_latex`
|
158
158
|
|
159
|
-
|
159
|
+
Converts a polynomial expression written in Python syntax to a LaTeX formatted string. This function parses algebraic expressions provided as strings using Python’s syntax and translates them into equivalent LaTeX representations, making them suitable for academic or professional documentation. The function supports inclusion of named variables, with an option to substitute specific values into the expression.
|
160
160
|
|
161
|
-
|
162
|
-
- `expression` (str):
|
161
|
+
• Parameters:
|
162
|
+
- `expression` (str): The algebraic expression to convert to LaTeX. This should be a string formatted with Python syntax acceptable by SymPy.
|
163
|
+
- `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where the keys are variable names in the expression, and the values are the numbers with which to substitute those variables.
|
163
164
|
|
164
|
-
|
165
|
+
• Returns:
|
166
|
+
- `str`: The LaTeX formatted string equivalent to the provided expression.
|
167
|
+
|
168
|
+
• Raises:
|
169
|
+
- `ValueError`: If the expression cannot be parsed due to syntax errors.
|
170
|
+
|
171
|
+
• Example:
|
172
|
+
|
173
|
+
from rgwfuncs import python_polynomial_expression_to_latex
|
174
|
+
|
175
|
+
# Convert a simple polynomial expression to LaTeX format
|
176
|
+
latex_result1 = python_polynomial_expression_to_latex("x**2 + y**2")
|
177
|
+
print(latex_result1) # Output: "x^{2} + y^{2}"
|
178
|
+
|
179
|
+
# Convert polynomial expression with substituted values
|
180
|
+
latex_result2 = python_polynomial_expression_to_latex("x**2 + y**2", {"x": 3, "y": 4})
|
181
|
+
print(latex_result2) # Output: "25"
|
182
|
+
|
183
|
+
# Another example with partial substitution
|
184
|
+
latex_result3 = python_polynomial_expression_to_latex("x**2 + y**2", {"x": 3})
|
185
|
+
print(latex_result3) # Output: "y^{2} + 9"
|
186
|
+
|
187
|
+
# Trigonometric functions included with symbolic variables
|
188
|
+
latex_result4 = python_polynomial_expression_to_latex("sin(x+z**2) + cos(y)", {"x": 55})
|
189
|
+
print(latex_result4) # Output: "cos y + sin \\left(z^{2} + 55\\right)"
|
190
|
+
|
191
|
+
# Simplified trigonometric functions example with substitution
|
192
|
+
latex_result5 = python_polynomial_expression_to_latex("sin(x) + cos(y)", {"x": 0})
|
193
|
+
print(latex_result5) # Output: "cos y"
|
194
|
+
|
195
|
+
--------------------------------------------------------------------------------
|
196
|
+
|
197
|
+
### 2. `compute_constant_expression`
|
198
|
+
|
199
|
+
Computes the numerical result of a given expression, which can evaluate to a constant, represented as a float. Evaluates an constant expression provided as a string and returns the computed result. Supports various arithmetic operations, including addition, subtraction, multiplication, division, and modulo, as well as mathematical functions from the math module.
|
200
|
+
|
201
|
+
• Parameters:
|
202
|
+
- `expression` (str): The constant expression to compute. This should be a string consisting of arithmetic operations and Python's math module functions.
|
203
|
+
|
204
|
+
• Returns:
|
165
205
|
- `float`: The computed numerical result.
|
166
206
|
|
167
|
-
|
207
|
+
• Example:
|
168
208
|
|
169
|
-
from rgwfuncs import
|
170
|
-
result1 =
|
209
|
+
from rgwfuncs import compute_constant_expression
|
210
|
+
result1 = compute_constant_expression("2 + 2")
|
171
211
|
print(result1) # Output: 4.0
|
172
212
|
|
173
|
-
result2 =
|
213
|
+
result2 = compute_constant_expression("10 % 3")
|
174
214
|
print(result2) # Output: 1.0
|
175
215
|
|
176
|
-
result3 =
|
216
|
+
result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
|
177
217
|
print(result3) # Output: 84852.8137423857
|
178
218
|
|
179
|
-
These examples illustrate the ability to handle basic arithmetic, the modulo operator, and functions utilizing the Python math module.
|
180
|
-
|
181
219
|
--------------------------------------------------------------------------------
|
182
220
|
|
183
|
-
###
|
221
|
+
### 3. `simplify_polynomial_expression`
|
184
222
|
|
185
|
-
Simplifies
|
223
|
+
Simplifies an algebraic expression in polynomial form and returns it in LaTeX format. Takes an algebraic expression, in polynomial form, written in Python syntax and simplifies it. The result is returned as a LaTeX formatted string, suitable for academic or professional documentation.
|
186
224
|
|
187
|
-
|
188
|
-
- `expression` (str):
|
225
|
+
• Parameters:
|
226
|
+
- `expression` (str): The algebraic expression, in polynomial form, to simplify. For instance, the expression 'np.diff(8*x**30) where as 'np.diff([2,5,9,11)' is not a polynomial.
|
189
227
|
- `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where keys are variable names and values are the numbers to substitute them with.
|
190
228
|
|
191
|
-
|
229
|
+
• Returns:
|
192
230
|
- `str`: The simplified expression formatted as a LaTeX string.
|
193
231
|
|
194
|
-
|
232
|
+
• Example Usage:
|
195
233
|
|
196
|
-
from rgwfuncs import
|
234
|
+
from rgwfuncs import simplify_polynomial_expression
|
197
235
|
|
198
236
|
# Example 1: Simplifying a polynomial expression without substitutions
|
199
|
-
simplified_expr1 =
|
237
|
+
simplified_expr1 = simplify_polynomial_expression("2*x + 3*x")
|
200
238
|
print(simplified_expr1) # Output: "5 x"
|
201
239
|
|
202
240
|
# Example 2: Simplifying a complex expression involving derivatives
|
203
|
-
simplified_expr2 =
|
204
|
-
"(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)"
|
205
|
-
)
|
241
|
+
simplified_expr2 = simplify_polynomial_expression("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)")
|
206
242
|
print(simplified_expr2) # Output: r"\frac{1}{110 x^{22} y^{3}}"
|
207
243
|
|
208
244
|
# Example 3: Simplifying with substitutions
|
209
|
-
simplified_expr3 =
|
245
|
+
simplified_expr3 = simplify_polynomial_expression("x**2 + y**2", subs={"x": 3, "y": 4})
|
210
246
|
print(simplified_expr3) # Output: "25"
|
211
247
|
|
212
248
|
# Example 4: Simplifying with partial substitution
|
213
|
-
simplified_expr4 =
|
214
|
-
print(simplified_expr4) # Output: "a
|
215
|
-
|
216
|
-
These examples demonstrate the simplification of polynomial expressions, handling complex ratios involving derivatives, and applying variable substitutions before simplifying. The function handles expressions both with and without substitutions, providing flexibility in its usage.
|
249
|
+
simplified_expr4 = simplify_polynomial_expression("a*b + b", subs={"b": 2})
|
250
|
+
print(simplified_expr4) # Output: "2 a + 2"
|
217
251
|
|
218
252
|
--------------------------------------------------------------------------------
|
219
253
|
|
220
|
-
###
|
254
|
+
### 4. `solve_homogeneous_polynomial_expression`
|
221
255
|
|
222
|
-
Solves equations for specified variables, with optional substitutions, returning LaTeX-formatted solutions.
|
256
|
+
Solves a homogeneous polynomial expression for a specified variable and returns solutions in LaTeX format. Assumes that the expression is homoegeneous (i.e. equal to zero), and solves for a designated variable. May optionally include substitutions for other variables in the equation. The solutions are provided as a LaTeX formatted string. The method solves equations for specified variables, with optional substitutions, returning LaTeX-formatted solutions.
|
223
257
|
|
224
|
-
|
225
|
-
- `expression` (str): A string of the
|
258
|
+
• Parameters:
|
259
|
+
- `expression` (str): A string of the homogeneous polynomial expression to solve.
|
226
260
|
- `variable` (str): The variable to solve for.
|
227
261
|
- `subs` (Optional[Dict[str, float]]): Substitutions for variables.
|
228
262
|
|
229
|
-
|
263
|
+
• Returns:
|
230
264
|
- `str`: Solutions formatted in LaTeX.
|
231
265
|
|
232
|
-
|
266
|
+
• Example:
|
233
267
|
|
234
|
-
from rgwfuncs import
|
235
|
-
solutions1 =
|
268
|
+
from rgwfuncs import solve_homogeneous_polynomial_expression
|
269
|
+
solutions1 = solve_homogeneous_polynomial_expression("a*x**2 + b*x + c", "x", {"a": 3, "b": 7, "c": 5})
|
236
270
|
print(solutions1) # Output: "\left[-7/6 - sqrt(11)*I/6, -7/6 + sqrt(11)*I/6\right]"
|
237
271
|
|
238
|
-
solutions2 =
|
272
|
+
solutions2 = solve_homogeneous_polynomial_expression("x**2 - 4", "x")
|
239
273
|
print(solutions2) # Output: "\left[-2, 2\right]"
|
240
274
|
|
241
|
-
Here, we solve both a quadratic equation with complex solutions and a simpler polynomial equation.
|
242
|
-
|
243
275
|
--------------------------------------------------------------------------------
|
244
276
|
|
245
|
-
###
|
277
|
+
### 5. `get_prime_factors_latex`
|
246
278
|
|
247
279
|
Computes prime factors of a number and presents them in LaTeX format.
|
248
280
|
|
249
|
-
|
281
|
+
• Parameters:
|
250
282
|
- `n` (int): The integer to factorize.
|
251
283
|
|
252
|
-
|
284
|
+
• Returns:
|
253
285
|
- `str`: Prime factorization in LaTeX.
|
254
286
|
|
255
|
-
|
287
|
+
• Example:
|
256
288
|
|
257
289
|
from rgwfuncs import get_prime_factors_latex
|
258
290
|
factors1 = get_prime_factors_latex(100)
|
@@ -266,64 +298,60 @@ Computes prime factors of a number and presents them in LaTeX format.
|
|
266
298
|
|
267
299
|
--------------------------------------------------------------------------------
|
268
300
|
|
269
|
-
###
|
301
|
+
### 6. `compute_matrix_expression`
|
270
302
|
|
271
|
-
Computes the results of 1D or 2D matrix operations and formats them as LaTeX strings.
|
303
|
+
Computes the results of expressions containing 1D or 2D matrix operations and formats them as LaTeX strings.
|
272
304
|
|
273
|
-
|
305
|
+
• Parameters:
|
274
306
|
- `expression` (str): A string representing a sequence of matrix operations involving either 1D or 2D lists. Supported operations include addition (`+`), subtraction (`-`), multiplication (`*`), and division (`/`).
|
275
307
|
|
276
|
-
|
308
|
+
• Returns:
|
277
309
|
- `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
|
278
310
|
|
279
|
-
|
311
|
+
• Example:
|
280
312
|
|
281
|
-
from rgwfuncs import
|
313
|
+
from rgwfuncs import compute_matrix_expression
|
282
314
|
|
283
315
|
# Example with addition of 2D matrices
|
284
|
-
result =
|
316
|
+
result = compute_matrix_expression("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
|
285
317
|
print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
|
286
318
|
|
287
319
|
# Example of mixed operations with 1D matrices treated as 2D
|
288
|
-
result =
|
320
|
+
result = compute_matrix_expression("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
|
289
321
|
print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
|
290
322
|
|
291
323
|
# Example with dimension mismatch
|
292
|
-
result =
|
324
|
+
result = compute_matrix_expression("[[4, 3, 51]] + [[1, 1]]")
|
293
325
|
print(result) # Output: Operations between matrices must involve matrices of the same dimension
|
294
326
|
|
295
|
-
This function performs elementwise operations on both 1D and 2D matrices represented as Python lists and formats the result as a LaTeX string. It handles operations sequentially from left to right and gracefully handles dimension mismatches by returning a meaningful message. It utilizes Python's `ast.literal_eval` for safe and robust parsing.
|
296
|
-
|
297
327
|
--------------------------------------------------------------------------------
|
298
328
|
|
299
|
-
###
|
329
|
+
### 7. `compute_ordered_series_expression`
|
300
330
|
|
301
|
-
Computes the result of operations on ordered series expressed as 1D lists
|
331
|
+
Computes the result of expressions containing operations on ordered series expressed as 1D lists. The syntax of the expression supports the discrete difference operator via the `ddd()` method.
|
302
332
|
|
303
|
-
|
304
|
-
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd` for discrete differences.
|
333
|
+
• Parameters:
|
334
|
+
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd()` for discrete differences.
|
305
335
|
|
306
|
-
|
336
|
+
• Returns:
|
307
337
|
- `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
|
308
338
|
|
309
|
-
|
339
|
+
• Example:
|
310
340
|
|
311
|
-
from rgwfuncs import
|
341
|
+
from rgwfuncs import compute_ordered_series_expression
|
312
342
|
|
313
343
|
# Example with addition and discrete differences
|
314
|
-
result =
|
344
|
+
result = compute_ordered_series_expression("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
|
315
345
|
print(result) # Output: [4, 3, 51] + [1, 1]
|
316
346
|
|
317
347
|
# Example with elementwise subtraction
|
318
|
-
result =
|
348
|
+
result = compute_ordered_series_expression("[10, 15, 21] - [5, 5, 5]")
|
319
349
|
print(result) # Output: [5, 10, 16]
|
320
350
|
|
321
351
|
# Example with length mismatch
|
322
|
-
result =
|
352
|
+
result = compute_ordered_series_expression("[4, 3, 51] + [1, 1]")
|
323
353
|
print(result) # Output: Operations between ordered series must involve series of equal length
|
324
354
|
|
325
|
-
This function first applies the discrete difference operator to any series where applicable, then evaluates arithmetic operations between series. It returns a string representation of the result or an error message if the series lengths do not match. The function is robust, directly parsing and evaluating given series expressions with safety checks in place.
|
326
|
-
|
327
355
|
--------------------------------------------------------------------------------
|
328
356
|
|
329
357
|
## String Based Functions
|
@@ -128,105 +128,137 @@ Print a list of available function names in alphabetical order. If a filter is p
|
|
128
128
|
|
129
129
|
This section provides comprehensive functions for handling algebraic expressions, performing tasks such as computation, simplification, solving equations, and prime factorization, all outputted in LaTeX format.
|
130
130
|
|
131
|
-
### 1. `
|
131
|
+
### 1. `python_polynomial_expression_to_latex`
|
132
132
|
|
133
|
-
|
133
|
+
Converts a polynomial expression written in Python syntax to a LaTeX formatted string. This function parses algebraic expressions provided as strings using Python’s syntax and translates them into equivalent LaTeX representations, making them suitable for academic or professional documentation. The function supports inclusion of named variables, with an option to substitute specific values into the expression.
|
134
134
|
|
135
|
-
|
136
|
-
- `expression` (str):
|
135
|
+
• Parameters:
|
136
|
+
- `expression` (str): The algebraic expression to convert to LaTeX. This should be a string formatted with Python syntax acceptable by SymPy.
|
137
|
+
- `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where the keys are variable names in the expression, and the values are the numbers with which to substitute those variables.
|
137
138
|
|
138
|
-
|
139
|
+
• Returns:
|
140
|
+
- `str`: The LaTeX formatted string equivalent to the provided expression.
|
141
|
+
|
142
|
+
• Raises:
|
143
|
+
- `ValueError`: If the expression cannot be parsed due to syntax errors.
|
144
|
+
|
145
|
+
• Example:
|
146
|
+
|
147
|
+
from rgwfuncs import python_polynomial_expression_to_latex
|
148
|
+
|
149
|
+
# Convert a simple polynomial expression to LaTeX format
|
150
|
+
latex_result1 = python_polynomial_expression_to_latex("x**2 + y**2")
|
151
|
+
print(latex_result1) # Output: "x^{2} + y^{2}"
|
152
|
+
|
153
|
+
# Convert polynomial expression with substituted values
|
154
|
+
latex_result2 = python_polynomial_expression_to_latex("x**2 + y**2", {"x": 3, "y": 4})
|
155
|
+
print(latex_result2) # Output: "25"
|
156
|
+
|
157
|
+
# Another example with partial substitution
|
158
|
+
latex_result3 = python_polynomial_expression_to_latex("x**2 + y**2", {"x": 3})
|
159
|
+
print(latex_result3) # Output: "y^{2} + 9"
|
160
|
+
|
161
|
+
# Trigonometric functions included with symbolic variables
|
162
|
+
latex_result4 = python_polynomial_expression_to_latex("sin(x+z**2) + cos(y)", {"x": 55})
|
163
|
+
print(latex_result4) # Output: "cos y + sin \\left(z^{2} + 55\\right)"
|
164
|
+
|
165
|
+
# Simplified trigonometric functions example with substitution
|
166
|
+
latex_result5 = python_polynomial_expression_to_latex("sin(x) + cos(y)", {"x": 0})
|
167
|
+
print(latex_result5) # Output: "cos y"
|
168
|
+
|
169
|
+
--------------------------------------------------------------------------------
|
170
|
+
|
171
|
+
### 2. `compute_constant_expression`
|
172
|
+
|
173
|
+
Computes the numerical result of a given expression, which can evaluate to a constant, represented as a float. Evaluates an constant expression provided as a string and returns the computed result. Supports various arithmetic operations, including addition, subtraction, multiplication, division, and modulo, as well as mathematical functions from the math module.
|
174
|
+
|
175
|
+
• Parameters:
|
176
|
+
- `expression` (str): The constant expression to compute. This should be a string consisting of arithmetic operations and Python's math module functions.
|
177
|
+
|
178
|
+
• Returns:
|
139
179
|
- `float`: The computed numerical result.
|
140
180
|
|
141
|
-
|
181
|
+
• Example:
|
142
182
|
|
143
|
-
from rgwfuncs import
|
144
|
-
result1 =
|
183
|
+
from rgwfuncs import compute_constant_expression
|
184
|
+
result1 = compute_constant_expression("2 + 2")
|
145
185
|
print(result1) # Output: 4.0
|
146
186
|
|
147
|
-
result2 =
|
187
|
+
result2 = compute_constant_expression("10 % 3")
|
148
188
|
print(result2) # Output: 1.0
|
149
189
|
|
150
|
-
result3 =
|
190
|
+
result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
|
151
191
|
print(result3) # Output: 84852.8137423857
|
152
192
|
|
153
|
-
These examples illustrate the ability to handle basic arithmetic, the modulo operator, and functions utilizing the Python math module.
|
154
|
-
|
155
193
|
--------------------------------------------------------------------------------
|
156
194
|
|
157
|
-
###
|
195
|
+
### 3. `simplify_polynomial_expression`
|
158
196
|
|
159
|
-
Simplifies
|
197
|
+
Simplifies an algebraic expression in polynomial form and returns it in LaTeX format. Takes an algebraic expression, in polynomial form, written in Python syntax and simplifies it. The result is returned as a LaTeX formatted string, suitable for academic or professional documentation.
|
160
198
|
|
161
|
-
|
162
|
-
- `expression` (str):
|
199
|
+
• Parameters:
|
200
|
+
- `expression` (str): The algebraic expression, in polynomial form, to simplify. For instance, the expression 'np.diff(8*x**30) where as 'np.diff([2,5,9,11)' is not a polynomial.
|
163
201
|
- `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where keys are variable names and values are the numbers to substitute them with.
|
164
202
|
|
165
|
-
|
203
|
+
• Returns:
|
166
204
|
- `str`: The simplified expression formatted as a LaTeX string.
|
167
205
|
|
168
|
-
|
206
|
+
• Example Usage:
|
169
207
|
|
170
|
-
from rgwfuncs import
|
208
|
+
from rgwfuncs import simplify_polynomial_expression
|
171
209
|
|
172
210
|
# Example 1: Simplifying a polynomial expression without substitutions
|
173
|
-
simplified_expr1 =
|
211
|
+
simplified_expr1 = simplify_polynomial_expression("2*x + 3*x")
|
174
212
|
print(simplified_expr1) # Output: "5 x"
|
175
213
|
|
176
214
|
# Example 2: Simplifying a complex expression involving derivatives
|
177
|
-
simplified_expr2 =
|
178
|
-
"(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)"
|
179
|
-
)
|
215
|
+
simplified_expr2 = simplify_polynomial_expression("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)")
|
180
216
|
print(simplified_expr2) # Output: r"\frac{1}{110 x^{22} y^{3}}"
|
181
217
|
|
182
218
|
# Example 3: Simplifying with substitutions
|
183
|
-
simplified_expr3 =
|
219
|
+
simplified_expr3 = simplify_polynomial_expression("x**2 + y**2", subs={"x": 3, "y": 4})
|
184
220
|
print(simplified_expr3) # Output: "25"
|
185
221
|
|
186
222
|
# Example 4: Simplifying with partial substitution
|
187
|
-
simplified_expr4 =
|
188
|
-
print(simplified_expr4) # Output: "a
|
189
|
-
|
190
|
-
These examples demonstrate the simplification of polynomial expressions, handling complex ratios involving derivatives, and applying variable substitutions before simplifying. The function handles expressions both with and without substitutions, providing flexibility in its usage.
|
223
|
+
simplified_expr4 = simplify_polynomial_expression("a*b + b", subs={"b": 2})
|
224
|
+
print(simplified_expr4) # Output: "2 a + 2"
|
191
225
|
|
192
226
|
--------------------------------------------------------------------------------
|
193
227
|
|
194
|
-
###
|
228
|
+
### 4. `solve_homogeneous_polynomial_expression`
|
195
229
|
|
196
|
-
Solves equations for specified variables, with optional substitutions, returning LaTeX-formatted solutions.
|
230
|
+
Solves a homogeneous polynomial expression for a specified variable and returns solutions in LaTeX format. Assumes that the expression is homoegeneous (i.e. equal to zero), and solves for a designated variable. May optionally include substitutions for other variables in the equation. The solutions are provided as a LaTeX formatted string. The method solves equations for specified variables, with optional substitutions, returning LaTeX-formatted solutions.
|
197
231
|
|
198
|
-
|
199
|
-
- `expression` (str): A string of the
|
232
|
+
• Parameters:
|
233
|
+
- `expression` (str): A string of the homogeneous polynomial expression to solve.
|
200
234
|
- `variable` (str): The variable to solve for.
|
201
235
|
- `subs` (Optional[Dict[str, float]]): Substitutions for variables.
|
202
236
|
|
203
|
-
|
237
|
+
• Returns:
|
204
238
|
- `str`: Solutions formatted in LaTeX.
|
205
239
|
|
206
|
-
|
240
|
+
• Example:
|
207
241
|
|
208
|
-
from rgwfuncs import
|
209
|
-
solutions1 =
|
242
|
+
from rgwfuncs import solve_homogeneous_polynomial_expression
|
243
|
+
solutions1 = solve_homogeneous_polynomial_expression("a*x**2 + b*x + c", "x", {"a": 3, "b": 7, "c": 5})
|
210
244
|
print(solutions1) # Output: "\left[-7/6 - sqrt(11)*I/6, -7/6 + sqrt(11)*I/6\right]"
|
211
245
|
|
212
|
-
solutions2 =
|
246
|
+
solutions2 = solve_homogeneous_polynomial_expression("x**2 - 4", "x")
|
213
247
|
print(solutions2) # Output: "\left[-2, 2\right]"
|
214
248
|
|
215
|
-
Here, we solve both a quadratic equation with complex solutions and a simpler polynomial equation.
|
216
|
-
|
217
249
|
--------------------------------------------------------------------------------
|
218
250
|
|
219
|
-
###
|
251
|
+
### 5. `get_prime_factors_latex`
|
220
252
|
|
221
253
|
Computes prime factors of a number and presents them in LaTeX format.
|
222
254
|
|
223
|
-
|
255
|
+
• Parameters:
|
224
256
|
- `n` (int): The integer to factorize.
|
225
257
|
|
226
|
-
|
258
|
+
• Returns:
|
227
259
|
- `str`: Prime factorization in LaTeX.
|
228
260
|
|
229
|
-
|
261
|
+
• Example:
|
230
262
|
|
231
263
|
from rgwfuncs import get_prime_factors_latex
|
232
264
|
factors1 = get_prime_factors_latex(100)
|
@@ -240,64 +272,60 @@ Computes prime factors of a number and presents them in LaTeX format.
|
|
240
272
|
|
241
273
|
--------------------------------------------------------------------------------
|
242
274
|
|
243
|
-
###
|
275
|
+
### 6. `compute_matrix_expression`
|
244
276
|
|
245
|
-
Computes the results of 1D or 2D matrix operations and formats them as LaTeX strings.
|
277
|
+
Computes the results of expressions containing 1D or 2D matrix operations and formats them as LaTeX strings.
|
246
278
|
|
247
|
-
|
279
|
+
• Parameters:
|
248
280
|
- `expression` (str): A string representing a sequence of matrix operations involving either 1D or 2D lists. Supported operations include addition (`+`), subtraction (`-`), multiplication (`*`), and division (`/`).
|
249
281
|
|
250
|
-
|
282
|
+
• Returns:
|
251
283
|
- `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
|
252
284
|
|
253
|
-
|
285
|
+
• Example:
|
254
286
|
|
255
|
-
from rgwfuncs import
|
287
|
+
from rgwfuncs import compute_matrix_expression
|
256
288
|
|
257
289
|
# Example with addition of 2D matrices
|
258
|
-
result =
|
290
|
+
result = compute_matrix_expression("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
|
259
291
|
print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
|
260
292
|
|
261
293
|
# Example of mixed operations with 1D matrices treated as 2D
|
262
|
-
result =
|
294
|
+
result = compute_matrix_expression("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
|
263
295
|
print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
|
264
296
|
|
265
297
|
# Example with dimension mismatch
|
266
|
-
result =
|
298
|
+
result = compute_matrix_expression("[[4, 3, 51]] + [[1, 1]]")
|
267
299
|
print(result) # Output: Operations between matrices must involve matrices of the same dimension
|
268
300
|
|
269
|
-
This function performs elementwise operations on both 1D and 2D matrices represented as Python lists and formats the result as a LaTeX string. It handles operations sequentially from left to right and gracefully handles dimension mismatches by returning a meaningful message. It utilizes Python's `ast.literal_eval` for safe and robust parsing.
|
270
|
-
|
271
301
|
--------------------------------------------------------------------------------
|
272
302
|
|
273
|
-
###
|
303
|
+
### 7. `compute_ordered_series_expression`
|
274
304
|
|
275
|
-
Computes the result of operations on ordered series expressed as 1D lists
|
305
|
+
Computes the result of expressions containing operations on ordered series expressed as 1D lists. The syntax of the expression supports the discrete difference operator via the `ddd()` method.
|
276
306
|
|
277
|
-
|
278
|
-
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd` for discrete differences.
|
307
|
+
• Parameters:
|
308
|
+
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd()` for discrete differences.
|
279
309
|
|
280
|
-
|
310
|
+
• Returns:
|
281
311
|
- `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
|
282
312
|
|
283
|
-
|
313
|
+
• Example:
|
284
314
|
|
285
|
-
from rgwfuncs import
|
315
|
+
from rgwfuncs import compute_ordered_series_expression
|
286
316
|
|
287
317
|
# Example with addition and discrete differences
|
288
|
-
result =
|
318
|
+
result = compute_ordered_series_expression("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
|
289
319
|
print(result) # Output: [4, 3, 51] + [1, 1]
|
290
320
|
|
291
321
|
# Example with elementwise subtraction
|
292
|
-
result =
|
322
|
+
result = compute_ordered_series_expression("[10, 15, 21] - [5, 5, 5]")
|
293
323
|
print(result) # Output: [5, 10, 16]
|
294
324
|
|
295
325
|
# Example with length mismatch
|
296
|
-
result =
|
326
|
+
result = compute_ordered_series_expression("[4, 3, 51] + [1, 1]")
|
297
327
|
print(result) # Output: Operations between ordered series must involve series of equal length
|
298
328
|
|
299
|
-
This function first applies the discrete difference operator to any series where applicable, then evaluates arithmetic operations between series. It returns a string representation of the result or an error message if the series lengths do not match. The function is robust, directly parsing and evaluating given series expressions with safety checks in place.
|
300
|
-
|
301
329
|
--------------------------------------------------------------------------------
|
302
330
|
|
303
331
|
## String Based Functions
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# This file is automatically generated
|
2
2
|
# Dynamically importing functions from modules
|
3
3
|
|
4
|
-
from .algebra_lib import
|
4
|
+
from .algebra_lib import compute_constant_expression, compute_matrix_expression, compute_ordered_series_expression, get_prime_factors_latex, python_polynomial_expression_to_latex, simplify_polynomial_expression, solve_homogeneous_polynomial_expression
|
5
5
|
from .df_lib import append_columns, append_percentile_classification_column, append_ranged_classification_column, append_ranged_date_classification_column, append_rows, append_xgb_labels, append_xgb_logistic_regression_predictions, append_xgb_regression_predictions, bag_union_join, bottom_n_unique_values, cascade_sort, delete_rows, drop_duplicates, drop_duplicates_retain_first, drop_duplicates_retain_last, filter_dataframe, filter_indian_mobiles, first_n_rows, from_raw_data, insert_dataframe_in_sqlite_database, last_n_rows, left_join, limit_dataframe, load_data_from_path, load_data_from_query, load_data_from_sqlite_path, mask_against_dataframe, mask_against_dataframe_converse, numeric_clean, order_columns, print_correlation, print_dataframe, print_memory_usage, print_n_frequency_cascading, print_n_frequency_linear, rename_columns, retain_columns, right_join, send_data_to_email, send_data_to_slack, send_dataframe_via_telegram, sync_dataframe_to_sqlite_database, top_n_unique_values, union_join, update_rows
|
6
6
|
from .docs_lib import docs
|
7
7
|
from .str_lib import send_telegram_message
|
@@ -4,20 +4,24 @@ import ast
|
|
4
4
|
# import numpy as np
|
5
5
|
from sympy import symbols, latex, simplify, solve, diff, Expr
|
6
6
|
from sympy.parsing.sympy_parser import parse_expr
|
7
|
+
from sympy import __all__ as sympy_functions
|
8
|
+
from sympy.parsing.sympy_parser import (standard_transformations, implicit_multiplication_application)
|
9
|
+
|
7
10
|
from typing import Tuple, List, Dict, Optional
|
8
11
|
|
9
12
|
|
10
|
-
def
|
13
|
+
def compute_constant_expression(expression: str) -> float:
|
11
14
|
"""
|
12
|
-
Computes the numerical result of a given
|
15
|
+
Computes the numerical result of a given expression, which can evaluate to a constant,
|
16
|
+
represented as a float.
|
13
17
|
|
14
|
-
Evaluates an
|
18
|
+
Evaluates an constant expression provided as a string and returns the computed result.
|
15
19
|
Supports various arithmetic operations, including addition, subtraction, multiplication,
|
16
20
|
division, and modulo, as well as mathematical functions from the math module.
|
17
21
|
|
18
22
|
Parameters:
|
19
|
-
expression (str): The
|
20
|
-
of arithmetic operations and
|
23
|
+
expression (str): The constant expression to compute. This should be a string consisting
|
24
|
+
of arithmetic operations and Python's math module functions.
|
21
25
|
|
22
26
|
Returns:
|
23
27
|
float: The evaluated numerical result of the expression.
|
@@ -36,18 +40,82 @@ def compute_algebraic_expression(expression: str) -> float:
|
|
36
40
|
raise ValueError(f"Error computing expression: {e}")
|
37
41
|
|
38
42
|
|
39
|
-
def
|
43
|
+
def python_polynomial_expression_to_latex(
|
40
44
|
expression: str,
|
41
45
|
subs: Optional[Dict[str, float]] = None
|
42
46
|
) -> str:
|
43
47
|
"""
|
44
|
-
|
48
|
+
Converts a polynomial expression written in Python syntax to LaTeX format.
|
45
49
|
|
46
|
-
|
47
|
-
|
50
|
+
This function takes an algebraic expression written in Python syntax and converts it
|
51
|
+
to a LaTeX formatted string. The expression is assumed to be in terms acceptable by
|
52
|
+
sympy, with named variables, and optionally includes substitutions for variables.
|
48
53
|
|
49
54
|
Parameters:
|
50
|
-
expression (str): The algebraic expression to
|
55
|
+
expression (str): The algebraic expression to convert to LaTeX. The expression should
|
56
|
+
be written using Python syntax.
|
57
|
+
subs (Optional[Dict[str, float]]): An optional dictionary of substitutions for variables
|
58
|
+
in the expression.
|
59
|
+
|
60
|
+
Returns:
|
61
|
+
str: The expression represented as a LaTeX string.
|
62
|
+
|
63
|
+
Raises:
|
64
|
+
ValueError: If the expression cannot be parsed due to syntax errors.
|
65
|
+
"""
|
66
|
+
|
67
|
+
transformations = standard_transformations + (implicit_multiplication_application,)
|
68
|
+
|
69
|
+
def parse_and_convert_expression(expr_str: str, sym_vars: Dict[str, Expr]) -> Expr:
|
70
|
+
try:
|
71
|
+
# Parse with transformations to handle implicit multiplication
|
72
|
+
expr = parse_expr(expr_str, local_dict=sym_vars, transformations=transformations)
|
73
|
+
if subs:
|
74
|
+
subs_symbols = {symbols(k): v for k, v in subs.items()}
|
75
|
+
expr = expr.subs(subs_symbols)
|
76
|
+
return expr
|
77
|
+
except (SyntaxError, ValueError, TypeError) as e:
|
78
|
+
raise ValueError(f"Error parsing expression: {expr_str}. Error: {e}")
|
79
|
+
|
80
|
+
# Extract variable names used in the expression
|
81
|
+
variable_names = set(re.findall(r'\b[a-zA-Z]\w*\b', expression))
|
82
|
+
sym_vars = {var: symbols(var) for var in variable_names}
|
83
|
+
|
84
|
+
# Import all general function names from SymPy into local scope
|
85
|
+
|
86
|
+
# Dynamically add SymPy functions to the symbol dictionary
|
87
|
+
for func_name in sympy_functions:
|
88
|
+
try:
|
89
|
+
candidate = globals().get(func_name) or locals().get(func_name)
|
90
|
+
if callable(candidate): # Ensure it's actually a callable
|
91
|
+
sym_vars[func_name] = candidate
|
92
|
+
except KeyError:
|
93
|
+
continue # Skip any non-callable or unavailable items
|
94
|
+
|
95
|
+
# Attempt to parse the expression
|
96
|
+
expr = parse_and_convert_expression(expression, sym_vars)
|
97
|
+
|
98
|
+
# Convert the expression to LaTeX format
|
99
|
+
latex_result = latex(expr)
|
100
|
+
return latex_result
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
def simplify_polynomial_expression(
|
105
|
+
expression: str,
|
106
|
+
subs: Optional[Dict[str, float]] = None
|
107
|
+
) -> str:
|
108
|
+
"""
|
109
|
+
Simplifies an algebraic expression in polynomial form and returns it in LaTeX format.
|
110
|
+
|
111
|
+
Takes an algebraic expression, in polynomial form, written in Python syntax and simplifies it.
|
112
|
+
The result is returned as a LaTeX formatted string, suitable for academic or professional
|
113
|
+
documentation.
|
114
|
+
|
115
|
+
Parameters:
|
116
|
+
expression (str): The algebraic expression, in polynomial form, to simplify. For instance,
|
117
|
+
the expression `np.diff(8*x**30)` is a polynomial, whereas np.diff([2,5,9,11)
|
118
|
+
is not a polynomial.
|
51
119
|
subs (Optional[Dict[str, float]]): An optional dictionary of substitutions for variables
|
52
120
|
in the expression.
|
53
121
|
|
@@ -58,7 +126,6 @@ def simplify_algebraic_expression(
|
|
58
126
|
ValueError: If the expression cannot be simplified due to errors in expression or parameters.
|
59
127
|
"""
|
60
128
|
|
61
|
-
|
62
129
|
def recursive_parse_function_call(
|
63
130
|
func_call: str, prefix: str, sym_vars: Dict[str, Expr]) -> Tuple[str, List[Expr]]:
|
64
131
|
# print(f"Parsing function call: {func_call}")
|
@@ -209,19 +276,21 @@ def simplify_algebraic_expression(
|
|
209
276
|
raise ValueError(f"Error simplifying expression: {e}")
|
210
277
|
|
211
278
|
|
212
|
-
def
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
279
|
+
def solve_homogeneous_polynomial_expression(
|
280
|
+
expression: str,
|
281
|
+
variable: str,
|
282
|
+
subs: Optional[Dict[str, float]] = None
|
283
|
+
) -> str:
|
217
284
|
"""
|
218
|
-
Solves
|
285
|
+
Solves a homogeneous polynomial expression for a specified variable and returns solutions
|
286
|
+
in LaTeX format.
|
219
287
|
|
220
|
-
|
221
|
-
|
288
|
+
Assumes that the expression is homoegeneous (i.e. equal to zero), and solves for a
|
289
|
+
designated variable. May optionally include substitutions for other variables in the
|
290
|
+
equation. The solutions are provided as a LaTeX formatted string.
|
222
291
|
|
223
292
|
Parameters:
|
224
|
-
expression (str): The
|
293
|
+
expression (str): The homogeneous polynomial expression to solve.
|
225
294
|
variable (str): The variable to solve the equation for.
|
226
295
|
subs (Optional[Dict[str, float]]): An optional dictionary of substitutions for variables
|
227
296
|
in the equation.
|
@@ -260,7 +329,7 @@ def solve_algebraic_expression(
|
|
260
329
|
raise ValueError(f"Error solving the expression: {e}")
|
261
330
|
|
262
331
|
|
263
|
-
def
|
332
|
+
def compute_matrix_expression(expression: str) -> str:
|
264
333
|
"""
|
265
334
|
Computes the result of a matrix-like operation on 1D or 2D list inputs and returns it as a LaTeX string.
|
266
335
|
|
@@ -350,7 +419,7 @@ def compute_matrix_operation(expression: str) -> str:
|
|
350
419
|
return f"Error computing matrix operation: {e}"
|
351
420
|
|
352
421
|
|
353
|
-
def
|
422
|
+
def compute_ordered_series_expression(expression: str) -> str:
|
354
423
|
"""
|
355
424
|
Computes the result of operations on ordered series expressed as 1D lists, including discrete difference (ddd),
|
356
425
|
and returns it as a string.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: rgwfuncs
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.30
|
4
4
|
Summary: A functional programming paradigm for mathematical modelling and data science
|
5
5
|
Home-page: https://github.com/ryangerardwilson/rgwfunc
|
6
6
|
Author: Ryan Gerard Wilson
|
@@ -154,105 +154,137 @@ Print a list of available function names in alphabetical order. If a filter is p
|
|
154
154
|
|
155
155
|
This section provides comprehensive functions for handling algebraic expressions, performing tasks such as computation, simplification, solving equations, and prime factorization, all outputted in LaTeX format.
|
156
156
|
|
157
|
-
### 1. `
|
157
|
+
### 1. `python_polynomial_expression_to_latex`
|
158
158
|
|
159
|
-
|
159
|
+
Converts a polynomial expression written in Python syntax to a LaTeX formatted string. This function parses algebraic expressions provided as strings using Python’s syntax and translates them into equivalent LaTeX representations, making them suitable for academic or professional documentation. The function supports inclusion of named variables, with an option to substitute specific values into the expression.
|
160
160
|
|
161
|
-
|
162
|
-
- `expression` (str):
|
161
|
+
• Parameters:
|
162
|
+
- `expression` (str): The algebraic expression to convert to LaTeX. This should be a string formatted with Python syntax acceptable by SymPy.
|
163
|
+
- `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where the keys are variable names in the expression, and the values are the numbers with which to substitute those variables.
|
163
164
|
|
164
|
-
|
165
|
+
• Returns:
|
166
|
+
- `str`: The LaTeX formatted string equivalent to the provided expression.
|
167
|
+
|
168
|
+
• Raises:
|
169
|
+
- `ValueError`: If the expression cannot be parsed due to syntax errors.
|
170
|
+
|
171
|
+
• Example:
|
172
|
+
|
173
|
+
from rgwfuncs import python_polynomial_expression_to_latex
|
174
|
+
|
175
|
+
# Convert a simple polynomial expression to LaTeX format
|
176
|
+
latex_result1 = python_polynomial_expression_to_latex("x**2 + y**2")
|
177
|
+
print(latex_result1) # Output: "x^{2} + y^{2}"
|
178
|
+
|
179
|
+
# Convert polynomial expression with substituted values
|
180
|
+
latex_result2 = python_polynomial_expression_to_latex("x**2 + y**2", {"x": 3, "y": 4})
|
181
|
+
print(latex_result2) # Output: "25"
|
182
|
+
|
183
|
+
# Another example with partial substitution
|
184
|
+
latex_result3 = python_polynomial_expression_to_latex("x**2 + y**2", {"x": 3})
|
185
|
+
print(latex_result3) # Output: "y^{2} + 9"
|
186
|
+
|
187
|
+
# Trigonometric functions included with symbolic variables
|
188
|
+
latex_result4 = python_polynomial_expression_to_latex("sin(x+z**2) + cos(y)", {"x": 55})
|
189
|
+
print(latex_result4) # Output: "cos y + sin \\left(z^{2} + 55\\right)"
|
190
|
+
|
191
|
+
# Simplified trigonometric functions example with substitution
|
192
|
+
latex_result5 = python_polynomial_expression_to_latex("sin(x) + cos(y)", {"x": 0})
|
193
|
+
print(latex_result5) # Output: "cos y"
|
194
|
+
|
195
|
+
--------------------------------------------------------------------------------
|
196
|
+
|
197
|
+
### 2. `compute_constant_expression`
|
198
|
+
|
199
|
+
Computes the numerical result of a given expression, which can evaluate to a constant, represented as a float. Evaluates an constant expression provided as a string and returns the computed result. Supports various arithmetic operations, including addition, subtraction, multiplication, division, and modulo, as well as mathematical functions from the math module.
|
200
|
+
|
201
|
+
• Parameters:
|
202
|
+
- `expression` (str): The constant expression to compute. This should be a string consisting of arithmetic operations and Python's math module functions.
|
203
|
+
|
204
|
+
• Returns:
|
165
205
|
- `float`: The computed numerical result.
|
166
206
|
|
167
|
-
|
207
|
+
• Example:
|
168
208
|
|
169
|
-
from rgwfuncs import
|
170
|
-
result1 =
|
209
|
+
from rgwfuncs import compute_constant_expression
|
210
|
+
result1 = compute_constant_expression("2 + 2")
|
171
211
|
print(result1) # Output: 4.0
|
172
212
|
|
173
|
-
result2 =
|
213
|
+
result2 = compute_constant_expression("10 % 3")
|
174
214
|
print(result2) # Output: 1.0
|
175
215
|
|
176
|
-
result3 =
|
216
|
+
result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
|
177
217
|
print(result3) # Output: 84852.8137423857
|
178
218
|
|
179
|
-
These examples illustrate the ability to handle basic arithmetic, the modulo operator, and functions utilizing the Python math module.
|
180
|
-
|
181
219
|
--------------------------------------------------------------------------------
|
182
220
|
|
183
|
-
###
|
221
|
+
### 3. `simplify_polynomial_expression`
|
184
222
|
|
185
|
-
Simplifies
|
223
|
+
Simplifies an algebraic expression in polynomial form and returns it in LaTeX format. Takes an algebraic expression, in polynomial form, written in Python syntax and simplifies it. The result is returned as a LaTeX formatted string, suitable for academic or professional documentation.
|
186
224
|
|
187
|
-
|
188
|
-
- `expression` (str):
|
225
|
+
• Parameters:
|
226
|
+
- `expression` (str): The algebraic expression, in polynomial form, to simplify. For instance, the expression 'np.diff(8*x**30) where as 'np.diff([2,5,9,11)' is not a polynomial.
|
189
227
|
- `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where keys are variable names and values are the numbers to substitute them with.
|
190
228
|
|
191
|
-
|
229
|
+
• Returns:
|
192
230
|
- `str`: The simplified expression formatted as a LaTeX string.
|
193
231
|
|
194
|
-
|
232
|
+
• Example Usage:
|
195
233
|
|
196
|
-
from rgwfuncs import
|
234
|
+
from rgwfuncs import simplify_polynomial_expression
|
197
235
|
|
198
236
|
# Example 1: Simplifying a polynomial expression without substitutions
|
199
|
-
simplified_expr1 =
|
237
|
+
simplified_expr1 = simplify_polynomial_expression("2*x + 3*x")
|
200
238
|
print(simplified_expr1) # Output: "5 x"
|
201
239
|
|
202
240
|
# Example 2: Simplifying a complex expression involving derivatives
|
203
|
-
simplified_expr2 =
|
204
|
-
"(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)"
|
205
|
-
)
|
241
|
+
simplified_expr2 = simplify_polynomial_expression("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)")
|
206
242
|
print(simplified_expr2) # Output: r"\frac{1}{110 x^{22} y^{3}}"
|
207
243
|
|
208
244
|
# Example 3: Simplifying with substitutions
|
209
|
-
simplified_expr3 =
|
245
|
+
simplified_expr3 = simplify_polynomial_expression("x**2 + y**2", subs={"x": 3, "y": 4})
|
210
246
|
print(simplified_expr3) # Output: "25"
|
211
247
|
|
212
248
|
# Example 4: Simplifying with partial substitution
|
213
|
-
simplified_expr4 =
|
214
|
-
print(simplified_expr4) # Output: "a
|
215
|
-
|
216
|
-
These examples demonstrate the simplification of polynomial expressions, handling complex ratios involving derivatives, and applying variable substitutions before simplifying. The function handles expressions both with and without substitutions, providing flexibility in its usage.
|
249
|
+
simplified_expr4 = simplify_polynomial_expression("a*b + b", subs={"b": 2})
|
250
|
+
print(simplified_expr4) # Output: "2 a + 2"
|
217
251
|
|
218
252
|
--------------------------------------------------------------------------------
|
219
253
|
|
220
|
-
###
|
254
|
+
### 4. `solve_homogeneous_polynomial_expression`
|
221
255
|
|
222
|
-
Solves equations for specified variables, with optional substitutions, returning LaTeX-formatted solutions.
|
256
|
+
Solves a homogeneous polynomial expression for a specified variable and returns solutions in LaTeX format. Assumes that the expression is homoegeneous (i.e. equal to zero), and solves for a designated variable. May optionally include substitutions for other variables in the equation. The solutions are provided as a LaTeX formatted string. The method solves equations for specified variables, with optional substitutions, returning LaTeX-formatted solutions.
|
223
257
|
|
224
|
-
|
225
|
-
- `expression` (str): A string of the
|
258
|
+
• Parameters:
|
259
|
+
- `expression` (str): A string of the homogeneous polynomial expression to solve.
|
226
260
|
- `variable` (str): The variable to solve for.
|
227
261
|
- `subs` (Optional[Dict[str, float]]): Substitutions for variables.
|
228
262
|
|
229
|
-
|
263
|
+
• Returns:
|
230
264
|
- `str`: Solutions formatted in LaTeX.
|
231
265
|
|
232
|
-
|
266
|
+
• Example:
|
233
267
|
|
234
|
-
from rgwfuncs import
|
235
|
-
solutions1 =
|
268
|
+
from rgwfuncs import solve_homogeneous_polynomial_expression
|
269
|
+
solutions1 = solve_homogeneous_polynomial_expression("a*x**2 + b*x + c", "x", {"a": 3, "b": 7, "c": 5})
|
236
270
|
print(solutions1) # Output: "\left[-7/6 - sqrt(11)*I/6, -7/6 + sqrt(11)*I/6\right]"
|
237
271
|
|
238
|
-
solutions2 =
|
272
|
+
solutions2 = solve_homogeneous_polynomial_expression("x**2 - 4", "x")
|
239
273
|
print(solutions2) # Output: "\left[-2, 2\right]"
|
240
274
|
|
241
|
-
Here, we solve both a quadratic equation with complex solutions and a simpler polynomial equation.
|
242
|
-
|
243
275
|
--------------------------------------------------------------------------------
|
244
276
|
|
245
|
-
###
|
277
|
+
### 5. `get_prime_factors_latex`
|
246
278
|
|
247
279
|
Computes prime factors of a number and presents them in LaTeX format.
|
248
280
|
|
249
|
-
|
281
|
+
• Parameters:
|
250
282
|
- `n` (int): The integer to factorize.
|
251
283
|
|
252
|
-
|
284
|
+
• Returns:
|
253
285
|
- `str`: Prime factorization in LaTeX.
|
254
286
|
|
255
|
-
|
287
|
+
• Example:
|
256
288
|
|
257
289
|
from rgwfuncs import get_prime_factors_latex
|
258
290
|
factors1 = get_prime_factors_latex(100)
|
@@ -266,64 +298,60 @@ Computes prime factors of a number and presents them in LaTeX format.
|
|
266
298
|
|
267
299
|
--------------------------------------------------------------------------------
|
268
300
|
|
269
|
-
###
|
301
|
+
### 6. `compute_matrix_expression`
|
270
302
|
|
271
|
-
Computes the results of 1D or 2D matrix operations and formats them as LaTeX strings.
|
303
|
+
Computes the results of expressions containing 1D or 2D matrix operations and formats them as LaTeX strings.
|
272
304
|
|
273
|
-
|
305
|
+
• Parameters:
|
274
306
|
- `expression` (str): A string representing a sequence of matrix operations involving either 1D or 2D lists. Supported operations include addition (`+`), subtraction (`-`), multiplication (`*`), and division (`/`).
|
275
307
|
|
276
|
-
|
308
|
+
• Returns:
|
277
309
|
- `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
|
278
310
|
|
279
|
-
|
311
|
+
• Example:
|
280
312
|
|
281
|
-
from rgwfuncs import
|
313
|
+
from rgwfuncs import compute_matrix_expression
|
282
314
|
|
283
315
|
# Example with addition of 2D matrices
|
284
|
-
result =
|
316
|
+
result = compute_matrix_expression("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
|
285
317
|
print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
|
286
318
|
|
287
319
|
# Example of mixed operations with 1D matrices treated as 2D
|
288
|
-
result =
|
320
|
+
result = compute_matrix_expression("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
|
289
321
|
print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
|
290
322
|
|
291
323
|
# Example with dimension mismatch
|
292
|
-
result =
|
324
|
+
result = compute_matrix_expression("[[4, 3, 51]] + [[1, 1]]")
|
293
325
|
print(result) # Output: Operations between matrices must involve matrices of the same dimension
|
294
326
|
|
295
|
-
This function performs elementwise operations on both 1D and 2D matrices represented as Python lists and formats the result as a LaTeX string. It handles operations sequentially from left to right and gracefully handles dimension mismatches by returning a meaningful message. It utilizes Python's `ast.literal_eval` for safe and robust parsing.
|
296
|
-
|
297
327
|
--------------------------------------------------------------------------------
|
298
328
|
|
299
|
-
###
|
329
|
+
### 7. `compute_ordered_series_expression`
|
300
330
|
|
301
|
-
Computes the result of operations on ordered series expressed as 1D lists
|
331
|
+
Computes the result of expressions containing operations on ordered series expressed as 1D lists. The syntax of the expression supports the discrete difference operator via the `ddd()` method.
|
302
332
|
|
303
|
-
|
304
|
-
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd` for discrete differences.
|
333
|
+
• Parameters:
|
334
|
+
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd()` for discrete differences.
|
305
335
|
|
306
|
-
|
336
|
+
• Returns:
|
307
337
|
- `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
|
308
338
|
|
309
|
-
|
339
|
+
• Example:
|
310
340
|
|
311
|
-
from rgwfuncs import
|
341
|
+
from rgwfuncs import compute_ordered_series_expression
|
312
342
|
|
313
343
|
# Example with addition and discrete differences
|
314
|
-
result =
|
344
|
+
result = compute_ordered_series_expression("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
|
315
345
|
print(result) # Output: [4, 3, 51] + [1, 1]
|
316
346
|
|
317
347
|
# Example with elementwise subtraction
|
318
|
-
result =
|
348
|
+
result = compute_ordered_series_expression("[10, 15, 21] - [5, 5, 5]")
|
319
349
|
print(result) # Output: [5, 10, 16]
|
320
350
|
|
321
351
|
# Example with length mismatch
|
322
|
-
result =
|
352
|
+
result = compute_ordered_series_expression("[4, 3, 51] + [1, 1]")
|
323
353
|
print(result) # Output: Operations between ordered series must involve series of equal length
|
324
354
|
|
325
|
-
This function first applies the discrete difference operator to any series where applicable, then evaluates arithmetic operations between series. It returns a string representation of the result or an error message if the series lengths do not match. The function is robust, directly parsing and evaluating given series expressions with safety checks in place.
|
326
|
-
|
327
355
|
--------------------------------------------------------------------------------
|
328
356
|
|
329
357
|
## String Based Functions
|
@@ -7,15 +7,37 @@ import math
|
|
7
7
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
8
8
|
|
9
9
|
from src.rgwfuncs.algebra_lib import (
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
python_polynomial_expression_to_latex,
|
11
|
+
compute_constant_expression,
|
12
|
+
simplify_polynomial_expression,
|
13
|
+
solve_homogeneous_polynomial_expression,
|
14
|
+
compute_matrix_expression,
|
15
|
+
compute_ordered_series_expression,
|
15
16
|
get_prime_factors_latex)
|
16
17
|
|
18
|
+
def test_python_polynomial_expression_to_latex():
|
19
|
+
test_cases = [
|
20
|
+
# Without substitutions
|
21
|
+
("x**2 + y**2", None, r"x^{2} + y^{2}"),
|
22
|
+
("3*a + 4*b", None, r"3 a + 4 b"),
|
23
|
+
|
24
|
+
# With substitutions
|
25
|
+
("x**2 + y**2", {"x": 3, "y": 4}, r"25"), # Shows substitution but not simplification
|
26
|
+
("x**2 + y**2", {"x": 3}, r"y^{2} + 9"),
|
27
|
+
("a*b + b", {"b": 2}, r"2 a + 2"),
|
28
|
+
("sin(x+z**2) + cos(y)", {"x": 55}, r"cos y + sin \left(z^{2} + 55\right)"),
|
29
|
+
("sin(x) + cos(y)", {"x": 0}, r"cos y")
|
30
|
+
]
|
31
|
+
|
32
|
+
for expression, subs, expected_output in test_cases:
|
33
|
+
output = python_polynomial_expression_to_latex(expression, subs)
|
34
|
+
assert output == expected_output, (
|
35
|
+
f"Test failed for expression: {expression} with substitutions: {subs}. "
|
36
|
+
f"Expected {expected_output}, got {output}"
|
37
|
+
)
|
17
38
|
|
18
|
-
|
39
|
+
|
40
|
+
def test_compute_constant_expression():
|
19
41
|
test_cases = [
|
20
42
|
("2 + 2", 4.0),
|
21
43
|
("5 - 3", 2.0),
|
@@ -27,10 +49,11 @@ def test_compute_algebraic_expression():
|
|
27
49
|
]
|
28
50
|
|
29
51
|
for input_data, expected_output in test_cases:
|
30
|
-
result =
|
52
|
+
result = compute_constant_expression(input_data)
|
31
53
|
assert math.isclose(result, expected_output, rel_tol=1e-9), f"Failed for {input_data}, got {result}"
|
32
54
|
|
33
|
-
|
55
|
+
|
56
|
+
def test_simplify_polynomial_expression():
|
34
57
|
test_cases = [
|
35
58
|
# Without substitutions
|
36
59
|
(("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)", None), r"\frac{1}{110 x^{22} y^{3}}"),
|
@@ -42,27 +65,21 @@ def test_simplify_algebraic_expression():
|
|
42
65
|
]
|
43
66
|
|
44
67
|
for (expression, subs), expected_output in test_cases:
|
45
|
-
output =
|
46
|
-
assert output == expected_output, (
|
47
|
-
f"Test failed for expression: {expression} with substitutions: {subs}. "
|
48
|
-
f"Expected {expected_output}, got {output}"
|
49
|
-
)
|
68
|
+
output = simplify_polynomial_expression(expression, subs)
|
69
|
+
assert output == expected_output, (f"Test failed for expression: {expression} with substitutions: {subs}. Expected {expected_output}, got {output}")
|
50
70
|
|
51
71
|
|
52
|
-
def
|
72
|
+
def test_solve_homogeneous_polynomial_expression():
|
53
73
|
test_cases = [
|
54
74
|
# Test case with substitutions
|
55
|
-
(
|
56
|
-
("a*x**2 + b*x + c", "x", {"a": 3, "b": 7, "c": 5}),
|
57
|
-
r"\left[-7/6 - sqrt(11)*I/6, -7/6 + sqrt(11)*I/6\right]"
|
58
|
-
),
|
75
|
+
(("a*x**2 + b*x + c", "x", {"a": 3, "b": 7, "c": 5}), r"\left[-7/6 - sqrt(11)*I/6, -7/6 + sqrt(11)*I/6\right]"),
|
59
76
|
]
|
60
77
|
|
61
78
|
for (expression, variable, subs), expected_output in test_cases:
|
62
|
-
assert
|
79
|
+
assert solve_homogeneous_polynomial_expression(expression, variable, subs) == expected_output
|
63
80
|
|
64
81
|
|
65
|
-
def
|
82
|
+
def test_compute_matrix_expression():
|
66
83
|
test_cases = [
|
67
84
|
("[[2, 6, 9],[1, 3, 5]] + [[1, 2, 3],[4, 5, 6]]", r"\begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}"),
|
68
85
|
("[[10, 10, 10],[2, 4, 6]] - [[5, 3, 2],[1, 2, 1]]", r"\begin{bmatrix}5 & 7 & 8\\1 & 2 & 5\end{bmatrix}"),
|
@@ -80,13 +97,13 @@ def test_compute_matrix_operation():
|
|
80
97
|
]
|
81
98
|
|
82
99
|
for input_data, expected_output in test_cases:
|
83
|
-
result =
|
100
|
+
result = compute_matrix_expression(input_data)
|
84
101
|
assert result == expected_output, f"Failed for {input_data}, got {result}"
|
85
102
|
|
86
103
|
# Example test function
|
87
104
|
|
88
105
|
|
89
|
-
def
|
106
|
+
def test_compute_ordered_series_expression():
|
90
107
|
test_cases = [
|
91
108
|
("[2, 6, 9] + [1, 2, 3]", "[3, 8, 12]"),
|
92
109
|
("[10, 15, 21] - [5, 5, 5]", "[5, 10, 16]"),
|
@@ -104,7 +121,7 @@ def test_compute_ordered_series_operations():
|
|
104
121
|
]
|
105
122
|
|
106
123
|
for input_data, expected_output in test_cases:
|
107
|
-
result =
|
124
|
+
result = compute_ordered_series_expression(input_data)
|
108
125
|
assert result == expected_output, f"Failed for {input_data}, got {result}"
|
109
126
|
|
110
127
|
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|