UNPKG

semantic-math-processor-client

Version:

Semantic Math Processor Client is a plugin for SemanticMathEditor, which allows making math calculation using Math Processor - our REST wrapper around Sympy CAS

1 lines 33.5 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("semantic-math-editor")):"function"==typeof define&&define.amd?define("SemanticMathProcessorClient",["semantic-math-editor"],e):"object"==typeof exports?exports.SemanticMathProcessorClient=e(require("semantic-math-editor")):t.SemanticMathProcessorClient=e(t.SemanticMathEditor)}(window,(function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=4)}([function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const r=n(3);function i(t){let e;return r.traverseStructures(t,t=>{t.name===r.MathNodeName.variable&&(e=t)}),e}function s(t,e){const n=e||new Map;return r.traverseStructures(t,t=>{if(t.name===r.MathNodeName.variable){const e=t,i=r.acceptMathNode(e,r.MathNodePrinter.instance);n.set(i,e)}}),n}e.unquote=function(t){return t.startsWith("'")&&t.endsWith("'")?t.substring(1,t.length-1):t},e.getSympyTrigName=function(t){switch(t){case r.MathTrigonometryType.sin:return"sin";case r.MathTrigonometryType.cos:return"cos";case r.MathTrigonometryType.tan:return"tan";case r.MathTrigonometryType.csc:return"csc";case r.MathTrigonometryType.sec:return"sec";case r.MathTrigonometryType.cot:return"cot";case r.MathTrigonometryType.arcsin:return"asin";case r.MathTrigonometryType.arccos:return"acos";case r.MathTrigonometryType.arctan:return"atan";case r.MathTrigonometryType.arccsc:return"acsc";case r.MathTrigonometryType.arcsec:return"asec";case r.MathTrigonometryType.arccot:return"acot";case r.MathTrigonometryType.sinh:return"sinh";case r.MathTrigonometryType.cosh:return"cosh";case r.MathTrigonometryType.tanh:return"tanh";case r.MathTrigonometryType.arcsinh:return"asinh";case r.MathTrigonometryType.arccosh:return"acosh";case r.MathTrigonometryType.arctanh:return"atanh";default:throw new Error("Unsopported trigonometric function:"+t)}},e.getAnyVariableFromNodes=function(t){for(const e of t){const t=i(e);if(t)return t}},e.getAllVariablesFromNodes=function(t){const e=new Map;for(const n of t)s(n,e);return[...e.values()]},e.getAnyVariable=i,e.getAllVariables=s},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const r=n(2);!function(t){t.BAD_ARGUMENT="BAD_ARGUMENT",t.METHOD_FAILURE="METHOD_FAILURE",t.BAD_METHOD="BAD_METHOD",t.CONNECTION_ERROR="CONNECTION_ERROR"}(e.SympyErrorCode||(e.SympyErrorCode={}));class i extends Error{constructor(t,e){super(e),this.code=t,this.message=e,Object.setPrototypeOf(this,i.prototype)}}e.SympyError=i;class s extends Error{constructor(t,e){super(e),this.name=t,Object.setPrototypeOf(this,s.prototype)}get preparedSympyCall(){return new a(this.wholeExpression)}}e.UnsupportedSympyConstruction=s;class a{constructor(t){this.token=t}stringify(){return this.token.accept(r.SympyStringify.instance)}}e.PreparedSympyCall=a,function(t){t.identical="identical",t.equiv="equiv",t.equivCalc="equivCalc",t.different="different"}(e.Equiv||(e.Equiv={})),function(t){t.first="first",t.second="second",t.none="none",t.unknown="unknown"}(e.Simpler||(e.Simpler={}));e.EquivResponse=class{constructor(t,e){this.eq=t,this.si=e}};e.PlotInterval=class{constructor(t,e,n){this.variable=t,this.from=e,this.to=n}asSympyTuple(){return"("+[this.variable.stringify(),this.from.stringify(),this.to.stringify()].join()+")"}};e.Plot2dParams=class{};e.Plot3dParams=class{}},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const r=n(0);class i{}e.SympyBaseToken=i;class s extends i{constructor(t){super(),this.type=t}}e.SympyToken=s;class a extends s{constructor(t,e){super(t),this.args=e}}e.SympyContainer=a;class o extends a{constructor(t,e){super(t,e)}accept(t){return t.visitFunction(this)}}e.SympyFunction=o;e.SympyMethod=class extends a{constructor(t,e,n){super("$Method",n),this.object=t,this.method=e}accept(t){return t.visitMethod(this)}};class c extends a{constructor(t,e){super("Function",e),this.name=t}accept(t){return t.visitUserFunction(this)}}e.SympyUserFunction=c;class p extends a{constructor(t,e){super("$Array",t),this.square=e}accept(t){return t.visitArray(this)}}e.SympyArray=p;class u extends s{constructor(t){super("$String"),this.v="",t&&(this.v=t)}append(t){this.v+=t}get value(){return this.v}accept(t){return t.visitString(this)}}e.SympyString=u;class y{visitFunction(t){return t.type+"("+t.args.map(t=>t.accept(this)).join(",")+")"}visitString(t){return t.value}visitArray(t){return t.square?"["+t.args.map(t=>t.accept(this)).join(",")+"]":"("+t.args.map(t=>t.accept(this)).join(",")+")"}visitUserFunction(t){return"Function("+t.name+")("+t.args.map(t=>t.accept(this)).join(",")+")"}visitMethod(t){return"("+t.object.accept(this)+")."+t.method+(t.args.length>0?"("+t.args.map(t=>t.accept(this)).join(",")+")":"")}}e.SympyStringify=y,y.instance=new y;class h extends i{}class l extends h{}class m extends h{}class S{constructor(){this.stack=[],this.currentString=null,this.inQuote=!1}push(t){this.stack.push(t),this.currentString=t instanceof u?t:null}parse(t){for(let e=0;e<t.length;e++){const n=t[e];if(this.inQuote)this.currentString.append(n),"'"===n&&(this.inQuote=!1);else switch("'"===n&&(this.inQuote=!0),n){case"(":this.push(new l);break;case"[":this.push(new m);break;case",":this.currentString=null;break;case")":{const t=[];for(;;){const e=this.stack.pop();if(!(e instanceof s)){if(e instanceof l){const e=this.stack.pop();if(e instanceof u){const n=new o(e.value,t);this.push(n)}else{e&&this.push(e);const n=new p(t,!1);this.push(n)}break}throw new Error("Unexpected token: "+JSON.stringify(e))}t.unshift(e)}}break;case"]":{const t=[];for(;;){const e=this.stack.pop();if(!(e instanceof s)){if(e instanceof m){const e=new p(t,!0);this.push(e);break}throw new Error("Unexpected token: "+JSON.stringify(e))}t.unshift(e)}}break;case" ":case"\t":case"\n":case"\r":break;default:this.currentString||this.push(new u),this.currentString.append(n)}}let e=!1;const n=this.stack.map(t=>{if(t instanceof s)return t;e=!0});if(e)throw new Error("Invalid expression:"+JSON.stringify(this.stack));return n}}class f{visitUserFunction(t){this.processArray(t.args)}visitFunction(t){this.processArray(t.args)}visitString(t){}visitArray(t){this.processArray(t.args)}visitMethod(t){this.processArray(t.args)}processArray(t){for(let e=0;e<t.length;e++){const n=this.asUserFunction(t[e],e+1<t.length?t[e+1]:null);n&&(t[e]=n,n.args.length>0&&this.shift(t,e+1))}for(const e of t)e.accept(this)}asUserFunction(t,e){if(t instanceof o&&"Function"===t.type&&1===t.args.length){const n=t.args[0];if(n instanceof u){const t=r.unquote(n.value);if(!e)return new c(t,[]);if(e instanceof p)return new c(t,e.args)}}}shift(t,e){for(let n=e;n<t.length-1;n++)t[n]=t[n+1];t.pop()}}f.instance=new f,e.rawParseSympyExpression=function(t){return(new S).parse(t)},e.parseSympyExpression=function(t){const e=function(t){return f.instance.processArray(t),t}((new S).parse(t));if(1===e.length)return e[0];throw new Error("More than one token as result: "+JSON.stringify(e))}},function(e,n){e.exports=t},function(t,e,n){"use strict";function r(t){for(var n in t)e.hasOwnProperty(n)||(e[n]=t[n])}Object.defineProperty(e,"__esModule",{value:!0}),r(n(1)),r(n(5)),r(n(0))},function(t,e,n){"use strict";var r=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))((function(i,s){function a(t){try{c(r.next(t))}catch(t){s(t)}}function o(t){try{c(r.throw(t))}catch(t){s(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,o)}c((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0});const i=n(2),s=n(6),a=n(7),o=n(8),c=n(0),p=n(3),u=n(1);class y{requestAsync(t,e,n,r,i){const s=new XMLHttpRequest;s.open(t,e,!0),s.onload=function(){if(this.status>=200&&this.status<400){const t=JSON.parse(this.response);r&&r(t)}else i&&i("bad HTTP status:"+this.status)},s.onerror=function(t){i&&i(t)},"POST"===t&&s.setRequestHeader("Content-Type","application/json;charset=UTF-8"),s.send(JSON.stringify(n))}}e.SympyClient=class{constructor(t,e){this.client=new s.SympyRESTClient(t,e||new y)}prepareCompute(t){return this.prepare(t,h.instance)}mirror(t,e){return r(this,void 0,void 0,(function*(){const n=t.token.accept(i.SympyStringify.instance);e&&console.log("RAW EXPRESSION TO SEND TO SYMPY: "+n);const r=yield this.client.callCustom("mirror",[n]);try{e&&console.log("RAW RESULT FROM SYMPY: "+r);const t=i.parseSympyExpression(r);return e&&console.log("PARSED RESULT FROM SYMPY: "+JSON.stringify(t)),a.fromSympy(t)}catch(t){throw console.log("RAW RESULT FROM SYMPY: "+r),t}}))}checkEquivalence(t,e){return r(this,void 0,void 0,(function*(){const n=[t.stringify(),e.stringify()];return this.client.callCustom("equiv",n)}))}compute(t){return r(this,void 0,void 0,(function*(){const e=yield this.client.callMethod(t.token.accept(i.SympyStringify.instance),"doit",[]),n=i.parseSympyExpression(e);return a.fromSympy(n)}))}simplify(t,e){return r(this,void 0,void 0,(function*(){const n=yield this.client.callFunction("simplify",[t.stringify()],{doit:!1});try{e&&console.log("RAW RESULT FROM SYMPY: "+n);const t=i.parseSympyExpression(n);return e&&console.log("PARSED RESULT FROM SYMPY: "+JSON.stringify(t)),a.fromSympy(t)}catch(t){throw console.log("RAW RESULT FROM SYMPY: "+n),t}}))}latex(t){return r(this,void 0,void 0,(function*(){return this.replaceAll(c.unquote(yield this.client.callFunction("latex",[t.token.accept(i.SympyStringify.instance)])),"\\\\\\\\","\\")}))}plot2dSrc(t,e,n,r){const i=t.map(t=>t.stringify());return n&&i.push(n.asSympyTuple()),this.client.plotSrc("plot",i,e,r)}plot2dParametricSrc(t,e,n,r){const i=t.map(t=>"("+t.x.stringify()+","+t.y.stringify()+")");return n&&i.push(n.asSympyTuple()),this.client.plotSrc("plot_parametric",i,e,r)}plot3dSrc(t,e,n,r){const i=t.map(t=>t.stringify());return n&&(i.push(n.i1.asSympyTuple()),i.push(n.i2.asSympyTuple())),this.client.plotSrc("plot3d",i,e,r)}preparePlotInterval(t,e,n){const r=this;return e||(e=-10),n||(n=10),new u.PlotInterval(this.prepareCompute(t),i(e),i(n));function i(t){return t<0?r.prepareCompute(new p.MathUnaryMinus(new p.MathNumber(""+Math.abs(t)))):r.prepareCompute(new p.MathNumber(""+t))}}plot3dParametricLineSrc(t,e,n){const r=t.map(t=>"("+t.x.stringify()+","+t.y.stringify()+","+t.z.stringify()+","+t.interval.asSympyTuple()+")");return this.client.plotSrc("plot3d_parametric_line",r,e,n)}plot3dParametricSurfaceSrc(t,e,n){const r=t.map(t=>"("+t.x.stringify()+","+t.y.stringify()+","+t.z.stringify()+","+t.r_u.asSympyTuple()+","+t.r_v.asSympyTuple()+")");return this.client.plotSrc("plot3d_parametric_surface",r,e,n)}plot2d(t,e,n,r,i){const s=document.createElement("img"),a=this;return n&&(s.onerror=()=>{const s=t.map(t=>t.stringify());r&&s.push(r.asSympyTuple()),a.client.checkPlotValidity("plot",s,e,i).catch(t=>{n(t)})}),s.src=this.plot2dSrc(t,e,r,i),s}plot3d(t,e,n,r,i){const s=document.createElement("img"),a=this;return n&&(s.onerror=()=>{const s=t.map(t=>t.stringify());r&&(s.push(r.i1.asSympyTuple()),s.push(r.i2.asSympyTuple())),a.client.checkPlotValidity("plot3d",s,e,i).catch(t=>{n(t)})}),s.src=this.plot3dSrc(t,e,r,i),s}plot2d_parametric(t,e,n,r,i){const s=document.createElement("img"),a=this;return n&&(s.onerror=()=>{const s=t.map(t=>"("+t.x.stringify()+","+t.y.stringify()+")");r&&s.push(r.asSympyTuple()),a.client.checkPlotValidity("plot_parametric",s,e,i).catch(t=>{n(t)})}),s.src=this.plot2dParametricSrc(t,e,r,i),s}plot3d_parametric_line(t,e,n,r){const i=document.createElement("img"),s=this;return n&&(i.onerror=()=>{const i=t.map(t=>"("+t.x.stringify()+","+t.y.stringify()+","+t.z.stringify()+","+t.interval.asSympyTuple()+")");s.client.checkPlotValidity("plot3d_parametric_line",i,e,r).catch(t=>{n(t)})}),i.src=this.plot3dParametricLineSrc(t,e,r),i}plot3d_parametric_surface(t,e,n,r){const i=document.createElement("img"),s=this;return n&&(i.onerror=()=>{const i=t.map(t=>"("+t.x.stringify()+","+t.y.stringify()+","+t.z.stringify()+","+t.r_u.asSympyTuple()+","+t.r_v.asSympyTuple()+")");s.client.checkPlotValidity("plot3d_parametric_surface",i,e,r).catch(t=>{n(t)})}),i.src=this.plot3dParametricSurfaceSrc(t,e,r),i}replaceAll(t,e,n){return t.replace(new RegExp(e,"g"),n)}prepare(t,e){try{return new u.PreparedSympyCall(p.acceptMathNode(t,e))}catch(t){if(t instanceof o.InvalidNodeError)return t.error;throw t}}};class h extends o.BaseSymPyVisitor{visitDifferential(t){throw new o.InvalidNodeError(t,"compute operation does not support standalone differential")}visitSet(t){throw new o.InvalidNodeError(t,"compute operation does not support sets")}visitSystemOfEquations(t){throw new o.InvalidNodeError(t,"compute operation does not support system of equations")}}h.instance=new h},function(t,e,n){"use strict";var r=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))((function(i,s){function a(t){try{c(r.next(t))}catch(t){s(t)}}function o(t){try{c(r.throw(t))}catch(t){s(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,o)}c((r=r.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0});const i=n(1);class s{constructor(t,e,n){this.method=t,this.args=e,this.params=n}}class a extends s{constructor(t,e,n){super(e,n),this.object=t}}e.SympyRESTClient=class{constructor(t,e){this.CUSTOM="/api/v1/custom",this.FUNCTION="/api/v1/function",this.METHOD="/api/v1/method",this.PLOT="/api/v1/plot",this.http=e,this.serverAddress=t}plotSrc(t,e,n,r,i){let s=this.serverAddress+this.PLOT+"?method="+t+"&args="+encodeURIComponent(JSON.stringify(e));return r&&(s+="&params="+encodeURIComponent(JSON.stringify(r))),i&&(s+="&checkOnly=true"),n&&(s+="&format=svg"),s}checkPlotValidity(t,e,n,s){return r(this,void 0,void 0,(function*(){const r=this,a=this.plotSrc(t,e,n,s,!0);return console.log(a),new Promise((t,e)=>{r.http.requestAsync("GET",a,null,n=>{n.ok?t(!0):e(new i.SympyError(n.errorCode,n.error))},t=>{e(new i.SympyError(i.SympyErrorCode.CONNECTION_ERROR,"connection error"))})})}))}callFunction(t,e,n){const r=this;return new Promise((a,o)=>{r.http.requestAsync("POST",this.serverAddress+this.FUNCTION,new s(t,e,n),t=>{t.ok?a(t.result):o(new i.SympyError(t.errorCode,t.error))},t=>{o(new i.SympyError(i.SympyErrorCode.CONNECTION_ERROR,"connection error"))})})}callMethod(t,e,n){const r=this;return new Promise((s,o)=>{r.http.requestAsync("POST",this.serverAddress+this.METHOD,new a(t,e,n),t=>{t.ok?s(t.result):o(new i.SympyError(t.errorCode,t.error))},t=>{o(new i.SympyError(i.SympyErrorCode.CONNECTION_ERROR,"connection error"))})})}callCustom(t,e){const n=this;return new Promise((r,a)=>{n.http.requestAsync("POST",this.serverAddress+this.CUSTOM,new s(t,e),t=>{t.ok?r(t.result):a(new i.SympyError(t.errorCode,t.error))},t=>{a(new i.SympyError(i.SympyErrorCode.CONNECTION_ERROR,"connection error"))})})}}},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const r=n(2),i=n(0),s=n(3),a=n(1);function o(t){if(t instanceof r.SympyString)return t.value;throw new Error("Expected string at: "+t.accept(r.SympyStringify.instance))}function c(t){if(t instanceof r.SympyString)return"true"===t.value;throw new Error("Expected string at: "+t.accept(r.SympyStringify.instance))}function p(t){if(t instanceof r.SympyString){const e=i.unquote(t.value);if("-"===e[0]){const t=new s.MathNumber(e.substring(1));return new s.MathUnaryMinus(t)}return new s.MathNumber(e)}throw new Error("Expected string at: "+t.accept(r.SympyStringify.instance))}e.fromSympy=function(t){try{return t.accept(l.instance)}catch(e){throw e instanceof a.UnsupportedSympyConstruction&&(e.wholeExpression=t),e}};const u=new Map;u.set("Integer",(t,e)=>p(t[0])),u.set("Float",(t,e)=>p(t[0])),u.set("Rational",(t,e)=>{let n=i.unquote(o(t[0])),r=i.unquote(o(t[1])),a=!1;n.startsWith("-")&&(a=!a,n=n.substring(1)),r.startsWith("-")&&(a=!a,r=r.substring(1));const c=new s.MathDivision(new s.MathNumber(n),new s.MathNumber(r));return a?new s.MathUnaryMinus(c):c}),u.set("Or",(t,e)=>new s.MathOr(e.accept(t[0]),e.accept(t[1]))),u.set("And",(t,e)=>new s.MathAnd(e.accept(t[0]),e.accept(t[1]))),u.set("Equality",(t,e)=>new s.MathEquality(s.MathEqualityType.equals,e.accept(t[0]),e.accept(t[1]))),u.set("Unequality",(t,e)=>new s.MathEquality(s.MathEqualityType.notEquals,e.accept(t[0]),e.accept(t[1]))),u.set("GreaterThan",(t,e)=>new s.MathEquality(s.MathEqualityType.greaterOrEquals,e.accept(t[0]),e.accept(t[1]))),u.set("LessThan",(t,e)=>new s.MathEquality(s.MathEqualityType.lessOrEquals,e.accept(t[0]),e.accept(t[1]))),u.set("StrictLessThan",(t,e)=>new s.MathEquality(s.MathEqualityType.lessThan,e.accept(t[0]),e.accept(t[1]))),u.set("StrictGreaterThan",(t,e)=>new s.MathEquality(s.MathEqualityType.greaterThan,e.accept(t[0]),e.accept(t[1]))),u.set("Point2D",(t,e)=>new s.MathPoint(e.join(t))),u.set("Point3D",(t,e)=>new s.MathPoint(e.join(t))),u.set("Pow",(t,e)=>{const n=e.accept(t[0]),i=t[1];if(i instanceof r.SympyFunction){if("Rational"===i.type){const t=i.args[0],e=i.args[1];if(t instanceof r.SympyString&&e instanceof r.SympyString&&"1"===t.value){if("2"===e.value)return new s.MathSquareRoot(n);if(e instanceof r.SympyString)return new s.MathRoot(n,p(e))}}if("Integer"===i.type){const t=i.args[0];if(t instanceof r.SympyString&&"-1"===t.value)return new s.MathDivision(new s.MathNumber("1"),n)}}return new s.MathPower(n,e.accept(i))}),u.set("Add",(t,e)=>{const n=e.accept(t[0]),r=e.accept(t[1]);return r instanceof s.MathUnaryMinus?new s.MathMinus(n,r.expression):new s.MathPlus(n,r)}),u.set("Mul",(t,e)=>{const n=e.accept(t[0]),r=e.accept(t[1]);return r instanceof s.MathDivision&&r.numerator instanceof s.MathNumber&&"1"===r.numerator.value?new s.MathDivision(n,r.denominator):n instanceof s.MathDivision&&n.numerator instanceof s.MathNumber&&"1"===n.numerator.value?new s.MathDivision(r,n.denominator):n instanceof s.MathNumber?new s.MathImplicitMultiply(n,r):new s.MathMultiply(n,r)}),u.set("Abs",(t,e)=>new s.MathAbs(e.accept(t[0]))),u.set("Interval",(t,e)=>{const n=e.accept(t[0]),r=e.accept(t[1]),i=t.length>2&&c(t[2]),a=t.length>3&&c(t[3]);let o;return o=i?a?s.MathBracketsType.open:s.MathBracketsType.rightClosed:a?s.MathBracketsType.leftClosed:s.MathBracketsType.closed,new s.MathInterval(o,n,r)});for(const t of Object.keys(s.MathTrigonometryType)){const e=s.MathTrigonometryType[t];u.set(i.getSympyTrigName(e),(t,n)=>new s.MathTrigonometricFunction(e,n.accept(t[0])))}u.set("log",(t,e)=>{const n=e.accept(t[0]);return t.length>1?new s.MathLogarithm(e.accept(t[1]),n):new s.MathNaturalLogarithm(n)}),u.set("Min",(t,e)=>new s.MathMin(e.join(t))),u.set("Max",(t,e)=>new s.MathMax(e.join(t))),u.set("Sum",(t,e)=>{const n=e.accept(t[0]),i=t[1];if(i instanceof r.SympyFunction&&"Tuple"===i.type){const t=e.accept(i.args[0]),r=e.accept(i.args[1]),a=e.accept(i.args[2]);return new s.MathSum(n,t,r,a)}for(const e of t)console.log(e.accept(r.SympyStringify.instance));throw new Error("^ Bad sum ^")}),u.set("Product",(t,e)=>{const n=e.accept(t[0]),i=t[1];if(i instanceof r.SympyFunction&&"Tuple"===i.type){const t=e.accept(i.args[0]),r=e.accept(i.args[1]),a=e.accept(i.args[2]);return new s.MathProduct(n,t,r,a)}for(const e of t)console.log(e.accept(r.SympyStringify.instance));throw new Error("^ Bad prod ^")}),u.set("Contains",(t,e)=>{const n=e.accept(t[0]),r=e.accept(t[1]);return new s.MathIncludeSet(n,r)});u.set("Complexes",()=>new s.MathNumericSet(s.MathNumericSetValues.C)),u.set("ComplexRegion",(t,e)=>{const n=t[0];if(n instanceof r.SympyFunction&&"ProductSet"===n.type){if(2!=n.args.length)throw new Error("Usupported complexes: "+t.map(t=>t.accept(r.SympyStringify.instance)).join(","));for(const e of n.args)if(!(e instanceof r.SympyString)||"Reals"!==e.value)throw new Error("Usupported complexes: "+t.map(t=>t.accept(r.SympyStringify.instance)).join(","));return new s.MathNumericSet(s.MathNumericSetValues.C)}}),u.set("Limit",(t,e)=>{let n=s.MathLimitType.unspecified;if(t.length>3){const e=t[3];if(e&&"Symbol"===e.type){const t=o(e.args[0]);"'+'"===t?n=s.MathLimitType.right:"'-'"===t&&(n=s.MathLimitType.left)}}return new s.MathLimit(e.accept(t[0]),e.accept(t[1]),e.accept(t[2]),n)}),u.set("Integral",(t,e)=>{const n=e.accept(t[0]),i=t[1];if(i instanceof r.SympyFunction&&"Tuple"===i.type){const t=i.args[0];return 1===i.args.length?new s.MathIndefiniteIntegral(n,e.accept(t)):2===i.args.length?new s.MathAreaIntegral(n,e.accept(t),e.accept(i.args[1])):new s.MathDefiniteIntegral(n,e.accept(t),e.accept(i.args[1]),e.accept(i.args[2]))}return new s.MathIndefiniteIntegral(n,e.accept(i))});const y=(t,e)=>{const n=[],i=t[0];if(i instanceof r.SympyArray){for(const t of i.args)if(t instanceof r.SympyArray){const r=e.join(t.args);n.push(r)}return new s.MathDeterminant(n)}};u.set("ImmutableDenseMatrix",y),u.set("MutableDenseMatrix",y),u.set("Matrix",y),u.set("Piecewise",(t,e)=>{let n=[],i=[];for(const s of t)if(s instanceof r.SympyFunction&&"ExprCondPair"===s.type){const t=e.accept(s.args[0]),r=e.accept(s.args[1]);n.push(t),i.push(r)}return new s.MathPiecewiseFunction(n,i)}),u.set("Heaviside",(t,e)=>new s.MathPiecewiseFunction([new s.MathNumber("0"),new s.MathNumber("1")],[new s.MathEquality(s.MathEqualityType.lessThan,e.accept(t[0]),new s.MathNumber("0")),new s.MathEquality(s.MathEqualityType.greaterOrEquals,e.accept(t[0]),new s.MathNumber("0"))])),u.set("AccumBounds",(t,e)=>new s.MathExpressionSet([p(t[0]),p(t[1])])),u.set("Union",(t,e)=>new s.MathUnion(e.accept(t[0]),e.accept(t[1]))),u.set("Intersection",(t,e)=>new s.MathIntersection(e.accept(t[0]),e.accept(t[1]))),u.set("Derivative",(t,e)=>{const n=e.accept(t[0]);if(!(t.length>1))return new s.MathDerivative(n,1,s.MathDerivativeType.prime);{const i=t[1];if(i instanceof r.SympyFunction){if("Tuple"===i.type){const t=i.args[0],r=i.args[1],a=e.accept(r);return new s.MathLeibnizDerivative(Number.parseInt(a.value),n,e.accept(t),s.MathLeibnizDerivativeType.basic)}return new s.MathLeibnizDerivative(1,n,e.accept(i),s.MathLeibnizDerivativeType.withParentheses)}}}),u.set("Symbol",(t,e)=>e.variableToMathStructure(i.unquote(o(t[0])))),u.set("EmptySet",(t,e)=>new s.MathEmptySet),u.set("Range",(t,e)=>{const n=t.map(t=>t instanceof r.SympyString?p(t):e.accept(t));return new s.MathExpressionSet(n)}),u.set("exp",(t,e)=>new s.MathPower(new s.MathConstant(s.MathConstants.e),e.accept(t[0])));const h=new Map;h.set("nan",t=>new s.MathUndefined),h.set("I",t=>new s.MathConstant(s.MathConstants.imaginaryUnit)),h.set("zoo",t=>new s.MathConstant(s.MathConstants.complexInfinity)),h.set("oo",t=>new s.MathConstant(s.MathConstants.infinity)),h.set("pi",t=>new s.MathConstant(s.MathConstants.pi)),h.set("E",t=>new s.MathConstant(s.MathConstants.e)),h.set("-E",t=>new s.MathUnaryMinus(new s.MathConstant(s.MathConstants.e))),h.set("-oo",t=>new s.MathUnaryMinus(new s.MathConstant(s.MathConstants.infinity))),h.set("-pi",t=>new s.MathUnaryMinus(new s.MathConstant(s.MathConstants.pi))),h.set("Naturals",t=>new s.MathNumericSet(s.MathNumericSetValues.N)),h.set("Complexes",t=>new s.MathNumericSet(s.MathNumericSetValues.C)),h.set("Reals",t=>new s.MathNumericSet(s.MathNumericSetValues.R)),h.set("Integers",t=>new s.MathNumericSet(s.MathNumericSetValues.Z)),h.set("true",t=>new s.MathNumber("1")),h.set("false",t=>new s.MathNumber("0")),h.set("EmptySet",t=>new s.MathEmptySet);class l{join(t){const e=this;return t.map(t=>t.accept(e))}accept(t){return t.accept(this)}visitMethod(t){throw new Error("Method in sumpy respone: "+t.accept(r.SympyStringify.instance))}visitUserFunction(t){const e=this.variableToMathStructure(t.name);return new s.MathFunction(e,this.join(t.args))}visitFunction(t){const e=u.get(t.type);if(e)return e(t.args,this);throw new a.UnsupportedSympyConstruction(t.type,"Unknown function: "+t.accept(r.SympyStringify.instance))}visitString(t){const e=h.get(t.value);if(e)return e(this);throw new a.UnsupportedSympyConstruction(t.value,"Unknown string: "+t.value)}visitArray(t){throw new Error("Standalone array found: "+t.accept(r.SympyStringify.instance))}variableToMathStructure(t){const e=t.split("_"),n=e[0],r=s.LettersUtils.getByValue(n,s.GreekLetters),i=1==e.length?void 0:[];for(let t=1;t<e.length;t++){const n=this.variableToMathStructure(e[t]);i.push(n)}return new s.MathVariable(n,!!r,i)}}l.instance=new l},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const r=n(2),i=n(0),s=n(3);class a extends Error{constructor(t,e){super(),this.error=new s.SemanticErrorDescription(s.MathValidationUtils.collectSymbols(t),e),Object.setPrototypeOf(this,a.prototype)}}function o(t){return new r.SympyFunction("Symbol",[new r.SympyString("'"+t+"'")])}function c(t){return new r.SympyFunction("Integer",[new r.SympyString(t.toString())])}e.InvalidNodeError=a,e.integer=c;e.BaseSymPyVisitor=class{accept(t){return s.acceptMathNode(t,this)}join(t){const e=this;return t.map(t=>e.accept(t))}visitAbs(t){return new r.SympyFunction("Abs",[this.accept(t.expression)])}visitAnd(t){return new r.SympyFunction("And",[this.accept(t.left),this.accept(t.right)])}visitAreaIntegral(t){return new r.SympyFunction("Integral",[this.accept(t.expression),new r.SympyArray([this.accept(t.differential),this.accept(t.area)],!1)])}visitBrackets(t){throw new a(t,"this operation does not support brackets")}visitComma(t){throw new a(t,"unexpected comma")}visitUndefined(t){return new r.SympyString("nan")}visitConstant(t){switch(t.value){case s.MathConstants.e:return new r.SympyString("E");case s.MathConstants.pi:return new r.SympyString("pi");case s.MathConstants.infinity:return new r.SympyString("oo");case s.MathConstants.imaginaryUnit:return new r.SympyString("I");case s.MathConstants.complexInfinity:return new r.SympyString("zoo");default:throw new Error("Unknown constant:"+t.value)}}visitDefiniteIntegral(t){return new r.SympyFunction("Integral",[this.accept(t.expression),new r.SympyArray(this.join([t.differential,t.lowerBound,t.upperBound]),!1)])}visitDerivative(t){if(1==t.degree)return new r.SympyFunction("Derivative",[this.accept(t.base)]);{const e=i.getAnyVariable(t.base);if(!e)throw new a(t,"Can't find any variable to derive");return new r.SympyFunction("Derivative",[this.accept(t.base),o(e.value),c(t.degree)])}}visitDeterminant(t){return new r.SympyFunction("Matrix",[new r.SympyArray(t.elements.map(t=>new r.SympyArray(this.join(t),!0)),!0)])}visitFunctionDifference(t){let e;if(t.downLimitVariable){if(!(t.downLimitVariable instanceof s.MathVariable&&t.upLimitValue instanceof s.MathVariable))throw new a(t,"Invalid limit variable");if(s.acceptMathNode(t.downLimitVariable,s.MathNodePrinter.instance)!==s.acceptMathNode(t.upLimitVariable,s.MathNodePrinter.instance))throw new a(t,"Up and down limits have different variables");e=t.downLimitVariable}else e=i.getAnyVariable(t);if(!e)throw new a(t,"The function contains no variable");const n=new r.SympyMethod(this.accept(t.func),"subs",[this.accept(e),this.accept(t.upLimitValue)]),o=new r.SympyMethod(this.accept(t.func),"subs",[this.accept(e),this.accept(t.downLimitValue)]);return new r.SympyFunction("Add",[n,new r.SympyFunction("Mul",[c(-1),o])])}visitDivision(t){return new r.SympyFunction("Mul",[this.accept(t.numerator),new r.SympyFunction("Pow",[this.accept(t.denominator),c(-1)])])}visitEmptySet(t){return new r.SympyString("EmptySet")}visitEquality(t){let e;switch(t.equalityType){case s.MathEqualityType.equals:e="Eq";break;case s.MathEqualityType.lessThan:e="Lt";break;case s.MathEqualityType.greaterThan:e="Gt";break;case s.MathEqualityType.lessOrEquals:e="Le";break;case s.MathEqualityType.greaterOrEquals:e="Ge";break;case s.MathEqualityType.notEquals:e="Ne";break;case s.MathEqualityType.approxEquals:throw new a(t,"SymPy does not support approximate equality")}return new r.SympyFunction(e,this.join([t.left,t.right]))}visitExpressionSet(t){return new r.SympyFunction("Range",this.join(t.set))}visitFunction(t){const e=this.accept(t.operator);if(e instanceof r.SympyFunction&&"Symbol"===e.type){const n=e.args[0].accept(r.SympyStringify.instance);return new r.SympyUserFunction(n,this.join(t.operands))}}visitImplicitMultiply(t){return new r.SympyFunction("Mul",this.join([t.left,t.right]))}visitIncludeSet(t){return new r.SympyMethod(this.accept(t.set),"contains",[this.accept(t.fexp)])}visitIndefiniteIntegral(t){return new r.SympyFunction("Integral",this.join([t.expression,t.differential]))}visitInfimum(t){return new r.SympyMethod(this.accept(t.operand),"inf",[])}visitIntersection(t){return new r.SympyMethod(this.accept(t.left),"intersect",[this.accept(t.right)])}visitInterval(t){let e;switch(t.intervalType){case s.MathBracketsType.open:e=".open";break;case s.MathBracketsType.closed:e="";break;case s.MathBracketsType.leftClosed:e=".Ropen";break;case s.MathBracketsType.rightClosed:e=".Lopen";break;default:throw Error("Unknown interval type "+t.intervalType)}return new r.SympyFunction("Interval"+e,this.join([t.leftBound,t.rightBound]))}visitLeibnizDerivative(t){return new r.SympyFunction("Derivative",[this.accept(t.operand),this.accept(t.variable),c(t.degree)])}visitLimit(t){let e;switch(t.limitType){case s.MathLimitType.unspecified:break;case s.MathLimitType.left:e=new r.SympyString("'-'");break;case s.MathLimitType.right:e=new r.SympyString("'+'")}const n=this.join([t.func,t.input,t.value]);return e&&n.push(e),new r.SympyFunction("Limit",n)}visitLogarithm(t){return new r.SympyFunction("log",this.join([t.operand,t.base]))}visitMax(t){return new r.SympyFunction("Max",this.join(t.operands))}visitMin(t){return new r.SympyFunction("Min",this.join(t.operands))}visitMinus(t){return new r.SympyFunction("Add",[this.accept(t.left),new r.SympyFunction("Mul",[c(-1),this.accept(t.right)])])}visitMultiply(t){return new r.SympyFunction("Mul",this.join([t.left,t.right]))}visitNaturalLogarithm(t){return new r.SympyFunction("log",[this.accept(t.operand)])}visitNumber(t){const e=+t.value;return Number.isInteger(e)?c(e):function(t){return new r.SympyFunction("Float",[new r.SympyString(t.toString())])}(e)}visitNumericSet(t){switch(t.value){case s.MathNumericSetValues.C:return new r.SympyMethod(new r.SympyString("S"),"Complexes",[]);case s.MathNumericSetValues.N:return new r.SympyMethod(new r.SympyString("S"),"Naturals",[]);case s.MathNumericSetValues.Q:throw new a(t,"SymPy does not support numeric set "+t.value);case s.MathNumericSetValues.R:return new r.SympyMethod(new r.SympyString("S"),"Reals",[]);case s.MathNumericSetValues.Z:return new r.SympyMethod(new r.SympyString("S"),"Integers",[]);default:throw Error("Unsupported numeric set "+t.value)}}visitOr(t){return new r.SympyFunction("Or",this.join([t.left,t.right]))}visitParentheses(t){return this.accept(t.expression)}visitPiecewiseFunction(t){return new r.SympyFunction("Piecewise",t.elements.map((e,n)=>new r.SympyArray([this.accept(e),this.accept(t.predicates[n])],!1)))}visitPlaceholder(t){throw new a(t,"unexpected empty space")}visitPlus(t){return new r.SympyFunction("Add",this.join([t.left,t.right]))}visitPoint(t){if(2===t.terms.length)return new r.SympyFunction("Point2D",this.join(t.terms));if(3===t.terms.length)return new r.SympyFunction("Point3D",this.join(t.terms));throw new a(t,"We only support 2D and 3D points")}visitPower(t){return new r.SympyFunction("Pow",this.join([t.base,t.exponent]))}visitPredicate(t){throw new a(t,"this operation does not support predicates")}visitProduct(t){return new r.SympyFunction("Product",[this.accept(t.sequence),new r.SympyArray(this.join([t.index,t.lowerBound,t.upperBound]),!1)])}visitRawText(t){throw new a(t,"unexpected raw text")}visitReversedFunction(t){throw new a(t,"this operation does not support reversed function")}visitRoot(t){return new r.SympyFunction("Pow",[this.accept(t.base),new r.SympyFunction("Rational",[c(1),this.accept(t.degree)])])}visitSquareRoot(t){return new r.SympyFunction("Pow",[this.accept(t.base),new r.SympyFunction("Rational",[c(1),c(2)])])}visitSubIndex(t){throw new a(t,"unexpected low index")}visitSum(t){return new r.SympyFunction("Sum",[this.accept(t.sequence),new r.SympyArray(this.join([t.index,t.lowerBound,t.upperBound]),!1)])}visitSupremum(t){return new r.SympyMethod(this.accept(t.operand),"sup",[])}visitSymbol(t){throw new a(t,"unexpected standalone symbol")}visitVerticalBar(t){throw new a(t,"unexpected vertical bar")}visitTransformation(t){throw new a(t,"unexpected symbol")}visitTrigonometricFunction(t){const e=i.getSympyTrigName(t.funcName);return new r.SympyFunction(e,[this.accept(t.operand)])}visitUnaryMinus(t){return new r.SympyFunction("Mul",[c(-1),this.accept(t.expression)])}visitUnion(t){return new r.SympyFunction("Union",this.join([t.left,t.right]))}visitVariable(t){let e=t.value;if(t.indexes.length>0){e+="_"+this.join(t.indexes).map(t=>t.accept(r.SympyStringify.instance)).join("_")}return o(e)}}}])}));