rgwfuncs 0.0.52__tar.gz → 0.0.53__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rgwfuncs
3
- Version: 0.0.52
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
- - Parameters:
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
- - Returns:
377
+ Returns:
378
378
  - `str`: The LaTeX formatted string of the expanded expression.
379
379
 
380
- - Raises:
380
+ Raises:
381
381
  - `ValueError`: If the expression cannot be parsed due to syntax errors.
382
382
 
383
- - Example:
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
- - Parameters:
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
- - Returns:
413
+ Returns:
414
414
  - `str`: The LaTeX formatted string representing the factored expression.
415
415
 
416
- - Raises:
416
+ Raises:
417
417
  - `ValueError`: If the expression cannot be parsed due to syntax errors.
418
418
 
419
- - Example:
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 Usage:
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
- - Parameters:
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
- - Returns:
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
- - Raises:
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
- - Example:
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
- ![Plot](./media/plot_polynomial_functions_example_1.svg)
553
+ ![Plot](./media/plot_polynomial_functions_example_1.svg)
553
554
 
554
555
  --------------------------------------------------------------------------------
555
556
 
@@ -343,17 +343,17 @@ Converts a polynomial expression written in Python syntax to a LaTeX formatted s
343
343
 
344
344
  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.
345
345
 
346
- - Parameters:
346
+ Parameters:
347
347
  - `expression` (str): The algebraic expression to expand and convert to LaTeX. This string should be formatted using Python syntax acceptable by SymPy.
348
348
  - `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.
349
349
 
350
- - Returns:
350
+ Returns:
351
351
  - `str`: The LaTeX formatted string of the expanded expression.
352
352
 
353
- - Raises:
353
+ Raises:
354
354
  - `ValueError`: If the expression cannot be parsed due to syntax errors.
355
355
 
356
- - Example:
356
+ Example:
357
357
 
358
358
  from rgwfuncs import expand_polynomial_expression
359
359
 
@@ -379,17 +379,17 @@ Expands a polynomial expression written in Python syntax and converts it into a
379
379
 
380
380
  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.
381
381
 
382
- - Parameters:
382
+ Parameters:
383
383
  - `expression` (str): The polynomial expression to factor and convert to LaTeX. This should be a valid expression formatted using Python syntax.
384
384
  - `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.
385
385
 
386
- - Returns:
386
+ Returns:
387
387
  - `str`: The LaTeX formatted string representing the factored expression.
388
388
 
389
- - Raises:
389
+ Raises:
390
390
  - `ValueError`: If the expression cannot be parsed due to syntax errors.
391
391
 
392
- - Example:
392
+ Example:
393
393
 
394
394
  from rgwfuncs import factor_polynomial_expression
395
395
 
@@ -414,7 +414,7 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
414
414
  • Returns:
415
415
  - `str`: The simplified expression formatted as a LaTeX string.
416
416
 
417
- • Example Usage:
417
+ • Example:
418
418
 
419
419
  from rgwfuncs import simplify_polynomial_expression
420
420
 
@@ -440,17 +440,17 @@ Simplifies an algebraic expression in polynomial form and returns it in LaTeX fo
440
440
 
441
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
442
 
443
- - Parameters:
443
+ Parameters:
444
444
  - `expression` (str): The algebraic expression to simplify and convert to LaTeX. This string should be formatted using Python syntax.
445
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
446
 
447
- - Returns:
447
+ Returns:
448
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
449
 
450
- - Raises:
450
+ Raises:
451
451
  - `ValueError`: If the expression cannot be parsed due to syntax errors or involves undefined operations, such as division by zero.
452
452
 
453
- - Example:
453
+ Example:
454
454
 
455
455
  from rgwfuncs import cancel_polynomial_expression
456
456
 
@@ -497,13 +497,14 @@ This function plots polynomial functions described by a list of expressions and
497
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
498
  - `zoom` (`float`): Determines the numeric axis range from `-zoom` to `+zoom` for both x and y axes (default is `10.0`).
499
499
  - `show_legend` (`bool`): Specifies whether to include a legend in the plot (default is `True`).
500
+ - `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).
501
+ - `save_path` (`Optional[str]`): If specified, saves the output string as a .svg at the indicated path (defaults to None).
500
502
 
501
503
  • Returns:
502
504
  - `str`: The raw SVG markup of the resulting plot.
503
505
 
504
506
  • Example:
505
507
 
506
- import subprocess
507
508
  from rgwfuncs import plot_polynomial_functions
508
509
 
509
510
  # Generate the SVG
@@ -522,7 +523,7 @@ This function plots polynomial functions described by a list of expressions and
522
523
 
523
524
  • Displaying the SVG:
524
525
 
525
- ![Plot](./media/plot_polynomial_functions_example_1.svg)
526
+ ![Plot](./media/plot_polynomial_functions_example_1.svg)
526
527
 
527
528
  --------------------------------------------------------------------------------
528
529
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "rgwfuncs"
7
- version = "0.0.52"
7
+ version = "0.0.53"
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.52
3
+ version = 0.0.53
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,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
- return buffer.getvalue().decode('utf-8')
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.52
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
- - Parameters:
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
- - Returns:
377
+ Returns:
378
378
  - `str`: The LaTeX formatted string of the expanded expression.
379
379
 
380
- - Raises:
380
+ Raises:
381
381
  - `ValueError`: If the expression cannot be parsed due to syntax errors.
382
382
 
383
- - Example:
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
- - Parameters:
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
- - Returns:
413
+ Returns:
414
414
  - `str`: The LaTeX formatted string representing the factored expression.
415
415
 
416
- - Raises:
416
+ Raises:
417
417
  - `ValueError`: If the expression cannot be parsed due to syntax errors.
418
418
 
419
- - Example:
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 Usage:
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
- - Parameters:
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
- - Returns:
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
- - Raises:
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
- - Example:
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
- ![Plot](./media/plot_polynomial_functions_example_1.svg)
553
+ ![Plot](./media/plot_polynomial_functions_example_1.svg)
553
554
 
554
555
  --------------------------------------------------------------------------------
555
556
 
File without changes