rgwfuncs 0.0.27__tar.gz → 0.0.28__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rgwfuncs
3
- Version: 0.0.27
3
+ Version: 0.0.28
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
@@ -182,24 +182,38 @@ These examples illustrate the ability to handle basic arithmetic, the modulo ope
182
182
 
183
183
  ### 2. `simplify_algebraic_expression`
184
184
 
185
- Simplifies expressions and returns them in LaTeX format.
185
+ Simplifies expressions and returns them in LaTeX format. Optionally applies substitutions to variables within the expression before simplifying.
186
186
 
187
187
  - **Parameters:**
188
- - `expression` (str): A string of the expression to simplify.
188
+ - `expression` (str): A string representing the algebraic expression to simplify.
189
+ - `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where keys are variable names and values are the numbers to substitute them with.
189
190
 
190
191
  - **Returns:**
191
- - `str`: Simplified expression in LaTeX.
192
+ - `str`: The simplified expression formatted as a LaTeX string.
192
193
 
193
- - **Example:**
194
+ - **Example Usage:**
194
195
 
195
196
  from rgwfuncs import simplify_algebraic_expression
197
+
198
+ # Example 1: Simplifying a polynomial expression without substitutions
196
199
  simplified_expr1 = simplify_algebraic_expression("2*x + 3*x")
197
200
  print(simplified_expr1) # Output: "5 x"
198
201
 
199
- simplified_expr2 = simplify_algebraic_expression("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)")
200
- print(simplified_expr2) # Output: "\frac{1}{110 x^{22} y^{3}}"
202
+ # Example 2: Simplifying a complex expression involving derivatives
203
+ simplified_expr2 = simplify_algebraic_expression(
204
+ "(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)"
205
+ )
206
+ print(simplified_expr2) # Output: r"\frac{1}{110 x^{22} y^{3}}"
207
+
208
+ # Example 3: Simplifying with substitutions
209
+ simplified_expr3 = simplify_algebraic_expression("x**2 + y**2", subs={"x": 3, "y": 4})
210
+ print(simplified_expr3) # Output: "25"
211
+
212
+ # Example 4: Simplifying with partial substitution
213
+ simplified_expr4 = simplify_algebraic_expression("a*b + b", subs={"b": 2})
214
+ print(simplified_expr4) # Output: "a \cdot 2 + 2"
201
215
 
202
- These examples demonstrate simplification of polynomial expressions and more complex ratios involving derivatives.
216
+ These examples demonstrate the simplification of polynomial expressions, handling complex ratios involving derivatives, and applying variable substitutions before simplifying. The function handles expressions both with and without substitutions, providing flexibility in its usage.
203
217
 
204
218
  --------------------------------------------------------------------------------
205
219
 
@@ -156,24 +156,38 @@ These examples illustrate the ability to handle basic arithmetic, the modulo ope
156
156
 
157
157
  ### 2. `simplify_algebraic_expression`
158
158
 
159
- Simplifies expressions and returns them in LaTeX format.
159
+ Simplifies expressions and returns them in LaTeX format. Optionally applies substitutions to variables within the expression before simplifying.
160
160
 
161
161
  - **Parameters:**
162
- - `expression` (str): A string of the expression to simplify.
162
+ - `expression` (str): A string representing the algebraic expression to simplify.
163
+ - `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where keys are variable names and values are the numbers to substitute them with.
163
164
 
164
165
  - **Returns:**
165
- - `str`: Simplified expression in LaTeX.
166
+ - `str`: The simplified expression formatted as a LaTeX string.
166
167
 
167
- - **Example:**
168
+ - **Example Usage:**
168
169
 
169
170
  from rgwfuncs import simplify_algebraic_expression
171
+
172
+ # Example 1: Simplifying a polynomial expression without substitutions
170
173
  simplified_expr1 = simplify_algebraic_expression("2*x + 3*x")
171
174
  print(simplified_expr1) # Output: "5 x"
172
175
 
173
- simplified_expr2 = simplify_algebraic_expression("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)")
174
- print(simplified_expr2) # Output: "\frac{1}{110 x^{22} y^{3}}"
176
+ # Example 2: Simplifying a complex expression involving derivatives
177
+ simplified_expr2 = simplify_algebraic_expression(
178
+ "(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)"
179
+ )
180
+ print(simplified_expr2) # Output: r"\frac{1}{110 x^{22} y^{3}}"
181
+
182
+ # Example 3: Simplifying with substitutions
183
+ simplified_expr3 = simplify_algebraic_expression("x**2 + y**2", subs={"x": 3, "y": 4})
184
+ print(simplified_expr3) # Output: "25"
185
+
186
+ # Example 4: Simplifying with partial substitution
187
+ simplified_expr4 = simplify_algebraic_expression("a*b + b", subs={"b": 2})
188
+ print(simplified_expr4) # Output: "a \cdot 2 + 2"
175
189
 
176
- These examples demonstrate simplification of polynomial expressions and more complex ratios involving derivatives.
190
+ These examples demonstrate the simplification of polynomial expressions, handling complex ratios involving derivatives, and applying variable substitutions before simplifying. The function handles expressions both with and without substitutions, providing flexibility in its usage.
177
191
 
178
192
  --------------------------------------------------------------------------------
179
193
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "rgwfuncs"
7
- version = "0.0.27"
7
+ version = "0.0.28"
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.27
3
+ version = 0.0.28
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
@@ -36,7 +36,10 @@ def compute_algebraic_expression(expression: str) -> float:
36
36
  raise ValueError(f"Error computing expression: {e}")
37
37
 
38
38
 
39
- def simplify_algebraic_expression(expression: str) -> str:
39
+ def simplify_algebraic_expression(
40
+ expression: str,
41
+ subs: Optional[Dict[str, float]] = None
42
+ ) -> str:
40
43
  """
41
44
  Simplifies an algebraic expression and returns it in LaTeX format.
42
45
 
@@ -45,11 +48,17 @@ def simplify_algebraic_expression(expression: str) -> str:
45
48
 
46
49
  Parameters:
47
50
  expression (str): The algebraic expression to simplify.
51
+ subs (Optional[Dict[str, float]]): An optional dictionary of substitutions for variables
52
+ in the expression.
48
53
 
49
54
  Returns:
50
55
  str: The simplified expression represented as a LaTeX string.
56
+
57
+ Raises:
58
+ ValueError: If the expression cannot be simplified due to errors in expression or parameters.
51
59
  """
52
60
 
61
+
53
62
  def recursive_parse_function_call(
54
63
  func_call: str, prefix: str, sym_vars: Dict[str, Expr]) -> Tuple[str, List[Expr]]:
55
64
  # print(f"Parsing function call: {func_call}")
@@ -178,13 +187,18 @@ def simplify_algebraic_expression(expression: str) -> str:
178
187
  # print("Level 2 processed_expression:", processed_expression)
179
188
 
180
189
  try:
181
- if processed_expression.startswith(
182
- '[') and processed_expression.endswith(']'):
183
- return processed_expression
184
-
190
+ # Parse the expression
185
191
  expr = parse_expr(processed_expression, local_dict=sym_vars)
192
+
193
+ # Apply substitutions if provided
194
+ if subs:
195
+ subs_symbols = {symbols(k): v for k, v in subs.items()}
196
+ expr = expr.subs(subs_symbols)
197
+
198
+ # Simplify the expression
186
199
  final_result = simplify(expr)
187
200
 
201
+ # Convert the result to LaTeX format
188
202
  if final_result.free_symbols:
189
203
  latex_result = latex(final_result)
190
204
  return latex_result
@@ -196,7 +210,10 @@ def simplify_algebraic_expression(expression: str) -> str:
196
210
 
197
211
 
198
212
  def solve_algebraic_expression(
199
- expression: str, variable: str, subs: Optional[Dict[str, float]] = None) -> str:
213
+ expression: str,
214
+ variable: str,
215
+ subs: Optional[Dict[str, float]] = None
216
+ ) -> str:
200
217
  """
201
218
  Solves an algebraic equation for a specified variable and returns solutions in LaTeX format.
202
219
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rgwfuncs
3
- Version: 0.0.27
3
+ Version: 0.0.28
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
@@ -182,24 +182,38 @@ These examples illustrate the ability to handle basic arithmetic, the modulo ope
182
182
 
183
183
  ### 2. `simplify_algebraic_expression`
184
184
 
185
- Simplifies expressions and returns them in LaTeX format.
185
+ Simplifies expressions and returns them in LaTeX format. Optionally applies substitutions to variables within the expression before simplifying.
186
186
 
187
187
  - **Parameters:**
188
- - `expression` (str): A string of the expression to simplify.
188
+ - `expression` (str): A string representing the algebraic expression to simplify.
189
+ - `subs` (Optional[Dict[str, float]]): An optional dictionary of substitutions where keys are variable names and values are the numbers to substitute them with.
189
190
 
190
191
  - **Returns:**
191
- - `str`: Simplified expression in LaTeX.
192
+ - `str`: The simplified expression formatted as a LaTeX string.
192
193
 
193
- - **Example:**
194
+ - **Example Usage:**
194
195
 
195
196
  from rgwfuncs import simplify_algebraic_expression
197
+
198
+ # Example 1: Simplifying a polynomial expression without substitutions
196
199
  simplified_expr1 = simplify_algebraic_expression("2*x + 3*x")
197
200
  print(simplified_expr1) # Output: "5 x"
198
201
 
199
- simplified_expr2 = simplify_algebraic_expression("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)")
200
- print(simplified_expr2) # Output: "\frac{1}{110 x^{22} y^{3}}"
202
+ # Example 2: Simplifying a complex expression involving derivatives
203
+ simplified_expr2 = simplify_algebraic_expression(
204
+ "(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)"
205
+ )
206
+ print(simplified_expr2) # Output: r"\frac{1}{110 x^{22} y^{3}}"
207
+
208
+ # Example 3: Simplifying with substitutions
209
+ simplified_expr3 = simplify_algebraic_expression("x**2 + y**2", subs={"x": 3, "y": 4})
210
+ print(simplified_expr3) # Output: "25"
211
+
212
+ # Example 4: Simplifying with partial substitution
213
+ simplified_expr4 = simplify_algebraic_expression("a*b + b", subs={"b": 2})
214
+ print(simplified_expr4) # Output: "a \cdot 2 + 2"
201
215
 
202
- These examples demonstrate simplification of polynomial expressions and more complex ratios involving derivatives.
216
+ These examples demonstrate the simplification of polynomial expressions, handling complex ratios involving derivatives, and applying variable substitutions before simplifying. The function handles expressions both with and without substitutions, providing flexibility in its usage.
203
217
 
204
218
  --------------------------------------------------------------------------------
205
219
 
@@ -30,14 +30,23 @@ def test_compute_algebraic_expression():
30
30
  result = compute_algebraic_expression(input_data)
31
31
  assert math.isclose(result, expected_output, rel_tol=1e-9), f"Failed for {input_data}, got {result}"
32
32
 
33
-
34
33
  def test_simplify_algebraic_expression():
35
34
  test_cases = [
36
- ("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)", r"\frac{1}{110 x^{22} y^{3}}"),
35
+ # Without substitutions
36
+ (("(np.diff(3*x**8)) / (np.diff(8*x**30) * 11*y**3)", None), r"\frac{1}{110 x^{22} y^{3}}"),
37
+
38
+ # With substitutions
39
+ (("x**2 + y**2", {"x": 3, "y": 4}), "25"),
40
+ (("a*b + b", {"b": 2}), r"2 a + 2"), # Assumes no simplification of `a*b`
41
+ (("(x**2 + y**2 + z**2)", {"x": 1, "y": 0, "z": 0}), "1")
37
42
  ]
38
43
 
39
- for input_data, expected_output in test_cases:
40
- assert simplify_algebraic_expression(input_data) == expected_output
44
+ for (expression, subs), expected_output in test_cases:
45
+ output = simplify_algebraic_expression(expression, subs)
46
+ assert output == expected_output, (
47
+ f"Test failed for expression: {expression} with substitutions: {subs}. "
48
+ f"Expected {expected_output}, got {output}"
49
+ )
41
50
 
42
51
 
43
52
  def test_solve_algebraic_expression():
@@ -59,13 +68,10 @@ def test_compute_matrix_operation():
59
68
  ("[[10, 10, 10],[2, 4, 6]] - [[5, 3, 2],[1, 2, 1]]", r"\begin{bmatrix}5 & 7 & 8\\1 & 2 & 5\end{bmatrix}"),
60
69
  ("[[2, 4],[6, 8]] * [[1, 0.5],[2, 0.25]]", r"\begin{bmatrix}2 & 2.0\\12 & 2.0\end{bmatrix}"),
61
70
  ("[[8, 16],[32, 64]] / [[2, 2],[8, 16]]", r"\begin{bmatrix}4.0 & 8.0\\4.0 & 4.0\end{bmatrix}"),
62
-
63
71
  ("[[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
72
  ("[2, 6, 9] + [1, 2, 3] - [1, 1, 1]", r"\begin{bmatrix}2 & 7 & 11\end{bmatrix}"),
65
73
  ("[[1, 2], [3, 4]] + [[2, 3], [4, 5]] + [[1, 1], [1, 1]]", r"\begin{bmatrix}4 & 6\\8 & 10\end{bmatrix}"),
66
74
  ("[3, 6, 9] - [1, 2, 3] + [5, 5, 5]", r"\begin{bmatrix}7 & 9 & 11\end{bmatrix}"),
67
-
68
-
69
75
  ("[3, 6, 9] - [1, 2, 3, 4]", r"Operations between matrices must involve matrices of the same dimension"),
70
76
 
71
77
  # Edge cases
@@ -88,6 +94,7 @@ def test_compute_ordered_series_operations():
88
94
  ("[8, 16, 32] / [2, 2, 8]", "[4.0, 8.0, 4.0]"),
89
95
  ("ddd([2, 6, 9, 60]) + ddd([78, 79, 80])", "Operations between ordered series must involve series of equal length"),
90
96
  ("ddd([1, 3, 6, 10]) - ddd([0, 1, 1, 2])", "[1, 3, 3]"),
97
+
91
98
  # Edge cases
92
99
  ("ddd([1])", "[]"), # Single-element list, becomes empty
93
100
  ("ddd([])", "[]"), # Empty list case
File without changes