diff --git a/LICENSE b/LICENSE index d212a13..85c56fb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2015 Patrick Roberts +Copyright (c) 2016 Patrick Roberts Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation diff --git a/README.md b/README.md index 5ee40db..3649a1d 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ input.addEventListener('change', function () { Note that the compiler creates a function rather than evaluating the expression that is compiled immediately. The function returned is -high-performace, since it caches all real-values in the expression +high-performace, since it caches all constant expressions in the string so that they don't need to be re-evaluated with each call. The following is an example where the compiler provides parameters @@ -1076,3 +1076,27 @@ __Arguments__ * `string` - A human-readable `String` of a math expression to be compiled. * `params` - An optional `Array[String]` of human-readable parameters to parse. * `skipFormat` - An optional `Boolean` to determine whether to skip pre-formatting. + +## License + +Copyright (c) 2016 Patrick Roberts + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the +Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/bower.json b/bower.json index 88e0b07..16ee682 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "complex-js", "description": "Complex math for the browser and Node.js", - "version": "3.1.3", + "version": "3.2.0", "authors": [ "Patrick Roberts" ], diff --git a/lib/complex.js b/lib/complex.js index f8cb149..0fa67a4 100644 --- a/lib/complex.js +++ b/lib/complex.js @@ -1,5 +1,5 @@ /** - * Copyright (c) 2015 Patrick Roberts + * Copyright (c) 2016 Patrick Roberts * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation @@ -180,13 +180,13 @@ if(Math.sinh === undefined) { Math.sinh = function(x) { - return (-Math.exp(-x) + Math.exp(x)) / 2; + return (Math.exp(x) - Math.exp(-x)) / 2; }; } if(Math.cosh === undefined) { Math.cosh = function(x) { - return (Math.exp(-x) + Math.exp(x)) / 2; + return (Math.exp(x) + Math.exp(-x)) / 2; }; } @@ -210,6 +210,8 @@ */ function Complex(r, i, m, t) { + i = i || 0; + if(!(this instanceof Complex)) { return new Complex(r, i, m, t); } @@ -1254,7 +1256,7 @@ throw new SyntaxError('"' + arg + '" is an invalid variable name.'); } - return name.toLowerCase(); + return name; }); for(i = 0; i < args.length; i++) { @@ -1454,7 +1456,19 @@ // ignores extraneous grouping str += (func === '(' ? '' : func); i += match.length; - str += compile(exp.substring(i, j), vars, namespace) + (func === '(' ? '' : ')'); + // efficiently compiles subgroup by first assuming a constant expression + try { + var ns = new Namespace(); + var body = compile(exp.substring(i, j), [], ns, true); + var comp = (new Function('', 'var C=this.Complex;return ' + body + ';')).bind(ns)(); + str += 'this[' + namespace.push(comp) + ']'; + // then compiling with specified variables if that fails + } catch(e) { + str += compile(exp.substring(i, j), vars, namespace, true); + } finally { + str += (func === '(' ? '' : ')'); + } + i = (exp[j] === ')' ? j + 1 : j); findOper = true; } @@ -1495,7 +1509,19 @@ str += oper; i += match.length; - str += compile(exp.substring(i, j), vars, namespace) + ')'; + // efficiently compiles subgroup by first assuming a constant expression + try { + var ns = new Namespace(); + var body = compile(exp.substring(i, j), [], ns, true); + var comp = (new Function('', 'var C=this.Complex;return ' + body + ';')).bind(ns)(); + str += 'this[' + namespace.push(comp) + ']'; + // then compiling with specified variables if that fails + } catch(e) { + str += compile(exp.substring(i, j), vars, namespace, true); + } finally { + str += ')'; + } + i = (exp[j] === ')' ? j + 1 : j); findOper = true; // no identifiable expressions left to parse @@ -1522,11 +1548,10 @@ if(typeof args === 'boolean') { skipFormat = args; args = []; - } else if(!(typeof args === 'object' && args !== null && args.constructor === Array)) { + } else if(!Array.isArray(args)) { args = []; } - // make everything lowercase because it's easier - str = str.toLowerCase(); + // default is to format the human-readable string if(!skipFormat) { str = Complex.formatFunction(str); @@ -1538,11 +1563,12 @@ // create namespace to bind to function namespace = new Namespace(), // compile function body from string given valid arguments and namespace to cache parsed numerical constants - body = compile(str, vars, namespace), + body = compile('(' + str + ')', vars, namespace), // convert compiled variable names map to array for function arguments params = args.map(function(arg) { return vars[arg]; }).join(','); + // the constructed function being returned return (new Function(params, 'var C=this.Complex;return ' + body + ';')).bind(namespace); }; diff --git a/lib/complex.min.js b/lib/complex.min.js index 7140d47..6d78fb2 100644 --- a/lib/complex.min.js +++ b/lib/complex.min.js @@ -1 +1 @@ -/* Copyright (c) 2015 Patrick Roberts */!function(t){"use strict";function r(t){return t>=0?t-t%1:t-(1+t%1)%1}function n(t){return t>=0?t+(1-t%1)%1:t-t%1}function e(t){return r(t+.5)}function i(t){return t-t%1}function o(t){return t%1}function a(t,r,n,e){return this instanceof a?(this.m="number"==typeof n?w.abs(n):w.sqrt(t*t+r*r),this.t="number"==typeof e?n>=0?e:e+b:w.atan2(r,t),this.t=(this.t%(2*b)+2*b)%(2*b),this.t-=this.t>b?2*b:0,this.r=t,void(this.i=r)):new a(t,r,n,e)}function c(t,r,n){var e;return q.test(t)?S.test(n.slice(r))?t:t.charAt(0).replace(x,c)+"*"+t.charAt(1).replace(x,c):O.test(t)?"(":I.test(t)?")":(e=t.search(T),t.charAt(-1!==e?e:"*"))}function s(t){var r,n,e,i=/[^a-z]+/gi,o="abcdefghijklmnopqrstuvwxyz",a=o.length,c={},s="";for(t=t.map(function(t){var r=t.replace(i,"");if(t!==r)throw new SyntaxError('"'+t+'" is an invalid variable name.');return r.toLowerCase()}),r=0;r=0;)e=n%a,n-=e,s=o.charAt(e)+s,n=n/a-1;c[t[r]]=s,s=""}return c}function u(t,r){for(var n=1;n>0&&r0)throw new SyntaxError('Expected ")".');return r-1}function h(t,r){var n,e,i=0;for(n=r;ni)throw new SyntaxError('Unexpected token ")".');if(0===i&&("+"===t[n]||"-"===t[n]&&!A[t[n-1]])&&(e=t.substr(w.max(0,n-3)).search(S),-1===e||e>1))return n}return t.length}function p(t,r){var n,e,i=0;for(n=r;ni)throw new SyntaxError('Unexpected token ")".');if(0===i){if(("+"===t[n]||"-"===t[n]&&!A[t[n-1]])&&(e=t.substr(w.max(0,n-3)).search(S),-1===e||e>1))return n;if("*"===t[n]||"/"===t[n]||" "===t[n]||"%"===t[n])return n}}return t.length}function f(t,r,n){switch(t){case"+":case"-":return h(r,n);case"*":case" ":case"/":case"%":case"^":return p(r,n);default:throw new SyntaxError('"'+t+'" is not a valid operator.')}}function d(){Array.apply(this,arguments)}function l(t,r,n){var e,i,o,c,s,d,m="",C=0,w=!1;if(0===t.length)throw new SyntaxError('"" is not a valid argument.');for(;Ct?b:0)}),a.ZERO=new a(0,0,0,0),a.ONE=new a(1,0,1,0),a.I=new a(0,1,1,b/2),a.NEG_I=new a(0,-1,1,3*b/2),a.PI=new a(b,0,b,0),a.E=new a(w.E,0,w.E,0),a.TWO=new a(2,0,2,0),a.TWO_I=new a(0,2,2,b/2),a.Polar=function(t,r){return new a(t*w.cos(r),t*w.sin(r),t,r)},a.prototype.toString=function(t){var r,n,e,i;return a.isFinite(this)||isNaN(this.m)?a.isNaN(this)?"NaN":t?(r=this.r.toPrecision().toUpperCase(),n=this.i.toPrecision().toUpperCase(),0===this.i?r:0===this.r?1===w.abs(this.i)?this.i>0?"i":"-i":n+" i":r+(this.i>0?"+":"")+(1===w.abs(this.i)?this.i>0?"i":"-i":n+" i")):(e=this.m.toPrecision().toUpperCase(),i=this.t.toPrecision().toUpperCase(),0===this.m||0===this.t?e:e+" e^("+i+" i)"):"Infinity"},a.prototype.equals=a.prototype["="]=function(t){var r=this.r,n=this.i,e=this.m,i=this.t,o=t.r,a=t.i,c=t.m,s=t.t,u=w.abs(r-o)<=w.max(w.abs(r),w.abs(o))*g.EPSILON,h=w.abs(n-a)<=w.max(w.abs(n),w.abs(a))*g.EPSILON,p=w.abs(e-c)<=w.max(w.abs(e),w.abs(c))*g.EPSILON,f=w.abs(i-s)<=w.max(w.abs(i),w.abs(s))*g.EPSILON;return u&&h||p&&f},a.prototype.re=a.prototype.real=function(){return this.r},a.prototype.im=a.prototype.imag=function(){return this.i},a.prototype.abs=a.prototype.mag=function(){return this.m},a.prototype.arg=a.prototype.angle=function(){return this.t},a.prototype.rAdd=function(t){return new a(this.r+t,this.i)},a.prototype.add=a.prototype["+"]=function(t){return new a(this.r+t.r,this.i+t.i)},a.prototype.rSub=function(t){return new a(this.r-t,this.i)},a.prototype.sub=a.prototype["-"]=function(t){return new a(this.r-t.r,this.i-t.i)},a.prototype.rMult=function(t){return a.Polar(this.m*t,this.t)},a.prototype.mult=a.prototype["*"]=function(t){return a.Polar(this.m*t.m,this.t+t.t)},a.prototype.rDiv=function(t){return a.Polar(this.m/t,this.t)},a.prototype.div=a.prototype["/"]=function(t){return a.Polar(this.m/t.m,this.t-t.t)},a.prototype.rMod=function(t){return new a(0===t?0:this.r%t,this.i)},a.prototype.mod=a.prototype["%"]=function(t){return new a(0===t.r?0:this.r%t.r,0===t.i?0:this.i%t.i)},a.prototype.rPow=function(t){return 0===t?new a(1,0,1,0):1===t?new a(this.r,this.i,this.m,this.t):2===t?a.square(this):3===t?a.cube(this):a.Polar(w.pow(this.m,t),this.t*t)},a.prototype.pow=a.prototype["^"]=function(t){return 0===t.i?this.rPow(t.r):a.Polar(w.pow(this.m,t.r)*w.exp(-t.i*this.t),t.i*w.log(this.m)+t.r*this.t)},a.neg=function(t){return new a(-t.r,-t.i,t.m,t.t+b)},a.re=function(t){return new a(t.r,0,w.abs(t.r),t.r<0?b:0)},a.im=function(t){return new a(t.i,0,w.abs(t.i),t.i<0?b:0)},a.abs=function(t){return new a(t.m,0,t.m,0)},a.arg=function(t){return new a(t.t,0,w.abs(t.t),t.t<0?b:0)},a.conj=function(t){return new a(t.r,-t.i,t.m,(2*b-t.t)%b*2)},a.norm=function(t){return a.Polar(t.m/t.m,0===t.m?0:t.t)},a.floor=function(t){return new a(r(t.r),r(t.i))},a.ceil=function(t){return new a(n(t.r),n(t.i))},a.round=function(t){return new a(e(t.r),e(t.i))},a.iPart=function(t){return new a(i(t.r),i(t.i))},a.fPart=function(t){return new a(o(t.r),o(t.i))},a.square=function(t){return new a(t.r*t.r-t.i*t.i,2*t.r*t.i,t.m*t.m,2*t.t)},a.cube=function(t){return new a(t.r*t.r*t.r-3*t.r*t.i*t.i,3*t.r*t.r*t.i-t.i*t.i*t.i,t.m*t.m*t.m,3*t.t)},a.sqrt=function(t){return t.rPow(.5)},a.cbrt=function(t){return t.rPow(1/3)},a.exp=function(t){return 0===t.i?a.E.rPow(t.r):a.Polar(w.exp(t.r),t.i)},a.log=function(t){return new a(w.log(t.m),t.t)},a.gamma=function(t){var r,n,e,i;if(t.r<.5)return a.PI.div(a.sin(a.PI.mult(t)).mult(a.gamma(a.ONE.sub(t))));for(r=t.sub(a.ONE),n=C[0],e=1;9>e;e++)n=n.add(C[e].div(r.add(v[e])));return i=r.add(new a(7.5,0,7.5,0)),m.mult(i.pow(r.add(new a(.5,0,.5,0)))).mult(a.exp(a.neg(i))).mult(n)},a.fact=function(t){return a.gamma(t.add(a.ONE))},a.cos=function(t){var r=t.mult(a.I);return a.exp(r).add(a.exp(a.neg(r))).div(a.TWO)},a.sin=function(t){var r=t.mult(a.I);return a.exp(r).sub(a.exp(a.neg(r))).div(a.TWO_I)},a.tan=function(t){var r=t.mult(a.I),n=a.exp(r),e=a.exp(a.neg(r));return n.sub(e).div(n.add(e).mult(a.I))},a.sec=function(t){var r=t.mult(a.I);return a.TWO.div(a.exp(r).add(a.exp(a.neg(r))))},a.csc=function(t){var r=t.mult(a.I);return a.TWO_I.div(a.exp(r).sub(a.exp(a.neg(r))))},a.cot=function(t){var r=t.mult(a.I),n=a.exp(r),e=a.exp(a.neg(r));return n.add(e).mult(a.I).div(n.sub(e))},a.arccos=function(t){return a.I.mult(a.log(t.add(a.NEG_I.mult(a.sqrt(a.ONE.sub(t.rPow(2)))))))},a.arcsin=function(t){return a.NEG_I.mult(a.log(t.mult(a.I).add(a.sqrt(a.ONE.sub(t.rPow(2))))))},a.arctan=function(t){var r=a.I;return r.mult(a.log(r.add(t).div(r.sub(t)))).div(a.TWO)},a.arcsec=function(t){return a.NEG_I.mult(a.log(t.rPow(-1).add(a.sqrt(a.ONE.sub(a.I.div(t.rPow(2)))))))},a.arccsc=function(t){return a.NEG_I.mult(a.log(a.I.div(t).add(a.sqrt(a.ONE.sub(t.rPow(-2))))))},a.arccot=function(t){var r=a.I;return r.mult(a.log(t.sub(r).div(t.add(r)))).div(a.TWO)},a.cosh=function(t){return a.exp(t).add(a.exp(a.neg(t))).div(a.TWO)},a.sinh=function(t){return a.exp(t).sub(a.exp(a.neg(t))).div(a.TWO)},a.tanh=function(t){var r=a.exp(t),n=a.exp(a.neg(t));return r.sub(n).div(r.add(n))},a.sech=function(t){return a.TWO.div(a.exp(t).add(a.exp(a.neg(t))))},a.csch=function(t){return a.TWO.div(a.exp(t).sub(a.exp(a.neg(t))))},a.coth=function(t){var r=a.exp(t),n=a.exp(a.neg(t));return r.add(n).div(r.sub(n))},a.arccosh=function(t){return a.log(t.add(a.sqrt(t.rPow(2).sub(a.ONE))))},a.arcsinh=function(t){return a.log(t.add(a.sqrt(t.rPow(2).add(a.ONE))))},a.arctanh=function(t){return a.log(a.ONE.add(t).div(a.ONE.sub(t))).div(a.TWO)},a.arcsech=function(t){return a.log(a.ONE.add(a.sqrt(a.ONE.sub(t.rPow(2)))).div(t))},a.arccsch=function(t){return a.log(a.ONE.add(a.sqrt(a.ONE.add(t.rPow(2)))).div(t))},a.arccoth=function(t){return a.log(t.add(a.ONE).div(t.sub(a.ONE))).div(a.TWO)},a.min=function(){var t=Array.prototype.slice.call(arguments).map(function(t){return t.m}),r=w.min.apply(w,t);return arguments[t.indexOf(r)]},a.max=function(){var t=Array.prototype.slice.call(arguments).map(function(t){return t.m}),r=w.max.apply(w,t);return arguments[t.indexOf(r)]},a.isNaN=function(t){return isNaN(t.r)||isNaN(t.i)||isNaN(t.m)||isNaN(t.t)},a.isFinite=function(t){return isFinite(t.m)},a.formatFunction=function(t){return t.replace(x,c)},d.prototype=[],d.prototype.push=function(){return Array.prototype.push.apply(this,arguments)-1},d.prototype.Complex=a,a.parseFunction=function(t,r,n){"boolean"==typeof r?(n=r,r=[]):("object"!=typeof r||null===r||r.constructor!==Array)&&(r=[]),t=t.toLowerCase(),n||(t=a.formatFunction(t));var e=s(r),i=new d,o=l(t,e,i),c=r.map(function(t){return e[t]}).join(",");return new Function(c,"var C=this.Complex;return "+o+";").bind(i)},"undefined"!=typeof module&&null!==module&&module.exports?module.exports=a:"function"==typeof define&&define.amd?define([],function(){return a}):t.Complex=a}("undefined"==typeof global?this:global) \ No newline at end of file +/* Copyright (c) 2016 Patrick Roberts */!function(a){"use strict";function u(a){return a>=0?a-a%1:a-(1+a%1)%1}function v(a){return a>=0?a+(1-a%1)%1:a-a%1}function w(a){return u(a+.5)}function x(a){return a-a%1}function y(a){return a%1}function z(a,c,e,f){return c=c||0,this instanceof z?("number"==typeof e?this.m=b.abs(e):this.m=b.sqrt(a*a+c*c),"number"==typeof f?this.t=e>=0?f:f+d:this.t=b.atan2(c,a),this.t=(this.t%(2*d)+2*d)%(2*d),this.t-=this.t>d?2*d:0,this.r=a,void(this.i=c)):new z(a,c,e,f)}function A(a,b,c){var d;return o.test(a)?p.test(c.slice(b))?a:a.charAt(0).replace(h,A)+"*"+a.charAt(1).replace(h,A):m.test(a)?"(":n.test(a)?")":(d=a.search(q),a.charAt(-1!==d?d:"*"))}function B(a){var g,h,i,b=/[^a-z]+/gi,c="abcdefghijklmnopqrstuvwxyz",d=c.length,e={},f="";for(a=a.map(function(a){var c=a.replace(b,"");if(a!==c)throw new SyntaxError('"'+a+'" is an invalid variable name.');return c}),g=0;g=0;)i=h%d,h-=i,f=c.charAt(i)+f,h=h/d-1;e[a[g]]=f,f=""}return e}function C(a,b){for(var c=1;c>0&&b0)throw new SyntaxError('Expected ")".');return b-1}function D(a,c){var e,f,d=0;for(e=c;ed)throw new SyntaxError('Unexpected token ")".');if(0===d&&("+"===a[e]||"-"===a[e]&&!s[a[e-1]])&&(f=a.substr(b.max(0,e-3)).search(p),-1===f||f>1))return e}return a.length}function E(a,c){var e,f,d=0;for(e=c;ed)throw new SyntaxError('Unexpected token ")".');if(0===d){if(("+"===a[e]||"-"===a[e]&&!s[a[e-1]])&&(f=a.substr(b.max(0,e-3)).search(p),-1===f||f>1))return e;if("*"===a[e]||"/"===a[e]||" "===a[e]||"%"===a[e])return e}}return a.length}function F(a,b,c){switch(a){case"+":case"-":return D(b,c);case"*":case" ":case"/":case"%":case"^":return E(b,c);default:throw new SyntaxError('"'+a+'" is not a valid operator.')}}function G(){Array.apply(this,arguments)}function H(a,b,c){var g,h,m,n,o,p,d="",e=0,f=!1;if(0===a.length)throw new SyntaxError('"" is not a valid argument.');for(;ea?d:0)}),z.ZERO=new z(0,0,0,0),z.ONE=new z(1,0,1,0),z.I=new z(0,1,1,d/2),z.NEG_I=new z(0,-1,1,3*d/2),z.PI=new z(d,0,d,0),z.E=new z(b.E,0,b.E,0),z.TWO=new z(2,0,2,0),z.TWO_I=new z(0,2,2,d/2),z.Polar=function(a,c){return new z(a*b.cos(c),a*b.sin(c),a,c)},z.prototype.toString=function(a){var c,d,e,f;return z.isFinite(this)||isNaN(this.m)?z.isNaN(this)?"NaN":a?(c=this.r.toPrecision().toUpperCase(),d=this.i.toPrecision().toUpperCase(),0===this.i?c:0===this.r?1===b.abs(this.i)?this.i>0?"i":"-i":d+" i":c+(this.i>0?"+":"")+(1===b.abs(this.i)?this.i>0?"i":"-i":d+" i")):(e=this.m.toPrecision().toUpperCase(),f=this.t.toPrecision().toUpperCase(),0===this.m||0===this.t?e:e+" e^("+f+" i)"):"Infinity"},z.prototype.equals=z.prototype["="]=function(a){var d=this.r,e=this.i,f=this.m,g=this.t,h=a.r,i=a.i,j=a.m,k=a.t,l=b.abs(d-h)<=b.max(b.abs(d),b.abs(h))*c.EPSILON,m=b.abs(e-i)<=b.max(b.abs(e),b.abs(i))*c.EPSILON,n=b.abs(f-j)<=b.max(b.abs(f),b.abs(j))*c.EPSILON,o=b.abs(g-k)<=b.max(b.abs(g),b.abs(k))*c.EPSILON;return l&&m||n&&o},z.prototype.re=z.prototype.real=function(){return this.r},z.prototype.im=z.prototype.imag=function(){return this.i},z.prototype.abs=z.prototype.mag=function(){return this.m},z.prototype.arg=z.prototype.angle=function(){return this.t},z.prototype.rAdd=function(a){return new z(this.r+a,this.i)},z.prototype.add=z.prototype["+"]=function(a){return new z(this.r+a.r,this.i+a.i)},z.prototype.rSub=function(a){return new z(this.r-a,this.i)},z.prototype.sub=z.prototype["-"]=function(a){return new z(this.r-a.r,this.i-a.i)},z.prototype.rMult=function(a){return z.Polar(this.m*a,this.t)},z.prototype.mult=z.prototype["*"]=function(a){return z.Polar(this.m*a.m,this.t+a.t)},z.prototype.rDiv=function(a){return z.Polar(this.m/a,this.t)},z.prototype.div=z.prototype["/"]=function(a){return z.Polar(this.m/a.m,this.t-a.t)},z.prototype.rMod=function(a){return new z(0===a?0:this.r%a,this.i)},z.prototype.mod=z.prototype["%"]=function(a){return new z(0===a.r?0:this.r%a.r,0===a.i?0:this.i%a.i)},z.prototype.rPow=function(a){return 0===a?new z(1,0,1,0):1===a?new z(this.r,this.i,this.m,this.t):2===a?z.square(this):3===a?z.cube(this):z.Polar(b.pow(this.m,a),this.t*a)},z.prototype.pow=z.prototype["^"]=function(a){return 0===a.i?this.rPow(a.r):z.Polar(b.pow(this.m,a.r)*b.exp(-a.i*this.t),a.i*b.log(this.m)+a.r*this.t)},z.neg=function(a){return new z(-a.r,-a.i,a.m,a.t+d)},z.re=function(a){return new z(a.r,0,b.abs(a.r),a.r<0?d:0)},z.im=function(a){return new z(a.i,0,b.abs(a.i),a.i<0?d:0)},z.abs=function(a){return new z(a.m,0,a.m,0)},z.arg=function(a){return new z(a.t,0,b.abs(a.t),a.t<0?d:0)},z.conj=function(a){return new z(a.r,-a.i,a.m,(2*d-a.t)%d*2)},z.norm=function(a){return z.Polar(a.m/a.m,0===a.m?0:a.t)},z.floor=function(a){return new z(u(a.r),u(a.i))},z.ceil=function(a){return new z(v(a.r),v(a.i))},z.round=function(a){return new z(w(a.r),w(a.i))},z.iPart=function(a){return new z(x(a.r),x(a.i))},z.fPart=function(a){return new z(y(a.r),y(a.i))},z.square=function(a){return new z(a.r*a.r-a.i*a.i,2*a.r*a.i,a.m*a.m,2*a.t)},z.cube=function(a){return new z(a.r*a.r*a.r-3*a.r*a.i*a.i,3*a.r*a.r*a.i-a.i*a.i*a.i,a.m*a.m*a.m,3*a.t)},z.sqrt=function(a){return a.rPow(.5)},z.cbrt=function(a){return a.rPow(1/3)},z.exp=function(a){return 0===a.i?z.E.rPow(a.r):z.Polar(b.exp(a.r),a.i)},z.log=function(a){return new z(b.log(a.m),a.t)},z.gamma=function(a){var b,c,d,h;if(a.r<.5)return z.PI.div(z.sin(z.PI.mult(a)).mult(z.gamma(z.ONE.sub(a))));for(b=a.sub(z.ONE),c=g[0],d=1;9>d;d++)c=c.add(g[d].div(b.add(f[d])));return h=b.add(new z(7.5,0,7.5,0)),e.mult(h.pow(b.add(new z(.5,0,.5,0)))).mult(z.exp(z.neg(h))).mult(c)},z.fact=function(a){return z.gamma(a.add(z.ONE))},z.cos=function(a){var b=a.mult(z.I);return z.exp(b).add(z.exp(z.neg(b))).div(z.TWO)},z.sin=function(a){var b=a.mult(z.I);return z.exp(b).sub(z.exp(z.neg(b))).div(z.TWO_I)},z.tan=function(a){var b=a.mult(z.I),c=z.exp(b),d=z.exp(z.neg(b));return c.sub(d).div(c.add(d).mult(z.I))},z.sec=function(a){var b=a.mult(z.I);return z.TWO.div(z.exp(b).add(z.exp(z.neg(b))))},z.csc=function(a){var b=a.mult(z.I);return z.TWO_I.div(z.exp(b).sub(z.exp(z.neg(b))))},z.cot=function(a){var b=a.mult(z.I),c=z.exp(b),d=z.exp(z.neg(b));return c.add(d).mult(z.I).div(c.sub(d))},z.arccos=function(a){return z.I.mult(z.log(a.add(z.NEG_I.mult(z.sqrt(z.ONE.sub(a.rPow(2)))))))},z.arcsin=function(a){return z.NEG_I.mult(z.log(a.mult(z.I).add(z.sqrt(z.ONE.sub(a.rPow(2))))))},z.arctan=function(a){var b=z.I;return b.mult(z.log(b.add(a).div(b.sub(a)))).div(z.TWO)},z.arcsec=function(a){return z.NEG_I.mult(z.log(a.rPow(-1).add(z.sqrt(z.ONE.sub(z.I.div(a.rPow(2)))))))},z.arccsc=function(a){return z.NEG_I.mult(z.log(z.I.div(a).add(z.sqrt(z.ONE.sub(a.rPow(-2))))))},z.arccot=function(a){var b=z.I;return b.mult(z.log(a.sub(b).div(a.add(b)))).div(z.TWO)},z.cosh=function(a){return z.exp(a).add(z.exp(z.neg(a))).div(z.TWO)},z.sinh=function(a){return z.exp(a).sub(z.exp(z.neg(a))).div(z.TWO)},z.tanh=function(a){var b=z.exp(a),c=z.exp(z.neg(a));return b.sub(c).div(b.add(c))},z.sech=function(a){return z.TWO.div(z.exp(a).add(z.exp(z.neg(a))))},z.csch=function(a){return z.TWO.div(z.exp(a).sub(z.exp(z.neg(a))))},z.coth=function(a){var b=z.exp(a),c=z.exp(z.neg(a));return b.add(c).div(b.sub(c))},z.arccosh=function(a){return z.log(a.add(z.sqrt(a.rPow(2).sub(z.ONE))))},z.arcsinh=function(a){return z.log(a.add(z.sqrt(a.rPow(2).add(z.ONE))))},z.arctanh=function(a){return z.log(z.ONE.add(a).div(z.ONE.sub(a))).div(z.TWO)},z.arcsech=function(a){return z.log(z.ONE.add(z.sqrt(z.ONE.sub(a.rPow(2)))).div(a))},z.arccsch=function(a){return z.log(z.ONE.add(z.sqrt(z.ONE.add(a.rPow(2)))).div(a))},z.arccoth=function(a){return z.log(a.add(z.ONE).div(a.sub(z.ONE))).div(z.TWO)},z.min=function(){var a=Array.prototype.slice.call(arguments).map(function(a){return a.m}),c=b.min.apply(b,a);return arguments[a.indexOf(c)]},z.max=function(){var a=Array.prototype.slice.call(arguments).map(function(a){return a.m}),c=b.max.apply(b,a);return arguments[a.indexOf(c)]},z.isNaN=function(a){return isNaN(a.r)||isNaN(a.i)||isNaN(a.m)||isNaN(a.t)},z.isFinite=function(a){return isFinite(a.m)},z.formatFunction=function(a){return a.replace(h,A)},G.prototype=[],G.prototype.push=function(){return Array.prototype.push.apply(this,arguments)-1},G.prototype.Complex=z,z.parseFunction=function(a,b,c){"boolean"==typeof b?(c=b,b=[]):Array.isArray(b)||(b=[]),c||(a=z.formatFunction(a));var d=B(b),e=new G,f=H("("+a+")",d,e),g=b.map(function(a){return d[a]}).join(",");return new Function(g,"var C=this.Complex;return "+f+";").bind(e)},"undefined"!=typeof module&&null!==module&&module.exports?module.exports=z:"function"==typeof define&&define.amd?define([],function(){return z}):a.Complex=z}("undefined"==typeof global?this:global); \ No newline at end of file diff --git a/package.json b/package.json index 8a15a65..59a8de2 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ ], "author": "Patrick Roberts", "main": "./index", - "version": "3.1.2", + "version": "3.2.0", "repository": { "type": "git", "url": "https://github.com/patrickroberts/complex-js.git"