rgwfuncs 0.0.26__tar.gz → 0.0.27__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.26/src/rgwfuncs.egg-info → rgwfuncs-0.0.27}/PKG-INFO +61 -1
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/README.md +60 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/pyproject.toml +1 -1
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/setup.cfg +1 -1
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs/__init__.py +1 -1
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs/algebra_lib.py +193 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27/src/rgwfuncs.egg-info}/PKG-INFO +61 -1
- rgwfuncs-0.0.27/tests/test_algebra_lib.py +115 -0
- rgwfuncs-0.0.26/tests/test_algebra_lib.py +0 -59
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/LICENSE +0 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs/df_lib.py +0 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs/docs_lib.py +0 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs/str_lib.py +0 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs.egg-info/SOURCES.txt +0 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs.egg-info/dependency_links.txt +0 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs.egg-info/entry_points.txt +0 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/src/rgwfuncs.egg-info/requires.txt +0 -0
- {rgwfuncs-0.0.26 → rgwfuncs-0.0.27}/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.27
|
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
|
@@ -252,6 +252,66 @@ Computes prime factors of a number and presents them in LaTeX format.
|
|
252
252
|
|
253
253
|
--------------------------------------------------------------------------------
|
254
254
|
|
255
|
+
### 5. `compute_matrix_operation`
|
256
|
+
|
257
|
+
Computes the results of 1D or 2D matrix operations and formats them as LaTeX strings.
|
258
|
+
|
259
|
+
- **Parameters:**
|
260
|
+
- `expression` (str): A string representing a sequence of matrix operations involving either 1D or 2D lists. Supported operations include addition (`+`), subtraction (`-`), multiplication (`*`), and division (`/`).
|
261
|
+
|
262
|
+
- **Returns:**
|
263
|
+
- `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
|
264
|
+
|
265
|
+
- **Example:**
|
266
|
+
|
267
|
+
from rgwfuncs import compute_matrix_operation
|
268
|
+
|
269
|
+
# Example with addition of 2D matrices
|
270
|
+
result = compute_matrix_operation("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
|
271
|
+
print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
|
272
|
+
|
273
|
+
# Example of mixed operations with 1D matrices treated as 2D
|
274
|
+
result = compute_matrix_operation("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
|
275
|
+
print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
|
276
|
+
|
277
|
+
# Example with dimension mismatch
|
278
|
+
result = compute_matrix_operation("[[4, 3, 51]] + [[1, 1]]")
|
279
|
+
print(result) # Output: Operations between matrices must involve matrices of the same dimension
|
280
|
+
|
281
|
+
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.
|
282
|
+
|
283
|
+
--------------------------------------------------------------------------------
|
284
|
+
|
285
|
+
### 6. `compute_ordered_series_operations`
|
286
|
+
|
287
|
+
Computes the result of operations on ordered series expressed as 1D lists, including the discrete difference operator `ddd`.
|
288
|
+
|
289
|
+
- **Parameters:**
|
290
|
+
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd` for discrete differences.
|
291
|
+
|
292
|
+
- **Returns:**
|
293
|
+
- `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
|
294
|
+
|
295
|
+
- **Example:**
|
296
|
+
|
297
|
+
from rgwfuncs import compute_ordered_series_operations
|
298
|
+
|
299
|
+
# Example with addition and discrete differences
|
300
|
+
result = compute_ordered_series_operations("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
|
301
|
+
print(result) # Output: [4, 3, 51] + [1, 1]
|
302
|
+
|
303
|
+
# Example with elementwise subtraction
|
304
|
+
result = compute_ordered_series_operations("[10, 15, 21] - [5, 5, 5]")
|
305
|
+
print(result) # Output: [5, 10, 16]
|
306
|
+
|
307
|
+
# Example with length mismatch
|
308
|
+
result = compute_ordered_series_operations("[4, 3, 51] + [1, 1]")
|
309
|
+
print(result) # Output: Operations between ordered series must involve series of equal length
|
310
|
+
|
311
|
+
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.
|
312
|
+
|
313
|
+
--------------------------------------------------------------------------------
|
314
|
+
|
255
315
|
## String Based Functions
|
256
316
|
|
257
317
|
### 1. send_telegram_message
|
@@ -226,6 +226,66 @@ Computes prime factors of a number and presents them in LaTeX format.
|
|
226
226
|
|
227
227
|
--------------------------------------------------------------------------------
|
228
228
|
|
229
|
+
### 5. `compute_matrix_operation`
|
230
|
+
|
231
|
+
Computes the results of 1D or 2D matrix operations and formats them as LaTeX strings.
|
232
|
+
|
233
|
+
- **Parameters:**
|
234
|
+
- `expression` (str): A string representing a sequence of matrix operations involving either 1D or 2D lists. Supported operations include addition (`+`), subtraction (`-`), multiplication (`*`), and division (`/`).
|
235
|
+
|
236
|
+
- **Returns:**
|
237
|
+
- `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
|
238
|
+
|
239
|
+
- **Example:**
|
240
|
+
|
241
|
+
from rgwfuncs import compute_matrix_operation
|
242
|
+
|
243
|
+
# Example with addition of 2D matrices
|
244
|
+
result = compute_matrix_operation("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
|
245
|
+
print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
|
246
|
+
|
247
|
+
# Example of mixed operations with 1D matrices treated as 2D
|
248
|
+
result = compute_matrix_operation("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
|
249
|
+
print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
|
250
|
+
|
251
|
+
# Example with dimension mismatch
|
252
|
+
result = compute_matrix_operation("[[4, 3, 51]] + [[1, 1]]")
|
253
|
+
print(result) # Output: Operations between matrices must involve matrices of the same dimension
|
254
|
+
|
255
|
+
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.
|
256
|
+
|
257
|
+
--------------------------------------------------------------------------------
|
258
|
+
|
259
|
+
### 6. `compute_ordered_series_operations`
|
260
|
+
|
261
|
+
Computes the result of operations on ordered series expressed as 1D lists, including the discrete difference operator `ddd`.
|
262
|
+
|
263
|
+
- **Parameters:**
|
264
|
+
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd` for discrete differences.
|
265
|
+
|
266
|
+
- **Returns:**
|
267
|
+
- `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
|
268
|
+
|
269
|
+
- **Example:**
|
270
|
+
|
271
|
+
from rgwfuncs import compute_ordered_series_operations
|
272
|
+
|
273
|
+
# Example with addition and discrete differences
|
274
|
+
result = compute_ordered_series_operations("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
|
275
|
+
print(result) # Output: [4, 3, 51] + [1, 1]
|
276
|
+
|
277
|
+
# Example with elementwise subtraction
|
278
|
+
result = compute_ordered_series_operations("[10, 15, 21] - [5, 5, 5]")
|
279
|
+
print(result) # Output: [5, 10, 16]
|
280
|
+
|
281
|
+
# Example with length mismatch
|
282
|
+
result = compute_ordered_series_operations("[4, 3, 51] + [1, 1]")
|
283
|
+
print(result) # Output: Operations between ordered series must involve series of equal length
|
284
|
+
|
285
|
+
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.
|
286
|
+
|
287
|
+
--------------------------------------------------------------------------------
|
288
|
+
|
229
289
|
## String Based Functions
|
230
290
|
|
231
291
|
### 1. send_telegram_message
|
@@ -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_algebraic_expression, get_prime_factors_latex, simplify_algebraic_expression, solve_algebraic_expression
|
4
|
+
from .algebra_lib import compute_algebraic_expression, compute_matrix_operation, compute_ordered_series_operation, get_prime_factors_latex, simplify_algebraic_expression, solve_algebraic_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
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import re
|
2
2
|
import math
|
3
3
|
import ast
|
4
|
+
# import numpy as np
|
4
5
|
from sympy import symbols, latex, simplify, solve, diff, Expr
|
5
6
|
from sympy.parsing.sympy_parser import parse_expr
|
6
7
|
from typing import Tuple, List, Dict, Optional
|
@@ -242,6 +243,198 @@ def solve_algebraic_expression(
|
|
242
243
|
raise ValueError(f"Error solving the expression: {e}")
|
243
244
|
|
244
245
|
|
246
|
+
def compute_matrix_operation(expression: str) -> str:
|
247
|
+
"""
|
248
|
+
Computes the result of a matrix-like operation on 1D or 2D list inputs and returns it as a LaTeX string.
|
249
|
+
|
250
|
+
Evaluates an operation where lists are treated as matrices, performs operations on them sequentially, and
|
251
|
+
returns the result formatted as a LaTeX-style string.
|
252
|
+
|
253
|
+
Parameters:
|
254
|
+
expression (str): The matrix operation expression to compute. Example format includes operations such as "+", "-", "*", "/".
|
255
|
+
|
256
|
+
Returns:
|
257
|
+
str: The LaTeX-formatted string representation of the result or a message indicating an error in dimensions.
|
258
|
+
"""
|
259
|
+
|
260
|
+
def elementwise_operation(matrix1: List[List[float]], matrix2: List[List[float]], operation: str) -> List[List[float]]:
|
261
|
+
if len(matrix1) != len(matrix2) or any(len(row1) != len(row2) for row1, row2 in zip(matrix1, matrix2)):
|
262
|
+
return "Operations between matrices must involve matrices of the same dimension"
|
263
|
+
|
264
|
+
if operation == '+':
|
265
|
+
return [[a + b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
|
266
|
+
elif operation == '-':
|
267
|
+
return [[a - b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
|
268
|
+
elif operation == '*':
|
269
|
+
return [[a * b for a, b in zip(row1, row2)] for row1, row2 in zip(matrix1, matrix2)]
|
270
|
+
elif operation == '/':
|
271
|
+
return [[a / b for a, b in zip(row1, row2) if b != 0] for row1, row2 in zip(matrix1, matrix2)]
|
272
|
+
else:
|
273
|
+
return f"Unsupported operation {operation}"
|
274
|
+
|
275
|
+
try:
|
276
|
+
# Use a stack-based method to properly parse matrices
|
277
|
+
elements = []
|
278
|
+
buffer = ''
|
279
|
+
bracket_level = 0
|
280
|
+
operators = set('+-*/')
|
281
|
+
|
282
|
+
for char in expression:
|
283
|
+
if char == '[':
|
284
|
+
if bracket_level == 0 and buffer.strip():
|
285
|
+
elements.append(buffer.strip())
|
286
|
+
buffer = ''
|
287
|
+
bracket_level += 1
|
288
|
+
elif char == ']':
|
289
|
+
bracket_level -= 1
|
290
|
+
if bracket_level == 0:
|
291
|
+
buffer += char
|
292
|
+
elements.append(buffer.strip())
|
293
|
+
buffer = ''
|
294
|
+
continue
|
295
|
+
if bracket_level == 0 and char in operators:
|
296
|
+
if buffer.strip():
|
297
|
+
elements.append(buffer.strip())
|
298
|
+
buffer = ''
|
299
|
+
elements.append(char)
|
300
|
+
else:
|
301
|
+
buffer += char
|
302
|
+
|
303
|
+
if buffer.strip():
|
304
|
+
elements.append(buffer.strip())
|
305
|
+
|
306
|
+
result = ast.literal_eval(elements[0])
|
307
|
+
|
308
|
+
if not any(isinstance(row, list) for row in result):
|
309
|
+
result = [result] # Convert 1D matrix to 2D
|
310
|
+
|
311
|
+
i = 1
|
312
|
+
while i < len(elements):
|
313
|
+
operation = elements[i]
|
314
|
+
matrix = ast.literal_eval(elements[i + 1])
|
315
|
+
|
316
|
+
if not any(isinstance(row, list) for row in matrix):
|
317
|
+
matrix = [matrix]
|
318
|
+
|
319
|
+
operation_result = elementwise_operation(result, matrix, operation)
|
320
|
+
|
321
|
+
# Check if the operation resulted in an error message
|
322
|
+
if isinstance(operation_result, str):
|
323
|
+
return operation_result
|
324
|
+
|
325
|
+
result = operation_result
|
326
|
+
i += 2
|
327
|
+
|
328
|
+
# Create a LaTeX-style matrix representation
|
329
|
+
matrix_entries = '\\\\'.join(' & '.join(str(x) for x in row) for row in result)
|
330
|
+
return r"\begin{bmatrix}" + f"{matrix_entries}" + r"\end{bmatrix}"
|
331
|
+
|
332
|
+
except Exception as e:
|
333
|
+
return f"Error computing matrix operation: {e}"
|
334
|
+
|
335
|
+
|
336
|
+
def compute_ordered_series_operation(expression: str) -> str:
|
337
|
+
"""
|
338
|
+
Computes the result of operations on ordered series expressed as 1D lists, including discrete difference (ddd),
|
339
|
+
and returns it as a string.
|
340
|
+
|
341
|
+
The function first applies the discrete difference operator to any series where applicable, then evaluates
|
342
|
+
arithmetic operations between series.
|
343
|
+
|
344
|
+
Parameters:
|
345
|
+
expression (str): The series operation expression to compute. Includes operations "+", "-", "*", "/", and "ddd".
|
346
|
+
|
347
|
+
Returns:
|
348
|
+
str: The string representation of the resultant series after performing operations, or an error message
|
349
|
+
if the series lengths do not match.
|
350
|
+
|
351
|
+
Raises:
|
352
|
+
ValueError: If the expression cannot be evaluated.
|
353
|
+
"""
|
354
|
+
|
355
|
+
def elementwise_operation(series1: List[float], series2: List[float], operation: str) -> List[float]:
|
356
|
+
if len(series1) != len(series2):
|
357
|
+
return "Operations between ordered series must involve series of equal length"
|
358
|
+
|
359
|
+
if operation == '+':
|
360
|
+
return [a + b for a, b in zip(series1, series2)]
|
361
|
+
elif operation == '-':
|
362
|
+
return [a - b for a, b in zip(series1, series2)]
|
363
|
+
elif operation == '*':
|
364
|
+
return [a * b for a, b in zip(series1, series2)]
|
365
|
+
elif operation == '/':
|
366
|
+
return [a / b for a, b in zip(series1, series2) if b != 0]
|
367
|
+
else:
|
368
|
+
return f"Unsupported operation {operation}"
|
369
|
+
|
370
|
+
def discrete_difference(series: list) -> list:
|
371
|
+
"""Computes the discrete difference of a series."""
|
372
|
+
return [series[i + 1] - series[i] for i in range(len(series) - 1)]
|
373
|
+
|
374
|
+
try:
|
375
|
+
# First, apply the discrete difference operator where applicable
|
376
|
+
pattern = r'ddd\((\[[^\]]*\])\)'
|
377
|
+
matches = re.findall(pattern, expression)
|
378
|
+
|
379
|
+
for match in matches:
|
380
|
+
if match.strip() == '[]':
|
381
|
+
result_series = [] # Handle the empty list case
|
382
|
+
else:
|
383
|
+
series = ast.literal_eval(match)
|
384
|
+
result_series = discrete_difference(series)
|
385
|
+
expression = expression.replace(f'ddd({match})', str(result_series))
|
386
|
+
|
387
|
+
# Now parse and evaluate the full expression with basic operations
|
388
|
+
elements = []
|
389
|
+
buffer = ''
|
390
|
+
bracket_level = 0
|
391
|
+
operators = set('+-*/')
|
392
|
+
|
393
|
+
for char in expression:
|
394
|
+
if char == '[':
|
395
|
+
if bracket_level == 0 and buffer.strip():
|
396
|
+
elements.append(buffer.strip())
|
397
|
+
buffer = ''
|
398
|
+
bracket_level += 1
|
399
|
+
elif char == ']':
|
400
|
+
bracket_level -= 1
|
401
|
+
if bracket_level == 0:
|
402
|
+
buffer += char
|
403
|
+
elements.append(buffer.strip())
|
404
|
+
buffer = ''
|
405
|
+
continue
|
406
|
+
if bracket_level == 0 and char in operators:
|
407
|
+
if buffer.strip():
|
408
|
+
elements.append(buffer.strip())
|
409
|
+
buffer = ''
|
410
|
+
elements.append(char)
|
411
|
+
else:
|
412
|
+
buffer += char
|
413
|
+
|
414
|
+
if buffer.strip():
|
415
|
+
elements.append(buffer.strip())
|
416
|
+
|
417
|
+
result = ast.literal_eval(elements[0])
|
418
|
+
|
419
|
+
i = 1
|
420
|
+
while i < len(elements):
|
421
|
+
operation = elements[i]
|
422
|
+
series = ast.literal_eval(elements[i + 1])
|
423
|
+
operation_result = elementwise_operation(result, series, operation)
|
424
|
+
|
425
|
+
# Check if the operation resulted in an error message
|
426
|
+
if isinstance(operation_result, str):
|
427
|
+
return operation_result
|
428
|
+
|
429
|
+
result = operation_result
|
430
|
+
i += 2
|
431
|
+
|
432
|
+
return str(result)
|
433
|
+
|
434
|
+
except Exception as e:
|
435
|
+
return f"Error computing ordered series operation: {e}"
|
436
|
+
|
437
|
+
|
245
438
|
def get_prime_factors_latex(n: int) -> str:
|
246
439
|
"""
|
247
440
|
Computes the prime factors of a number and returns the factorization as a LaTeX string.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: rgwfuncs
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.27
|
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
|
@@ -252,6 +252,66 @@ Computes prime factors of a number and presents them in LaTeX format.
|
|
252
252
|
|
253
253
|
--------------------------------------------------------------------------------
|
254
254
|
|
255
|
+
### 5. `compute_matrix_operation`
|
256
|
+
|
257
|
+
Computes the results of 1D or 2D matrix operations and formats them as LaTeX strings.
|
258
|
+
|
259
|
+
- **Parameters:**
|
260
|
+
- `expression` (str): A string representing a sequence of matrix operations involving either 1D or 2D lists. Supported operations include addition (`+`), subtraction (`-`), multiplication (`*`), and division (`/`).
|
261
|
+
|
262
|
+
- **Returns:**
|
263
|
+
- `str`: The LaTeX-formatted string representation of the computed matrix, or an error message if the operations cannot be performed due to dimensional mismatches.
|
264
|
+
|
265
|
+
- **Example:**
|
266
|
+
|
267
|
+
from rgwfuncs import compute_matrix_operation
|
268
|
+
|
269
|
+
# Example with addition of 2D matrices
|
270
|
+
result = compute_matrix_operation("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]]")
|
271
|
+
print(result) # Output: \begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}
|
272
|
+
|
273
|
+
# Example of mixed operations with 1D matrices treated as 2D
|
274
|
+
result = compute_matrix_operation("[3, 6, 9] + [1, 2, 3] - [2, 2, 2]")
|
275
|
+
print(result) # Output: \begin{bmatrix}2 & 6 & 10\end{bmatrix}
|
276
|
+
|
277
|
+
# Example with dimension mismatch
|
278
|
+
result = compute_matrix_operation("[[4, 3, 51]] + [[1, 1]]")
|
279
|
+
print(result) # Output: Operations between matrices must involve matrices of the same dimension
|
280
|
+
|
281
|
+
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.
|
282
|
+
|
283
|
+
--------------------------------------------------------------------------------
|
284
|
+
|
285
|
+
### 6. `compute_ordered_series_operations`
|
286
|
+
|
287
|
+
Computes the result of operations on ordered series expressed as 1D lists, including the discrete difference operator `ddd`.
|
288
|
+
|
289
|
+
- **Parameters:**
|
290
|
+
- `expression` (str): A series operation expression. Supports operations such as "+", "-", "*", "/", and `ddd` for discrete differences.
|
291
|
+
|
292
|
+
- **Returns:**
|
293
|
+
- `str`: The string representation of the resultant series after performing operations, or an error message if series lengths do not match.
|
294
|
+
|
295
|
+
- **Example:**
|
296
|
+
|
297
|
+
from rgwfuncs import compute_ordered_series_operations
|
298
|
+
|
299
|
+
# Example with addition and discrete differences
|
300
|
+
result = compute_ordered_series_operations("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])")
|
301
|
+
print(result) # Output: [4, 3, 51] + [1, 1]
|
302
|
+
|
303
|
+
# Example with elementwise subtraction
|
304
|
+
result = compute_ordered_series_operations("[10, 15, 21] - [5, 5, 5]")
|
305
|
+
print(result) # Output: [5, 10, 16]
|
306
|
+
|
307
|
+
# Example with length mismatch
|
308
|
+
result = compute_ordered_series_operations("[4, 3, 51] + [1, 1]")
|
309
|
+
print(result) # Output: Operations between ordered series must involve series of equal length
|
310
|
+
|
311
|
+
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.
|
312
|
+
|
313
|
+
--------------------------------------------------------------------------------
|
314
|
+
|
255
315
|
## String Based Functions
|
256
316
|
|
257
317
|
### 1. send_telegram_message
|
@@ -0,0 +1,115 @@
|
|
1
|
+
import sys
|
2
|
+
import os
|
3
|
+
import math
|
4
|
+
|
5
|
+
# flake8: noqa: E402
|
6
|
+
# Allow the following import statement to be AFTER sys.path modifications
|
7
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
8
|
+
|
9
|
+
from src.rgwfuncs.algebra_lib import (
|
10
|
+
compute_algebraic_expression,
|
11
|
+
simplify_algebraic_expression,
|
12
|
+
solve_algebraic_expression,
|
13
|
+
compute_matrix_operation,
|
14
|
+
compute_ordered_series_operation,
|
15
|
+
get_prime_factors_latex)
|
16
|
+
|
17
|
+
|
18
|
+
def test_compute_algebraic_expression():
|
19
|
+
test_cases = [
|
20
|
+
("2 + 2", 4.0),
|
21
|
+
("5 - 3", 2.0),
|
22
|
+
("3 * 3", 9.0),
|
23
|
+
("8 / 2", 4.0),
|
24
|
+
("10 % 3", 1.0),
|
25
|
+
("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000", 84852.8137423857),
|
26
|
+
# ("np.diff([2,6,9,60])", r"\left[\begin{matrix}4\\3\\51\end{matrix}\right]"),
|
27
|
+
]
|
28
|
+
|
29
|
+
for input_data, expected_output in test_cases:
|
30
|
+
result = compute_algebraic_expression(input_data)
|
31
|
+
assert math.isclose(result, expected_output, rel_tol=1e-9), f"Failed for {input_data}, got {result}"
|
32
|
+
|
33
|
+
|
34
|
+
def test_simplify_algebraic_expression():
|
35
|
+
test_cases = [
|
36
|
+
("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)", r"\frac{1}{110 x^{22} y^{3}}"),
|
37
|
+
]
|
38
|
+
|
39
|
+
for input_data, expected_output in test_cases:
|
40
|
+
assert simplify_algebraic_expression(input_data) == expected_output
|
41
|
+
|
42
|
+
|
43
|
+
def test_solve_algebraic_expression():
|
44
|
+
test_cases = [
|
45
|
+
# Test case with substitutions
|
46
|
+
(
|
47
|
+
("a*x**2 + b*x + c", "x", {"a": 3, "b": 7, "c": 5}),
|
48
|
+
r"\left[-7/6 - sqrt(11)*I/6, -7/6 + sqrt(11)*I/6\right]"
|
49
|
+
),
|
50
|
+
]
|
51
|
+
|
52
|
+
for (expression, variable, subs), expected_output in test_cases:
|
53
|
+
assert solve_algebraic_expression(expression, variable, subs) == expected_output
|
54
|
+
|
55
|
+
|
56
|
+
def test_compute_matrix_operation():
|
57
|
+
test_cases = [
|
58
|
+
("[[2, 6, 9],[1, 3, 5]] + [[1, 2, 3],[4, 5, 6]]", r"\begin{bmatrix}3 & 8 & 12\\5 & 8 & 11\end{bmatrix}"),
|
59
|
+
("[[10, 10, 10],[2, 4, 6]] - [[5, 3, 2],[1, 2, 1]]", r"\begin{bmatrix}5 & 7 & 8\\1 & 2 & 5\end{bmatrix}"),
|
60
|
+
("[[2, 4],[6, 8]] * [[1, 0.5],[2, 0.25]]", r"\begin{bmatrix}2 & 2.0\\12 & 2.0\end{bmatrix}"),
|
61
|
+
("[[8, 16],[32, 64]] / [[2, 2],[8, 16]]", r"\begin{bmatrix}4.0 & 8.0\\4.0 & 4.0\end{bmatrix}"),
|
62
|
+
|
63
|
+
("[[2, 6, 9], [1, 3, 5]] + [[1, 2, 3], [4, 5, 6]] - [[1, 1, 1], [1, 1, 1]]", r"\begin{bmatrix}2 & 7 & 11\\4 & 7 & 10\end{bmatrix}"),
|
64
|
+
("[2, 6, 9] + [1, 2, 3] - [1, 1, 1]", r"\begin{bmatrix}2 & 7 & 11\end{bmatrix}"),
|
65
|
+
("[[1, 2], [3, 4]] + [[2, 3], [4, 5]] + [[1, 1], [1, 1]]", r"\begin{bmatrix}4 & 6\\8 & 10\end{bmatrix}"),
|
66
|
+
("[3, 6, 9] - [1, 2, 3] + [5, 5, 5]", r"\begin{bmatrix}7 & 9 & 11\end{bmatrix}"),
|
67
|
+
|
68
|
+
|
69
|
+
("[3, 6, 9] - [1, 2, 3, 4]", r"Operations between matrices must involve matrices of the same dimension"),
|
70
|
+
|
71
|
+
# Edge cases
|
72
|
+
("[]", r"\begin{bmatrix}\end{bmatrix}"), # Empty list
|
73
|
+
("[5]", r"\begin{bmatrix}5\end{bmatrix}"), # Single-element list
|
74
|
+
]
|
75
|
+
|
76
|
+
for input_data, expected_output in test_cases:
|
77
|
+
result = compute_matrix_operation(input_data)
|
78
|
+
assert result == expected_output, f"Failed for {input_data}, got {result}"
|
79
|
+
|
80
|
+
# Example test function
|
81
|
+
|
82
|
+
|
83
|
+
def test_compute_ordered_series_operations():
|
84
|
+
test_cases = [
|
85
|
+
("[2, 6, 9] + [1, 2, 3]", "[3, 8, 12]"),
|
86
|
+
("[10, 15, 21] - [5, 5, 5]", "[5, 10, 16]"),
|
87
|
+
("[2, 4, 6] * [1, 2, 3]", "[2, 8, 18]"),
|
88
|
+
("[8, 16, 32] / [2, 2, 8]", "[4.0, 8.0, 4.0]"),
|
89
|
+
("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])", "Operations between ordered series must involve series of equal length"),
|
90
|
+
("ddd([1, 3, 6, 10]) - ddd([0, 1, 1, 2])", "[1, 3, 3]"),
|
91
|
+
# Edge cases
|
92
|
+
("ddd([1])", "[]"), # Single-element list, becomes empty
|
93
|
+
("ddd([])", "[]"), # Empty list case
|
94
|
+
("[5]", "[5]"), # Single-element list, unchanged
|
95
|
+
("[]", "[]"), # Empty list
|
96
|
+
("[4, 3, 51] + [1, 1]", "Operations between ordered series must involve series of equal length"), # Test unequal lengths
|
97
|
+
]
|
98
|
+
|
99
|
+
for input_data, expected_output in test_cases:
|
100
|
+
result = compute_ordered_series_operation(input_data)
|
101
|
+
assert result == expected_output, f"Failed for {input_data}, got {result}"
|
102
|
+
|
103
|
+
|
104
|
+
def test_get_prime_factors_latex():
|
105
|
+
test_cases = [
|
106
|
+
(100, "2^{2} \\cdot 5^{2}"),
|
107
|
+
(60, "2^{2} \\cdot 3 \\cdot 5"),
|
108
|
+
(45, "3^{2} \\cdot 5"),
|
109
|
+
(1, ""), # Handle case with 1, which has no prime factors
|
110
|
+
(17, "17") # Prime number itself
|
111
|
+
]
|
112
|
+
|
113
|
+
for n, expected_output in test_cases:
|
114
|
+
result = get_prime_factors_latex(n)
|
115
|
+
assert result == expected_output, f"Failed for {n}, got {result}"
|
@@ -1,59 +0,0 @@
|
|
1
|
-
from src.rgwfuncs.algebra_lib import compute_algebraic_expression, simplify_algebraic_expression, solve_algebraic_expression, get_prime_factors_latex
|
2
|
-
import sys
|
3
|
-
import os
|
4
|
-
import math
|
5
|
-
|
6
|
-
# flake8: noqa: E402
|
7
|
-
# Allow the following import statement to be AFTER sys.path modifications
|
8
|
-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
9
|
-
|
10
|
-
|
11
|
-
def test_compute_algebraic_expression():
|
12
|
-
test_cases = [
|
13
|
-
("2 + 2", 4.0),
|
14
|
-
("5 - 3", 2.0),
|
15
|
-
("3 * 3", 9.0),
|
16
|
-
("8 / 2", 4.0),
|
17
|
-
("10 % 3", 1.0),
|
18
|
-
("math.gcd(36, 60) * math.sin(math.radians(45)) * 10000", 84852.8137423857),
|
19
|
-
]
|
20
|
-
|
21
|
-
for input_data, expected_output in test_cases:
|
22
|
-
result = compute_algebraic_expression(input_data)
|
23
|
-
assert math.isclose(result, expected_output, rel_tol=1e-9), f"Failed for {input_data}, got {result}"
|
24
|
-
|
25
|
-
|
26
|
-
def test_simplify_algebraic_expression():
|
27
|
-
test_cases = [
|
28
|
-
("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)", r"\frac{1}{110 x^{22} y^{3}}"),
|
29
|
-
]
|
30
|
-
|
31
|
-
for input_data, expected_output in test_cases:
|
32
|
-
assert simplify_algebraic_expression(input_data) == expected_output
|
33
|
-
|
34
|
-
|
35
|
-
def test_solve_algebraic_expression():
|
36
|
-
test_cases = [
|
37
|
-
# Test case with substitutions
|
38
|
-
(
|
39
|
-
("a*x**2 + b*x + c", "x", {"a": 3, "b": 7, "c": 5}),
|
40
|
-
r"\left[-7/6 - sqrt(11)*I/6, -7/6 + sqrt(11)*I/6\right]"
|
41
|
-
),
|
42
|
-
]
|
43
|
-
|
44
|
-
for (expression, variable, subs), expected_output in test_cases:
|
45
|
-
assert solve_algebraic_expression(expression, variable, subs) == expected_output
|
46
|
-
|
47
|
-
|
48
|
-
def test_get_prime_factors_latex():
|
49
|
-
test_cases = [
|
50
|
-
(100, "2^{2} \\cdot 5^{2}"),
|
51
|
-
(60, "2^{2} \\cdot 3 \\cdot 5"),
|
52
|
-
(45, "3^{2} \\cdot 5"),
|
53
|
-
(1, ""), # Handle case with 1, which has no prime factors
|
54
|
-
(17, "17") # Prime number itself
|
55
|
-
]
|
56
|
-
|
57
|
-
for n, expected_output in test_cases:
|
58
|
-
result = get_prime_factors_latex(n)
|
59
|
-
assert result == expected_output, f"Failed for {n}, got {result}"
|
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
|