rgwfuncs 0.0.49__tar.gz → 0.0.52__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {rgwfuncs-0.0.49/src/rgwfuncs.egg-info → rgwfuncs-0.0.52}/PKG-INFO +69 -30
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/README.md +68 -29
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/pyproject.toml +1 -1
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/setup.cfg +1 -1
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs/__init__.py +1 -1
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs/algebra_lib.py +223 -71
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52/src/rgwfuncs.egg-info}/PKG-INFO +69 -30
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/LICENSE +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs/df_lib.py +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs/docs_lib.py +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs/interactive_shell_lib.py +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs/str_lib.py +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs.egg-info/SOURCES.txt +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs.egg-info/dependency_links.txt +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs.egg-info/entry_points.txt +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/src/rgwfuncs.egg-info/requires.txt +0 -0
- {rgwfuncs-0.0.49 → rgwfuncs-0.0.52}/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.52
|
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
|
@@ -430,35 +430,7 @@ Factors a polynomial expression written in Python syntax and converts it into a
|
|
430
430
|
|
431
431
|
--------------------------------------------------------------------------------
|
432
432
|
|
433
|
-
### 8. `
|
434
|
-
|
435
|
-
Cancels common factors within a polynomial expression written in Python syntax and converts it to a LaTeX formatted string. This function parses an algebraic expression, cancels common factors using SymPy, and translates the reduced expression into a LaTeX representation. It can also accommodate optional substitutions to be made prior to simplification.
|
436
|
-
|
437
|
-
- Parameters:
|
438
|
-
- `expression` (str): The algebraic expression to simplify and convert to LaTeX. This string should be formatted using Python syntax.
|
439
|
-
- `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 to substitute.
|
440
|
-
|
441
|
-
- Returns:
|
442
|
-
- `str`: The LaTeX formatted string of the simplified expression. If the expression involves indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
|
443
|
-
|
444
|
-
- Raises:
|
445
|
-
- `ValueError`: If the expression cannot be parsed due to syntax errors or involves undefined operations, such as division by zero.
|
446
|
-
|
447
|
-
- Example:
|
448
|
-
|
449
|
-
from rgwfuncs import cancel_polynomial_expression
|
450
|
-
|
451
|
-
# Cancel common factors within a polynomial expression
|
452
|
-
latex_result1 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)")
|
453
|
-
print(latex_result1) # Output: "x + 2"
|
454
|
-
|
455
|
-
# Cancel with substituted values
|
456
|
-
latex_result2 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)", {"x": 2})
|
457
|
-
print(latex_result2) # Output: "Undefined result. This could be a division by zero error."
|
458
|
-
|
459
|
-
--------------------------------------------------------------------------------
|
460
|
-
|
461
|
-
### 9. `simplify_polynomial_expression`
|
433
|
+
### 8. `simplify_polynomial_expression`
|
462
434
|
|
463
435
|
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.
|
464
436
|
|
@@ -491,6 +463,34 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
|
|
491
463
|
|
492
464
|
--------------------------------------------------------------------------------
|
493
465
|
|
466
|
+
### 9. `cancel_polynomial_expression`
|
467
|
+
|
468
|
+
Cancels common factors within a polynomial expression written in Python syntax and converts it to a LaTeX formatted string. This function parses an algebraic expression, cancels common factors using SymPy, and translates the reduced expression into a LaTeX representation. It can also accommodate optional substitutions to be made prior to simplification.
|
469
|
+
|
470
|
+
- Parameters:
|
471
|
+
- `expression` (str): The algebraic expression to simplify and convert to LaTeX. This string should be formatted using Python syntax.
|
472
|
+
- `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 to substitute.
|
473
|
+
|
474
|
+
- Returns:
|
475
|
+
- `str`: The LaTeX formatted string of the simplified expression. If the expression involves indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
|
476
|
+
|
477
|
+
- Raises:
|
478
|
+
- `ValueError`: If the expression cannot be parsed due to syntax errors or involves undefined operations, such as division by zero.
|
479
|
+
|
480
|
+
- Example:
|
481
|
+
|
482
|
+
from rgwfuncs import cancel_polynomial_expression
|
483
|
+
|
484
|
+
# Cancel common factors within a polynomial expression
|
485
|
+
latex_result1 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)")
|
486
|
+
print(latex_result1) # Output: "x + 2"
|
487
|
+
|
488
|
+
# Cancel with substituted values
|
489
|
+
latex_result2 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)", {"x": 2})
|
490
|
+
print(latex_result2) # Output: "Undefined result. This could be a division by zero error."
|
491
|
+
|
492
|
+
--------------------------------------------------------------------------------
|
493
|
+
|
494
494
|
### 10. `solve_homogeneous_polynomial_expression`
|
495
495
|
|
496
496
|
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.
|
@@ -514,6 +514,45 @@ Solves a homogeneous polynomial expression for a specified variable and returns
|
|
514
514
|
|
515
515
|
--------------------------------------------------------------------------------
|
516
516
|
|
517
|
+
### 11. `plot_polynomial_functions`
|
518
|
+
|
519
|
+
This function plots polynomial functions described by a list of expressions and their corresponding substitution dictionaries. It generates SVG markup of the plots, with options for specifying the domain, axis zoom, and legend display.
|
520
|
+
|
521
|
+
• Parameters:
|
522
|
+
- `functions` (`List[Dict[str, Dict[str, Any]]]`): A list of dictionaries, each containing:
|
523
|
+
- A key which is a string representing a Python/NumPy expression (e.g., `"x**2"`, `"np.diff(x,2)"`).
|
524
|
+
- A value which is a dictionary containing substitutions for the expression. Must include an `"x"` key, either as `"*"` for default domain or a NumPy array.
|
525
|
+
- `zoom` (`float`): Determines the numeric axis range from `-zoom` to `+zoom` for both x and y axes (default is `10.0`).
|
526
|
+
- `show_legend` (`bool`): Specifies whether to include a legend in the plot (default is `True`).
|
527
|
+
|
528
|
+
• Returns:
|
529
|
+
- `str`: The raw SVG markup of the resulting plot.
|
530
|
+
|
531
|
+
• Example:
|
532
|
+
|
533
|
+
import subprocess
|
534
|
+
from rgwfuncs import plot_polynomial_functions
|
535
|
+
|
536
|
+
# Generate the SVG
|
537
|
+
plot_svg_string = plot_polynomial_functions(
|
538
|
+
functions=[
|
539
|
+
{"x**2": {"x": "*"}}, # Single expression, "*" means plot all discernable points
|
540
|
+
{"x**2/(2 + a) + a": {"x": np.linspace(-3, 4, 101), "a": 1.23}},
|
541
|
+
{"np.diff(x**3, 2)": {"x": np.linspace(-2, 2, 10)}}
|
542
|
+
],
|
543
|
+
zoom=2
|
544
|
+
)
|
545
|
+
|
546
|
+
# Write the SVG to an actual file
|
547
|
+
with open("plot.svg", "w", encoding="utf-8") as file:
|
548
|
+
file.write(plot_svg_string)
|
549
|
+
|
550
|
+
• Displaying the SVG:
|
551
|
+
|
552
|
+

|
553
|
+
|
554
|
+
--------------------------------------------------------------------------------
|
555
|
+
|
517
556
|
## String Based Functions
|
518
557
|
|
519
558
|
### 1. send_telegram_message
|
@@ -403,35 +403,7 @@ Factors a polynomial expression written in Python syntax and converts it into a
|
|
403
403
|
|
404
404
|
--------------------------------------------------------------------------------
|
405
405
|
|
406
|
-
### 8. `
|
407
|
-
|
408
|
-
Cancels common factors within a polynomial expression written in Python syntax and converts it to a LaTeX formatted string. This function parses an algebraic expression, cancels common factors using SymPy, and translates the reduced expression into a LaTeX representation. It can also accommodate optional substitutions to be made prior to simplification.
|
409
|
-
|
410
|
-
- Parameters:
|
411
|
-
- `expression` (str): The algebraic expression to simplify and convert to LaTeX. This string should be formatted using Python syntax.
|
412
|
-
- `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 to substitute.
|
413
|
-
|
414
|
-
- Returns:
|
415
|
-
- `str`: The LaTeX formatted string of the simplified expression. If the expression involves indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
|
416
|
-
|
417
|
-
- Raises:
|
418
|
-
- `ValueError`: If the expression cannot be parsed due to syntax errors or involves undefined operations, such as division by zero.
|
419
|
-
|
420
|
-
- Example:
|
421
|
-
|
422
|
-
from rgwfuncs import cancel_polynomial_expression
|
423
|
-
|
424
|
-
# Cancel common factors within a polynomial expression
|
425
|
-
latex_result1 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)")
|
426
|
-
print(latex_result1) # Output: "x + 2"
|
427
|
-
|
428
|
-
# Cancel with substituted values
|
429
|
-
latex_result2 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)", {"x": 2})
|
430
|
-
print(latex_result2) # Output: "Undefined result. This could be a division by zero error."
|
431
|
-
|
432
|
-
--------------------------------------------------------------------------------
|
433
|
-
|
434
|
-
### 9. `simplify_polynomial_expression`
|
406
|
+
### 8. `simplify_polynomial_expression`
|
435
407
|
|
436
408
|
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.
|
437
409
|
|
@@ -464,6 +436,34 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
|
|
464
436
|
|
465
437
|
--------------------------------------------------------------------------------
|
466
438
|
|
439
|
+
### 9. `cancel_polynomial_expression`
|
440
|
+
|
441
|
+
Cancels common factors within a polynomial expression written in Python syntax and converts it to a LaTeX formatted string. This function parses an algebraic expression, cancels common factors using SymPy, and translates the reduced expression into a LaTeX representation. It can also accommodate optional substitutions to be made prior to simplification.
|
442
|
+
|
443
|
+
- Parameters:
|
444
|
+
- `expression` (str): The algebraic expression to simplify and convert to LaTeX. This string should be formatted using Python syntax.
|
445
|
+
- `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 to substitute.
|
446
|
+
|
447
|
+
- Returns:
|
448
|
+
- `str`: The LaTeX formatted string of the simplified expression. If the expression involves indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
|
449
|
+
|
450
|
+
- Raises:
|
451
|
+
- `ValueError`: If the expression cannot be parsed due to syntax errors or involves undefined operations, such as division by zero.
|
452
|
+
|
453
|
+
- Example:
|
454
|
+
|
455
|
+
from rgwfuncs import cancel_polynomial_expression
|
456
|
+
|
457
|
+
# Cancel common factors within a polynomial expression
|
458
|
+
latex_result1 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)")
|
459
|
+
print(latex_result1) # Output: "x + 2"
|
460
|
+
|
461
|
+
# Cancel with substituted values
|
462
|
+
latex_result2 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)", {"x": 2})
|
463
|
+
print(latex_result2) # Output: "Undefined result. This could be a division by zero error."
|
464
|
+
|
465
|
+
--------------------------------------------------------------------------------
|
466
|
+
|
467
467
|
### 10. `solve_homogeneous_polynomial_expression`
|
468
468
|
|
469
469
|
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.
|
@@ -487,6 +487,45 @@ Solves a homogeneous polynomial expression for a specified variable and returns
|
|
487
487
|
|
488
488
|
--------------------------------------------------------------------------------
|
489
489
|
|
490
|
+
### 11. `plot_polynomial_functions`
|
491
|
+
|
492
|
+
This function plots polynomial functions described by a list of expressions and their corresponding substitution dictionaries. It generates SVG markup of the plots, with options for specifying the domain, axis zoom, and legend display.
|
493
|
+
|
494
|
+
• Parameters:
|
495
|
+
- `functions` (`List[Dict[str, Dict[str, Any]]]`): A list of dictionaries, each containing:
|
496
|
+
- A key which is a string representing a Python/NumPy expression (e.g., `"x**2"`, `"np.diff(x,2)"`).
|
497
|
+
- A value which is a dictionary containing substitutions for the expression. Must include an `"x"` key, either as `"*"` for default domain or a NumPy array.
|
498
|
+
- `zoom` (`float`): Determines the numeric axis range from `-zoom` to `+zoom` for both x and y axes (default is `10.0`).
|
499
|
+
- `show_legend` (`bool`): Specifies whether to include a legend in the plot (default is `True`).
|
500
|
+
|
501
|
+
• Returns:
|
502
|
+
- `str`: The raw SVG markup of the resulting plot.
|
503
|
+
|
504
|
+
• Example:
|
505
|
+
|
506
|
+
import subprocess
|
507
|
+
from rgwfuncs import plot_polynomial_functions
|
508
|
+
|
509
|
+
# Generate the SVG
|
510
|
+
plot_svg_string = plot_polynomial_functions(
|
511
|
+
functions=[
|
512
|
+
{"x**2": {"x": "*"}}, # Single expression, "*" means plot all discernable points
|
513
|
+
{"x**2/(2 + a) + a": {"x": np.linspace(-3, 4, 101), "a": 1.23}},
|
514
|
+
{"np.diff(x**3, 2)": {"x": np.linspace(-2, 2, 10)}}
|
515
|
+
],
|
516
|
+
zoom=2
|
517
|
+
)
|
518
|
+
|
519
|
+
# Write the SVG to an actual file
|
520
|
+
with open("plot.svg", "w", encoding="utf-8") as file:
|
521
|
+
file.write(plot_svg_string)
|
522
|
+
|
523
|
+
• Displaying the SVG:
|
524
|
+
|
525
|
+

|
526
|
+
|
527
|
+
--------------------------------------------------------------------------------
|
528
|
+
|
490
529
|
## String Based Functions
|
491
530
|
|
492
531
|
### 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 cancel_polynomial_expression, compute_constant_expression, compute_constant_expression_involving_matrices, compute_constant_expression_involving_ordered_series, compute_prime_factors, expand_polynomial_expression, factor_polynomial_expression, python_polynomial_expression_to_latex, simplify_polynomial_expression, solve_homogeneous_polynomial_expression
|
4
|
+
from .algebra_lib import cancel_polynomial_expression, compute_constant_expression, compute_constant_expression_involving_matrices, compute_constant_expression_involving_ordered_series, compute_prime_factors, expand_polynomial_expression, factor_polynomial_expression, plot_polynomial_functions, 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 .interactive_shell_lib import interactive_shell
|
@@ -1,7 +1,6 @@
|
|
1
1
|
import re
|
2
2
|
import math
|
3
3
|
import ast
|
4
|
-
# import numpy as np
|
5
4
|
from sympy import symbols, latex, simplify, solve, diff, Expr, factor, cancel, Eq
|
6
5
|
from sympy.core.sympify import SympifyError
|
7
6
|
from sympy.core import S
|
@@ -9,7 +8,12 @@ from sympy.parsing.sympy_parser import parse_expr
|
|
9
8
|
from sympy import __all__ as sympy_functions
|
10
9
|
from sympy.parsing.sympy_parser import (standard_transformations, implicit_multiplication_application)
|
11
10
|
|
12
|
-
from typing import Tuple, List, Dict, Optional
|
11
|
+
from typing import Tuple, List, Dict, Optional, Any
|
12
|
+
|
13
|
+
|
14
|
+
import numpy as np
|
15
|
+
import matplotlib.pyplot as plt
|
16
|
+
from io import BytesIO
|
13
17
|
|
14
18
|
|
15
19
|
def compute_prime_factors(n: int) -> str:
|
@@ -286,7 +290,6 @@ def python_polynomial_expression_to_latex(
|
|
286
290
|
Raises:
|
287
291
|
ValueError: If the expression cannot be parsed due to syntax errors.
|
288
292
|
"""
|
289
|
-
|
290
293
|
transformations = standard_transformations + (implicit_multiplication_application,)
|
291
294
|
|
292
295
|
def parse_and_convert_expression(expr_str: str, sym_vars: Dict[str, Expr]) -> Expr:
|
@@ -412,69 +415,6 @@ def factor_polynomial_expression(
|
|
412
415
|
return latex_result
|
413
416
|
|
414
417
|
|
415
|
-
def cancel_polynomial_expression(
|
416
|
-
expression: str,
|
417
|
-
subs: Optional[Dict[str, float]] = None
|
418
|
-
) -> str:
|
419
|
-
"""
|
420
|
-
Cancels common factors within a polynomial expression and converts it to LaTeX format.
|
421
|
-
|
422
|
-
This function parses an algebraic expression given in Python syntax, cancels any common factors,
|
423
|
-
and converts the resulting simplified expression into a LaTeX formatted string. The function can
|
424
|
-
also handle optional substitutions of variables before performing the cancellation.
|
425
|
-
|
426
|
-
Parameters:
|
427
|
-
expression (str): The algebraic expression to simplify and convert to LaTeX.
|
428
|
-
It should be a valid expression formatted using Python syntax.
|
429
|
-
subs (Optional[Dict[str, float]]): An optional dictionary where the keys are variable names in the
|
430
|
-
expression, and the values are the corresponding numbers to substitute
|
431
|
-
into the expression before simplification.
|
432
|
-
|
433
|
-
Returns:
|
434
|
-
str: The LaTeX formatted string of the simplified expression. If the expression involves
|
435
|
-
indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
|
436
|
-
|
437
|
-
Raises:
|
438
|
-
ValueError: If the expression cannot be parsed due to syntax errors or if operations result in
|
439
|
-
undefined behavior, such as division by zero.
|
440
|
-
|
441
|
-
"""
|
442
|
-
transformations = standard_transformations + (implicit_multiplication_application,)
|
443
|
-
|
444
|
-
def parse_and_cancel_expression(expr_str: str, sym_vars: Dict[str, symbols]) -> symbols:
|
445
|
-
try:
|
446
|
-
expr = parse_expr(expr_str, local_dict=sym_vars, transformations=transformations)
|
447
|
-
if subs:
|
448
|
-
if not isinstance(subs, dict):
|
449
|
-
raise ValueError(f"Substitutions must be a dictionary. Received: {subs}")
|
450
|
-
subs_symbols = {symbols(k): v for k, v in subs.items()}
|
451
|
-
expr = expr.subs(subs_symbols)
|
452
|
-
|
453
|
-
canceled_expr = cancel(expr)
|
454
|
-
|
455
|
-
# Check for NaN or indeterminate forms
|
456
|
-
if canceled_expr.has(S.NaN) or canceled_expr.has(S.Infinity) or canceled_expr.has(S.ComplexInfinity):
|
457
|
-
return "Undefined result. This could be a division by zero error."
|
458
|
-
|
459
|
-
return canceled_expr
|
460
|
-
|
461
|
-
except (SyntaxError, ValueError, TypeError, AttributeError, ZeroDivisionError, SympifyError) as e:
|
462
|
-
return f"Error: {str(e)}"
|
463
|
-
|
464
|
-
variable_names = set(re.findall(r'\b[a-zA-Z]\w*\b', expression))
|
465
|
-
sym_vars = {var: symbols(var) for var in variable_names}
|
466
|
-
|
467
|
-
expr = parse_and_cancel_expression(expression, sym_vars)
|
468
|
-
|
469
|
-
# If the expression is already a string (i.e., "Undefined" or error message), return it directly
|
470
|
-
if isinstance(expr, str):
|
471
|
-
return expr
|
472
|
-
|
473
|
-
# Otherwise, convert to LaTeX as usual
|
474
|
-
latex_result = latex(expr)
|
475
|
-
return latex_result
|
476
|
-
|
477
|
-
|
478
418
|
def simplify_polynomial_expression(
|
479
419
|
expression: str,
|
480
420
|
subs: Optional[Dict[str, float]] = None
|
@@ -650,6 +590,69 @@ def simplify_polynomial_expression(
|
|
650
590
|
raise ValueError(f"Error simplifying expression: {e}")
|
651
591
|
|
652
592
|
|
593
|
+
def cancel_polynomial_expression(
|
594
|
+
expression: str,
|
595
|
+
subs: Optional[Dict[str, float]] = None
|
596
|
+
) -> str:
|
597
|
+
"""
|
598
|
+
Cancels common factors within a polynomial expression and converts it to LaTeX format.
|
599
|
+
|
600
|
+
This function parses an algebraic expression given in Python syntax, cancels any common factors,
|
601
|
+
and converts the resulting simplified expression into a LaTeX formatted string. The function can
|
602
|
+
also handle optional substitutions of variables before performing the cancellation.
|
603
|
+
|
604
|
+
Parameters:
|
605
|
+
expression (str): The algebraic expression to simplify and convert to LaTeX.
|
606
|
+
It should be a valid expression formatted using Python syntax.
|
607
|
+
subs (Optional[Dict[str, float]]): An optional dictionary where the keys are variable names in the
|
608
|
+
expression, and the values are the corresponding numbers to substitute
|
609
|
+
into the expression before simplification.
|
610
|
+
|
611
|
+
Returns:
|
612
|
+
str: The LaTeX formatted string of the simplified expression. If the expression involves
|
613
|
+
indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
|
614
|
+
|
615
|
+
Raises:
|
616
|
+
ValueError: If the expression cannot be parsed due to syntax errors or if operations result in
|
617
|
+
undefined behavior, such as division by zero.
|
618
|
+
|
619
|
+
"""
|
620
|
+
transformations = standard_transformations + (implicit_multiplication_application,)
|
621
|
+
|
622
|
+
def parse_and_cancel_expression(expr_str: str, sym_vars: Dict[str, symbols]) -> symbols:
|
623
|
+
try:
|
624
|
+
expr = parse_expr(expr_str, local_dict=sym_vars, transformations=transformations)
|
625
|
+
if subs:
|
626
|
+
if not isinstance(subs, dict):
|
627
|
+
raise ValueError(f"Substitutions must be a dictionary. Received: {subs}")
|
628
|
+
subs_symbols = {symbols(k): v for k, v in subs.items()}
|
629
|
+
expr = expr.subs(subs_symbols)
|
630
|
+
|
631
|
+
canceled_expr = cancel(expr)
|
632
|
+
|
633
|
+
# Check for NaN or indeterminate forms
|
634
|
+
if canceled_expr.has(S.NaN) or canceled_expr.has(S.Infinity) or canceled_expr.has(S.ComplexInfinity):
|
635
|
+
return "Undefined result. This could be a division by zero error."
|
636
|
+
|
637
|
+
return canceled_expr
|
638
|
+
|
639
|
+
except (SyntaxError, ValueError, TypeError, AttributeError, ZeroDivisionError, SympifyError) as e:
|
640
|
+
return f"Error: {str(e)}"
|
641
|
+
|
642
|
+
variable_names = set(re.findall(r'\b[a-zA-Z]\w*\b', expression))
|
643
|
+
sym_vars = {var: symbols(var) for var in variable_names}
|
644
|
+
|
645
|
+
expr = parse_and_cancel_expression(expression, sym_vars)
|
646
|
+
|
647
|
+
# If the expression is already a string (i.e., "Undefined" or error message), return it directly
|
648
|
+
if isinstance(expr, str):
|
649
|
+
return expr
|
650
|
+
|
651
|
+
# Otherwise, convert to LaTeX as usual
|
652
|
+
latex_result = latex(expr)
|
653
|
+
return latex_result
|
654
|
+
|
655
|
+
|
653
656
|
def solve_homogeneous_polynomial_expression(
|
654
657
|
expression: str,
|
655
658
|
variable: str,
|
@@ -677,21 +680,170 @@ def solve_homogeneous_polynomial_expression(
|
|
677
680
|
"""
|
678
681
|
|
679
682
|
try:
|
683
|
+
# Handle symbols
|
680
684
|
variable_symbols = set(re.findall(r'\b[a-zA-Z]\w*\b', expression))
|
681
685
|
sym_vars = {var: symbols(var) for var in variable_symbols}
|
686
|
+
|
687
|
+
# Parse the expression
|
682
688
|
expr = parse_expr(expression, local_dict=sym_vars)
|
689
|
+
|
690
|
+
# Apply substitutions
|
683
691
|
if subs:
|
684
692
|
expr = expr.subs({symbols(k): v for k, v in subs.items()})
|
693
|
+
|
694
|
+
# Solve the equation
|
685
695
|
var_symbol = symbols(variable)
|
686
696
|
eq = Eq(expr, 0)
|
687
697
|
solutions = solve(eq, var_symbol)
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
698
|
+
|
699
|
+
# Convert solutions to LaTeX strings with handling for exact representations
|
700
|
+
latex_solutions = [latex(sol) for sol in solutions]
|
701
|
+
|
702
|
+
result = r"\left[" + ", ".join(latex_solutions) + r"\right]"
|
703
|
+
print("693", result)
|
693
704
|
return result
|
694
705
|
|
695
706
|
except Exception as e:
|
696
707
|
raise ValueError(f"Error solving the expression: {e}")
|
697
708
|
|
709
|
+
|
710
|
+
def plot_polynomial_functions(
|
711
|
+
functions: List[Dict[str, Dict[str, Any]]],
|
712
|
+
zoom: float = 10.0,
|
713
|
+
show_legend: bool = True
|
714
|
+
) -> str:
|
715
|
+
"""
|
716
|
+
Plots expressions described by a list of dictionaries of the form:
|
717
|
+
[
|
718
|
+
{ "expression_string": { "x": "*", "a":..., "b":... } },
|
719
|
+
{ "expression_string": { "x": np.linspace(...), "a":..., ... } },
|
720
|
+
...
|
721
|
+
]
|
722
|
+
|
723
|
+
In each top-level dictionary, there is exactly one key (a string
|
724
|
+
representing a Python/NumPy expression) and one value (a dictionary of
|
725
|
+
substitutions). This substitutions dictionary must have an "x" key:
|
726
|
+
• "x": "*" -> Use a default domain from -zoom..+zoom.
|
727
|
+
• "x": np.array(...) -> Use that array as the domain.
|
728
|
+
Other variables (like "a", "b", etc.) may also appear in the same dict.
|
729
|
+
|
730
|
+
Additionally, we use latexify_expression(...) to transform the expression
|
731
|
+
into a nice LaTeX form for the legend, including a special Δ notation for np.diff(...).
|
732
|
+
|
733
|
+
Parameters
|
734
|
+
----------
|
735
|
+
functions : List[Dict[str, Dict[str, Any]]]
|
736
|
+
A list of items. Each item is a dictionary:
|
737
|
+
key = expression string (e.g., "x**2", "np.diff(x,2)", etc.)
|
738
|
+
value = a dictionary of substitutions. Must contain "x",
|
739
|
+
either as "*" or a NumPy array. May contain additional
|
740
|
+
parameters like "a", "b", etc.
|
741
|
+
zoom : float
|
742
|
+
Sets the numeric axis range from -zoom..+zoom in both x and y.
|
743
|
+
show_legend : bool
|
744
|
+
Whether to add a legend to the plot (defaults to True).
|
745
|
+
|
746
|
+
Returns
|
747
|
+
-------
|
748
|
+
str
|
749
|
+
The raw SVG markup of the resulting plot.
|
750
|
+
"""
|
751
|
+
|
752
|
+
def latexify_expression(expr_str: str) -> str:
|
753
|
+
# Regex to locate np.diff(...) with an optional second argument
|
754
|
+
DIFF_PATTERN = r"np\.diff\s*\(\s*([^,\)]+)(?:,\s*(\d+))?\)"
|
755
|
+
|
756
|
+
def diff_replacer(match: re.Match) -> str:
|
757
|
+
inside = match.group(1).strip()
|
758
|
+
exponent = match.group(2)
|
759
|
+
inside_no_np = inside.replace("np.", "")
|
760
|
+
if exponent:
|
761
|
+
return rf"\Delta^{exponent}\left({inside_no_np}\right)"
|
762
|
+
else:
|
763
|
+
return rf"\Delta\left({inside_no_np}\right)"
|
764
|
+
|
765
|
+
expr_tmp = re.sub(DIFF_PATTERN, diff_replacer, expr_str)
|
766
|
+
expr_tmp = expr_tmp.replace("np.", "")
|
767
|
+
try:
|
768
|
+
latex_expr = python_polynomial_expression_to_latex(expr_tmp)
|
769
|
+
return latex_expr
|
770
|
+
except Exception:
|
771
|
+
# Fallback: just do naive **
|
772
|
+
return expr_tmp.replace("**", "^")
|
773
|
+
|
774
|
+
buffer = BytesIO()
|
775
|
+
fig, ax = plt.subplots()
|
776
|
+
|
777
|
+
for entry in functions:
|
778
|
+
if len(entry) != 1:
|
779
|
+
print("Skipping invalid item. Must have exactly 1 expression->substitutions pair.")
|
780
|
+
continue
|
781
|
+
|
782
|
+
expression, sub_dict = list(entry.items())[0]
|
783
|
+
if "x" not in sub_dict:
|
784
|
+
print(f"Skipping '{expression}' because sub-dict lacks 'x' key.")
|
785
|
+
continue
|
786
|
+
|
787
|
+
# If "x" is "*", create a default domain
|
788
|
+
x_val = sub_dict["x"]
|
789
|
+
if isinstance(x_val, str) and x_val == "*":
|
790
|
+
x_values = np.linspace(-zoom, zoom, 1201)
|
791
|
+
sub_dict["x"] = x_values
|
792
|
+
elif isinstance(x_val, np.ndarray):
|
793
|
+
x_values = x_val
|
794
|
+
else:
|
795
|
+
print(f"Skipping '{expression}' because 'x' is neither '*' nor a NumPy array.")
|
796
|
+
continue
|
797
|
+
|
798
|
+
# Evaluate the expression
|
799
|
+
try:
|
800
|
+
eval_context = {"np": np}
|
801
|
+
eval_context.update(sub_dict)
|
802
|
+
y_values = eval(expression, {"np": np}, eval_context)
|
803
|
+
except Exception as e:
|
804
|
+
print(f"Error evaluating expression '{expression}': {e}")
|
805
|
+
continue
|
806
|
+
|
807
|
+
if not isinstance(y_values, np.ndarray):
|
808
|
+
print(f"Skipping '{expression}' because it did not produce a NumPy array.")
|
809
|
+
continue
|
810
|
+
|
811
|
+
# If y_values is shorter than x_values (like np.diff), truncate x
|
812
|
+
if len(y_values) < len(x_values):
|
813
|
+
x_values = x_values[:len(y_values)]
|
814
|
+
|
815
|
+
# Convert expression to a nice LaTeX string
|
816
|
+
label_expr = latexify_expression(expression)
|
817
|
+
ax.plot(x_values, y_values, label=rf"${label_expr}$")
|
818
|
+
|
819
|
+
# Configure axes
|
820
|
+
ax.set_xlim(-zoom, zoom)
|
821
|
+
ax.set_ylim(-zoom, zoom)
|
822
|
+
ax.spines['left'].set_position('zero')
|
823
|
+
ax.spines['bottom'].set_position('zero')
|
824
|
+
ax.spines['right'].set_color('none')
|
825
|
+
ax.spines['top'].set_color('none')
|
826
|
+
ax.set_aspect('equal', 'box')
|
827
|
+
ax.xaxis.set_ticks_position('bottom')
|
828
|
+
ax.yaxis.set_ticks_position('left')
|
829
|
+
ax.grid(True)
|
830
|
+
|
831
|
+
# Show legend
|
832
|
+
if show_legend:
|
833
|
+
leg = ax.legend(
|
834
|
+
loc='upper center',
|
835
|
+
bbox_to_anchor=(0.5, -0.03),
|
836
|
+
fancybox=True,
|
837
|
+
shadow=True,
|
838
|
+
ncol=1
|
839
|
+
)
|
840
|
+
plt.savefig(
|
841
|
+
buffer,
|
842
|
+
format='svg',
|
843
|
+
bbox_inches='tight',
|
844
|
+
bbox_extra_artists=[leg] # ensures the legend is fully captured
|
845
|
+
)
|
846
|
+
else:
|
847
|
+
plt.savefig(buffer, format='svg', bbox_inches='tight')
|
848
|
+
plt.close(fig)
|
849
|
+
return buffer.getvalue().decode('utf-8')
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: rgwfuncs
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.52
|
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
|
@@ -430,35 +430,7 @@ Factors a polynomial expression written in Python syntax and converts it into a
|
|
430
430
|
|
431
431
|
--------------------------------------------------------------------------------
|
432
432
|
|
433
|
-
### 8. `
|
434
|
-
|
435
|
-
Cancels common factors within a polynomial expression written in Python syntax and converts it to a LaTeX formatted string. This function parses an algebraic expression, cancels common factors using SymPy, and translates the reduced expression into a LaTeX representation. It can also accommodate optional substitutions to be made prior to simplification.
|
436
|
-
|
437
|
-
- Parameters:
|
438
|
-
- `expression` (str): The algebraic expression to simplify and convert to LaTeX. This string should be formatted using Python syntax.
|
439
|
-
- `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 to substitute.
|
440
|
-
|
441
|
-
- Returns:
|
442
|
-
- `str`: The LaTeX formatted string of the simplified expression. If the expression involves indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
|
443
|
-
|
444
|
-
- Raises:
|
445
|
-
- `ValueError`: If the expression cannot be parsed due to syntax errors or involves undefined operations, such as division by zero.
|
446
|
-
|
447
|
-
- Example:
|
448
|
-
|
449
|
-
from rgwfuncs import cancel_polynomial_expression
|
450
|
-
|
451
|
-
# Cancel common factors within a polynomial expression
|
452
|
-
latex_result1 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)")
|
453
|
-
print(latex_result1) # Output: "x + 2"
|
454
|
-
|
455
|
-
# Cancel with substituted values
|
456
|
-
latex_result2 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)", {"x": 2})
|
457
|
-
print(latex_result2) # Output: "Undefined result. This could be a division by zero error."
|
458
|
-
|
459
|
-
--------------------------------------------------------------------------------
|
460
|
-
|
461
|
-
### 9. `simplify_polynomial_expression`
|
433
|
+
### 8. `simplify_polynomial_expression`
|
462
434
|
|
463
435
|
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.
|
464
436
|
|
@@ -491,6 +463,34 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
|
|
491
463
|
|
492
464
|
--------------------------------------------------------------------------------
|
493
465
|
|
466
|
+
### 9. `cancel_polynomial_expression`
|
467
|
+
|
468
|
+
Cancels common factors within a polynomial expression written in Python syntax and converts it to a LaTeX formatted string. This function parses an algebraic expression, cancels common factors using SymPy, and translates the reduced expression into a LaTeX representation. It can also accommodate optional substitutions to be made prior to simplification.
|
469
|
+
|
470
|
+
- Parameters:
|
471
|
+
- `expression` (str): The algebraic expression to simplify and convert to LaTeX. This string should be formatted using Python syntax.
|
472
|
+
- `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 to substitute.
|
473
|
+
|
474
|
+
- Returns:
|
475
|
+
- `str`: The LaTeX formatted string of the simplified expression. If the expression involves indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
|
476
|
+
|
477
|
+
- Raises:
|
478
|
+
- `ValueError`: If the expression cannot be parsed due to syntax errors or involves undefined operations, such as division by zero.
|
479
|
+
|
480
|
+
- Example:
|
481
|
+
|
482
|
+
from rgwfuncs import cancel_polynomial_expression
|
483
|
+
|
484
|
+
# Cancel common factors within a polynomial expression
|
485
|
+
latex_result1 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)")
|
486
|
+
print(latex_result1) # Output: "x + 2"
|
487
|
+
|
488
|
+
# Cancel with substituted values
|
489
|
+
latex_result2 = cancel_polynomial_expression("(x**2 - 4) / (x - 2)", {"x": 2})
|
490
|
+
print(latex_result2) # Output: "Undefined result. This could be a division by zero error."
|
491
|
+
|
492
|
+
--------------------------------------------------------------------------------
|
493
|
+
|
494
494
|
### 10. `solve_homogeneous_polynomial_expression`
|
495
495
|
|
496
496
|
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.
|
@@ -514,6 +514,45 @@ Solves a homogeneous polynomial expression for a specified variable and returns
|
|
514
514
|
|
515
515
|
--------------------------------------------------------------------------------
|
516
516
|
|
517
|
+
### 11. `plot_polynomial_functions`
|
518
|
+
|
519
|
+
This function plots polynomial functions described by a list of expressions and their corresponding substitution dictionaries. It generates SVG markup of the plots, with options for specifying the domain, axis zoom, and legend display.
|
520
|
+
|
521
|
+
• Parameters:
|
522
|
+
- `functions` (`List[Dict[str, Dict[str, Any]]]`): A list of dictionaries, each containing:
|
523
|
+
- A key which is a string representing a Python/NumPy expression (e.g., `"x**2"`, `"np.diff(x,2)"`).
|
524
|
+
- A value which is a dictionary containing substitutions for the expression. Must include an `"x"` key, either as `"*"` for default domain or a NumPy array.
|
525
|
+
- `zoom` (`float`): Determines the numeric axis range from `-zoom` to `+zoom` for both x and y axes (default is `10.0`).
|
526
|
+
- `show_legend` (`bool`): Specifies whether to include a legend in the plot (default is `True`).
|
527
|
+
|
528
|
+
• Returns:
|
529
|
+
- `str`: The raw SVG markup of the resulting plot.
|
530
|
+
|
531
|
+
• Example:
|
532
|
+
|
533
|
+
import subprocess
|
534
|
+
from rgwfuncs import plot_polynomial_functions
|
535
|
+
|
536
|
+
# Generate the SVG
|
537
|
+
plot_svg_string = plot_polynomial_functions(
|
538
|
+
functions=[
|
539
|
+
{"x**2": {"x": "*"}}, # Single expression, "*" means plot all discernable points
|
540
|
+
{"x**2/(2 + a) + a": {"x": np.linspace(-3, 4, 101), "a": 1.23}},
|
541
|
+
{"np.diff(x**3, 2)": {"x": np.linspace(-2, 2, 10)}}
|
542
|
+
],
|
543
|
+
zoom=2
|
544
|
+
)
|
545
|
+
|
546
|
+
# Write the SVG to an actual file
|
547
|
+
with open("plot.svg", "w", encoding="utf-8") as file:
|
548
|
+
file.write(plot_svg_string)
|
549
|
+
|
550
|
+
• Displaying the SVG:
|
551
|
+
|
552
|
+

|
553
|
+
|
554
|
+
--------------------------------------------------------------------------------
|
555
|
+
|
517
556
|
## String Based Functions
|
518
557
|
|
519
558
|
### 1. send_telegram_message
|
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
|
File without changes
|