mathjson-solver 1.20.2__tar.gz → 2.2.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.20.2 → mathjson_solver-2.2.0}/.gitignore +6 -0
- mathjson_solver-2.2.0/CHANGELOG.md +70 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/PKG-INFO +45 -10
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/README.md +44 -9
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/docs/README.md +298 -26
- mathjson_solver-2.2.0/pyproject.toml +41 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/src/mathjson_solver/__init__.py +1 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/src/mathjson_solver/__main__.py +476 -30
- mathjson_solver-2.2.0/tests/test_cortexjs_compat.py +88 -0
- mathjson_solver-2.2.0/tests/test_cortexjs_tier3.py +158 -0
- mathjson_solver-2.2.0/tests/test_legacy_compat.py +73 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_simple_cases.py +5 -1
- mathjson_solver-1.20.2/.claude/settings.local.json +0 -7
- mathjson_solver-1.20.2/.github/ISSUE_TEMPLATE/bug_report.md +0 -25
- mathjson_solver-1.20.2/.github/workflows/python-app.yml +0 -33
- mathjson_solver-1.20.2/.github/workflows/test-coverage.yml +0 -51
- mathjson_solver-1.20.2/logo.png +0 -0
- mathjson_solver-1.20.2/logo.svg +0 -105
- mathjson_solver-1.20.2/pyproject.toml +0 -20
- mathjson_solver-1.20.2/untracked/activity_stuff.json +0 -2156
- mathjson_solver-1.20.2/untracked/ehab312.pdf +0 -0
- mathjson_solver-1.20.2/untracked/ehab312_supplementary_data/ehab312-suppl_data/Supplementary material_20210604_v2.docx +0 -0
- mathjson_solver-1.20.2/untracked/score2-op.json +0 -968
- mathjson_solver-1.20.2/untracked/score2.json +0 -976
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/CODE_OF_CONDUCT.md +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/CONTRIBUTING.md +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/LICENSE +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/SECURITY.md +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/docs/integration-test-cases.md +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_TrapezoidalIntegratel.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_atindex.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_cumulative.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_datetime.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_equal.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_exceptions.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_extract_variables.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_findinterval.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_gail_model.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_interpolate.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_range.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_reduce_simple.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_scalars.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_sets.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_slice.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_state_tuples_reduce.py +0 -0
- {mathjson_solver-1.20.2 → mathjson_solver-2.2.0}/tests/test_with_pytest.py +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/).
|
|
7
|
+
|
|
8
|
+
## [2.2.0] - 2026-09-08
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **Migration helper for 1.x users:** `translate_v1_mathjson(expr)` rewrites the one breaking change from 2.0.0 (`["Log", x]`, previously natural log) to its 2.x equivalent (`["Ln", x]`), leaving everything else untouched. `create_solver(parameters, legacy_v1=True)` applies this automatically, so existing 1.x expressions keep evaluating to the same results on 2.x without hand-editing, while still gaining access to functions added in 2.x.
|
|
13
|
+
|
|
14
|
+
[2.2.0]: https://github.com/LongenesisLtd/mathjson-solver/compare/v2.1.1...v2.2.0
|
|
15
|
+
|
|
16
|
+
## [2.1.1] - 2026-08-19
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
|
|
20
|
+
- Fixed packaging: the sdist now only includes the intended package, tests, docs, and metadata files.
|
|
21
|
+
|
|
22
|
+
[2.1.1]: https://github.com/LongenesisLtd/mathjson-solver/compare/v2.1.0...v2.1.1
|
|
23
|
+
|
|
24
|
+
## [2.1.0] - 2026-08-19
|
|
25
|
+
|
|
26
|
+
Continues the CortexJS compatibility pass: `If`, `Map`, `Filter`, and `Reduce` now also accept CortexJS calling conventions, alongside the existing Python-specific forms.
|
|
27
|
+
|
|
28
|
+
### Fixed
|
|
29
|
+
|
|
30
|
+
- **Correctness fix:** a locally-bound name (a `Constants` binding, a `Reduce` accumulator/current/index variable, or a `Function` parameter, see below) now correctly shadows a top-level solver parameter of the same name, instead of the global value silently winning. Previously `create_solver({"x": 5})` evaluating `["Constants", ["x", 100], ["Add", "x", 1]]` returned `6` instead of `101`. If you were unknowingly relying on the old (backwards) precedence, this will change your result.
|
|
31
|
+
|
|
32
|
+
### Added
|
|
33
|
+
|
|
34
|
+
- **`Function`**: now a real CortexJS-style lambda, `["Function", body, param1, param2, ...]`. With no parameter names, `body` can reference its arguments via the anonymous placeholders `"_"` (first argument only) and `"_1"`, `"_2"`, ... Meant to be passed as the function argument to `Map`, `Filter`, and `Reduce`; evaluated on its own it returns unevaluated. (Previously a non-functional stub that always returned `0`.)
|
|
35
|
+
- **`If`**: now also accepts the CortexJS flat form, `["If", cond, then]` / `["If", cond, then, else]`, including the no-else form (returns `None`/CortexJS `Nothing` when the condition is false). The existing Python pair form, `["If", [cond, val], ..., else_val]`, is unchanged and detected automatically. (Note: a Python-form condition that is a bare parameter reference, e.g. `["If", ["my_flag", "yes"], "no"]`, is still correctly disambiguated — but wrapping such conditions in `IsTrue`/`IsFalse` remains the clearer style.)
|
|
36
|
+
- **`Map` / `StrictMap` / `Filter`**: the function argument can now be a `["Function", ...]` expression (see above), in addition to the existing call-template form (e.g. `["Square"]`).
|
|
37
|
+
- **`Reduce`**: now also accepts the CortexJS form, `["Reduce", collection, fn]` / `["Reduce", collection, fn, initial]`, where `fn` is applied as `fn(accumulator, current_item)` (call-template or `Function` form). Without an initial value, the first element seeds the accumulator. The existing 6-argument Python form (with named accumulator/current/index variables) is unchanged and detected automatically via argument count.
|
|
38
|
+
- **`Product`**: `["Product", array]` multiplies together the numeric elements of `array`.
|
|
39
|
+
|
|
40
|
+
[2.1.0]: https://github.com/LongenesisLtd/mathjson-solver/compare/v2.0.0...v2.1.0
|
|
41
|
+
|
|
42
|
+
## [2.0.0] - 2026-08-19
|
|
43
|
+
|
|
44
|
+
Steers the solver back towards greater compatibility with [CortexJS MathJSON](https://cortexjs.io/compute-engine/), adding aliases and constructs that were previously CortexJS-only.
|
|
45
|
+
|
|
46
|
+
### Changed
|
|
47
|
+
|
|
48
|
+
- **BREAKING:** `Log` now matches CortexJS: `["Log", x]` is log base 10, and `["Log", x, b]` is log base `b`. Previously `Log` was natural log; use `Ln` for that.
|
|
49
|
+
- `Max` and `Min` now also accept a variadic (CortexJS) form, e.g. `["Max", 5, 2, -1]`, in addition to the existing single-array form.
|
|
50
|
+
|
|
51
|
+
### Added
|
|
52
|
+
|
|
53
|
+
- CortexJS aliases: `Lb` (`Log2`), `Lg` (`Log10`), `List` (`Array`), `Mean` (`Average`), `Count` (`Length`), `Which` (`Switch`).
|
|
54
|
+
- New math functions: `LogOnePlus`, `Chop`, `Mod`, `Clamp`, `GCD`, `LCM`, `Factorial`, `Binomial`, `IsPrime`, `Erf`, `Erfc`.
|
|
55
|
+
- New logic functions: `Xor`, `Nand`, `Nor`, `Implies`, `Equivalent`.
|
|
56
|
+
- New trigonometric functions: `Arctan2`, `Cot`, `Sec`, `Csc`, `Arccot`, `Arcsec`, `Arccsc`, `Sinh`, `Cosh`, `Tanh`, `Coth`, `Sech`, `Csch`, `Arsinh`, `Arcosh`, `Artanh`, `Arcoth`, `Arsech`, `Arcsch`, `Hypot`, `Sinc`.
|
|
57
|
+
- New constants: `Degrees`, `ExponentialE`, `GoldenRatio`.
|
|
58
|
+
- New statistics: `Variance`, `StandardDeviation`.
|
|
59
|
+
- New array/collection functions: `First`, `Last`, `Rest`, `Most`, `Reverse`, `Sort`, `IsEmpty`, `Range` (CortexJS-compatible, distinct from `GenerateRange`), `Join`, `Unique`, `Zip`, `At` (1-indexed, distinct from `AtIndex`).
|
|
60
|
+
|
|
61
|
+
[2.0.0]: https://github.com/LongenesisLtd/mathjson-solver/compare/v1.20.2...v2.0.0
|
|
62
|
+
|
|
63
|
+
## [1.20.2] - 2026-08-18
|
|
64
|
+
|
|
65
|
+
### Fixed
|
|
66
|
+
|
|
67
|
+
- Fix `extract_variables` for Constants (thanks [@nkimdwave](https://github.com/nkimdwave)!)
|
|
68
|
+
- Add missing functions to constructs in `extract_variables` (thanks [@nkimdwave](https://github.com/nkimdwave)!)
|
|
69
|
+
|
|
70
|
+
[1.20.2]: https://github.com/LongenesisLtd/mathjson-solver/compare/461bc00...v1.20.2
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: mathjson-solver
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: Utilities for MathJSON evaluation
|
|
5
5
|
Project-URL: Homepage, https://github.com/LongenesisLtd/mathjson-solver
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/LongenesisLtd/mathjson-solver/issues
|
|
@@ -18,6 +18,8 @@ Description-Content-Type: text/markdown
|
|
|
18
18
|
[](https://pepy.tech/projects/mathjson-solver)
|
|
19
19
|
[](https://www.python.org/downloads/)
|
|
20
20
|
|
|
21
|
+
> **Heads up:** Version 2 introduces a breaking change (see [CHANGELOG.md](CHANGELOG.md)) as part of steering back towards greater compatibility with [CortexJS MathJSON](https://cortexjs.io/compute-engine/): `Log` is now log base 10 instead of natural log. Stuck on pre-2.0.0 expressions but want the functions added in 2.x? Pass `legacy_v1=True` to `create_solver()` (see [Migrating from 1.x](#migrating-from-1x)) instead of hand-migrating every expression. Bugfix releases for the 1.x line also continue on the [`1.x` branch](https://github.com/LongenesisLtd/mathjson-solver/tree/1.x).
|
|
22
|
+
|
|
21
23
|
A reliable Python library for numerically evaluating mathematical expressions in MathJSON format. Perfect for applications that need to safely execute user-provided formulas, calculate dynamic equations, or process mathematical data.
|
|
22
24
|
|
|
23
25
|
**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.
|
|
@@ -25,12 +27,14 @@ A reliable Python library for numerically evaluating mathematical expressions in
|
|
|
25
27
|
## Table of Contents
|
|
26
28
|
- [Installation](#installation)
|
|
27
29
|
- [Quick Start](#quick-start)
|
|
30
|
+
- [Migrating from 1.x](#migrating-from-1x)
|
|
28
31
|
- [Supported Operations](#supported-operations)
|
|
29
32
|
- [Error Handling](#error-handling)
|
|
30
33
|
- [Use Cases](#use-cases)
|
|
31
34
|
- [Testing](#testing)
|
|
32
35
|
- [Community](#community)
|
|
33
36
|
- [Contributing](#contributing)
|
|
37
|
+
- [Related Projects](#related-projects)
|
|
34
38
|
|
|
35
39
|
## Installation
|
|
36
40
|
|
|
@@ -74,26 +78,52 @@ except MathJSONException as e:
|
|
|
74
78
|
solver(["Map", ["Array", 1, 2, 3, 4], ["Multiply"], 2]) # [2, 4, 6, 8]
|
|
75
79
|
solver(["Reduce", ["Array", 1, 2, 3, 4], 0, ["Add", "acc", "item"],
|
|
76
80
|
["Variable", "acc"], ["Variable", "item"], ["Variable", "i"]]) # 10
|
|
81
|
+
|
|
82
|
+
# CortexJS-style forms also work: a lambda via Function, and a 2-argument Reduce
|
|
83
|
+
solver(["Map", ["Array", 1, 2, 3, 4], ["Function", ["Multiply", "_", 2]]]) # [2, 4, 6, 8]
|
|
84
|
+
solver(["Reduce", ["Array", 1, 2, 3, 4], ["Add"]]) # 10
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Migrating from 1.x
|
|
88
|
+
|
|
89
|
+
Version 2.0.0's only breaking change is `Log`: pre-2.0.0 it was always natural log, `["Log", x]` == `math.log(x)`. From 2.0.0 on it matches [CortexJS](https://cortexjs.io/compute-engine/) — `["Log", x]` is log base 10, `["Log", x, b]` is log base `b` — and natural log moved to `Ln`.
|
|
90
|
+
|
|
91
|
+
If you have existing expressions built for 1.x and don't want to hand-edit every `Log` node just to pick up functions added in 2.x (`Product`, the CortexJS forms of `If`/`Map`/`Filter`/`Reduce`, the new aliases, etc.), pass `legacy_v1=True` when creating the solver. It rewrites every `["Log", x]` to `["Ln", x]` before evaluating, so old expressions keep producing the same results without modification:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
solver = create_solver(parameters, legacy_v1=True)
|
|
95
|
+
solver(["Log", 8]) # 2.0794... (natural log, matching pre-2.0.0 behavior)
|
|
77
96
|
```
|
|
78
97
|
|
|
98
|
+
You can also run the rewrite yourself and inspect or store the translated expression:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from mathjson_solver import translate_v1_mathjson
|
|
102
|
+
|
|
103
|
+
translate_v1_mathjson(["Add", ["Log", 8], 1]) # ["Add", ["Ln", 8], 1]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`legacy_v1=True` only affects `Log`. Every other 1.x expression already evaluates identically on 2.x without any translation.
|
|
107
|
+
|
|
79
108
|
## Supported Operations
|
|
80
109
|
|
|
81
110
|
The library supports a comprehensive set of mathematical operations:
|
|
82
111
|
|
|
83
112
|
* **Arithmetic:** Add, Sum, Subtract, Multiply, Divide, Negate, Power, Square, Root, Sqrt, Abs, Round, Floor, Ceil
|
|
84
|
-
* **Trigonometry:** Sin, Cos, Tan, Arcsin, Arccos, Arctan
|
|
85
|
-
* **Logarithms:** Log, Log2, Log10, Exp
|
|
113
|
+
* **Trigonometry:** Sin, Cos, Tan, Arcsin, Arccos, Arctan, Arctan2, Cot, Sec, Csc (+ inverses), Sinh, Cosh, Tanh, Coth, Sech, Csch (+ inverses), Hypot, Sinc
|
|
114
|
+
* **Logarithms:** Log (base 10, or base b), Log2/Lb, Log10/Lg, Ln (natural log), LogOnePlus, Exp
|
|
86
115
|
* **Comparison:** Equal, StrictEqual, NotEqual, Greater, GreaterEqual, Less, LessEqual
|
|
87
|
-
* **Logic & Sets:** Any, All, Not, And, Or, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
88
|
-
* **Statistics:** Average, Max, Min, Median, Length
|
|
89
|
-
* **Functional Programming:** Map, Reduce, Filter
|
|
90
|
-
* **Arrays:** Array creation, GenerateRange, AtIndex, Slice, Appended, CumulativeSum, CumulativeProduct
|
|
91
|
-
* **Control Flow:** If statements, Switch-Case, Constants definition
|
|
116
|
+
* **Logic & Sets:** Any, All, Not, And, Or, Xor, Nand, Nor, Implies, Equivalent, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
117
|
+
* **Statistics:** Average/Mean, Max, Min (both list and variadic forms), Median, Variance, StandardDeviation, Length/Count
|
|
118
|
+
* **Functional Programming:** Map/StrictMap, Reduce, Filter, Product (all also accept CortexJS calling conventions, including `Function` lambdas)
|
|
119
|
+
* **Arrays:** Array/List creation, GenerateRange, Range, AtIndex, At, Slice, Appended, First, Last, Rest, Most, Reverse, Sort, Unique, Join, Zip, IsEmpty, CumulativeSum, CumulativeProduct
|
|
120
|
+
* **Control Flow:** If statements (Python pair form and CortexJS flat form), Switch-Case/Which, Constants definition
|
|
92
121
|
* **Type Conversion:** Int, Float, Str, IsDefined
|
|
93
122
|
* **Date/Time:** Strptime, Strftime, Today, Now, TimeDelta functions (Weeks, Days, Hours, Minutes)
|
|
123
|
+
* **Number Theory:** Chop, Mod, Clamp, GCD, LCM, Factorial, Binomial, IsPrime, Erf, Erfc
|
|
94
124
|
* **Integration:** TrapezoidalIntegrate (requires numpy), Interp, FindIntervalIndex, Variable references
|
|
95
125
|
* **Advanced:** HasMatchingSublist for pattern matching
|
|
96
|
-
* **Constants:** Pi
|
|
126
|
+
* **Constants:** Pi, Degrees, ExponentialE, GoldenRatio
|
|
97
127
|
|
|
98
128
|
[View complete documentation with examples →](https://github.com/LongenesisLtd/mathjson-solver/blob/main/docs/README.md)
|
|
99
129
|
|
|
@@ -154,6 +184,9 @@ We welcome contributions! Please feel free to:
|
|
|
154
184
|
- Report bugs or request features via [GitHub Issues](https://github.com/LongenesisLtd/mathjson-solver/issues)
|
|
155
185
|
- Submit pull requests with improvements
|
|
156
186
|
|
|
187
|
+
## Related Projects
|
|
188
|
+
|
|
189
|
+
We also created [`londec`](https://pypi.org/project/londec/) — evaluate tree-structured conditions against an ordered history of events. It uses mathjson-solver internally.
|
|
157
190
|
|
|
158
191
|
## License
|
|
159
192
|
|
|
@@ -161,7 +194,9 @@ We welcome contributions! Please feel free to:
|
|
|
161
194
|
|
|
162
195
|
## References
|
|
163
196
|
|
|
164
|
-
This
|
|
197
|
+
This library implements the [MathJSON](https://cortexjs.io/mathjson/) format as defined by the [CortexJS Compute Engine](https://cortexjs.io/compute-engine/). Since 2.0.0, mathjson-solver has been steering towards greater compatibility with CortexJS's calling conventions and function set — while remaining an independent Python implementation, not a port or dependency of CortexJS.
|
|
198
|
+
|
|
199
|
+
**Scope:** mathjson-solver targets compatibility with CortexJS's array-form calling conventions and standard function names — not the full [Compute Engine](https://cortexjs.io/compute-engine/), which is a symbolic CAS.
|
|
165
200
|
|
|
166
201
|
---
|
|
167
202
|
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
[](https://pepy.tech/projects/mathjson-solver)
|
|
5
5
|
[](https://www.python.org/downloads/)
|
|
6
6
|
|
|
7
|
+
> **Heads up:** Version 2 introduces a breaking change (see [CHANGELOG.md](CHANGELOG.md)) as part of steering back towards greater compatibility with [CortexJS MathJSON](https://cortexjs.io/compute-engine/): `Log` is now log base 10 instead of natural log. Stuck on pre-2.0.0 expressions but want the functions added in 2.x? Pass `legacy_v1=True` to `create_solver()` (see [Migrating from 1.x](#migrating-from-1x)) instead of hand-migrating every expression. Bugfix releases for the 1.x line also continue on the [`1.x` branch](https://github.com/LongenesisLtd/mathjson-solver/tree/1.x).
|
|
8
|
+
|
|
7
9
|
A reliable Python library for numerically evaluating mathematical expressions in MathJSON format. Perfect for applications that need to safely execute user-provided formulas, calculate dynamic equations, or process mathematical data.
|
|
8
10
|
|
|
9
11
|
**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.
|
|
@@ -11,12 +13,14 @@ A reliable Python library for numerically evaluating mathematical expressions in
|
|
|
11
13
|
## Table of Contents
|
|
12
14
|
- [Installation](#installation)
|
|
13
15
|
- [Quick Start](#quick-start)
|
|
16
|
+
- [Migrating from 1.x](#migrating-from-1x)
|
|
14
17
|
- [Supported Operations](#supported-operations)
|
|
15
18
|
- [Error Handling](#error-handling)
|
|
16
19
|
- [Use Cases](#use-cases)
|
|
17
20
|
- [Testing](#testing)
|
|
18
21
|
- [Community](#community)
|
|
19
22
|
- [Contributing](#contributing)
|
|
23
|
+
- [Related Projects](#related-projects)
|
|
20
24
|
|
|
21
25
|
## Installation
|
|
22
26
|
|
|
@@ -60,26 +64,52 @@ except MathJSONException as e:
|
|
|
60
64
|
solver(["Map", ["Array", 1, 2, 3, 4], ["Multiply"], 2]) # [2, 4, 6, 8]
|
|
61
65
|
solver(["Reduce", ["Array", 1, 2, 3, 4], 0, ["Add", "acc", "item"],
|
|
62
66
|
["Variable", "acc"], ["Variable", "item"], ["Variable", "i"]]) # 10
|
|
67
|
+
|
|
68
|
+
# CortexJS-style forms also work: a lambda via Function, and a 2-argument Reduce
|
|
69
|
+
solver(["Map", ["Array", 1, 2, 3, 4], ["Function", ["Multiply", "_", 2]]]) # [2, 4, 6, 8]
|
|
70
|
+
solver(["Reduce", ["Array", 1, 2, 3, 4], ["Add"]]) # 10
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Migrating from 1.x
|
|
74
|
+
|
|
75
|
+
Version 2.0.0's only breaking change is `Log`: pre-2.0.0 it was always natural log, `["Log", x]` == `math.log(x)`. From 2.0.0 on it matches [CortexJS](https://cortexjs.io/compute-engine/) — `["Log", x]` is log base 10, `["Log", x, b]` is log base `b` — and natural log moved to `Ln`.
|
|
76
|
+
|
|
77
|
+
If you have existing expressions built for 1.x and don't want to hand-edit every `Log` node just to pick up functions added in 2.x (`Product`, the CortexJS forms of `If`/`Map`/`Filter`/`Reduce`, the new aliases, etc.), pass `legacy_v1=True` when creating the solver. It rewrites every `["Log", x]` to `["Ln", x]` before evaluating, so old expressions keep producing the same results without modification:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
solver = create_solver(parameters, legacy_v1=True)
|
|
81
|
+
solver(["Log", 8]) # 2.0794... (natural log, matching pre-2.0.0 behavior)
|
|
63
82
|
```
|
|
64
83
|
|
|
84
|
+
You can also run the rewrite yourself and inspect or store the translated expression:
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from mathjson_solver import translate_v1_mathjson
|
|
88
|
+
|
|
89
|
+
translate_v1_mathjson(["Add", ["Log", 8], 1]) # ["Add", ["Ln", 8], 1]
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`legacy_v1=True` only affects `Log`. Every other 1.x expression already evaluates identically on 2.x without any translation.
|
|
93
|
+
|
|
65
94
|
## Supported Operations
|
|
66
95
|
|
|
67
96
|
The library supports a comprehensive set of mathematical operations:
|
|
68
97
|
|
|
69
98
|
* **Arithmetic:** Add, Sum, Subtract, Multiply, Divide, Negate, Power, Square, Root, Sqrt, Abs, Round, Floor, Ceil
|
|
70
|
-
* **Trigonometry:** Sin, Cos, Tan, Arcsin, Arccos, Arctan
|
|
71
|
-
* **Logarithms:** Log, Log2, Log10, Exp
|
|
99
|
+
* **Trigonometry:** Sin, Cos, Tan, Arcsin, Arccos, Arctan, Arctan2, Cot, Sec, Csc (+ inverses), Sinh, Cosh, Tanh, Coth, Sech, Csch (+ inverses), Hypot, Sinc
|
|
100
|
+
* **Logarithms:** Log (base 10, or base b), Log2/Lb, Log10/Lg, Ln (natural log), LogOnePlus, Exp
|
|
72
101
|
* **Comparison:** Equal, StrictEqual, NotEqual, Greater, GreaterEqual, Less, LessEqual
|
|
73
|
-
* **Logic & Sets:** Any, All, Not, And, Or, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
74
|
-
* **Statistics:** Average, Max, Min, Median, Length
|
|
75
|
-
* **Functional Programming:** Map, Reduce, Filter
|
|
76
|
-
* **Arrays:** Array creation, GenerateRange, AtIndex, Slice, Appended, CumulativeSum, CumulativeProduct
|
|
77
|
-
* **Control Flow:** If statements, Switch-Case, Constants definition
|
|
102
|
+
* **Logic & Sets:** Any, All, Not, And, Or, Xor, Nand, Nor, Implies, Equivalent, In, NotIn, ContainsAnyOf, ContainsAllOf, ContainsNoneOf
|
|
103
|
+
* **Statistics:** Average/Mean, Max, Min (both list and variadic forms), Median, Variance, StandardDeviation, Length/Count
|
|
104
|
+
* **Functional Programming:** Map/StrictMap, Reduce, Filter, Product (all also accept CortexJS calling conventions, including `Function` lambdas)
|
|
105
|
+
* **Arrays:** Array/List creation, GenerateRange, Range, AtIndex, At, Slice, Appended, First, Last, Rest, Most, Reverse, Sort, Unique, Join, Zip, IsEmpty, CumulativeSum, CumulativeProduct
|
|
106
|
+
* **Control Flow:** If statements (Python pair form and CortexJS flat form), Switch-Case/Which, Constants definition
|
|
78
107
|
* **Type Conversion:** Int, Float, Str, IsDefined
|
|
79
108
|
* **Date/Time:** Strptime, Strftime, Today, Now, TimeDelta functions (Weeks, Days, Hours, Minutes)
|
|
109
|
+
* **Number Theory:** Chop, Mod, Clamp, GCD, LCM, Factorial, Binomial, IsPrime, Erf, Erfc
|
|
80
110
|
* **Integration:** TrapezoidalIntegrate (requires numpy), Interp, FindIntervalIndex, Variable references
|
|
81
111
|
* **Advanced:** HasMatchingSublist for pattern matching
|
|
82
|
-
* **Constants:** Pi
|
|
112
|
+
* **Constants:** Pi, Degrees, ExponentialE, GoldenRatio
|
|
83
113
|
|
|
84
114
|
[View complete documentation with examples →](https://github.com/LongenesisLtd/mathjson-solver/blob/main/docs/README.md)
|
|
85
115
|
|
|
@@ -140,6 +170,9 @@ We welcome contributions! Please feel free to:
|
|
|
140
170
|
- Report bugs or request features via [GitHub Issues](https://github.com/LongenesisLtd/mathjson-solver/issues)
|
|
141
171
|
- Submit pull requests with improvements
|
|
142
172
|
|
|
173
|
+
## Related Projects
|
|
174
|
+
|
|
175
|
+
We also created [`londec`](https://pypi.org/project/londec/) — evaluate tree-structured conditions against an ordered history of events. It uses mathjson-solver internally.
|
|
143
176
|
|
|
144
177
|
## License
|
|
145
178
|
|
|
@@ -147,7 +180,9 @@ We welcome contributions! Please feel free to:
|
|
|
147
180
|
|
|
148
181
|
## References
|
|
149
182
|
|
|
150
|
-
This
|
|
183
|
+
This library implements the [MathJSON](https://cortexjs.io/mathjson/) format as defined by the [CortexJS Compute Engine](https://cortexjs.io/compute-engine/). Since 2.0.0, mathjson-solver has been steering towards greater compatibility with CortexJS's calling conventions and function set — while remaining an independent Python implementation, not a port or dependency of CortexJS.
|
|
184
|
+
|
|
185
|
+
**Scope:** mathjson-solver targets compatibility with CortexJS's array-form calling conventions and standard function names — not the full [Compute Engine](https://cortexjs.io/compute-engine/), which is a symbolic CAS.
|
|
151
186
|
|
|
152
187
|
---
|
|
153
188
|
|