rgwfuncs 0.0.30__tar.gz → 0.0.31__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.2
2
2
  Name: rgwfuncs
3
- Version: 0.0.30
3
+ Version: 0.0.31
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,7 +154,88 @@ 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. `python_polynomial_expression_to_latex`
157
+ ### 1. `compute_constant_expression`
158
+
159
+ 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.
160
+
161
+ • Parameters:
162
+ - `expression` (str): The constant expression to compute. This should be a string consisting of arithmetic operations and Python's math module functions.
163
+
164
+ • Returns:
165
+ - `float`: The computed numerical result.
166
+
167
+ • Example:
168
+
169
+ from rgwfuncs import compute_constant_expression
170
+ result1 = compute_constant_expression("2 + 2")
171
+ print(result1) # Output: 4.0
172
+
173
+ result2 = compute_constant_expression("10 % 3")
174
+ print(result2) # Output: 1.0
175
+
176
+ result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
177
+ print(result3) # Output: 84852.8137423857
178
+
179
+ --------------------------------------------------------------------------------
180
+
181
+ ### 2. `compute_constant_expression_involving_matrices`
182
+
183
+ Computes the result of a constant expression involving matrices and returns it as a LaTeX string.
184
+
185
+ • Parameters:
186
+ - `expression` (str): The constant expression involving matrices. Example format includes operations such as "+", "-", "*", "/".
187
+
188
+ • Returns:
189
+ - `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
190
+
191
+ • Example:
192
+
193
+ from rgwfuncs import compute_constant_expression_involving_matrices
194
+
195
+ # Example with addition of 2D matrices
196
+ result = compute_constant_expression_involving_matrices("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
197
+ print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
198
+
199
+ # Example of mixed operations with 1D matrices treated as 2D
200
+ result = compute_constant_expression_involving_matrices("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
201
+ print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
202
+
203
+ # Example with dimension mismatch
204
+ result = compute_constant_expression_involving_matrices("[[4, 3, 51]] + [[1, 1]]")
205
+ print(result) # Output: Operations between matrices must involve matrices of the same dimension
206
+
207
+ --------------------------------------------------------------------------------
208
+
209
+ ### 3. `compute_constant_expression_involving_ordered_series`
210
+
211
+ Computes the result of a constant expression involving ordered series, and returns it as a Latex string.
212
+
213
+
214
+ • Parameters:
215
+ - `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `dd()` for discrete differences.
216
+
217
+ • Returns:
218
+ - `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
219
+
220
+ • Example:
221
+
222
+ from rgwfuncs import compute_constant_expression_involving_ordered_series
223
+
224
+ # Example with addition and discrete differences
225
+ result = compute_constant_expression_involving_ordered_series("dd([2, 6, 9, 60]) + dd([78, 79, 80])")
226
+ print(result) # Output: [4, 3, 51] + [1, 1]
227
+
228
+ # Example with elementwise subtraction
229
+ result = compute_constant_expression_involving_ordered_series("[10, 15, 21] - [5, 5, 5]")
230
+ print(result) # Output: [5, 10, 16]
231
+
232
+ # Example with length mismatch
233
+ result = compute_constant_expression_involving_ordered_series("[4, 3, 51] + [1, 1]")
234
+ print(result) # Output: Operations between ordered series must involve series of equal length
235
+
236
+ --------------------------------------------------------------------------------
237
+
238
+ ### 4. `python_polynomial_expression_to_latex`
158
239
 
159
240
  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
241
 
@@ -194,31 +275,7 @@ Converts a polynomial expression written in Python syntax to a LaTeX formatted s
194
275
 
195
276
  --------------------------------------------------------------------------------
196
277
 
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:
205
- - `float`: The computed numerical result.
206
-
207
- • Example:
208
-
209
- from rgwfuncs import compute_constant_expression
210
- result1 = compute_constant_expression("2 + 2")
211
- print(result1) # Output: 4.0
212
-
213
- result2 = compute_constant_expression("10 % 3")
214
- print(result2) # Output: 1.0
215
-
216
- result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
217
- print(result3) # Output: 84852.8137423857
218
-
219
- --------------------------------------------------------------------------------
220
-
221
- ### 3. `simplify_polynomial_expression`
278
+ ### 5. `simplify_polynomial_expression`
222
279
 
223
280
  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.
224
281
 
@@ -251,7 +308,7 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
251
308
 
252
309
  --------------------------------------------------------------------------------
253
310
 
254
- ### 4. `solve_homogeneous_polynomial_expression`
311
+ ### 6. `solve_homogeneous_polynomial_expression`
255
312
 
256
313
  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.
257
314
 
@@ -274,7 +331,7 @@ Solves a homogeneous polynomial expression for a specified variable and returns
274
331
 
275
332
  --------------------------------------------------------------------------------
276
333
 
277
- ### 5. `get_prime_factors_latex`
334
+ ### 7. `get_prime_factors_latex`
278
335
 
279
336
  Computes prime factors of a number and presents them in LaTeX format.
280
337
 
@@ -298,62 +355,6 @@ Computes prime factors of a number and presents them in LaTeX format.
298
355
 
299
356
  --------------------------------------------------------------------------------
300
357
 
301
- ### 6. `compute_matrix_expression`
302
-
303
- Computes the results of expressions containing 1D or 2D matrix operations and formats them as LaTeX strings.
304
-
305
- • Parameters:
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 (`/`).
307
-
308
- • Returns:
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.
310
-
311
- • Example:
312
-
313
- from rgwfuncs import compute_matrix_expression
314
-
315
- # Example with addition of 2D matrices
316
- result = compute_matrix_expression("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
317
- print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
318
-
319
- # Example of mixed operations with 1D matrices treated as 2D
320
- result = compute_matrix_expression("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
321
- print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
322
-
323
- # Example with dimension mismatch
324
- result = compute_matrix_expression("[[4, 3, 51]] + [[1, 1]]")
325
- print(result) # Output: Operations between matrices must involve matrices of the same dimension
326
-
327
- --------------------------------------------------------------------------------
328
-
329
- ### 7. `compute_ordered_series_expression`
330
-
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.
332
-
333
- • Parameters:
334
- - `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd()` for discrete differences.
335
-
336
- • Returns:
337
- - `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
338
-
339
- • Example:
340
-
341
- from rgwfuncs import compute_ordered_series_expression
342
-
343
- # Example with addition and discrete differences
344
- result = compute_ordered_series_expression("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
345
- print(result) # Output: [4, 3, 51] + [1, 1]
346
-
347
- # Example with elementwise subtraction
348
- result = compute_ordered_series_expression("[10, 15, 21] - [5, 5, 5]")
349
- print(result) # Output: [5, 10, 16]
350
-
351
- # Example with length mismatch
352
- result = compute_ordered_series_expression("[4, 3, 51] + [1, 1]")
353
- print(result) # Output: Operations between ordered series must involve series of equal length
354
-
355
- --------------------------------------------------------------------------------
356
-
357
358
  ## String Based Functions
358
359
 
359
360
  ### 1. send_telegram_message
@@ -128,7 +128,88 @@ 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. `python_polynomial_expression_to_latex`
131
+ ### 1. `compute_constant_expression`
132
+
133
+ 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.
134
+
135
+ • Parameters:
136
+ - `expression` (str): The constant expression to compute. This should be a string consisting of arithmetic operations and Python's math module functions.
137
+
138
+ • Returns:
139
+ - `float`: The computed numerical result.
140
+
141
+ • Example:
142
+
143
+ from rgwfuncs import compute_constant_expression
144
+ result1 = compute_constant_expression("2 + 2")
145
+ print(result1) # Output: 4.0
146
+
147
+ result2 = compute_constant_expression("10 % 3")
148
+ print(result2) # Output: 1.0
149
+
150
+ result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
151
+ print(result3) # Output: 84852.8137423857
152
+
153
+ --------------------------------------------------------------------------------
154
+
155
+ ### 2. `compute_constant_expression_involving_matrices`
156
+
157
+ Computes the result of a constant expression involving matrices and returns it as a LaTeX string.
158
+
159
+ • Parameters:
160
+ - `expression` (str): The constant expression involving matrices. Example format includes operations such as "+", "-", "*", "/".
161
+
162
+ • Returns:
163
+ - `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
164
+
165
+ • Example:
166
+
167
+ from rgwfuncs import compute_constant_expression_involving_matrices
168
+
169
+ # Example with addition of 2D matrices
170
+ result = compute_constant_expression_involving_matrices("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
171
+ print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
172
+
173
+ # Example of mixed operations with 1D matrices treated as 2D
174
+ result = compute_constant_expression_involving_matrices("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
175
+ print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
176
+
177
+ # Example with dimension mismatch
178
+ result = compute_constant_expression_involving_matrices("[[4, 3, 51]] + [[1, 1]]")
179
+ print(result) # Output: Operations between matrices must involve matrices of the same dimension
180
+
181
+ --------------------------------------------------------------------------------
182
+
183
+ ### 3. `compute_constant_expression_involving_ordered_series`
184
+
185
+ Computes the result of a constant expression involving ordered series, and returns it as a Latex string.
186
+
187
+
188
+ • Parameters:
189
+ - `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `dd()` for discrete differences.
190
+
191
+ • Returns:
192
+ - `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
193
+
194
+ • Example:
195
+
196
+ from rgwfuncs import compute_constant_expression_involving_ordered_series
197
+
198
+ # Example with addition and discrete differences
199
+ result = compute_constant_expression_involving_ordered_series("dd([2, 6, 9, 60]) + dd([78, 79, 80])")
200
+ print(result) # Output: [4, 3, 51] + [1, 1]
201
+
202
+ # Example with elementwise subtraction
203
+ result = compute_constant_expression_involving_ordered_series("[10, 15, 21] - [5, 5, 5]")
204
+ print(result) # Output: [5, 10, 16]
205
+
206
+ # Example with length mismatch
207
+ result = compute_constant_expression_involving_ordered_series("[4, 3, 51] + [1, 1]")
208
+ print(result) # Output: Operations between ordered series must involve series of equal length
209
+
210
+ --------------------------------------------------------------------------------
211
+
212
+ ### 4. `python_polynomial_expression_to_latex`
132
213
 
133
214
  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
215
 
@@ -168,31 +249,7 @@ Converts a polynomial expression written in Python syntax to a LaTeX formatted s
168
249
 
169
250
  --------------------------------------------------------------------------------
170
251
 
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:
179
- - `float`: The computed numerical result.
180
-
181
- • Example:
182
-
183
- from rgwfuncs import compute_constant_expression
184
- result1 = compute_constant_expression("2 + 2")
185
- print(result1) # Output: 4.0
186
-
187
- result2 = compute_constant_expression("10 % 3")
188
- print(result2) # Output: 1.0
189
-
190
- result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
191
- print(result3) # Output: 84852.8137423857
192
-
193
- --------------------------------------------------------------------------------
194
-
195
- ### 3. `simplify_polynomial_expression`
252
+ ### 5. `simplify_polynomial_expression`
196
253
 
197
254
  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.
198
255
 
@@ -225,7 +282,7 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
225
282
 
226
283
  --------------------------------------------------------------------------------
227
284
 
228
- ### 4. `solve_homogeneous_polynomial_expression`
285
+ ### 6. `solve_homogeneous_polynomial_expression`
229
286
 
230
287
  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.
231
288
 
@@ -248,7 +305,7 @@ Solves a homogeneous polynomial expression for a specified variable and returns
248
305
 
249
306
  --------------------------------------------------------------------------------
250
307
 
251
- ### 5. `get_prime_factors_latex`
308
+ ### 7. `get_prime_factors_latex`
252
309
 
253
310
  Computes prime factors of a number and presents them in LaTeX format.
254
311
 
@@ -272,62 +329,6 @@ Computes prime factors of a number and presents them in LaTeX format.
272
329
 
273
330
  --------------------------------------------------------------------------------
274
331
 
275
- ### 6. `compute_matrix_expression`
276
-
277
- Computes the results of expressions containing 1D or 2D matrix operations and formats them as LaTeX strings.
278
-
279
- • Parameters:
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 (`/`).
281
-
282
- • Returns:
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.
284
-
285
- • Example:
286
-
287
- from rgwfuncs import compute_matrix_expression
288
-
289
- # Example with addition of 2D matrices
290
- result = compute_matrix_expression("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
291
- print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
292
-
293
- # Example of mixed operations with 1D matrices treated as 2D
294
- result = compute_matrix_expression("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
295
- print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
296
-
297
- # Example with dimension mismatch
298
- result = compute_matrix_expression("[[4, 3, 51]] + [[1, 1]]")
299
- print(result) # Output: Operations between matrices must involve matrices of the same dimension
300
-
301
- --------------------------------------------------------------------------------
302
-
303
- ### 7. `compute_ordered_series_expression`
304
-
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.
306
-
307
- • Parameters:
308
- - `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd()` for discrete differences.
309
-
310
- • Returns:
311
- - `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
312
-
313
- • Example:
314
-
315
- from rgwfuncs import compute_ordered_series_expression
316
-
317
- # Example with addition and discrete differences
318
- result = compute_ordered_series_expression("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
319
- print(result) # Output: [4, 3, 51] + [1, 1]
320
-
321
- # Example with elementwise subtraction
322
- result = compute_ordered_series_expression("[10, 15, 21] - [5, 5, 5]")
323
- print(result) # Output: [5, 10, 16]
324
-
325
- # Example with length mismatch
326
- result = compute_ordered_series_expression("[4, 3, 51] + [1, 1]")
327
- print(result) # Output: Operations between ordered series must involve series of equal length
328
-
329
- --------------------------------------------------------------------------------
330
-
331
332
  ## String Based Functions
332
333
 
333
334
  ### 1. send_telegram_message
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "rgwfuncs"
7
- version = "0.0.30"
7
+ version = "0.0.31"
8
8
  authors = [
9
9
  { name = "Ryan Gerard Wilson", email = "ryangerardwilson@gmail.com" },
10
10
  ]
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = rgwfuncs
3
- version = 0.0.30
3
+ version = 0.0.31
4
4
  author = Ryan Gerard Wilson
5
5
  author_email = ryangerardwilson@gmail.com
6
6
  description = A functional programming paradigm for mathematical modelling and data science
@@ -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 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
4
+ from .algebra_lib import compute_constant_expression, compute_constant_expression_involving_matrices, compute_constant_expression_involving_ordered_series, 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
@@ -40,6 +40,198 @@ def compute_constant_expression(expression: str) -> float:
40
40
  raise ValueError(f"Error computing expression: {e}")
41
41
 
42
42
 
43
+ def compute_constant_expression_involving_matrices(expression: str) -> str:
44
+ """
45
+ Computes the result of a constant expression involving matrices and returns it as a LaTeX string.
46
+
47
+ Parameters:
48
+ expression (str): The constant expression involving matrices. Example format includes operations such as "+",
49
+ "-", "*", "/".
50
+
51
+ Returns:
52
+ str: The LaTeX-formatted string representation of the result or a message indicating an error in dimensions.
53
+ """
54
+
55
+ def elementwise_operation(matrix1: List[List[float]], matrix2: List[List[float]], operation: str) -> List[List[float]]:
56
+ if len(matrix1) != len(matrix2) or any(len(row1) != len(row2) for row1, row2 in zip(matrix1, matrix2)):
57
+ return "Operations between matrices must involve matrices of the same dimension"
58
+
59
+ if operation == '+':
60
+ return [[a + b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
61
+ elif operation == '-':
62
+ return [[a - b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
63
+ elif operation == '*':
64
+ return [[a * b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
65
+ elif operation == '/':
66
+ return [[a / b for a, b in zip(row1, row2) if b != 0] for row1, row2 in zip(matrix1, matrix2)]
67
+ else:
68
+ return f"Unsupported operation {operation}"
69
+
70
+ try:
71
+ # Use a stack-based method to properly parse matrices
72
+ elements = []
73
+ buffer = ''
74
+ bracket_level = 0
75
+ operators = set('+-*/')
76
+
77
+ for char in expression:
78
+ if char == '[':
79
+ if bracket_level == 0 and buffer.strip():
80
+ elements.append(buffer.strip())
81
+ buffer = ''
82
+ bracket_level += 1
83
+ elif char == ']':
84
+ bracket_level -= 1
85
+ if bracket_level == 0:
86
+ buffer += char
87
+ elements.append(buffer.strip())
88
+ buffer = ''
89
+ continue
90
+ if bracket_level == 0 and char in operators:
91
+ if buffer.strip():
92
+ elements.append(buffer.strip())
93
+ buffer = ''
94
+ elements.append(char)
95
+ else:
96
+ buffer += char
97
+
98
+ if buffer.strip():
99
+ elements.append(buffer.strip())
100
+
101
+ result = ast.literal_eval(elements[0])
102
+
103
+ if not any(isinstance(row, list) for row in result):
104
+ result = [result] # Convert 1D matrix to 2D
105
+
106
+ i = 1
107
+ while i < len(elements):
108
+ operation = elements[i]
109
+ matrix = ast.literal_eval(elements[i + 1])
110
+
111
+ if not any(isinstance(row, list) for row in matrix):
112
+ matrix = [matrix]
113
+
114
+ operation_result = elementwise_operation(result, matrix, operation)
115
+
116
+ # Check if the operation resulted in an error message
117
+ if isinstance(operation_result, str):
118
+ return operation_result
119
+
120
+ result = operation_result
121
+ i += 2
122
+
123
+ # Create a LaTeX-style matrix representation
124
+ matrix_entries = '\\\\'.join(' & '.join(str(x) for x in row) for row in result)
125
+ return r"\begin{bmatrix}" + f"{matrix_entries}" + r"\end{bmatrix}"
126
+
127
+ except Exception as e:
128
+ return f"Error computing matrix operation: {e}"
129
+
130
+
131
+ def compute_constant_expression_involving_ordered_series(expression: str) -> str:
132
+ """
133
+ Computes the result of a constant expression involving ordered series, and returns it as a Latex string.
134
+ Supports operations lile "+", "-", "*", "/", as well as "dd()" (the discrete difference operator).
135
+
136
+ The function first applies the discrete difference operator to any series where applicable, then evaluates
137
+ arithmetic operations between series.
138
+
139
+ Parameters:
140
+ expression (str): The series operation expression to compute. Includes operations "+", "-", "*", "/", and "dd()".
141
+
142
+ Returns:
143
+ str: The string representation of the resultant series after performing operations, or an error message
144
+ if the series lengths do not match.
145
+
146
+ Raises:
147
+ ValueError: If the expression cannot be evaluated.
148
+ """
149
+
150
+ def elementwise_operation(series1: List[float], series2: List[float], operation: str) -> List[float]:
151
+ if len(series1) != len(series2):
152
+ return "Operations between ordered series must involve series of equal length"
153
+
154
+ if operation == '+':
155
+ return [a + b for a, b in zip(series1, series2)]
156
+ elif operation == '-':
157
+ return [a - b for a, b in zip(series1, series2)]
158
+ elif operation == '*':
159
+ return [a * b for a, b in zip(series1, series2)]
160
+ elif operation == '/':
161
+ return [a / b for a, b in zip(series1, series2) if b != 0]
162
+ else:
163
+ return f"Unsupported operation {operation}"
164
+
165
+ def discrete_difference(series: list) -> list:
166
+ """Computes the discrete difference of a series."""
167
+ return [series[i + 1] - series[i] for i in range(len(series) - 1)]
168
+
169
+ try:
170
+ # First, apply the discrete difference operator where applicable
171
+ pattern = r'dd\((\[[^\]]*\])\)'
172
+ matches = re.findall(pattern, expression)
173
+
174
+ for match in matches:
175
+ if match.strip() == '[]':
176
+ result_series = [] # Handle the empty list case
177
+ else:
178
+ series = ast.literal_eval(match)
179
+ result_series = discrete_difference(series)
180
+ expression = expression.replace(f'dd({match})', str(result_series))
181
+
182
+ # Now parse and evaluate the full expression with basic operations
183
+ elements = []
184
+ buffer = ''
185
+ bracket_level = 0
186
+ operators = set('+-*/')
187
+
188
+ for char in expression:
189
+ if char == '[':
190
+ if bracket_level == 0 and buffer.strip():
191
+ elements.append(buffer.strip())
192
+ buffer = ''
193
+ bracket_level += 1
194
+ elif char == ']':
195
+ bracket_level -= 1
196
+ if bracket_level == 0:
197
+ buffer += char
198
+ elements.append(buffer.strip())
199
+ buffer = ''
200
+ continue
201
+ if bracket_level == 0 and char in operators:
202
+ if buffer.strip():
203
+ elements.append(buffer.strip())
204
+ buffer = ''
205
+ elements.append(char)
206
+ else:
207
+ buffer += char
208
+
209
+ if buffer.strip():
210
+ elements.append(buffer.strip())
211
+
212
+ result = ast.literal_eval(elements[0])
213
+
214
+ i = 1
215
+ while i < len(elements):
216
+ operation = elements[i]
217
+ series = ast.literal_eval(elements[i + 1])
218
+ operation_result = elementwise_operation(result, series, operation)
219
+
220
+ # Check if the operation resulted in an error message
221
+ if isinstance(operation_result, str):
222
+ return operation_result
223
+
224
+ result = operation_result
225
+ i += 2
226
+
227
+ return str(result)
228
+
229
+ except Exception as e:
230
+ return f"Error computing ordered series operation: {e}"
231
+
232
+
233
+
234
+
43
235
  def python_polynomial_expression_to_latex(
44
236
  expression: str,
45
237
  subs: Optional[Dict[str, float]] = None
@@ -100,7 +292,6 @@ def python_polynomial_expression_to_latex(
100
292
  return latex_result
101
293
 
102
294
 
103
-
104
295
  def simplify_polynomial_expression(
105
296
  expression: str,
106
297
  subs: Optional[Dict[str, float]] = None
@@ -109,7 +300,7 @@ def simplify_polynomial_expression(
109
300
  Simplifies an algebraic expression in polynomial form and returns it in LaTeX format.
110
301
 
111
302
  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
303
+ The result is returned as a LaTeX formatted string, suitable for academic or professional
113
304
  documentation.
114
305
 
115
306
  Parameters:
@@ -282,11 +473,11 @@ def solve_homogeneous_polynomial_expression(
282
473
  subs: Optional[Dict[str, float]] = None
283
474
  ) -> str:
284
475
  """
285
- Solves a homogeneous polynomial expression for a specified variable and returns solutions
476
+ Solves a homogeneous polynomial expression for a specified variable and returns solutions
286
477
  in LaTeX format.
287
478
 
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
479
+ Assumes that the expression is homoegeneous (i.e. equal to zero), and solves for a
480
+ designated variable. May optionally include substitutions for other variables in the
290
481
  equation. The solutions are provided as a LaTeX formatted string.
291
482
 
292
483
  Parameters:
@@ -329,196 +520,6 @@ def solve_homogeneous_polynomial_expression(
329
520
  raise ValueError(f"Error solving the expression: {e}")
330
521
 
331
522
 
332
- def compute_matrix_expression(expression: str) -> str:
333
- """
334
- Computes the result of a matrix-like operation on 1D or 2D list inputs and returns it as a LaTeX string.
335
-
336
- Evaluates an operation where lists are treated as matrices, performs operations on them sequentially, and
337
- returns the result formatted as a LaTeX-style string.
338
-
339
- Parameters:
340
- expression (str): The matrix operation expression to compute. Example format includes operations such as "+", "-", "*", "/".
341
-
342
- Returns:
343
- str: The LaTeX-formatted string representation of the result or a message indicating an error in dimensions.
344
- """
345
-
346
- def elementwise_operation(matrix1: List[List[float]], matrix2: List[List[float]], operation: str) -> List[List[float]]:
347
- if len(matrix1) != len(matrix2) or any(len(row1) != len(row2) for row1, row2 in zip(matrix1, matrix2)):
348
- return "Operations between matrices must involve matrices of the same dimension"
349
-
350
- if operation == '+':
351
- return [[a + b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
352
- elif operation == '-':
353
- return [[a - b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
354
- elif operation == '*':
355
- return [[a * b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
356
- elif operation == '/':
357
- return [[a / b for a, b in zip(row1, row2) if b != 0] for row1, row2 in zip(matrix1, matrix2)]
358
- else:
359
- return f"Unsupported operation {operation}"
360
-
361
- try:
362
- # Use a stack-based method to properly parse matrices
363
- elements = []
364
- buffer = ''
365
- bracket_level = 0
366
- operators = set('+-*/')
367
-
368
- for char in expression:
369
- if char == '[':
370
- if bracket_level == 0 and buffer.strip():
371
- elements.append(buffer.strip())
372
- buffer = ''
373
- bracket_level += 1
374
- elif char == ']':
375
- bracket_level -= 1
376
- if bracket_level == 0:
377
- buffer += char
378
- elements.append(buffer.strip())
379
- buffer = ''
380
- continue
381
- if bracket_level == 0 and char in operators:
382
- if buffer.strip():
383
- elements.append(buffer.strip())
384
- buffer = ''
385
- elements.append(char)
386
- else:
387
- buffer += char
388
-
389
- if buffer.strip():
390
- elements.append(buffer.strip())
391
-
392
- result = ast.literal_eval(elements[0])
393
-
394
- if not any(isinstance(row, list) for row in result):
395
- result = [result] # Convert 1D matrix to 2D
396
-
397
- i = 1
398
- while i < len(elements):
399
- operation = elements[i]
400
- matrix = ast.literal_eval(elements[i + 1])
401
-
402
- if not any(isinstance(row, list) for row in matrix):
403
- matrix = [matrix]
404
-
405
- operation_result = elementwise_operation(result, matrix, operation)
406
-
407
- # Check if the operation resulted in an error message
408
- if isinstance(operation_result, str):
409
- return operation_result
410
-
411
- result = operation_result
412
- i += 2
413
-
414
- # Create a LaTeX-style matrix representation
415
- matrix_entries = '\\\\'.join(' & '.join(str(x) for x in row) for row in result)
416
- return r"\begin{bmatrix}" + f"{matrix_entries}" + r"\end{bmatrix}"
417
-
418
- except Exception as e:
419
- return f"Error computing matrix operation: {e}"
420
-
421
-
422
- def compute_ordered_series_expression(expression: str) -> str:
423
- """
424
- Computes the result of operations on ordered series expressed as 1D lists, including discrete difference (ddd),
425
- and returns it as a string.
426
-
427
- The function first applies the discrete difference operator to any series where applicable, then evaluates
428
- arithmetic operations between series.
429
-
430
- Parameters:
431
- expression (str): The series operation expression to compute. Includes operations "+", "-", "*", "/", and "ddd".
432
-
433
- Returns:
434
- str: The string representation of the resultant series after performing operations, or an error message
435
- if the series lengths do not match.
436
-
437
- Raises:
438
- ValueError: If the expression cannot be evaluated.
439
- """
440
-
441
- def elementwise_operation(series1: List[float], series2: List[float], operation: str) -> List[float]:
442
- if len(series1) != len(series2):
443
- return "Operations between ordered series must involve series of equal length"
444
-
445
- if operation == '+':
446
- return [a + b for a, b in zip(series1, series2)]
447
- elif operation == '-':
448
- return [a - b for a, b in zip(series1, series2)]
449
- elif operation == '*':
450
- return [a * b for a, b in zip(series1, series2)]
451
- elif operation == '/':
452
- return [a / b for a, b in zip(series1, series2) if b != 0]
453
- else:
454
- return f"Unsupported operation {operation}"
455
-
456
- def discrete_difference(series: list) -> list:
457
- """Computes the discrete difference of a series."""
458
- return [series[i + 1] - series[i] for i in range(len(series) - 1)]
459
-
460
- try:
461
- # First, apply the discrete difference operator where applicable
462
- pattern = r'ddd\((\[[^\]]*\])\)'
463
- matches = re.findall(pattern, expression)
464
-
465
- for match in matches:
466
- if match.strip() == '[]':
467
- result_series = [] # Handle the empty list case
468
- else:
469
- series = ast.literal_eval(match)
470
- result_series = discrete_difference(series)
471
- expression = expression.replace(f'ddd({match})', str(result_series))
472
-
473
- # Now parse and evaluate the full expression with basic operations
474
- elements = []
475
- buffer = ''
476
- bracket_level = 0
477
- operators = set('+-*/')
478
-
479
- for char in expression:
480
- if char == '[':
481
- if bracket_level == 0 and buffer.strip():
482
- elements.append(buffer.strip())
483
- buffer = ''
484
- bracket_level += 1
485
- elif char == ']':
486
- bracket_level -= 1
487
- if bracket_level == 0:
488
- buffer += char
489
- elements.append(buffer.strip())
490
- buffer = ''
491
- continue
492
- if bracket_level == 0 and char in operators:
493
- if buffer.strip():
494
- elements.append(buffer.strip())
495
- buffer = ''
496
- elements.append(char)
497
- else:
498
- buffer += char
499
-
500
- if buffer.strip():
501
- elements.append(buffer.strip())
502
-
503
- result = ast.literal_eval(elements[0])
504
-
505
- i = 1
506
- while i < len(elements):
507
- operation = elements[i]
508
- series = ast.literal_eval(elements[i + 1])
509
- operation_result = elementwise_operation(result, series, operation)
510
-
511
- # Check if the operation resulted in an error message
512
- if isinstance(operation_result, str):
513
- return operation_result
514
-
515
- result = operation_result
516
- i += 2
517
-
518
- return str(result)
519
-
520
- except Exception as e:
521
- return f"Error computing ordered series operation: {e}"
522
523
 
523
524
 
524
525
  def get_prime_factors_latex(n: int) -> str:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rgwfuncs
3
- Version: 0.0.30
3
+ Version: 0.0.31
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,7 +154,88 @@ 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. `python_polynomial_expression_to_latex`
157
+ ### 1. `compute_constant_expression`
158
+
159
+ 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.
160
+
161
+ • Parameters:
162
+ - `expression` (str): The constant expression to compute. This should be a string consisting of arithmetic operations and Python's math module functions.
163
+
164
+ • Returns:
165
+ - `float`: The computed numerical result.
166
+
167
+ • Example:
168
+
169
+ from rgwfuncs import compute_constant_expression
170
+ result1 = compute_constant_expression("2 + 2")
171
+ print(result1) # Output: 4.0
172
+
173
+ result2 = compute_constant_expression("10 % 3")
174
+ print(result2) # Output: 1.0
175
+
176
+ result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
177
+ print(result3) # Output: 84852.8137423857
178
+
179
+ --------------------------------------------------------------------------------
180
+
181
+ ### 2. `compute_constant_expression_involving_matrices`
182
+
183
+ Computes the result of a constant expression involving matrices and returns it as a LaTeX string.
184
+
185
+ • Parameters:
186
+ - `expression` (str): The constant expression involving matrices. Example format includes operations such as "+", "-", "*", "/".
187
+
188
+ • Returns:
189
+ - `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
190
+
191
+ • Example:
192
+
193
+ from rgwfuncs import compute_constant_expression_involving_matrices
194
+
195
+ # Example with addition of 2D matrices
196
+ result = compute_constant_expression_involving_matrices("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
197
+ print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
198
+
199
+ # Example of mixed operations with 1D matrices treated as 2D
200
+ result = compute_constant_expression_involving_matrices("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
201
+ print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
202
+
203
+ # Example with dimension mismatch
204
+ result = compute_constant_expression_involving_matrices("[[4, 3, 51]] + [[1, 1]]")
205
+ print(result) # Output: Operations between matrices must involve matrices of the same dimension
206
+
207
+ --------------------------------------------------------------------------------
208
+
209
+ ### 3. `compute_constant_expression_involving_ordered_series`
210
+
211
+ Computes the result of a constant expression involving ordered series, and returns it as a Latex string.
212
+
213
+
214
+ • Parameters:
215
+ - `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `dd()` for discrete differences.
216
+
217
+ • Returns:
218
+ - `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
219
+
220
+ • Example:
221
+
222
+ from rgwfuncs import compute_constant_expression_involving_ordered_series
223
+
224
+ # Example with addition and discrete differences
225
+ result = compute_constant_expression_involving_ordered_series("dd([2, 6, 9, 60]) + dd([78, 79, 80])")
226
+ print(result) # Output: [4, 3, 51] + [1, 1]
227
+
228
+ # Example with elementwise subtraction
229
+ result = compute_constant_expression_involving_ordered_series("[10, 15, 21] - [5, 5, 5]")
230
+ print(result) # Output: [5, 10, 16]
231
+
232
+ # Example with length mismatch
233
+ result = compute_constant_expression_involving_ordered_series("[4, 3, 51] + [1, 1]")
234
+ print(result) # Output: Operations between ordered series must involve series of equal length
235
+
236
+ --------------------------------------------------------------------------------
237
+
238
+ ### 4. `python_polynomial_expression_to_latex`
158
239
 
159
240
  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
241
 
@@ -194,31 +275,7 @@ Converts a polynomial expression written in Python syntax to a LaTeX formatted s
194
275
 
195
276
  --------------------------------------------------------------------------------
196
277
 
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:
205
- - `float`: The computed numerical result.
206
-
207
- • Example:
208
-
209
- from rgwfuncs import compute_constant_expression
210
- result1 = compute_constant_expression("2 + 2")
211
- print(result1) # Output: 4.0
212
-
213
- result2 = compute_constant_expression("10 % 3")
214
- print(result2) # Output: 1.0
215
-
216
- result3 = compute_constant_expression("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000")
217
- print(result3) # Output: 84852.8137423857
218
-
219
- --------------------------------------------------------------------------------
220
-
221
- ### 3. `simplify_polynomial_expression`
278
+ ### 5. `simplify_polynomial_expression`
222
279
 
223
280
  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.
224
281
 
@@ -251,7 +308,7 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
251
308
 
252
309
  --------------------------------------------------------------------------------
253
310
 
254
- ### 4. `solve_homogeneous_polynomial_expression`
311
+ ### 6. `solve_homogeneous_polynomial_expression`
255
312
 
256
313
  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.
257
314
 
@@ -274,7 +331,7 @@ Solves a homogeneous polynomial expression for a specified variable and returns
274
331
 
275
332
  --------------------------------------------------------------------------------
276
333
 
277
- ### 5. `get_prime_factors_latex`
334
+ ### 7. `get_prime_factors_latex`
278
335
 
279
336
  Computes prime factors of a number and presents them in LaTeX format.
280
337
 
@@ -298,62 +355,6 @@ Computes prime factors of a number and presents them in LaTeX format.
298
355
 
299
356
  --------------------------------------------------------------------------------
300
357
 
301
- ### 6. `compute_matrix_expression`
302
-
303
- Computes the results of expressions containing 1D or 2D matrix operations and formats them as LaTeX strings.
304
-
305
- • Parameters:
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 (`/`).
307
-
308
- • Returns:
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.
310
-
311
- • Example:
312
-
313
- from rgwfuncs import compute_matrix_expression
314
-
315
- # Example with addition of 2D matrices
316
- result = compute_matrix_expression("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
317
- print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
318
-
319
- # Example of mixed operations with 1D matrices treated as 2D
320
- result = compute_matrix_expression("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
321
- print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
322
-
323
- # Example with dimension mismatch
324
- result = compute_matrix_expression("[[4, 3, 51]] + [[1, 1]]")
325
- print(result) # Output: Operations between matrices must involve matrices of the same dimension
326
-
327
- --------------------------------------------------------------------------------
328
-
329
- ### 7. `compute_ordered_series_expression`
330
-
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.
332
-
333
- • Parameters:
334
- - `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd()` for discrete differences.
335
-
336
- • Returns:
337
- - `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
338
-
339
- • Example:
340
-
341
- from rgwfuncs import compute_ordered_series_expression
342
-
343
- # Example with addition and discrete differences
344
- result = compute_ordered_series_expression("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
345
- print(result) # Output: [4, 3, 51] + [1, 1]
346
-
347
- # Example with elementwise subtraction
348
- result = compute_ordered_series_expression("[10, 15, 21] - [5, 5, 5]")
349
- print(result) # Output: [5, 10, 16]
350
-
351
- # Example with length mismatch
352
- result = compute_ordered_series_expression("[4, 3, 51] + [1, 1]")
353
- print(result) # Output: Operations between ordered series must involve series of equal length
354
-
355
- --------------------------------------------------------------------------------
356
-
357
358
  ## String Based Functions
358
359
 
359
360
  ### 1. send_telegram_message
@@ -7,35 +7,14 @@ 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
- python_polynomial_expression_to_latex,
11
10
  compute_constant_expression,
11
+ compute_constant_expression_involving_matrices,
12
+ compute_constant_expression_involving_ordered_series,
13
+ python_polynomial_expression_to_latex,
12
14
  simplify_polynomial_expression,
13
15
  solve_homogeneous_polynomial_expression,
14
- compute_matrix_expression,
15
- compute_ordered_series_expression,
16
16
  get_prime_factors_latex)
17
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
- )
38
-
39
18
 
40
19
  def test_compute_constant_expression():
41
20
  test_cases = [
@@ -52,34 +31,7 @@ def test_compute_constant_expression():
52
31
  result = compute_constant_expression(input_data)
53
32
  assert math.isclose(result, expected_output, rel_tol=1e-9), f"Failed for {input_data}, got {result}"
54
33
 
55
-
56
- def test_simplify_polynomial_expression():
57
- test_cases = [
58
- # Without substitutions
59
- (("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)", None), r"\frac{1}{110 x^{22} y^{3}}"),
60
-
61
- # With substitutions
62
- (("x**2 + y**2", {"x": 3, "y": 4}), "25"),
63
- (("a*b + b", {"b": 2}), r"2 a + 2"), # Assumes no simplification of `a*b`
64
- (("(x**2 + y**2 + z**2)", {"x": 1, "y": 0, "z": 0}), "1")
65
- ]
66
-
67
- for (expression, subs), expected_output in test_cases:
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}")
70
-
71
-
72
- def test_solve_homogeneous_polynomial_expression():
73
- test_cases = [
74
- # Test case with substitutions
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]"),
76
- ]
77
-
78
- for (expression, variable, subs), expected_output in test_cases:
79
- assert solve_homogeneous_polynomial_expression(expression, variable, subs) == expected_output
80
-
81
-
82
- def test_compute_matrix_expression():
34
+ def test_compute_constant_expression_involving_matrices():
83
35
  test_cases = [
84
36
  ("[[2, 6, 9],[1, 3, 5]] + [[1, 2, 3],[4, 5, 6]]", r"\begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}"),
85
37
  ("[[10, 10, 10],[2, 4, 6]] - [[5, 3, 2],[1, 2, 1]]", r"\begin{bmatrix}5 & 7 & 8\\1 & 2 & 5\end{bmatrix}"),
@@ -97,34 +49,84 @@ def test_compute_matrix_expression():
97
49
  ]
98
50
 
99
51
  for input_data, expected_output in test_cases:
100
- result = compute_matrix_expression(input_data)
52
+ result = compute_constant_expression_involving_matrices(input_data)
101
53
  assert result == expected_output, f"Failed for {input_data}, got {result}"
102
54
 
103
55
  # Example test function
104
56
 
105
57
 
106
- def test_compute_ordered_series_expression():
58
+ def test_compute_constant_expression_involving_ordered_series():
107
59
  test_cases = [
108
60
  ("[2, 6, 9] + [1, 2, 3]", "[3, 8, 12]"),
109
61
  ("[10, 15, 21] - [5, 5, 5]", "[5, 10, 16]"),
110
62
  ("[2, 4, 6] * [1, 2, 3]", "[2, 8, 18]"),
111
63
  ("[8, 16, 32] / [2, 2, 8]", "[4.0, 8.0, 4.0]"),
112
- ("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])", "Operations between ordered series must involve series of equal length"),
113
- ("ddd([1, 3, 6, 10]) - ddd([0, 1, 1, 2])", "[1, 3, 3]"),
64
+ ("dd([2, 6, 9, 60]) + dd([78, 79, 80])", "Operations between ordered series must involve series of equal length"),
65
+ ("dd([1, 3, 6, 10]) - dd([0, 1, 1, 2])", "[1, 3, 3]"),
114
66
 
115
67
  # Edge cases
116
- ("ddd([1])", "[]"), # Single-element list, becomes empty
117
- ("ddd([])", "[]"), # Empty list case
68
+ ("dd([1])", "[]"), # Single-element list, becomes empty
69
+ ("dd([])", "[]"), # Empty list case
118
70
  ("[5]", "[5]"), # Single-element list, unchanged
119
71
  ("[]", "[]"), # Empty list
120
72
  ("[4, 3, 51] + [1, 1]", "Operations between ordered series must involve series of equal length"), # Test unequal lengths
121
73
  ]
122
74
 
123
75
  for input_data, expected_output in test_cases:
124
- result = compute_ordered_series_expression(input_data)
76
+ result = compute_constant_expression_involving_ordered_series(input_data)
125
77
  assert result == expected_output, f"Failed for {input_data}, got {result}"
126
78
 
127
79
 
80
+ def test_python_polynomial_expression_to_latex():
81
+ test_cases = [
82
+ # Without substitutions
83
+ ("x**2 + y**2", None, r"x^{2} + y^{2}"),
84
+ ("3*a + 4*b", None, r"3 a + 4 b"),
85
+ ("3*x**3 / (7*y*m**(n+2) + 103)", None, r"\frac{3 x^{3}}{7 m^{n + 2} y + 103}"),
86
+
87
+ # With substitutions
88
+ ("x**2 + y**2", {"x": 3, "y": 4}, r"25"),
89
+ ("x**2 + y**2", {"x": 3}, r"y^{2} + 9"),
90
+ ("a*b + b", {"b": 2}, r"2 a + 2"),
91
+ ("sin(x+z**2) + cos(y)", {"x": 55}, r"cos y + sin \left(z^{2} + 55\right)"),
92
+ ("sin(x) + cos(y)", {"x": 0}, r"cos y")
93
+ ]
94
+
95
+ for expression, subs, expected_output in test_cases:
96
+ output = python_polynomial_expression_to_latex(expression, subs)
97
+ assert output == expected_output, (
98
+ f"Test failed for expression: {expression} with substitutions: {subs}. "
99
+ f"Expected {expected_output}, got {output}"
100
+ )
101
+
102
+
103
+
104
+ def test_simplify_polynomial_expression():
105
+ test_cases = [
106
+ # Without substitutions
107
+ (("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)", None), r"\frac{1}{110 x^{22} y^{3}}"),
108
+
109
+ # With substitutions
110
+ (("x**2 + y**2", {"x": 3, "y": 4}), "25"),
111
+ (("a*b + b", {"b": 2}), r"2 a + 2"), # Assumes no simplification of `a*b`
112
+ (("(x**2 + y**2 + z**2)", {"x": 1, "y": 0, "z": 0}), "1")
113
+ ]
114
+
115
+ for (expression, subs), expected_output in test_cases:
116
+ output = simplify_polynomial_expression(expression, subs)
117
+ assert output == expected_output, (f"Test failed for expression: {expression} with substitutions: {subs}. Expected {expected_output}, got {output}")
118
+
119
+
120
+ def test_solve_homogeneous_polynomial_expression():
121
+ test_cases = [
122
+ # Test case with substitutions
123
+ (("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]"),
124
+ ]
125
+
126
+ for (expression, variable, subs), expected_output in test_cases:
127
+ assert solve_homogeneous_polynomial_expression(expression, variable, subs) == expected_output
128
+
129
+
128
130
  def test_get_prime_factors_latex():
129
131
  test_cases = [
130
132
  (100, "2^{2} \\cdot 5^{2}"),
File without changes