UNPKG

complex-js

Version:
3 lines (2 loc) 15.3 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("nearley");function t(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function r(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var n=function(e){return 1&e._has||(e._real=function(e,t){return e*Math.cos(t)}(e._abs,e._arg),e._has|=1),e._real},s=function(e){return 2&e._has||(e._imag=function(e,t){return e*Math.sin(t)}(e._abs,e._arg),e._has|=2),e._imag},i=function(e){return 4&e._has||(e._abs=function(e,t){return Math.hypot(e,t)}(e._real,e._imag),e._has|=4),e._abs},a=function(e){return 8&e._has||(e._arg=function(e,t){return Math.atan2(t,e)}(e._real,e._imag),e._has|=8),e._arg},o=function(e,t,r){return new e(n(t)+n(r),s(t)+s(r),0,0,3)},l=function(e,t,r){return new e(n(t)-n(r),s(t)-s(r),0,0,3)},c=function(e,t,r){var o=t._has&r._has;if(!(12&o)&&3&o){var l=n(t),c=s(t),p=n(r),u=s(r);return new e(l*p-c*u,l*u+c*p,0,0,3)}return new e(0,0,i(t)*i(r),a(t)+a(r),12)},p=function(e,t,r){var o=t._has&r._has;if(!(12&o)&&3&o){var l=n(t),c=s(t),p=n(r),u=s(r),m=p*p+u*u;return new e((l*p+c*u)/m,(c*p-l*u)/m,0,0,3)}return new e(0,0,i(t)/i(r),a(t)-a(r),12)},u=function(e,t){return new e(Math.trunc(n(t)),Math.trunc(s(t)),0,0,3)},m=function(e,t,r){return l(e,t,c(e,u(e,p(e,t,r)),r))},f=function(e,t,r){var o=n(r),l=s(r);if(0===l)switch(o){case-1:return p(e,new e(1,0,1,0,15),t);case 0:return new e(1,0,1,0,15);case 1:return t;case 2:return c(e,t,t)}var u=i(t),m=a(t);return new e(0,0,Math.pow(u,o)*Math.exp(-m*l),l*Math.log(u)+m*o,12)},y=function(e){return"".concat(function(e){var t=e.real;return Object.is(t,-0)?"-0":"".concat(t)}(e)).concat(function(e){var t=e.imag;return(t||1/t)<0?"-".concat(-t):"+".concat(t)}(e),"*i")},h=function(){function e(t,n,s,i,a){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),r(this,"_real",void 0),r(this,"_imag",void 0),r(this,"_abs",void 0),r(this,"_arg",void 0),r(this,"_has",void 0),this._real=t,this._imag=n,this._abs=s,this._arg=function(e){var t=2*Math.PI,r=e%t;return r>Math.PI?r-t:r>-Math.PI?r:r+t}(i),this._has=a}var u,h,x;return u=e,(h=[{key:"real",get:function(){return n(this)}},{key:"imag",get:function(){return s(this)}},{key:"abs",get:function(){return i(this)}},{key:"arg",get:function(){return a(this)}},{key:"norm",get:function(){return 4&(e=this)._has?e._abs*e._abs:e._real*e._real+e._imag*e._imag;var e}},{key:"add",value:function(t){return o(e,this,t)}},{key:"sub",value:function(t){return l(e,this,t)}},{key:"mul",value:function(t){return c(e,this,t)}},{key:"div",value:function(t){return p(e,this,t)}},{key:"mod",value:function(t){return m(e,this,t)}},{key:"pow",value:function(t){return f(e,this,t)}},{key:"toString",value:function(){return y(this)}}])&&t(u.prototype,h),x&&t(u,x),e}();function x(e){return e[0]}var g,b,E=function(e){return e[1]},_=function(e){return{type:"BinaryExpression",operator:e[2],left:e[0],right:e[4]}},v=function(e){return e.join("")},d={Lexer:void 0,ParserRules:[{name:"Expression",symbols:["_","AdditiveExpression","_"],postprocess:E},{name:"AdditiveExpression",symbols:["MultiplicativeExpression"],postprocess:x},{name:"AdditiveExpression",symbols:["AdditiveExpression","_",{literal:"+"},"_","MultiplicativeExpression"],postprocess:_},{name:"AdditiveExpression",symbols:["AdditiveExpression","_",{literal:"-"},"_","MultiplicativeExpression"],postprocess:_},{name:"MultiplicativeExpression",symbols:["ExponentiationExpression"],postprocess:x},{name:"MultiplicativeExpression",symbols:["MultiplicativeExpression","_",{literal:"*"},"_","ExponentiationExpression"],postprocess:_},{name:"MultiplicativeExpression",symbols:["MultiplicativeExpression","_",{literal:"/"},"_","ExponentiationExpression"],postprocess:_},{name:"MultiplicativeExpression",symbols:["MultiplicativeExpression","_",{literal:"%"},"_","ExponentiationExpression"],postprocess:_},{name:"ExponentiationExpression",symbols:["UnaryExpression"],postprocess:x},{name:"ExponentiationExpression",symbols:["LeftHandSideExpression","_","ExponentiationOperator","_","ExponentiationExpression"],postprocess:_},{name:"ExponentiationOperator",symbols:[{literal:"*"},{literal:"*"}],postprocess:v},{name:"UnaryExpression",symbols:["LeftHandSideExpression"],postprocess:x},{name:"UnaryExpression",symbols:["UnaryOperator","_","UnaryExpression"],postprocess:function(e){return{type:"UnaryExpression",operator:e[0],argument:e[2]}}},{name:"UnaryOperator",symbols:[{literal:"+"}],postprocess:x},{name:"UnaryOperator",symbols:[{literal:"-"}],postprocess:x},{name:"LeftHandSideExpression",symbols:["PrimaryExpression"],postprocess:x},{name:"LeftHandSideExpression",symbols:["CallExpression"],postprocess:x},{name:"CallExpression",symbols:["Identifier","_","Arguments"],postprocess:function(e){return{type:"CallExpression",callee:e[0],arguments:e[2]}}},{name:"Arguments",symbols:[{literal:"("},"_",{literal:")"}],postprocess:function(){return[]}},{name:"Arguments",symbols:[{literal:"("},"ArgumentList",{literal:")"}],postprocess:E},{name:"ArgumentList",symbols:["Expression"]},{name:"ArgumentList",symbols:["ArgumentList",{literal:","},"Expression"],postprocess:function(e){return e[0].concat(e[2])}},{name:"PrimaryExpression",symbols:["Identifier"],postprocess:x},{name:"PrimaryExpression",symbols:["Literal"],postprocess:x},{name:"PrimaryExpression",symbols:["ParenthesizedExpression"],postprocess:x},{name:"ParenthesizedExpression",symbols:[{literal:"("},"Expression",{literal:")"}],postprocess:E},{name:"Identifier",symbols:["IdentifierName"],postprocess:function(e){return{type:"Identifier",name:e[0]}}},{name:"IdentifierName",symbols:["IdentifierStart"],postprocess:v},{name:"IdentifierName",symbols:["IdentifierName","IdentifierPart"],postprocess:v},{name:"IdentifierStart",symbols:[/[$A-Z_a-z]/]},{name:"IdentifierPart",symbols:[/[$0-9A-Z_a-z]/]},{name:"Literal",symbols:["NumericLiteral"],postprocess:function(e){return{type:"Literal",value:Number(e[0])}}},{name:"NumericLiteral",symbols:["DecimalLiteral"],postprocess:x},{name:"NumericLiteral",symbols:["NonDecimalIntegerLiteral"],postprocess:x},{name:"DecimalLiteral",symbols:["DecimalIntegerLiteral",{literal:"."}],postprocess:v},{name:"DecimalLiteral",symbols:["DecimalIntegerLiteral",{literal:"."},"DecimalDigits"],postprocess:v},{name:"DecimalLiteral",symbols:["DecimalIntegerLiteral",{literal:"."},"DecimalDigits","ExponentPart"],postprocess:v},{name:"DecimalLiteral",symbols:["DecimalIntegerLiteral",{literal:"."},"ExponentPart"],postprocess:v},{name:"DecimalLiteral",symbols:[{literal:"."},"DecimalDigits"],postprocess:v},{name:"DecimalLiteral",symbols:[{literal:"."},"DecimalDigits","ExponentPart"],postprocess:v},{name:"DecimalLiteral",symbols:["DecimalIntegerLiteral"],postprocess:v},{name:"DecimalLiteral",symbols:["DecimalIntegerLiteral","ExponentPart"],postprocess:v},{name:"DecimalIntegerLiteral",symbols:[{literal:"0"}]},{name:"DecimalIntegerLiteral",symbols:["NonZeroDigit"],postprocess:x},{name:"DecimalIntegerLiteral",symbols:["NonZeroDigit","DecimalDigits"],postprocess:v},{name:"NonZeroDigit",symbols:[/[1-9]/]},{name:"ExponentPart",symbols:["ExponentIndicator","SignedInteger"],postprocess:v},{name:"ExponentIndicator",symbols:[{literal:"e"}]},{name:"ExponentIndicator",symbols:[{literal:"E"}]},{name:"SignedInteger",symbols:["DecimalDigits"],postprocess:x},{name:"SignedInteger",symbols:[{literal:"+"},"DecimalDigits"],postprocess:v},{name:"SignedInteger",symbols:[{literal:"-"},"DecimalDigits"],postprocess:v},{name:"NonDecimalIntegerLiteral",symbols:["BinaryIntegerLiteral"],postprocess:x},{name:"NonDecimalIntegerLiteral",symbols:["OctalIntegerLiteral"],postprocess:x},{name:"NonDecimalIntegerLiteral",symbols:["HexIntegerLiteral"],postprocess:x},{name:"BinaryIntegerLiteral",symbols:[{literal:"0"},{literal:"b"},"BinaryDigits"],postprocess:v},{name:"BinaryIntegerLiteral",symbols:[{literal:"0"},{literal:"B"},"BinaryDigits"],postprocess:v},{name:"OctalIntegerLiteral",symbols:[{literal:"0"},{literal:"o"},"OctalDigits"],postprocess:v},{name:"OctalIntegerLiteral",symbols:[{literal:"0"},{literal:"O"},"OctalDigits"],postprocess:v},{name:"HexIntegerLiteral",symbols:[{literal:"0"},{literal:"x"},"HexDigits"],postprocess:v},{name:"HexIntegerLiteral",symbols:[{literal:"0"},{literal:"X"},"HexDigits"],postprocess:v},{name:"DecimalDigits",symbols:["DecimalDigit"],postprocess:x},{name:"DecimalDigits",symbols:["DecimalDigits","DecimalDigit"],postprocess:v},{name:"BinaryDigits",symbols:["BinaryDigit"],postprocess:x},{name:"BinaryDigits",symbols:["BinaryDigits","BinaryDigit"],postprocess:v},{name:"OctalDigits",symbols:["OctalDigit"],postprocess:x},{name:"OctalDigits",symbols:["OctalDigits","OctalDigit"],postprocess:v},{name:"HexDigits",symbols:["HexDigit"],postprocess:x},{name:"HexDigits",symbols:["HexDigits","HexDigit"],postprocess:v},{name:"DecimalDigit",symbols:[/[0-9]/]},{name:"BinaryDigit",symbols:[/[0-1]/]},{name:"OctalDigit",symbols:[/[0-7]/]},{name:"HexDigit",symbols:[/[0-9A-Fa-f]/]},{name:"_",symbols:[]},{name:"_",symbols:[{literal:" "},"_"],postprocess:function(){return null}}],ParserStart:"Expression"},D=function(t){g?g.restore(b):(g=new e.Parser(e.Grammar.fromCompiled(d)),b=g.save());var r=function(e){try{return g.feed(e)}catch(a){var t=a.token,r=a.offset,n=t.value,s=r+1,i=" ".repeat(r);throw new SyntaxError("Unexpected ".concat(n," at column ").concat(s,":\n\n").concat(e,"\n").concat(i,"^"))}}(t).results;if(0===r.length)throw new SyntaxError("Unexpected end of input");return r[0]},M=new h(Math.E,0,Math.E,0,15),w=new h(0,1,1,Math.PI/2,15),L=new h(0,.5,.5,Math.PI/2,15),I=new h(Math.LN10,0,Math.LN10,0,15),P=new h(Math.LN2,0,Math.LN2,0,15),O=new h(Math.LOG10E,0,Math.LOG10E,0,15),S=new h(Math.LOG2E,0,Math.LOG2E,0,15),N=new h(0,-1,1,-Math.PI/2,15),A=new h(1,0,1,0,15),B=new h(.5,0,.5,0,15),H=new h(Math.PI,0,Math.PI,0,15),k=new h(Math.PI/2,0,Math.PI/2,0,15),U=new h(Math.SQRT1_2,0,Math.SQRT1_2,0,15),j=new h(Math.SQRT2,0,Math.SQRT2,0,15),T=function(e){return new h(Math.log(i(e)),a(e),0,0,3)},C=function(e){return new h(0,0,Math.sqrt(i(e)),a(e)/2,12)},R=function(e){return o(h,k,c(h,w,T(o(h,C(l(h,A,c(h,e,e))),c(h,w,e)))))},G=function(e){return T(o(h,e,c(h,C(l(h,e,A)),C(o(h,e,A)))))},Q=function(e,t){return o(h,e,t)},Z=function(e){return c(h,N,T(o(h,C(l(h,A,c(h,e,e))),c(h,w,e))))},q=function(e){return T(o(h,C(o(h,c(h,e,e),A)),e))},z=function(e){var t=c(h,w,e);return c(h,L,l(h,T(l(h,A,t)),T(o(h,A,t))))},$=function(e){return c(h,B,l(h,T(o(h,A,e)),T(l(h,A,e))))},F=function(e){return new h(0,0,Math.cbrt(i(e)),a(e)/3,12)},X=function(e){return new h(e._real,0-e._imag,e._abs,0-e._arg,e._has)},J=function(e){var t=n(e),r=s(e);return new h(Math.cos(t)*Math.cosh(r),0-Math.sin(t)*Math.sinh(r),0,0,3)},K=function(e){var t=n(e),r=s(e);return new h(Math.cosh(t)*Math.cos(r),Math.sinh(t)*Math.sin(r),0,0,3)},V=function(e,t){return p(h,e,t)},W=function(e){return new h(0,0,Math.exp(n(e)),s(e),12)},Y=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return"number"==typeof e?new h(e,t,0,0,3):e},ee=function(e,t){return m(h,e,t)},te=function(e,t){return c(h,e,t)},re=function(e,t){return f(h,e,t)},ne=function(e){var t=(e||1/e)<0?-0:0;return new h(1/0,t,1/0,t,15)},se=function(e){return 3!=(3&e._has)||1/e._real!=0&&1/e._imag!=0?12==(12&e._has)&&1/e._abs==0?ne(e._arg):e:ne(e._imag)},ie=function(e){var t=n(e),r=s(e);return new h(Math.sin(t)*Math.cosh(r),Math.cos(t)*Math.sinh(r),0,0,3)},ae=function(e){var t=n(e),r=s(e);return new h(Math.sinh(t)*Math.cos(r),Math.cosh(t)*Math.sin(r),0,0,3)},oe=function(e,t){return l(h,e,t)},le=function(e){var t=2*n(e),r=2*s(e),i=Math.cos(t)+Math.cosh(r);return new h(Math.sin(t)/i,Math.sinh(r)/i,0,0,3)},ce=function(e){var t=2*n(e),r=2*s(e),i=Math.cosh(t)+Math.cos(r);return new h(Math.sinh(t)/i,Math.sin(r)/i,0,0,3)},pe=function(e){return u(h,e)};function ue(e){return(ue="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var me={acos:R,acosh:G,asin:Z,asinh:q,atan:z,atanh:$,cbrt:F,conj:X,cos:J,cosh:K,e:M,exp:W,i:w,log:T,mod:ee,pi:H,pow:re,proj:se,sin:ie,sinh:ae,sqrt:C,tan:le,tanh:ce,trunc:pe},fe=function(e){return new TypeError("Unexpected expression type ".concat(e))},ye={"+":Q,"-":oe,"*":te,"/":V,"**":re,"%":ee},he={"+":function(e){return e},"-":function(e){return new h(0-e._real,0-e._imag,e._abs,0-e._arg,e._has)}},xe=function(e){if("object"!==ue(e))throw new TypeError("".concat(ue(e)," is not an object"));return e},ge=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},be=function e(t,r){var n=t.type;switch(n){case"BinaryExpression":var s=ye[t.operator],i=e(t.left,r),a=e(t.right,r);return function(e){return s(xe(i(e)),xe(a(e)))};case"CallExpression":var o=e(t.callee,r),l=t.arguments.map((function(t){return e(t,r)}));return function(e){return function(e){if("function"!=typeof e)throw new TypeError("".concat(ue(e)," is not a function"));return e}(o(e)).apply(void 0,l.map((function(t){return xe(t(e))})))};case"Identifier":var c=t.name;return function(e){if(!ge(e,c)){if(!ge(r,c))throw new ReferenceError("".concat(c," is not defined"));return r[c]}return e[c]};case"Literal":var p=t.value;return function(){return p};case"UnaryExpression":var u=he[t.operator],m=e(t.argument,r);return function(e){return u(xe(m(e)))};default:throw fe(n)}},Ee=function e(t,r){var n=t.type;switch(n){case"BinaryExpression":return e(t.left,r)&&e(t.right,r);case"CallExpression":return e(t.callee,r)&&t.arguments.every((function(t){return e(t,r)}));case"Identifier":return ge(r,t.name);case"Literal":return!0;case"UnaryExpression":return e(t.argument,r);default:throw fe(n)}},_e=function(e,t){if(!Ee(e,t))return e;var r=be(e,t);return{type:"Literal",value:xe(r(t))}},ve=function e(t,r){var n=t.type;switch(n){case"BinaryExpression":return _e({type:n,operator:t.operator,left:e(t.left,r),right:e(t.right,r)},r);case"CallExpression":return _e({type:n,callee:t.callee,arguments:t.arguments.map((function(t){return e(t,r)}))},r);case"Identifier":return t;case"Literal":return{type:n,value:Y(t.value)};case"UnaryExpression":return _e({type:n,operator:t.operator,argument:e(t.argument,r)},r);default:throw fe(n)}};exports.Complex=h,exports.E=M,exports.I=w,exports.LN10=I,exports.LN2=P,exports.LOG10E=O,exports.LOG2E=S,exports.PI=H,exports.SQRT1_2=U,exports.SQRT2=j,exports.acos=R,exports.acosh=G,exports.add=Q,exports.asin=Z,exports.asinh=q,exports.atan=z,exports.atanh=$,exports.bindings=me,exports.cartesian=function(e,t){return new h(e,t,0,0,3)},exports.cbrt=F,exports.compile=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=Object.assign({},me,t),n=be(ve(D(e),r),r);return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return n(e)}},exports.conj=X,exports.cos=J,exports.cosh=K,exports.div=V,exports.exp=W,exports.from=Y,exports.log=T,exports.mod=ee,exports.mul=te,exports.parse=D,exports.polar=function(e,t){return new h(0,0,e,t,12)},exports.pow=re,exports.proj=se,exports.sin=ie,exports.sinh=ae,exports.sqrt=C,exports.sub=oe,exports.tan=le,exports.tanh=ce,exports.trunc=pe; //# sourceMappingURL=complex.js.map