(c: any)
| 80 | |
| 81 | // --- run CE in-process ----------------------------------------------------- |
| 82 | function runCE(c: any) { |
| 83 | const inp = c.ce, op = inp.op; |
| 84 | try { |
| 85 | if (op === 'factor' || op === 'expand') { |
| 86 | const head = op === 'factor' ? 'Factor' : 'Expand'; |
| 87 | const timing = timeit(() => ce.expr([head, inp.mathjson]).evaluate()); |
| 88 | const r = ce.expr([head, inp.mathjson]).evaluate(); |
| 89 | if (c.verify.kind === 'mantexp') { |
| 90 | // constant expansion: grade the exact components (huge magnitudes — |
| 91 | // .N()/.re would overflow float64) |
| 92 | if (!r.isNumberLiteral) |
| 93 | return { status: 'unsolved', text: r.toString().slice(0, 200), values: [], ...timing }; |
| 94 | const values = [...mantExp10(r.bignumRe?.toString()), ...mantExp10(r.bignumIm?.toString())]; |
| 95 | return { status: 'ok', text: r.toString().slice(0, 200), values, ...timing }; |
| 96 | } |
| 97 | return { status: 'ok', text: r.toString().slice(0, 200), values: sampleResult(r, c), ...timing }; |
| 98 | } |
| 99 | if (op === 'gcd') { |
| 100 | const timing = timeit(() => ce.expr(inp.mathjson).evaluate()); |
| 101 | const r = ce.expr(inp.mathjson).evaluate(); |
| 102 | // unevaluated GCD comes back as a gcd(...) function node |
| 103 | const unsolved = r.operator === 'GCD' || /\bgcd\(/i.test(r.toString()); |
| 104 | return { status: unsolved ? 'unsolved' : 'ok', text: r.toString(), values: sampleResult(r, c), ...timing }; |
| 105 | } |
| 106 | if (op === 'simplify') { |
| 107 | const timing = timeit(() => ce.expr(inp.mathjson).simplify()); |
| 108 | const r = ce.expr(inp.mathjson).simplify(); |
| 109 | return { status: 'ok', text: r.toString(), values: sampleResult(r, c), ...timing }; |
| 110 | } |
| 111 | if (op === 'integrate') { |
| 112 | const build = () => ce.expr(['Integrate', inp.mathjson, ['Tuple', inp.var]]).evaluate(); |
| 113 | const timing = timeit(build); |
| 114 | const F = build(); |
| 115 | if (F.operator === 'Integrate' || /\bint\(/.test(F.toString())) |
| 116 | return { status: 'unsolved', text: F.toString(), values: [], ...timing }; |
| 117 | const dF = ce.expr(['D', F, inp.var]).evaluate(); |
| 118 | return { status: 'ok', text: F.toString(), values: sampleResult(dF, c), ...timing }; |
| 119 | } |
| 120 | if (op === 'limit') { |
| 121 | // CE evaluates limits to an exact symbolic closed form (e.g. 1/2, e); we |
| 122 | // sample that result numerically only to grade it against the reference. |
| 123 | const build = () => ce.expr(['Limit', ['Function', inp.mathjson, inp.var], inp.point]).evaluate(); |
| 124 | const timing = timeit(build); |
| 125 | const L = build(); |
| 126 | const val = num(L); |
| 127 | if (!isFinite(val)) return { status: 'unsolved', text: L.toString(), values: [], ...timing }; |
| 128 | return { status: 'ok', text: L.toString(), values: [val], ...timing }; |
| 129 | } |
| 130 | return { status: 'error', error: `unknown op ${op}` }; |
| 131 | } catch (e: any) { |
| 132 | return { status: 'error', error: String(e?.message ?? e).slice(0, 160) }; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // --- run SymPy batch ------------------------------------------------------- |
| 137 | function runSymPyAll(): Record<string, any> { |
no test coverage detected