mathjson-solver 1.16.0__tar.gz → 1.17.0__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.
- {mathjson_solver-1.16.0/src/mathjson_solver.egg-info → mathjson_solver-1.17.0}/PKG-INFO +21 -9
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/README.md +20 -7
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/setup.cfg +2 -2
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/setup.py +2 -2
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/src/mathjson_solver/__main__.py +105 -12
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0/src/mathjson_solver.egg-info}/PKG-INFO +21 -9
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/src/mathjson_solver.egg-info/SOURCES.txt +8 -2
- mathjson_solver-1.16.0/tests/test_CumulativeProduct.py → mathjson_solver-1.17.0/tests/test_cumulative.py +11 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_equal.py +5 -0
- mathjson_solver-1.17.0/tests/test_gail_model_nci_validated_no_beta.py +619 -0
- mathjson_solver-1.17.0/tests/test_gail_model_nci_validated_with_beta.py +618 -0
- mathjson_solver-1.17.0/tests/test_nci_discrete_implementation_no_beta.py +480 -0
- mathjson_solver-1.17.0/tests/test_nci_discrete_implementation_with_beta.py +503 -0
- mathjson_solver-1.17.0/tests/test_reduce_simple.py +209 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_simple_cases.py +6 -0
- mathjson_solver-1.17.0/tests/test_state_tuples_reduce.py +152 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/LICENSE +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/pyproject.toml +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/src/mathjson_solver/__init__.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/src/mathjson_solver.egg-info/dependency_links.txt +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/src/mathjson_solver.egg-info/top_level.txt +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_TrapezoidalIntegratel.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_atindex.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_exceptions.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_extract_variables.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_findinterval.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_gail_model.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_interpolate.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_range.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_scalars.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_sets.py +0 -0
- /mathjson_solver-1.16.0/tests/test_Slice.py → /mathjson_solver-1.17.0/tests/test_slice.py +0 -0
- {mathjson_solver-1.16.0 → mathjson_solver-1.17.0}/tests/test_with_pytest.py +0 -0
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mathjson-solver
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.17.0
|
|
4
4
|
Summary: Utilities for MathJSON evaluation
|
|
5
5
|
Home-page: https://github.com/LongenesisLtd/mathjson-solver
|
|
6
6
|
Author: Martins Mednis
|
|
7
7
|
Author-email: mrt@mednis.info
|
|
8
8
|
Project-URL: Bug Tracker, https://github.com/LongenesisLtd/mathjson-solver/issues
|
|
9
9
|
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
11
10
|
Classifier: Operating System :: OS Independent
|
|
12
11
|
Requires-Python: >=3.6
|
|
13
12
|
Description-Content-Type: text/markdown
|
|
@@ -33,8 +32,6 @@ A reliable Python library for numerically evaluating mathematical expressions in
|
|
|
33
32
|
|
|
34
33
|
**What is MathJSON?** MathJSON represents mathematical expressions as JSON arrays, like `["Add", 1, 2, 3]` for 1+2+3. This format is safe, structured, and easy to generate programmatically.
|
|
35
34
|
|
|
36
|
-
Inspired by [CortexJS Compute Engine](https://cortexjs.io/compute-engine/) though designed as an independent implementation focused on our specific use cases.
|
|
37
|
-
|
|
38
35
|
## Table of Contents
|
|
39
36
|
- [Installation](#installation)
|
|
40
37
|
- [Quick Start](#quick-start)
|
|
@@ -80,21 +77,32 @@ except MathJSONException as e:
|
|
|
80
77
|
# Math error: Problem in Divide. ['Divide', 1, 0]. division by zero
|
|
81
78
|
```
|
|
82
79
|
|
|
80
|
+
**Think Functional:** MathJSON Solver embraces functional programming principles. Instead of writing loops and modifying variables, you compose expressions that transform data. Functions like `Map`, `Reduce`, and `Filter` let you process arrays elegantly, while immutable operations ensure predictable, side-effect-free calculations. Don't worry if you're new to functional programming — the examples will guide you naturally into this powerful paradigm.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
# Functional approach: transform data with expressions
|
|
84
|
+
solver(["Map", ["Array", 1, 2, 3, 4], ["Multiply"], 2]) # [2, 4, 6, 8]
|
|
85
|
+
solver(["Reduce", ["Array", 1, 2, 3, 4], 0, ["Add", "acc", "item"],
|
|
86
|
+
["Variable", "acc"], ["Variable", "item"], ["Variable", "i"]]) # 10
|
|
87
|
+
```
|
|
88
|
+
|
|
83
89
|
## Supported Operations
|
|
84
90
|
|
|
85
91
|
The library supports a comprehensive set of mathematical operations:
|
|
86
92
|
|
|
87
|
-
* **Arithmetic:** Add, Sum, Subtract, Multiply, Divide, Negate, Power, Square, Root, Sqrt, Abs, Round
|
|
93
|
+
* **Arithmetic:** Add, Sum, Subtract, Multiply, Divide, Negate, Power, Square, Root, Sqrt, Abs, Round, Floor, Ceil
|
|
88
94
|
* **Trigonometry:** Sin, Cos, Tan, Arcsin, Arccos, Arctan
|
|
89
95
|
* **Logarithms:** Log, Log2, Log10, Exp
|
|
90
96
|
* **Comparison:** Equal, StrictEqual, NotEqual, Greater, GreaterEqual, Less, LessEqual
|
|
91
|
-
* **Logic & Sets:** Any, All, Not, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
97
|
+
* **Logic & Sets:** Any, All, Not, And, Or, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
92
98
|
* **Statistics:** Average, Max, Min, Median, Length
|
|
93
|
-
* **
|
|
99
|
+
* **Functional Programming:** Map, Reduce, Filter
|
|
100
|
+
* **Arrays:** Array creation, GenerateRange, AtIndex, Slice, Appended, CumulativeSum, CumulativeProduct
|
|
94
101
|
* **Control Flow:** If statements, Switch-Case, Constants definition
|
|
95
102
|
* **Type Conversion:** Int, Float, Str, IsDefined
|
|
96
|
-
* **Date/Time:** Strptime, Strftime, Today, Now, TimeDelta functions
|
|
97
|
-
* **
|
|
103
|
+
* **Date/Time:** Strptime, Strftime, Today, Now, TimeDelta functions (Weeks, Days, Hours, Minutes)
|
|
104
|
+
* **Integration:** TrapezoidalIntegrate (requires numpy), Interp, FindIntervalIndex, Variable references
|
|
105
|
+
* **Advanced:** HasMatchingSublist for pattern matching
|
|
98
106
|
* **Constants:** Pi
|
|
99
107
|
|
|
100
108
|
[View complete documentation with examples →](https://github.com/LongenesisLtd/mathjson-solver/blob/main/docs/README.md)
|
|
@@ -161,6 +169,10 @@ We welcome contributions! Please feel free to:
|
|
|
161
169
|
|
|
162
170
|
[View license information](https://github.com/LongenesisLtd/mathjson-solver/blob/main/LICENSE)
|
|
163
171
|
|
|
172
|
+
## References
|
|
173
|
+
|
|
174
|
+
This implementation was inspired by the [CortexJS Compute Engine](https://cortexjs.io/compute-engine/), though designed as an independent implementation focused on our specific use cases.
|
|
175
|
+
|
|
164
176
|
---
|
|
165
177
|
|
|
166
178
|
Made with ❤️ by [Longenesis](https://longenesis.com/team)
|
|
@@ -8,8 +8,6 @@ A reliable Python library for numerically evaluating mathematical expressions in
|
|
|
8
8
|
|
|
9
9
|
**What is MathJSON?** MathJSON represents mathematical expressions as JSON arrays, like `["Add", 1, 2, 3]` for 1+2+3. This format is safe, structured, and easy to generate programmatically.
|
|
10
10
|
|
|
11
|
-
Inspired by [CortexJS Compute Engine](https://cortexjs.io/compute-engine/) though designed as an independent implementation focused on our specific use cases.
|
|
12
|
-
|
|
13
11
|
## Table of Contents
|
|
14
12
|
- [Installation](#installation)
|
|
15
13
|
- [Quick Start](#quick-start)
|
|
@@ -55,21 +53,32 @@ except MathJSONException as e:
|
|
|
55
53
|
# Math error: Problem in Divide. ['Divide', 1, 0]. division by zero
|
|
56
54
|
```
|
|
57
55
|
|
|
56
|
+
**Think Functional:** MathJSON Solver embraces functional programming principles. Instead of writing loops and modifying variables, you compose expressions that transform data. Functions like `Map`, `Reduce`, and `Filter` let you process arrays elegantly, while immutable operations ensure predictable, side-effect-free calculations. Don't worry if you're new to functional programming — the examples will guide you naturally into this powerful paradigm.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
# Functional approach: transform data with expressions
|
|
60
|
+
solver(["Map", ["Array", 1, 2, 3, 4], ["Multiply"], 2]) # [2, 4, 6, 8]
|
|
61
|
+
solver(["Reduce", ["Array", 1, 2, 3, 4], 0, ["Add", "acc", "item"],
|
|
62
|
+
["Variable", "acc"], ["Variable", "item"], ["Variable", "i"]]) # 10
|
|
63
|
+
```
|
|
64
|
+
|
|
58
65
|
## Supported Operations
|
|
59
66
|
|
|
60
67
|
The library supports a comprehensive set of mathematical operations:
|
|
61
68
|
|
|
62
|
-
* **Arithmetic:** Add, Sum, Subtract, Multiply, Divide, Negate, Power, Square, Root, Sqrt, Abs, Round
|
|
69
|
+
* **Arithmetic:** Add, Sum, Subtract, Multiply, Divide, Negate, Power, Square, Root, Sqrt, Abs, Round, Floor, Ceil
|
|
63
70
|
* **Trigonometry:** Sin, Cos, Tan, Arcsin, Arccos, Arctan
|
|
64
71
|
* **Logarithms:** Log, Log2, Log10, Exp
|
|
65
72
|
* **Comparison:** Equal, StrictEqual, NotEqual, Greater, GreaterEqual, Less, LessEqual
|
|
66
|
-
* **Logic & Sets:** Any, All, Not, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
73
|
+
* **Logic & Sets:** Any, All, Not, And, Or, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
67
74
|
* **Statistics:** Average, Max, Min, Median, Length
|
|
68
|
-
* **
|
|
75
|
+
* **Functional Programming:** Map, Reduce, Filter
|
|
76
|
+
* **Arrays:** Array creation, GenerateRange, AtIndex, Slice, Appended, CumulativeSum, CumulativeProduct
|
|
69
77
|
* **Control Flow:** If statements, Switch-Case, Constants definition
|
|
70
78
|
* **Type Conversion:** Int, Float, Str, IsDefined
|
|
71
|
-
* **Date/Time:** Strptime, Strftime, Today, Now, TimeDelta functions
|
|
72
|
-
* **
|
|
79
|
+
* **Date/Time:** Strptime, Strftime, Today, Now, TimeDelta functions (Weeks, Days, Hours, Minutes)
|
|
80
|
+
* **Integration:** TrapezoidalIntegrate (requires numpy), Interp, FindIntervalIndex, Variable references
|
|
81
|
+
* **Advanced:** HasMatchingSublist for pattern matching
|
|
73
82
|
* **Constants:** Pi
|
|
74
83
|
|
|
75
84
|
[View complete documentation with examples →](https://github.com/LongenesisLtd/mathjson-solver/blob/main/docs/README.md)
|
|
@@ -136,6 +145,10 @@ We welcome contributions! Please feel free to:
|
|
|
136
145
|
|
|
137
146
|
[View license information](https://github.com/LongenesisLtd/mathjson-solver/blob/main/LICENSE)
|
|
138
147
|
|
|
148
|
+
## References
|
|
149
|
+
|
|
150
|
+
This implementation was inspired by the [CortexJS Compute Engine](https://cortexjs.io/compute-engine/), though designed as an independent implementation focused on our specific use cases.
|
|
151
|
+
|
|
139
152
|
---
|
|
140
153
|
|
|
141
154
|
Made with ❤️ by [Longenesis](https://longenesis.com/team)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[metadata]
|
|
2
2
|
name = mathjson-solver
|
|
3
|
-
version = 1.
|
|
3
|
+
version = 1.17.0
|
|
4
4
|
author = Martins Mednis
|
|
5
5
|
author_email = mrt@mednis.info
|
|
6
6
|
description = Utilities for MathJSON evaluation
|
|
@@ -11,8 +11,8 @@ project_urls =
|
|
|
11
11
|
Bug Tracker = https://github.com/LongenesisLtd/mathjson-solver/issues
|
|
12
12
|
classifiers =
|
|
13
13
|
Programming Language :: Python :: 3
|
|
14
|
-
License :: OSI Approved :: MIT License
|
|
15
14
|
Operating System :: OS Independent
|
|
15
|
+
license_expr = MIT
|
|
16
16
|
|
|
17
17
|
[options]
|
|
18
18
|
package_dir =
|
|
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
|
|
5
5
|
|
|
6
6
|
setuptools.setup(
|
|
7
7
|
name="mathjson-solver",
|
|
8
|
-
version="1.
|
|
8
|
+
version="1.17.0",
|
|
9
9
|
author="Martins Mednis",
|
|
10
10
|
author_email="mrt@mednis.info",
|
|
11
11
|
description="Utilities for MathJSON evaluation",
|
|
@@ -15,9 +15,9 @@ setuptools.setup(
|
|
|
15
15
|
project_urls={
|
|
16
16
|
"Bug Tracker": "https://github.com/LongenesisLtd/mathjson-solver/issues",
|
|
17
17
|
},
|
|
18
|
+
license_expr="MIT",
|
|
18
19
|
classifiers=[
|
|
19
20
|
"Programming Language :: Python :: 3",
|
|
20
|
-
"License :: OSI Approved :: MIT License",
|
|
21
21
|
"Operating System :: OS Independent",
|
|
22
22
|
],
|
|
23
23
|
package_dir={"": "src"},
|
|
@@ -4,8 +4,6 @@ from functools import reduce
|
|
|
4
4
|
import math
|
|
5
5
|
from copy import deepcopy
|
|
6
6
|
from statistics import median
|
|
7
|
-
import logging
|
|
8
|
-
import traceback
|
|
9
7
|
import datetime
|
|
10
8
|
|
|
11
9
|
NUMPY_AVAILABLE = False
|
|
@@ -72,6 +70,16 @@ def _CumulativeProduct(l: list) -> list:
|
|
|
72
70
|
return res
|
|
73
71
|
|
|
74
72
|
|
|
73
|
+
def _CumulativeSum(l: list) -> list:
|
|
74
|
+
res = []
|
|
75
|
+
for i, x in enumerate(l):
|
|
76
|
+
if i == 0:
|
|
77
|
+
res.append(x)
|
|
78
|
+
else:
|
|
79
|
+
res.append(reduce(lambda a, b: a + b, l[: i + 1]))
|
|
80
|
+
return res
|
|
81
|
+
|
|
82
|
+
|
|
75
83
|
def find_interpolation_bounds_indexes(
|
|
76
84
|
l: list, target: int | float
|
|
77
85
|
) -> Union[Union[int, float], tuple[Union[int, float], Union[int, float]]]:
|
|
@@ -344,7 +352,6 @@ def create_mathjson_solver(solver_parameters):
|
|
|
344
352
|
s_ = [float(f(x, c)) for x in f(s[1], c) if is_numeric(f(x, c))]
|
|
345
353
|
else:
|
|
346
354
|
s_ = [float(f(x, c)) for x in s[1][1:] if is_numeric(f(x, c))]
|
|
347
|
-
# print(f"{s_} {sum(s_)}/{len(s_)}")
|
|
348
355
|
try:
|
|
349
356
|
return sum(s_) / len(s_)
|
|
350
357
|
except ZeroDivisionError:
|
|
@@ -377,6 +384,12 @@ def create_mathjson_solver(solver_parameters):
|
|
|
377
384
|
def Float(s):
|
|
378
385
|
return float(f(s[1], c))
|
|
379
386
|
|
|
387
|
+
def Floor(s):
|
|
388
|
+
return math.floor(f(s[1], c))
|
|
389
|
+
|
|
390
|
+
def Ceil(s):
|
|
391
|
+
return math.ceil(f(s[1], c))
|
|
392
|
+
|
|
380
393
|
def Constants(s):
|
|
381
394
|
for x in s[1:-1]:
|
|
382
395
|
c[x[0]] = f(x[1], c)
|
|
@@ -419,12 +432,9 @@ def create_mathjson_solver(solver_parameters):
|
|
|
419
432
|
try:
|
|
420
433
|
return f(x[1], c)
|
|
421
434
|
except MathJSONException:
|
|
422
|
-
|
|
423
|
-
"MathJSONException: %s", traceback.format_exc()
|
|
424
|
-
)
|
|
435
|
+
# Branch failed, try next condition
|
|
425
436
|
continue
|
|
426
437
|
except MathJSONException:
|
|
427
|
-
logging.error("MathJSONException: %s", traceback.format_exc())
|
|
428
438
|
return f(s[-1], c) # return default value (else)
|
|
429
439
|
|
|
430
440
|
return f(s[-1], c)
|
|
@@ -635,6 +645,26 @@ def create_mathjson_solver(solver_parameters):
|
|
|
635
645
|
except TypeError:
|
|
636
646
|
return False
|
|
637
647
|
|
|
648
|
+
def BooleanAnd(s):
|
|
649
|
+
"""
|
|
650
|
+
Boolean AND operation.
|
|
651
|
+
["And", condition1, condition2, ...]
|
|
652
|
+
"""
|
|
653
|
+
for x in s[1:]:
|
|
654
|
+
if not f(x, c):
|
|
655
|
+
return False
|
|
656
|
+
return True
|
|
657
|
+
|
|
658
|
+
def BooleanOr(s):
|
|
659
|
+
"""
|
|
660
|
+
Boolean OR operation.
|
|
661
|
+
["Or", condition1, condition2, ...]
|
|
662
|
+
"""
|
|
663
|
+
for x in s[1:]:
|
|
664
|
+
if f(x, c):
|
|
665
|
+
return True
|
|
666
|
+
return False
|
|
667
|
+
|
|
638
668
|
# sin, cos, tan, arcsin, arccos, arctan
|
|
639
669
|
def Sin(s):
|
|
640
670
|
return math.sin(f(s[1], c))
|
|
@@ -846,6 +876,18 @@ def create_mathjson_solver(solver_parameters):
|
|
|
846
876
|
return ["Array"] + _CumulativeProduct(array)
|
|
847
877
|
# return _CumulativeProduct(array)
|
|
848
878
|
|
|
879
|
+
def CumulativeSum(s):
|
|
880
|
+
"""
|
|
881
|
+
["CumulativeSum", array]
|
|
882
|
+
The `array` must be an array of numeric values.
|
|
883
|
+
"""
|
|
884
|
+
array = f(s[1], c)
|
|
885
|
+
if not (isinstance(array, list) and array[0] == "Array"):
|
|
886
|
+
raise ValueError("Parameter 1 must be an array.")
|
|
887
|
+
array = [f(x, c) for x in array[1:]]
|
|
888
|
+
return ["Array"] + _CumulativeSum(array)
|
|
889
|
+
# return
|
|
890
|
+
|
|
849
891
|
def Interp(s):
|
|
850
892
|
"""
|
|
851
893
|
["Interp", x_array, y_array, target_x]
|
|
@@ -902,11 +944,53 @@ def create_mathjson_solver(solver_parameters):
|
|
|
902
944
|
c[variable_name] = variable_value
|
|
903
945
|
values.append(f(function_expression, c))
|
|
904
946
|
h = (end - start) / n
|
|
905
|
-
# print("values=", values)
|
|
906
947
|
return h * (0.5 * values[0] + np.sum(values[1:-1]) + 0.5 * values[-1])
|
|
907
948
|
|
|
908
949
|
# return total_area
|
|
909
950
|
|
|
951
|
+
def Reduce(s):
|
|
952
|
+
"""
|
|
953
|
+
["Reduce", list, initial_value, function, str_name_of_accumulator, str_name_of_current, str_name_of_index]
|
|
954
|
+
"""
|
|
955
|
+
the_list = f(s[1], c)[1:]
|
|
956
|
+
initial_value = f(s[2], c)
|
|
957
|
+
function_expression = s[3]
|
|
958
|
+
|
|
959
|
+
_accumulator = s[4]
|
|
960
|
+
name_accumulator = _accumulator[1]
|
|
961
|
+
|
|
962
|
+
_current = s[5]
|
|
963
|
+
name_current = _current[1]
|
|
964
|
+
|
|
965
|
+
_index = s[6]
|
|
966
|
+
name_index = _index[1]
|
|
967
|
+
|
|
968
|
+
c[name_accumulator] = initial_value
|
|
969
|
+
|
|
970
|
+
for i, x in enumerate(the_list):
|
|
971
|
+
c[name_current] = x
|
|
972
|
+
c[name_index] = i
|
|
973
|
+
c[name_accumulator] = f(function_expression, c)
|
|
974
|
+
|
|
975
|
+
return c[name_accumulator]
|
|
976
|
+
|
|
977
|
+
def Appended(s):
|
|
978
|
+
"""
|
|
979
|
+
["Appended", array, value]
|
|
980
|
+
The `array` must be an array of values.
|
|
981
|
+
The `value` is the value to append to the array.
|
|
982
|
+
"""
|
|
983
|
+
array = f(s[1], c)
|
|
984
|
+
|
|
985
|
+
if not (isinstance(array, list) and array[0] == "Array"):
|
|
986
|
+
raise ValueError("Parameter 1 must be an array.")
|
|
987
|
+
|
|
988
|
+
value = f(s[2], c)
|
|
989
|
+
|
|
990
|
+
array = [x for x in array[1:]]
|
|
991
|
+
array.append(value)
|
|
992
|
+
return ["Array"] + array
|
|
993
|
+
|
|
910
994
|
constructs = {
|
|
911
995
|
"Sum": Sum,
|
|
912
996
|
"Add": Add,
|
|
@@ -946,6 +1030,8 @@ def create_mathjson_solver(solver_parameters):
|
|
|
946
1030
|
# "NotEqual": lambda s: f(s[1], c) != f(s[2], c),
|
|
947
1031
|
"NotEqual": lambda s: comparison_safe_converter(f(s[1], c))
|
|
948
1032
|
!= comparison_safe_converter(f(s[2], c)),
|
|
1033
|
+
"And": BooleanAnd,
|
|
1034
|
+
"Or": BooleanOr,
|
|
949
1035
|
"Abs": lambda s: abs(f(s[1], c)),
|
|
950
1036
|
"Round": lambda s: (
|
|
951
1037
|
round(f(s[1], c), f(s[2], c))
|
|
@@ -971,6 +1057,8 @@ def create_mathjson_solver(solver_parameters):
|
|
|
971
1057
|
"ContainsNoneOf": Contains_none_of,
|
|
972
1058
|
"Int": Int,
|
|
973
1059
|
"Float": Float,
|
|
1060
|
+
"Floor": Floor,
|
|
1061
|
+
"Ceil": Ceil,
|
|
974
1062
|
"Str": Str,
|
|
975
1063
|
"Not": Not,
|
|
976
1064
|
# "IsDefined": lambda s: s[1] in c,
|
|
@@ -999,9 +1087,12 @@ def create_mathjson_solver(solver_parameters):
|
|
|
999
1087
|
"AtIndex": AtIndex,
|
|
1000
1088
|
"Slice": Slice,
|
|
1001
1089
|
"CumulativeProduct": CumulativeProduct,
|
|
1090
|
+
"CumulativeSum": CumulativeSum,
|
|
1002
1091
|
"Interp": Interp,
|
|
1003
1092
|
"FindIntervalIndex": FindIntervalIndex,
|
|
1004
1093
|
"TrapezoidalIntegrate": TrapezoidalIntegrate,
|
|
1094
|
+
"Reduce": Reduce,
|
|
1095
|
+
"Appended": Appended,
|
|
1005
1096
|
"Sin": Sin,
|
|
1006
1097
|
"Cos": Cos,
|
|
1007
1098
|
"Tan": Tan,
|
|
@@ -1021,13 +1112,13 @@ def create_mathjson_solver(solver_parameters):
|
|
|
1021
1112
|
# return s[0]
|
|
1022
1113
|
# except Exception as e:
|
|
1023
1114
|
except TypeError as e:
|
|
1024
|
-
raise MathJSONException(e, s, mathjson_construct=s[0])
|
|
1115
|
+
raise MathJSONException(e, s, mathjson_construct=s[0]) from e
|
|
1025
1116
|
except ValueError as e:
|
|
1026
|
-
raise MathJSONException(e, s, mathjson_construct=s[0])
|
|
1117
|
+
raise MathJSONException(e, s, mathjson_construct=s[0]) from e
|
|
1027
1118
|
except IndexError as e:
|
|
1028
|
-
raise MathJSONException(e, s, mathjson_construct=s[0])
|
|
1119
|
+
raise MathJSONException(e, s, mathjson_construct=s[0]) from e
|
|
1029
1120
|
except ZeroDivisionError as e:
|
|
1030
|
-
raise MathJSONException(e, s, mathjson_construct=s[0])
|
|
1121
|
+
raise MathJSONException(e, s, mathjson_construct=s[0]) from e
|
|
1031
1122
|
else:
|
|
1032
1123
|
# raise MathJSONException(
|
|
1033
1124
|
# NotImplementedError(f"'{s[0]}' is not supported"), s
|
|
@@ -1073,6 +1164,8 @@ def extract_variables(s: Union[list, int, float, str], li: set, ignore_list: set
|
|
|
1073
1164
|
"Less",
|
|
1074
1165
|
"LessEqual",
|
|
1075
1166
|
"NotEqual",
|
|
1167
|
+
"And",
|
|
1168
|
+
"Or",
|
|
1076
1169
|
"Abs",
|
|
1077
1170
|
"Round",
|
|
1078
1171
|
"Max",
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mathjson-solver
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.17.0
|
|
4
4
|
Summary: Utilities for MathJSON evaluation
|
|
5
5
|
Home-page: https://github.com/LongenesisLtd/mathjson-solver
|
|
6
6
|
Author: Martins Mednis
|
|
7
7
|
Author-email: mrt@mednis.info
|
|
8
8
|
Project-URL: Bug Tracker, https://github.com/LongenesisLtd/mathjson-solver/issues
|
|
9
9
|
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
11
10
|
Classifier: Operating System :: OS Independent
|
|
12
11
|
Requires-Python: >=3.6
|
|
13
12
|
Description-Content-Type: text/markdown
|
|
@@ -33,8 +32,6 @@ A reliable Python library for numerically evaluating mathematical expressions in
|
|
|
33
32
|
|
|
34
33
|
**What is MathJSON?** MathJSON represents mathematical expressions as JSON arrays, like `["Add", 1, 2, 3]` for 1+2+3. This format is safe, structured, and easy to generate programmatically.
|
|
35
34
|
|
|
36
|
-
Inspired by [CortexJS Compute Engine](https://cortexjs.io/compute-engine/) though designed as an independent implementation focused on our specific use cases.
|
|
37
|
-
|
|
38
35
|
## Table of Contents
|
|
39
36
|
- [Installation](#installation)
|
|
40
37
|
- [Quick Start](#quick-start)
|
|
@@ -80,21 +77,32 @@ except MathJSONException as e:
|
|
|
80
77
|
# Math error: Problem in Divide. ['Divide', 1, 0]. division by zero
|
|
81
78
|
```
|
|
82
79
|
|
|
80
|
+
**Think Functional:** MathJSON Solver embraces functional programming principles. Instead of writing loops and modifying variables, you compose expressions that transform data. Functions like `Map`, `Reduce`, and `Filter` let you process arrays elegantly, while immutable operations ensure predictable, side-effect-free calculations. Don't worry if you're new to functional programming — the examples will guide you naturally into this powerful paradigm.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
# Functional approach: transform data with expressions
|
|
84
|
+
solver(["Map", ["Array", 1, 2, 3, 4], ["Multiply"], 2]) # [2, 4, 6, 8]
|
|
85
|
+
solver(["Reduce", ["Array", 1, 2, 3, 4], 0, ["Add", "acc", "item"],
|
|
86
|
+
["Variable", "acc"], ["Variable", "item"], ["Variable", "i"]]) # 10
|
|
87
|
+
```
|
|
88
|
+
|
|
83
89
|
## Supported Operations
|
|
84
90
|
|
|
85
91
|
The library supports a comprehensive set of mathematical operations:
|
|
86
92
|
|
|
87
|
-
* **Arithmetic:** Add, Sum, Subtract, Multiply, Divide, Negate, Power, Square, Root, Sqrt, Abs, Round
|
|
93
|
+
* **Arithmetic:** Add, Sum, Subtract, Multiply, Divide, Negate, Power, Square, Root, Sqrt, Abs, Round, Floor, Ceil
|
|
88
94
|
* **Trigonometry:** Sin, Cos, Tan, Arcsin, Arccos, Arctan
|
|
89
95
|
* **Logarithms:** Log, Log2, Log10, Exp
|
|
90
96
|
* **Comparison:** Equal, StrictEqual, NotEqual, Greater, GreaterEqual, Less, LessEqual
|
|
91
|
-
* **Logic & Sets:** Any, All, Not, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
97
|
+
* **Logic & Sets:** Any, All, Not, And, Or, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
92
98
|
* **Statistics:** Average, Max, Min, Median, Length
|
|
93
|
-
* **
|
|
99
|
+
* **Functional Programming:** Map, Reduce, Filter
|
|
100
|
+
* **Arrays:** Array creation, GenerateRange, AtIndex, Slice, Appended, CumulativeSum, CumulativeProduct
|
|
94
101
|
* **Control Flow:** If statements, Switch-Case, Constants definition
|
|
95
102
|
* **Type Conversion:** Int, Float, Str, IsDefined
|
|
96
|
-
* **Date/Time:** Strptime, Strftime, Today, Now, TimeDelta functions
|
|
97
|
-
* **
|
|
103
|
+
* **Date/Time:** Strptime, Strftime, Today, Now, TimeDelta functions (Weeks, Days, Hours, Minutes)
|
|
104
|
+
* **Integration:** TrapezoidalIntegrate (requires numpy), Interp, FindIntervalIndex, Variable references
|
|
105
|
+
* **Advanced:** HasMatchingSublist for pattern matching
|
|
98
106
|
* **Constants:** Pi
|
|
99
107
|
|
|
100
108
|
[View complete documentation with examples →](https://github.com/LongenesisLtd/mathjson-solver/blob/main/docs/README.md)
|
|
@@ -161,6 +169,10 @@ We welcome contributions! Please feel free to:
|
|
|
161
169
|
|
|
162
170
|
[View license information](https://github.com/LongenesisLtd/mathjson-solver/blob/main/LICENSE)
|
|
163
171
|
|
|
172
|
+
## References
|
|
173
|
+
|
|
174
|
+
This implementation was inspired by the [CortexJS Compute Engine](https://cortexjs.io/compute-engine/), though designed as an independent implementation focused on our specific use cases.
|
|
175
|
+
|
|
164
176
|
---
|
|
165
177
|
|
|
166
178
|
Made with ❤️ by [Longenesis](https://longenesis.com/team)
|
|
@@ -9,18 +9,24 @@ src/mathjson_solver.egg-info/PKG-INFO
|
|
|
9
9
|
src/mathjson_solver.egg-info/SOURCES.txt
|
|
10
10
|
src/mathjson_solver.egg-info/dependency_links.txt
|
|
11
11
|
src/mathjson_solver.egg-info/top_level.txt
|
|
12
|
-
tests/test_CumulativeProduct.py
|
|
13
|
-
tests/test_Slice.py
|
|
14
12
|
tests/test_TrapezoidalIntegratel.py
|
|
15
13
|
tests/test_atindex.py
|
|
14
|
+
tests/test_cumulative.py
|
|
16
15
|
tests/test_equal.py
|
|
17
16
|
tests/test_exceptions.py
|
|
18
17
|
tests/test_extract_variables.py
|
|
19
18
|
tests/test_findinterval.py
|
|
20
19
|
tests/test_gail_model.py
|
|
20
|
+
tests/test_gail_model_nci_validated_no_beta.py
|
|
21
|
+
tests/test_gail_model_nci_validated_with_beta.py
|
|
21
22
|
tests/test_interpolate.py
|
|
23
|
+
tests/test_nci_discrete_implementation_no_beta.py
|
|
24
|
+
tests/test_nci_discrete_implementation_with_beta.py
|
|
22
25
|
tests/test_range.py
|
|
26
|
+
tests/test_reduce_simple.py
|
|
23
27
|
tests/test_scalars.py
|
|
24
28
|
tests/test_sets.py
|
|
25
29
|
tests/test_simple_cases.py
|
|
30
|
+
tests/test_slice.py
|
|
31
|
+
tests/test_state_tuples_reduce.py
|
|
26
32
|
tests/test_with_pytest.py
|
|
@@ -16,3 +16,14 @@ def test_CumulativeProduct1():
|
|
|
16
16
|
solver = create_solver(parameters)
|
|
17
17
|
# assert solver(expression) == expected_result
|
|
18
18
|
assert solver(expression) == expected_result
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_CumulativeSum1():
|
|
22
|
+
parameters = {"test_array": ["Array", 1, 2, 3, 4, 5]}
|
|
23
|
+
|
|
24
|
+
expression = ["CumulativeSum", "test_array"]
|
|
25
|
+
|
|
26
|
+
expected_result = ["Array", 1, 3, 6, 10, 15]
|
|
27
|
+
solver = create_solver(parameters)
|
|
28
|
+
# assert solver(expression) == expected_result
|
|
29
|
+
assert solver(expression) == expected_result
|
|
@@ -43,6 +43,11 @@ from mathjson_solver import create_solver
|
|
|
43
43
|
({}, ["NotEqual", 1, 2], True),
|
|
44
44
|
({}, ["NotEqual", "aaa", "bbb"], True),
|
|
45
45
|
({}, ["NotEqual", "aaa", 0], True),
|
|
46
|
+
({}, ["And", True, True], True),
|
|
47
|
+
({}, ["And", True, False], False),
|
|
48
|
+
({}, ["Or", True, False], True),
|
|
49
|
+
({}, ["Or", True, True], True),
|
|
50
|
+
({}, ["Or", False, False], False),
|
|
46
51
|
],
|
|
47
52
|
)
|
|
48
53
|
def test_solver_comparison(parameters, expression, expected_result):
|