Calculate
calculateEvaluate math expressions, simplify algebraic expressions, or compute symbolic derivatives. One expression per call. Supports arithmetic, trigonometry, statistics, matrices, complex numbers, units, and combinatorics.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| scope | No | Variable assignments for the expression. Example: { "x": 5, "y": 3 } makes "x + y" evaluate to 8. | |
| variable | No | Variable to differentiate with respect to. Required when operation is "derivative". Empty string is treated as omitted. Example: "x". | |
| operation | No | Operation to perform. "evaluate" computes a numeric result (default). "simplify" reduces an algebraic expression symbolically (e.g., "2x + 3x" -> "5 * x"). Supports algebraic and trigonometric identities. When the simplifier cannot reduce the expression further (e.g. rational expressions requiring polynomial factoring), the result is returned unchanged and unchanged: true is set in the output. "derivative" computes the symbolic derivative (requires the variable parameter). | evaluate |
| precision | No | Significant digits (1–16) for numeric results. Omit for full precision. Empty string is treated as omitted. Ignored for symbolic operations (simplify, derivative). | |
| expression | Yes | One mathematical expression per call — neither `;` nor newlines separate statements. Inside matrices, `;` separates rows (e.g. `[1, 2; 3, 4]`). Supports arithmetic (+, -, *, /, ^, %), functions across arithmetic/trig (sin, cos, sqrt, log, abs, round), statistics (mean, median, std, variance), combinatorics (factorial, permutations, combinations), and matrix (det, inv, transpose), plus constants (pi, e, phi, i), units (5 kg to lbs), and variables (when scope is provided). Standard notation `ln` and `arc*` (e.g. `arcsin`, `arctan`) is accepted alongside the math.js names `log` and `asin`/`atan`; common synonyms such as `stdev`, `permute`, `nCr`, and `length`/`len` resolve to their math.js names (`std`, `permutations`, `combinations`, `count`). | |
| numericType | No | Numeric type for evaluate. "number" (default): 64-bit IEEE 754 float — fastest, standard precision. "BigNumber": arbitrary-precision decimal — use when intermediate values overflow 64-bit float (e.g. large factorial ratios like 10000!/9999!); slower than "number". "Fraction": exact rational arithmetic — eliminates floating-point rounding (e.g. 0.1 + 0.2 = 0.3 exactly); limited to expressions with exactly-rational results — an irrational or transcendental result (sqrt, sin, log, …) fails with fraction_unsupported, so use "number" or "BigNumber" for those. Ignored for symbolic operations (simplify, derivative). When "number" evaluation produces a non-finite result (undefined_result error), retry with "BigNumber". | number |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| error | No | Present when the call failed. Absent on success. | |
| result | No | The computed result as a string. | |
| operation | No | The operation that was applied. | |
| scopeVars | No | Keys from the scope that were active during evaluation. Omitted when no scope was provided. Values are omitted to keep output compact. | |
| unchanged | No | Present only for simplify operations. true means the simplifier returned the expression unchanged — it could not reduce it further (e.g. rational expressions requiring polynomial factoring are beyond math.js's built-in simplifier). false means simplification made progress. Omitted for evaluate and derivative. | |
| expression | No | The original expression as received. | |
| resultType | No | Type of result as reported by math.js: number, BigNumber, Complex, DenseMatrix, Unit, string, boolean. Symbolic operations return "string". | |
| precisionUsed | No | Significant-digit precision applied to the result. Omitted when full precision was used or the operation is symbolic. |