forked from Pomax/mathparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathFunction.min.js
More file actions
executable file
·35 lines (35 loc) · 17.5 KB
/
MathFunction.min.js
File metadata and controls
executable file
·35 lines (35 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(function(L,G,D,n,H,w){var e=function(){};e.prototype={left:!1,right:!1,hasRight:function(){return!1!==this.right},hasLeft:function(){return!1!==this.left},setLeaves:function(a,b){this.left=a;this.right=b},hasLeaves:function(){return this.left&&this.right},evaluate:function(){return!1},getParameters:function(){var a=[];this.addParametersFromChild(a,this.left);this.addParametersFromChild(a,this.right);return a},addParametersFromChild:function(a,b){b&&b.getParameters().forEach(function(b){-1<a.indexOf(b)||
a.push(b)})},plot:function(a,b,c,d,e){var h=[],f=[],g=[a];e=e||[];for(e.forEach(function(a){g.push(a.label);f.push(a.value)});b<=c;b+=d)a=this.evaluate(g,[b].concat(f)),h.push([b,a]);return h},replace:function(a,b){var c=this.left,d=this.right;c&&(c instanceof k&&c.label===a)&&(this.left=b);d&&(d instanceof k&&d.label===a)&&(this.right=b)}};var l=function(a){this.value=parseFloat(a)};l.prototype=new e;l.prototype.nf=function(a){var b=(""+a).length-1-(""+(a|0)).length;0>b&&(b=0);return a.toFixed(b)};
l.prototype.evaluate=function(){return this.value};l.prototype.toString=function(){return this.nf(this.value)};l.prototype.toLaTeX=function(){return"{"+this.nf(this.value)+"}"};l.prototype.getParameters=function(){return[]};l.prototype.derive=function(){return this};var k=function(a){this.label=a};k.prototype=new e;k.prototype.evaluate=function(){return 1==arguments.length?this.evaluate$1(arguments[0]):2==arguments.length?this.evaluate$2(arguments[0],arguments[1]):!1};k.prototype.evaluate$1=function(a){return a};
k.prototype.evaluate$2=function(a,b){for(var c=0,d=a.length;c<d;c++)if(a[c]==this.label)return b[c];return!1};k.prototype.getParameters=function(){return[this.label]};k.prototype.toString=function(){return this.label};k.prototype.toLaTeX=function(){return this.label};var g=function(a,b){this.label=a;this.value=b};g.prototype=new l;var E=function(){};E.prototype=new g("\u03c0",Math.PI);E.prototype.toLaTeX=function(){return"\u03c0"};var F=function(){};F.prototype=new g("e",Math.E);F.prototype.toLaTeX=
function(){return"e"};var I=function(a){return"\u03c0"==a||"pi"==a?new E:"e"==a?new F:a==parseFloat(a)?new l(a):new k(a)},g=function(a,b){this.operator=a;this.strength=b};g.prototype=new e;g.prototype.getStrength=function(){return this.strength};g.prototype.toString=function(){var a=this.left,b=this.right;return(a?a.toString():"")+this.operator+(b?b.toString():"")};g.prototype.toLaTeX=function(){return this.left.toLaTeX()+this.operator+this.right.toLaTeX()};var p={},x={},f=function(){};f.prototype=
new g("+",1);f.prototype.evaluate=function(a,b){var c=this.right;return this.left.evaluate(a,b)+c.evaluate(a,b)};p["+"]=f;f=function(){};f.prototype=new g("-",1);f.prototype.evaluate=function(a,b){var c=this.right;return this.left.evaluate(a,b)-c.evaluate(a,b)};p["-"]=f;f=function(){};f.prototype=new g("*",2);f.prototype.evaluate=function(a,b){var c=this.right;return this.left.evaluate(a,b)*c.evaluate(a,b)};f.prototype.toLaTeX=function(){return this.left.toLaTeX()+" \\cdot "+this.right.toLaTeX()};
p["*"]=f;f=function(){};f.prototype=new g("/",2);f.prototype.evaluate=function(a,b){var c=this.right;return this.left.evaluate(a,b)/c.evaluate(a,b)};f.prototype.toLaTeX=function(){return"\\frac{"+this.left.toLaTeX()+"}{"+this.right.toLaTeX()+"}"};p["/"]=f;f=function(){};f.prototype=new g("^",4);f.prototype.evaluate=function(a,b){var c=this.right;return Math.pow(this.left.evaluate(a,b),c.evaluate(a,b))};p["^"]=f;f=function(){};f.prototype=new g("-",3);f.prototype.hasLeft=function(){return!1};f.prototype.hasLeaves=
function(){return!1!==this.right};f.prototype.setLeaves=function(a,b){this.right=b};f.prototype.evaluate=function(a,b){return-this.right.evaluate(a,b)};f.prototype.toLaTeX=function(){return"-"+this.right.toLaTeX()};x["-"]=f;f=function(){};f.prototype=new g("!",5);f.prototype.hasLeft=function(){return!1};f.prototype.hasLeaves=function(){return!1!==this.left};f.prototype.setLeaves=function(a){this.left=a};f.prototype.factorial=function(a){return 1>=a?1:a*this.factorial(a-1)};f.prototype.evaluate=function(a,
b){var c=this.left.evaluate(a,b)|0;return this.factorial(c)};f.prototype.toLaTeX=function(){return this.left.toLaTeX()+"!"};x["!"]=f;g=function(a){this.label=a};g.prototype=new e;g.prototype.setContent=function(a){this.setLeaves(!1,a);this.content=a};g.prototype.replace=function(a,b){if(this.content instanceof k&&this.content.label===a){var c=new m(b.toString());this.setContent(c.functionTree)}else this.content.replace(a,b)};g.prototype.toString=function(){return this.label+"("+this.content.toString()+
")"};g.prototype.toLaTeX=function(){return this.label+"\\left ( "+this.content.toLaTeX()+" \\right ) "};var t=function(a){this.setContent(a)};t.prototype=new g("wrapper");t.prototype.toString=function(){return"("+this.content.toString()+")"};t.prototype.toLaTeX=function(){return" \\left ( "+this.content.toLaTeX()+" \\right ) "};t.prototype.evaluate=function(a,b){return this.content.evaluate(a,b)};var j={},e=function(a){this.setContent(a)};e.prototype=new g("sin");e.prototype.evaluate=function(a,b){return Math.sin(this.content.evaluate(a,
b))};j.sin=e;e=function(a){this.setContent(a)};e.prototype=new g("cos");e.prototype.evaluate=function(a,b){return Math.cos(this.content.evaluate(a,b))};j.cos=e;e=function(a){this.setContent(a)};e.prototype=new g("tan");e.prototype.evaluate=function(a,b){return Math.tan(this.content.evaluate(a,b))};j.tan=e;e=function(a){this.setContent(a)};e.prototype=new g("asin");e.prototype.evaluate=function(a,b){return Math.asin(this.content.evaluate(a,b))};j.asin=e;e=function(a){this.setContent(a)};e.prototype=
new g("acos");e.prototype.evaluate=function(a,b){return Math.acos(this.content.evaluate(a,b))};j.acos=e;e=function(a){this.setContent(a)};e.prototype=new g("atan");e.prototype.evaluate=function(a,b){return Math.atan(this.content.evaluate(a,b))};j.atan=e;e=function(a){this.setContent(a)};e.prototype=new g("sinh");e.prototype.evaluate=function(a,b){return Math.sinh(this.content.evaluate(a,b))};j.sinh=e;e=function(a){this.setContent(a)};e.prototype=new g("cosh");e.prototype.evaluate=function(a,b){return Math.cosh(this.content.evaluate(a,
b))};j.cosh=e;e=function(a){this.setContent(a)};e.prototype=new g("tanh");e.prototype.evaluate=function(a,b){return Math.tanh(this.content.evaluate(a,b))};j.tanh=e;e=function(a){this.setContent(a)};e.prototype=new g("ln");e.prototype.evaluate=function(a,b){var c=this.content.evaluate(a,b);return Math.log(c)/Math.log(Math.E)};j.ln=e;e=function(a){this.setContent(a)};e.prototype=new g("log");e.prototype.evaluate=function(a,b){var c=this.content.evaluate(a,b);return Math.log(c)/Math.log(10)};j.log=e;
e=function(a){this.setContent(a)};e.prototype=new g("sqrt");e.prototype.evaluate=function(a,b){return Math.sqrt(this.content.evaluate(a,b))};e.prototype.toLaTeX=function(){return"\\sqrt{"+this.content.toLaTeX()+"}"};j.sqrt=e;e=function(a){this.setContent(a)};e.prototype=new g("abs");e.prototype.evaluate=function(a,b){return Math.abs(this.content.evaluate(a,b))};e.prototype.toLaTeX=function(){return"|"+this.content.toLaTeX()+"|"};j.abs=e;e=function(a){this.label=a};e.prototype=new g;e.prototype.getParameters=
function(){return this.content.getParameters()};e.prototype.bindAll=function(a,b,c){this.startValue=c;this.setContent(b);params=[];a.forEach(function(a){params.push(I(a))});this.parameters=params};e.prototype.evaluate=function(a,b){var c;c=a.indexOf("n");-1===c&&(c=a.length,a.push("n"));var d=this.parameters[0].evaluate(a,b),e=this.parameters[1].evaluate(a,b);return this.computeAggregate(d,e,c,a,b)};var y=function(a,b){this.bindAll(a,b,0)};y.prototype=new e("sum");y.prototype.computeAggregate=function(a,
b,c,d,e){var h=this.startValue;a=Math.round(a)|0;for(b=Math.round(b)|0;a<=b;a++)e[c]=a,h+=this.content.evaluate(d,e);return h};y.prototype.toLaTeX=function(){var a=this.parameters;return"\\sum_{n="+a[0].toLaTeX()+"}^{"+a[1].toLaTeX()+"} "+this.content.toLaTeX()};var z=function(a,b){this.bindAll(a,b,1)};z.prototype=new e("prod");z.prototype.computeAggregate=function(a,b,c,d,e){var h=this.startValue;a=Math.round(a)|0;for(b=Math.round(b)|0;a<=b;a++)e[c]=a,h*=this.content.evaluate(d,e);return h};z.prototype.toLaTeX=
function(){var a=this.parameters;return"\\prod_{n="+a[0].toLaTeX()+"}^{"+a[1].toLaTeX()+"} "+this.content.toLaTeX()};var A=function(a,b){this.bindAll(a,b,0)};A.prototype=new e("area");A.prototype.computeAggregate=function(a,b,c,d,e){c=this.parameters;var h=c[2].evaluate(),h=(b-a)/h,f=c[3].getParameters()[0],g=this.startValue;for(c=0;c<d.length&&d[c]!=f;c++);for(f=a+h/2;f<b;f+=h)e[c]=f,a=this.content.evaluate(d,e),g+=h*Math.abs(a);return g};A.prototype.toLaTeX=function(){var a=this.parameters;return"\\int_{"+
a[0].toLaTeX()+"}^{"+a[1].toLaTeX()+"} "+this.content.toLaTeX()+" d"+a[3].label};var J=function(a,b){b&&(a=a.replace(/[ \t\n\s]/g,""));this.data=a.split("");this.length=a.length;this.position=0};J.prototype={read:function(){return this.data[this.position]},more:function(){return this.position<this.length},advance:function(){this.position<this.length-1&&this.position++},next:function(){return this.data[this.position++]},peek:function(){return this.data[this.position+1]},skipGroup:function(a,b){for(var c=
"",d;this.position<this.length;)if(d=this.data[this.position++],d==a)c+=this.skipGroup(a,b);else if(d==b)break;else c+=d;return a+c+b},toString:function(){var a=join(this.data,"");return a=a.substring(0,this.position)+" "+this.data[this.position]+" "+a.substring(this.position+1)}};var q=function(a){this.setup(a)};q.afCount=1;q.prototype={setup:function(a){this.uid=q.afCount++;this.sumarguments=[];this.fragment=a;this.children=[];this.functor="";this.expanded=this.wrapped=!1},getBalance:function(){var a=
this.fragment.split(""),b=0,c,d;c=0;for(d=a.length;c<d;c++)"("==a[c]&&b++,")"==a[c]&&b--;return b},isFunctionWrapped:function(a){if(!a.match(/^\w+\(.+\)/))return!1;a=a.replace(/^\w+/g,"");return this.isParensWrapped(a)},isParensWrapped:function(a){if(!a.match(/^\(.*\)$/))return!1;a=a.split("");var b=0,c,d;d=0;for(c=a.length;d<c;d++)if(""!==a[d]&&("("===a[d]&&b++,")"===a[d]&&b--,0===b&&d<c-1))return!1;return 0===b},isArithmeticOperator:function(a){return"+"==a||"-"==a||"*"==a||"/"==a||"^"==a||"!"==
a},expand:function(){if(!this.expanded){var a=!1,b=!1;this.isFunctionWrapped(this.fragment)&&(b=!0,this.functor=this.fragment.substring(0,this.fragment.indexOf("(")),this.fragment=this.fragment.replace(RegExp("^"+this.functor),""));this.isParensWrapped(this.fragment)&&(b||(this.wrapped=!0),b=!0,this.fragment=this.fragment.substring(1,this.fragment.length-1));if(("sum"==this.functor||"prod"==this.functor||"area"==this.functor)&&""!==this.fragment.trim()){var c=this.fragment.indexOf(","),d=this.fragment.indexOf("(");
-1===d&&(d=this.fragment.length());for(var e="";-1<c&&c<d;)e=this.fragment.substring(0,c),this.fragment=this.fragment.replace(RegExp("^"+e+",","g"),""),d--,this.sumarguments.push(e),c=this.fragment.indexOf(",")}for(var c=new J(this.fragment,!0),d="",h=!1;c.more();)e=c.next(),this.isArithmeticOperator(e)?(a=!0,""!==d.trim()||h?(""!==d.trim()&&(this.fragment="",this.children.push(new q(d))),"!"==e?this.children.push(new B(e)):this.children.push(new u(e)),d=""):"-"===e&&this.children.push(new B("-")),
h=!0):"("===e?d+=c.skipGroup("(",")"):(h=!1,d+=e);if((a||b)&&""!==d)this.children.push(new q(d)),a=!0;a&&this.children.forEach(function(a){a.expand()});this.expanded=!0}},formFunctionTree:function(){var a;a=[];this.expanded||this.expand();if(0<this.children.length){var b,c=this.children.length;for(b=0;b<c;b++){var d=this.children[b];d instanceof B?a.push(x[d.operator]?new x[d.operator]:!1):d instanceof u?a.push(p[d.operator]?new p[d.operator]:!1):a.push(d.formFunctionTree())}for(var e=d=!1,h,c=6;0<=
c;c--)for(b=a.length-1;0<=b;b--)h=a[b],h.getStrength&&(h.getStrength()==c&&!h.hasLeaves())&&(d=(d=h.hasRight())?!1:a.splice(b+1,1)[0],e=(e=h.hasLeft())?!1:a.splice(b-1,1)[0],h.setLeaves(e,d));a=0<a.length?a[0]:h}else a=I(this.fragment);this.functor&&(b=this.functor,h=this.sumarguments,c=a,b="sum"==b?new y(h,c):"prod"==b?new z(h,c):"area"==b?new A(h,c):!1,b?a=b:(b=this.functor,a=j[b]?new j[b](a):!1));this.wrapped&&(a=new t(a));return a},toString:function(a){if(!a)return this.toString(" ");var b="["+
this.uid+"] ",b=this.functor?this.functor+"[":"";if(0<this.children.length){for(var c=[],d=0,e=this.children.length;d<e;d++)c[d]=this.children[d].toString(a+" ");b+=c.join("")}else b+=this.fragment;return"{"+b+(this.functor?"]":"")+"}"}};var u=function(a){this.setup("");this.operator=a};u.prototype=new q("");u.prototype.toString=function(){return""+this.operator};var B=function(a){this.setup("");this.operator=a};B.prototype=new u("");n||D&&D.warn&&D.warn("MathJax is not available, math functions will not be able to render LaTeX.");
var K,r=!1,s=!1,v=!1;K=function(a,b){if(!1===r&&!1===s)return r=a,s=b,!1;if(!1===v)return v=(b-s)/(a-r),r=a,s=b,!1;var c=(b-s)/(a-r),d=4<c&&-4>v||-4>c&&4<v;r=a;s=b;v=c;return d};var C=function(a,b){return function(c,d,e,h){if(H(c))return!1;if(Infinity==c)return w(2,31);if(-Infinity==c)return-w(2,31);h=a+(b-a)*((c-d)/(e-d));return H(h)?!1:Infinity==h?w(2,31):-Infinity==h?-w(2,31):h}},m=function(a){this.plotCanvas=!1;this.LaTeX=this.functionString="";this.functionTree=this.arithmeticFragment=!1;this.init(a)};
L.MathFunction=m;m.prototype={init:function(a){this.functionString=a.replace(/\s/g,"");this.arithmeticFragment=new q(this.functionString);a=this.arithmeticFragment.getBalance();if(0===a)this.functionTree=this.arithmeticFragment.formFunctionTree(),this.LaTeX=this.functionTree.toLaTeX();else throw"Function is unbalanced: "+this.arithmeticFragment.toString()+" has "+a+" open groups";},build:function(){},getParameters:function(){return this.functionTree.getParameters()},render:function(a){n?(a.innerHTML=
"\\["+this.LaTeX+"\\]",n.Hub.Queue(["Typeset",n.Hub,a])):a.innerHTML=this.functionTree.toString()},plot:function(a,b,c){this.options=b;c=c||{minx:0,maxx:a.clientWidth,miny:0,maxy:a.clientHeight};if(this.plotCanvas)a=this.plotCanvas.getContext("2d");else{var d=G.createElement("canvas");d.width=b.width||400;this.mapx=C(0,d.width);d.height=b.height||400;this.mapy=C(d.height,0);d.style.border="1px solid black";a.innerHTML="";a.appendChild(d);this.plotCanvas=d;a=this.plotCanvas.getContext("2d");this.clear(a)}d=
b.variable;b=this.functionTree.plot(d.label,d.start,d.end,d.step,b.clamped);d=c;d={minx:d.minx,maxx:d.maxx,miny:d.miny,maxy:d.maxy,asymptotes:[]};this.drawAxes(a,c.axes,d);this.drawPlotData(a,b,d)},drawAxes:function(a,b,c){b=b||{x:0,y:0};var d;d=this.mapx(b.x,c.minx,c.maxx);a.strokeStyle="#999";a.moveTo(d,0);a.lineTo(d,400);a.stroke();a.beginPath();d=this.mapy(b.y,c.miny,c.maxy);a.moveTo(0,d);a.lineTo(400,d);a.stroke();a.beginPath()},drawPlotData:function(a,b,c){var d=c.asymptotes;a.strokeStyle="black";
var e,h=b.length,f,g,j;for(e=0;e<h;e++)f=b[e][0],g=this.mapx(f,c.minx,c.maxx),j=this.mapy(b[e][1],c.miny,c.maxy),(asym=K(g,j))?(d.push(f),a.stroke(),a.beginPath(),first=!0):a.lineTo(g,j);a.stroke();this.drawAsymptotes(a,c)},drawAsymptotes:function(a,b){var c=this;this.functionTree.x||(a.strokeStyle="rgba(255,0,0,0.5)",b.asymptotes.forEach(function(d){d=c.mapx(d,b.minx,b.maxx);a.beginPath();a.moveTo(d,0);a.lineTo(d,400);a.stroke()}))},clear:function(){this.plotCanvas&&(this.plotCanvas.width=this.options.width,
this.plotCanvas.height=this.options.height)},replace:function(a,b){"string"===typeof b&&(b=new m(b));this.functionTree.replace(a,b.functionTree);b=new m(this.functionTree.toString());this.functionString=b.functionString;this.LaTeX=b.LaTeX;this.arithmeticFragment=b.arithmeticFragment;this.functionTree=b.functionTree},toString:function(){return this.functionString},toLaTeX:function(){return this.LaTeX}};m.Compound=function(a){var b=[],c=function(a){b.push(new m(a))};a instanceof Array?a.forEach(c):
Array.prototype.forEach.call(arguments,c);this.functions=b};m.Compound.prototype={getParameters:function(){var a=[];this.functions.forEach(function(b){b.functionTree.getParameters().forEach(function(b){-1===a.indexOf(b)&&a.push(b)})});return a},render:function(a){var b="\\[\\left \\{ \\begin{array}{l}\n",c=[],d=1;this.functions.forEach(function(a){c.push("f_"+d++ +" = "+a.LaTeX)});b+=c.join("\\\\\n")+"\n\\end{array} \\right . \\]";a.innerHTML=b;n&&n.Hub.Queue(["Typeset",n.Hub,a])},plot:function(a,
b,c){this.options=b;c=c||{minx:0,maxx:a.clientWidth,miny:0,maxy:a.clientHeight};if(this.plotCanvas)a=this.plotCanvas.getContext("2d");else{var d=G.createElement("canvas");d.width=b.width||400;this.mapx=C(0,d.width);d.height=b.height||400;this.mapy=C(d.height,0);d.style.border="1px solid black";a.innerHTML="";a.appendChild(d);this.plotCanvas=d;a=this.plotCanvas.getContext("2d");this.clear(a);this.functions.forEach(function(a){a.plotCanvas=d})}var e=b.variable,f=[],g=[],j=c;this.functions.forEach(function(a){f.push(a.functionTree.plot(e.label,
e.start,e.end,e.step,b.clamped));g.push({minx:j.minx,maxx:j.maxx,miny:j.miny,maxy:j.maxy,asymptotes:[]})});this.drawAxes(a,c.order,c.axes,g);this.drawPlotData(a,c.order,f,g)},drawAxes:function(a,b,c,d){b=b||[0,1];c=c||{x:0,y:0};var e=b[0];b=b[1];e=this.mapx(c.x,d[e].minx,d[e].maxx);a.strokeStyle="#999";a.moveTo(e,0);a.lineTo(e,400);a.stroke();a.beginPath();e=this.mapy(c.y,d[b].miny,d[b].maxy);a.moveTo(0,e);a.lineTo(400,e);a.stroke();a.beginPath()},drawPlotData:function(a,b,c,d){b=b||[0,1];a.strokeStyle=
"black";var e=c[0].length,f,g,j=b[0],k=b[1];for(b=0;b<e;b++)f=c[j][b][1],g=c[k][b][1],f=this.mapx(f,d[j].minx,d[j].maxx),g=this.mapy(g,d[k].miny,d[k].maxy),a.lineTo(f,g);a.stroke()},clear:function(a){m.prototype.clear.call(this,a)},toString:function(){var a=[];this.functions.forEach(function(b){a.push(b.toString())});return a.join(", ")},toLaTeX:function(){var a=[];this.functions.forEach(function(b){a.push(b.toLaTeX())});return a}}})(window,document,window.console,MathJax,isNaN,Math.pow);