rgwfuncs 0.0.52__py3-none-any.whl → 0.0.53__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- rgwfuncs/algebra_lib.py +43 -5
- {rgwfuncs-0.0.52.dist-info → rgwfuncs-0.0.53.dist-info}/METADATA +17 -16
- {rgwfuncs-0.0.52.dist-info → rgwfuncs-0.0.53.dist-info}/RECORD +7 -7
- {rgwfuncs-0.0.52.dist-info → rgwfuncs-0.0.53.dist-info}/LICENSE +0 -0
- {rgwfuncs-0.0.52.dist-info → rgwfuncs-0.0.53.dist-info}/WHEEL +0 -0
- {rgwfuncs-0.0.52.dist-info → rgwfuncs-0.0.53.dist-info}/entry_points.txt +0 -0
- {rgwfuncs-0.0.52.dist-info → rgwfuncs-0.0.53.dist-info}/top_level.txt +0 -0
rgwfuncs/algebra_lib.py
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
import re
|
2
2
|
import math
|
3
3
|
import ast
|
4
|
+
import subprocess
|
5
|
+
import tempfile
|
4
6
|
from sympy import symbols, latex, simplify, solve, diff, Expr, factor, cancel, Eq
|
5
7
|
from sympy.core.sympify import SympifyError
|
6
8
|
from sympy.core import S
|
7
9
|
from sympy.parsing.sympy_parser import parse_expr
|
8
10
|
from sympy import __all__ as sympy_functions
|
9
11
|
from sympy.parsing.sympy_parser import (standard_transformations, implicit_multiplication_application)
|
10
|
-
|
11
12
|
from typing import Tuple, List, Dict, Optional, Any
|
12
|
-
|
13
|
-
|
14
13
|
import numpy as np
|
15
14
|
import matplotlib.pyplot as plt
|
16
15
|
from io import BytesIO
|
@@ -710,7 +709,9 @@ def solve_homogeneous_polynomial_expression(
|
|
710
709
|
def plot_polynomial_functions(
|
711
710
|
functions: List[Dict[str, Dict[str, Any]]],
|
712
711
|
zoom: float = 10.0,
|
713
|
-
show_legend: bool = True
|
712
|
+
show_legend: bool = True,
|
713
|
+
open_file: bool = False,
|
714
|
+
save_path: Optional[str] = None,
|
714
715
|
) -> str:
|
715
716
|
"""
|
716
717
|
Plots expressions described by a list of dictionaries of the form:
|
@@ -742,6 +743,13 @@ def plot_polynomial_functions(
|
|
742
743
|
Sets the numeric axis range from -zoom..+zoom in both x and y.
|
743
744
|
show_legend : bool
|
744
745
|
Whether to add a legend to the plot (defaults to True).
|
746
|
+
open_file : bool
|
747
|
+
If saving to path is not desireable, opens the svg as a temp file, else opens
|
748
|
+
the file from the actual location using the system's default viewer (defaults to
|
749
|
+
False).
|
750
|
+
save_path : Optional[str]
|
751
|
+
If specified, saves the output string as a .svg at the indicated path (defaults to
|
752
|
+
None).
|
745
753
|
|
746
754
|
Returns
|
747
755
|
-------
|
@@ -771,6 +779,33 @@ def plot_polynomial_functions(
|
|
771
779
|
# Fallback: just do naive **
|
772
780
|
return expr_tmp.replace("**", "^")
|
773
781
|
|
782
|
+
def handle_open_and_save(
|
783
|
+
svg_string: str,
|
784
|
+
open_file: bool,
|
785
|
+
save_path: Optional[str]
|
786
|
+
) -> None:
|
787
|
+
# Save the SVG to a file if a save path is provided
|
788
|
+
if save_path:
|
789
|
+
try:
|
790
|
+
with open(save_path, 'w', encoding='utf-8') as file:
|
791
|
+
file.write(svg_string)
|
792
|
+
print(f"[INFO] SVG saved to: {save_path}")
|
793
|
+
except IOError as e:
|
794
|
+
print(f"[ERROR] Failed to save SVG to {save_path}. IOError: {e}")
|
795
|
+
|
796
|
+
# Handle opening the file if requested
|
797
|
+
if open_file and save_path:
|
798
|
+
result = subprocess.run(["xdg-open", save_path], stderr=subprocess.DEVNULL)
|
799
|
+
if result.returncode != 0:
|
800
|
+
print("[ERROR] Failed to open the SVG file with the default viewer.")
|
801
|
+
elif open_file:
|
802
|
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".svg") as tmpfile:
|
803
|
+
temp_svg_path = tmpfile.name
|
804
|
+
tmpfile.write(svg_string.encode('utf-8'))
|
805
|
+
result = subprocess.run(["xdg-open", temp_svg_path], stderr=subprocess.DEVNULL)
|
806
|
+
if result.returncode != 0:
|
807
|
+
print("[ERROR] Failed to open the SVG file with the default viewer.")
|
808
|
+
|
774
809
|
buffer = BytesIO()
|
775
810
|
fig, ax = plt.subplots()
|
776
811
|
|
@@ -845,5 +880,8 @@ def plot_polynomial_functions(
|
|
845
880
|
)
|
846
881
|
else:
|
847
882
|
plt.savefig(buffer, format='svg', bbox_inches='tight')
|
883
|
+
|
848
884
|
plt.close(fig)
|
849
|
-
|
885
|
+
svg_string = buffer.getvalue().decode('utf-8')
|
886
|
+
handle_open_and_save(svg_string, open_file, save_path)
|
887
|
+
return svg_string
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: rgwfuncs
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.53
|
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
|
@@ -370,17 +370,17 @@ Converts a polynomial expression written in Python syntax to a LaTeX formatted s
|
|
370
370
|
|
371
371
|
Expands a polynomial expression written in Python syntax and converts it into a LaTeX formatted string. This function takes algebraic expressions provided as strings using Python's syntax, applies polynomial expansion through SymPy, and translates them into LaTeX representations, suitable for academic or professional documentation. It supports expressions with named variables and provides an option to substitute specific values into the expression before expansion.
|
372
372
|
|
373
|
-
|
373
|
+
• Parameters:
|
374
374
|
- `expression` (str): The algebraic expression to expand and convert to LaTeX. This string should be formatted using Python syntax acceptable by SymPy.
|
375
375
|
- `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where the keys are variable names in the expression, and the values are the numbers with which to substitute those variables before expanding.
|
376
376
|
|
377
|
-
|
377
|
+
• Returns:
|
378
378
|
- `str`: The LaTeX formatted string of the expanded expression.
|
379
379
|
|
380
|
-
|
380
|
+
• Raises:
|
381
381
|
- `ValueError`: If the expression cannot be parsed due to syntax errors.
|
382
382
|
|
383
|
-
|
383
|
+
• Example:
|
384
384
|
|
385
385
|
from rgwfuncs import expand_polynomial_expression
|
386
386
|
|
@@ -406,17 +406,17 @@ Expands a polynomial expression written in Python syntax and converts it into a
|
|
406
406
|
|
407
407
|
Factors a polynomial expression written in Python syntax and converts it into a LaTeX formatted string. This function parses an algebraic expression, performs polynomial factoring using SymPy, and converts the factored expression into a LaTeX representation, ideal for academic or professional use. Optional substitutions can be made before factoring.
|
408
408
|
|
409
|
-
|
409
|
+
• Parameters:
|
410
410
|
- `expression` (str): The polynomial expression to factor and convert to LaTeX. This should be a valid expression formatted using Python syntax.
|
411
411
|
- `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions. The keys are variable names in the expression, and the values are numbers that replace these variables.
|
412
412
|
|
413
|
-
|
413
|
+
• Returns:
|
414
414
|
- `str`: The LaTeX formatted string representing the factored expression.
|
415
415
|
|
416
|
-
|
416
|
+
• Raises:
|
417
417
|
- `ValueError`: If the expression cannot be parsed due to syntax errors.
|
418
418
|
|
419
|
-
|
419
|
+
• Example:
|
420
420
|
|
421
421
|
from rgwfuncs import factor_polynomial_expression
|
422
422
|
|
@@ -441,7 +441,7 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
|
|
441
441
|
• Returns:
|
442
442
|
- `str`: The simplified expression formatted as a LaTeX string.
|
443
443
|
|
444
|
-
• Example
|
444
|
+
• Example:
|
445
445
|
|
446
446
|
from rgwfuncs import simplify_polynomial_expression
|
447
447
|
|
@@ -467,17 +467,17 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
|
|
467
467
|
|
468
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
469
|
|
470
|
-
|
470
|
+
• Parameters:
|
471
471
|
- `expression` (str): The algebraic expression to simplify and convert to LaTeX. This string should be formatted using Python syntax.
|
472
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
473
|
|
474
|
-
|
474
|
+
• Returns:
|
475
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
476
|
|
477
|
-
|
477
|
+
• Raises:
|
478
478
|
- `ValueError`: If the expression cannot be parsed due to syntax errors or involves undefined operations, such as division by zero.
|
479
479
|
|
480
|
-
|
480
|
+
• Example:
|
481
481
|
|
482
482
|
from rgwfuncs import cancel_polynomial_expression
|
483
483
|
|
@@ -524,13 +524,14 @@ This function plots polynomial functions described by a list of expressions and
|
|
524
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
525
|
- `zoom` (`float`): Determines the numeric axis range from `-zoom` to `+zoom` for both x and y axes (default is `10.0`).
|
526
526
|
- `show_legend` (`bool`): Specifies whether to include a legend in the plot (default is `True`).
|
527
|
+
- `open_file` (`bool`): If saving to path is not desireable, opens the svg as a temp file, else opens the file from the actual location using the system's default viewer (defaults to False).
|
528
|
+
- `save_path` (`Optional[str]`): If specified, saves the output string as a .svg at the indicated path (defaults to None).
|
527
529
|
|
528
530
|
• Returns:
|
529
531
|
- `str`: The raw SVG markup of the resulting plot.
|
530
532
|
|
531
533
|
• Example:
|
532
534
|
|
533
|
-
import subprocess
|
534
535
|
from rgwfuncs import plot_polynomial_functions
|
535
536
|
|
536
537
|
# Generate the SVG
|
@@ -549,7 +550,7 @@ This function plots polynomial functions described by a list of expressions and
|
|
549
550
|
|
550
551
|
• Displaying the SVG:
|
551
552
|
|
552
|
-

|
553
|
+

|
553
554
|
|
554
555
|
--------------------------------------------------------------------------------
|
555
556
|
|
@@ -1,12 +1,12 @@
|
|
1
1
|
rgwfuncs/__init__.py,sha256=JjkuVeRV_Bkw8vnqSbJsRcYQRm4iCI3futR86cf3tRI,1637
|
2
|
-
rgwfuncs/algebra_lib.py,sha256
|
2
|
+
rgwfuncs/algebra_lib.py,sha256=AcGurkWyq3sd-HXPqUDnioEyiYwC_H70PBkJHOcXO7I,35183
|
3
3
|
rgwfuncs/df_lib.py,sha256=qqRQdakheLy8wMZRBfHwKyIp8DmdZIWfAiLKWgq03QU,68977
|
4
4
|
rgwfuncs/docs_lib.py,sha256=y3wSAOPO3qsA4HZ7xAtW8HimM8w-c8hjcEzMRLJ96ao,1960
|
5
5
|
rgwfuncs/interactive_shell_lib.py,sha256=A7EWsYxAfDev_N0-2GjRvAtp0bAwBPHIczXb8Gu9fzI,1107
|
6
6
|
rgwfuncs/str_lib.py,sha256=rtAdRlnSJIu3JhI-tA_A0wCiPK2m-zn5RoGpBxv_g-4,2228
|
7
|
-
rgwfuncs-0.0.
|
8
|
-
rgwfuncs-0.0.
|
9
|
-
rgwfuncs-0.0.
|
10
|
-
rgwfuncs-0.0.
|
11
|
-
rgwfuncs-0.0.
|
12
|
-
rgwfuncs-0.0.
|
7
|
+
rgwfuncs-0.0.53.dist-info/LICENSE,sha256=7EI8xVBu6h_7_JlVw-yPhhOZlpY9hP8wal7kHtqKT_E,1074
|
8
|
+
rgwfuncs-0.0.53.dist-info/METADATA,sha256=dkIVCAMIVUoI1teXGoalzHFYQT1usUebDMG5Mr6bafE,57140
|
9
|
+
rgwfuncs-0.0.53.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
10
|
+
rgwfuncs-0.0.53.dist-info/entry_points.txt,sha256=j-c5IOPIQ0252EaOV6j6STio56sbXl2C4ym_fQ0lXx0,43
|
11
|
+
rgwfuncs-0.0.53.dist-info/top_level.txt,sha256=aGuVIzWsKiV1f2gCb6mynx0zx5ma0B1EwPGFKVEMTi4,9
|
12
|
+
rgwfuncs-0.0.53.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|