rgwfuncs 0.0.48__py3-none-any.whl → 0.0.51__py3-none-any.whl

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/algebra_lib.py CHANGED
@@ -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
@@ -412,69 +411,6 @@ def factor_polynomial_expression(
412
411
  return latex_result
413
412
 
414
413
 
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
414
  def simplify_polynomial_expression(
479
415
  expression: str,
480
416
  subs: Optional[Dict[str, float]] = None
@@ -650,6 +586,69 @@ def simplify_polynomial_expression(
650
586
  raise ValueError(f"Error simplifying expression: {e}")
651
587
 
652
588
 
589
+ def cancel_polynomial_expression(
590
+ expression: str,
591
+ subs: Optional[Dict[str, float]] = None
592
+ ) -> str:
593
+ """
594
+ Cancels common factors within a polynomial expression and converts it to LaTeX format.
595
+
596
+ This function parses an algebraic expression given in Python syntax, cancels any common factors,
597
+ and converts the resulting simplified expression into a LaTeX formatted string. The function can
598
+ also handle optional substitutions of variables before performing the cancellation.
599
+
600
+ Parameters:
601
+ expression (str): The algebraic expression to simplify and convert to LaTeX.
602
+ It should be a valid expression formatted using Python syntax.
603
+ subs (Optional[Dict[str, float]]): An optional dictionary where the keys are variable names in the
604
+ expression, and the values are the corresponding numbers to substitute
605
+ into the expression before simplification.
606
+
607
+ Returns:
608
+ str: The LaTeX formatted string of the simplified expression. If the expression involves
609
+ indeterminate forms due to operations like division by zero, a descriptive error message is returned instead.
610
+
611
+ Raises:
612
+ ValueError: If the expression cannot be parsed due to syntax errors or if operations result in
613
+ undefined behavior, such as division by zero.
614
+
615
+ """
616
+ transformations = standard_transformations + (implicit_multiplication_application,)
617
+
618
+ def parse_and_cancel_expression(expr_str: str, sym_vars: Dict[str, symbols]) -> symbols:
619
+ try:
620
+ expr = parse_expr(expr_str, local_dict=sym_vars, transformations=transformations)
621
+ if subs:
622
+ if not isinstance(subs, dict):
623
+ raise ValueError(f"Substitutions must be a dictionary. Received: {subs}")
624
+ subs_symbols = {symbols(k): v for k, v in subs.items()}
625
+ expr = expr.subs(subs_symbols)
626
+
627
+ canceled_expr = cancel(expr)
628
+
629
+ # Check for NaN or indeterminate forms
630
+ if canceled_expr.has(S.NaN) or canceled_expr.has(S.Infinity) or canceled_expr.has(S.ComplexInfinity):
631
+ return "Undefined result. This could be a division by zero error."
632
+
633
+ return canceled_expr
634
+
635
+ except (SyntaxError, ValueError, TypeError, AttributeError, ZeroDivisionError, SympifyError) as e:
636
+ return f"Error: {str(e)}"
637
+
638
+ variable_names = set(re.findall(r'\b[a-zA-Z]\w*\b', expression))
639
+ sym_vars = {var: symbols(var) for var in variable_names}
640
+
641
+ expr = parse_and_cancel_expression(expression, sym_vars)
642
+
643
+ # If the expression is already a string (i.e., "Undefined" or error message), return it directly
644
+ if isinstance(expr, str):
645
+ return expr
646
+
647
+ # Otherwise, convert to LaTeX as usual
648
+ latex_result = latex(expr)
649
+ return latex_result
650
+
651
+
653
652
  def solve_homogeneous_polynomial_expression(
654
653
  expression: str,
655
654
  variable: str,
@@ -677,36 +676,28 @@ def solve_homogeneous_polynomial_expression(
677
676
  """
678
677
 
679
678
  try:
680
- print("679", expression)
681
- print("681", variable)
682
- print("682", subs)
683
-
679
+ # Handle symbols
684
680
  variable_symbols = set(re.findall(r'\b[a-zA-Z]\w*\b', expression))
685
681
  sym_vars = {var: symbols(var) for var in variable_symbols}
686
682
 
683
+ # Parse the expression
687
684
  expr = parse_expr(expression, local_dict=sym_vars)
688
- print("Parsed expression:", expr)
689
685
 
686
+ # Apply substitutions
690
687
  if subs:
691
688
  expr = expr.subs({symbols(k): v for k, v in subs.items()})
692
- print("Expression after substitution:", expr)
693
689
 
690
+ # Solve the equation
694
691
  var_symbol = symbols(variable)
695
-
696
692
  eq = Eq(expr, 0)
697
- print("Equation to solve:", eq)
698
-
699
693
  solutions = solve(eq, var_symbol)
700
694
 
701
- if solutions:
702
- latex_solutions = [latex(simplify(sol)) for sol in solutions]
703
- result = r"\left[" + ", ".join(latex_solutions) + r"\right]"
704
- else:
705
- result = r"\left[\right]"
695
+ # Convert solutions to LaTeX strings with handling for exact representations
696
+ latex_solutions = [latex(sol) for sol in solutions]
706
697
 
707
- print("704", result)
698
+ result = r"\left[" + ", ".join(latex_solutions) + r"\right]"
699
+ print("693", result)
708
700
  return result
709
701
 
710
702
  except Exception as e:
711
703
  raise ValueError(f"Error solving the expression: {e}")
712
-
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rgwfuncs
3
- Version: 0.0.48
3
+ Version: 0.0.51
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. `cancel_polynomial_expression`
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.
@@ -1,12 +1,12 @@
1
1
  rgwfuncs/__init__.py,sha256=vs8xv3IVx7KGpPxEXrIePD3gb_QI1XTXpD_n9817foA,1610
2
- rgwfuncs/algebra_lib.py,sha256=Y9DMuEMa3R2kgReHPdqlpAt80aorvMRR7LmtDqbQFSU,28360
2
+ rgwfuncs/algebra_lib.py,sha256=sLQUxXG4mNNO55XclTgIrwVrg5LbwELOFsiSAm_9G20,28212
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.48.dist-info/LICENSE,sha256=7EI8xVBu6h_7_JlVw-yPhhOZlpY9hP8wal7kHtqKT_E,1074
8
- rgwfuncs-0.0.48.dist-info/METADATA,sha256=e-1-ftsJikT2kND8zramQCjqc1BBzN3d1XYhm1Iawdk,55152
9
- rgwfuncs-0.0.48.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
10
- rgwfuncs-0.0.48.dist-info/entry_points.txt,sha256=j-c5IOPIQ0252EaOV6j6STio56sbXl2C4ym_fQ0lXx0,43
11
- rgwfuncs-0.0.48.dist-info/top_level.txt,sha256=aGuVIzWsKiV1f2gCb6mynx0zx5ma0B1EwPGFKVEMTi4,9
12
- rgwfuncs-0.0.48.dist-info/RECORD,,
7
+ rgwfuncs-0.0.51.dist-info/LICENSE,sha256=7EI8xVBu6h_7_JlVw-yPhhOZlpY9hP8wal7kHtqKT_E,1074
8
+ rgwfuncs-0.0.51.dist-info/METADATA,sha256=Q-t10jPxJYG_orABThvIy6q1-WQvwOYwe_3TNLuLYRw,55152
9
+ rgwfuncs-0.0.51.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
10
+ rgwfuncs-0.0.51.dist-info/entry_points.txt,sha256=j-c5IOPIQ0252EaOV6j6STio56sbXl2C4ym_fQ0lXx0,43
11
+ rgwfuncs-0.0.51.dist-info/top_level.txt,sha256=aGuVIzWsKiV1f2gCb6mynx0zx5ma0B1EwPGFKVEMTi4,9
12
+ rgwfuncs-0.0.51.dist-info/RECORD,,