complex.js
Advanced tools
Comparing version 2.0.12 to 2.0.13
{ | ||
"name": "complex.js", | ||
"main": "complex.js", | ||
"version": "2.0.12", | ||
"version": "2.0.13", | ||
"homepage": "https://github.com/infusion/Complex.js", | ||
@@ -6,0 +6,0 @@ "description": "A complex number library", |
/** | ||
* @license Complex.js v2.0.12 11/02/2016 | ||
* @license Complex.js v2.0.13 12/05/2020 | ||
* | ||
* Copyright (c) 2016, Robert Eisele (robert@xarg.org) | ||
* Copyright (c) 2020, Robert Eisele (robert@xarg.org) | ||
* Dual licensed under the MIT or GPL Version 2 licenses. | ||
@@ -462,3 +462,3 @@ **/ | ||
if (b === 0) { | ||
if (b === 0 && a > 0) { | ||
@@ -1287,2 +1287,10 @@ return new Complex(Math.pow(a, z['re']), 0); | ||
if (Math.abs(a) < Complex['EPSILON']) { | ||
a = 0; | ||
} | ||
if (Math.abs(b) < Complex['EPSILON']) { | ||
b = 0; | ||
} | ||
// If is real number | ||
@@ -1384,3 +1392,3 @@ if (b === 0) { | ||
Complex['NAN'] = new Complex(NaN, NaN); | ||
Complex['EPSILON'] = 1e-16; | ||
Complex['EPSILON'] = 1e-15; | ||
@@ -1387,0 +1395,0 @@ if (typeof define === 'function' && define['amd']) { |
/* | ||
Complex.js v2.0.12 11/02/2016 | ||
Complex.js v2.0.13 12/05/2020 | ||
Copyright (c) 2016, Robert Eisele (robert@xarg.org) | ||
Copyright (c) 2020, Robert Eisele (robert@xarg.org) | ||
Dual licensed under the MIT or GPL Version 2 licenses. | ||
@@ -12,7 +12,7 @@ */ | ||
0):new d(this.re*c.re-this.im*c.im,this.re*c.im+this.im*c.re)},div:function(a,b){var c=new d(a,b);if(this.isZero()&&c.isZero()||this.isInfinite()&&c.isInfinite())return d.NAN;if(this.isInfinite()||c.isZero())return d.INFINITY;if(this.isZero()||c.isInfinite())return d.ZERO;a=this.re;b=this.im;var e=c.re,f=c.im;if(0===f)return new d(a/e,b/e);if(Math.abs(e)<Math.abs(f))return c=e/f,e=e*c+f,new d((a*c+b)/e,(b*c-a)/e);c=f/e;e=f*c+e;return new d((a+b*c)/e,(b-a*c)/e)},pow:function(a,b){var c=new d(a,b); | ||
a=this.re;b=this.im;if(c.isZero())return d.ONE;if(0===c.im){if(0===b)return new d(Math.pow(a,c.re),0);if(0===a)switch((c.re%4+4)%4){case 0:return new d(Math.pow(b,c.re),0);case 1:return new d(0,Math.pow(b,c.re));case 2:return new d(-Math.pow(b,c.re),0);case 3:return new d(0,-Math.pow(b,c.re))}}if(0===a&&0===b&&0<c.re&&0<=c.im)return d.ZERO;var e=Math.atan2(b,a),f=p(a,b);a=Math.exp(c.re*f-c.im*e);b=c.im*f+c.re*e;return new d(a*Math.cos(b),a*Math.sin(b))},sqrt:function(){var a=this.re,b=this.im,c=this.abs(); | ||
if(0<=a){if(0===b)return new d(Math.sqrt(a),0);var e=.5*Math.sqrt(2*(c+a))}else e=Math.abs(b)/Math.sqrt(2*(c-a));a=0>=a?.5*Math.sqrt(2*(c-a)):Math.abs(b)/Math.sqrt(2*(c+a));return new d(e,0>b?-a:a)},exp:function(){var a=Math.exp(this.re);return new d(a*Math.cos(this.im),a*Math.sin(this.im))},expm1:function(){var a=this.re,b=this.im,c=Math.expm1(a)*Math.cos(b);var e=Math.PI/4;b<-e||b>e?e=Math.cos(b)-1:(e=b*b,e*=-.5+e*(1/24+e*(-1/720+e*(1/40320+e*(-1/3628800+e*(1/4790014600+e*(-1/87178291200+1/20922789888E3* | ||
e)))))));return new d(c+e,Math.exp(a)*Math.sin(b))},log:function(){var a=this.re,b=this.im;return new d(p(a,b),Math.atan2(b,a))},abs:function(){var a=this.re;var b=this.im,c=Math.abs(a),e=Math.abs(b);3E3>c&&3E3>e?a=Math.sqrt(c*c+e*e):(c<e?(c=e,e=a/b):e=b/a,a=c*Math.sqrt(1+e*e));return a},arg:function(){return Math.atan2(this.im,this.re)},sin:function(){var a=this.re,b=this.im;return new d(Math.sin(a)*g(b),Math.cos(a)*k(b))},cos:function(){var a=this.re,b=this.im;return new d(Math.cos(a)*g(b),-Math.sin(a)* | ||
k(b))},tan:function(){var a=2*this.re,b=2*this.im,c=Math.cos(a)+g(b);return new d(Math.sin(a)/c,k(b)/c)},cot:function(){var a=2*this.re,b=2*this.im,c=Math.cos(a)-g(b);return new d(-Math.sin(a)/c,k(b)/c)},sec:function(){var a=this.re,b=this.im,c=.5*g(2*b)+.5*Math.cos(2*a);return new d(Math.cos(a)*g(b)/c,Math.sin(a)*k(b)/c)},csc:function(){var a=this.re,b=this.im,c=.5*g(2*b)-.5*Math.cos(2*a);return new d(Math.sin(a)*g(b)/c,-Math.cos(a)*k(b)/c)},asin:function(){var a=this.re,b=this.im,c=(new d(b*b-a* | ||
a+1,-2*a*b)).sqrt();a=(new d(c.re-b,c.im+a)).log();return new d(a.im,-a.re)},acos:function(){var a=this.re,b=this.im,c=(new d(b*b-a*a+1,-2*a*b)).sqrt();a=(new d(c.re-b,c.im+a)).log();return new d(Math.PI/2-a.im,a.re)},atan:function(){var a=this.re,b=this.im;if(0===a){if(1===b)return new d(0,Infinity);if(-1===b)return new d(0,-Infinity)}var c=a*a+(1-b)*(1-b);a=(new d((1-b*b-a*a)/c,-2*a/c)).log();return new d(-.5*a.im,.5*a.re)},acot:function(){var a=this.re,b=this.im;if(0===b)return new d(Math.atan2(1, | ||
a=this.re;b=this.im;if(c.isZero())return d.ONE;if(0===c.im){if(0===b&&0<a)return new d(Math.pow(a,c.re),0);if(0===a)switch((c.re%4+4)%4){case 0:return new d(Math.pow(b,c.re),0);case 1:return new d(0,Math.pow(b,c.re));case 2:return new d(-Math.pow(b,c.re),0);case 3:return new d(0,-Math.pow(b,c.re))}}if(0===a&&0===b&&0<c.re&&0<=c.im)return d.ZERO;var e=Math.atan2(b,a),f=p(a,b);a=Math.exp(c.re*f-c.im*e);b=c.im*f+c.re*e;return new d(a*Math.cos(b),a*Math.sin(b))},sqrt:function(){var a=this.re,b=this.im, | ||
c=this.abs();if(0<=a){if(0===b)return new d(Math.sqrt(a),0);var e=.5*Math.sqrt(2*(c+a))}else e=Math.abs(b)/Math.sqrt(2*(c-a));a=0>=a?.5*Math.sqrt(2*(c-a)):Math.abs(b)/Math.sqrt(2*(c+a));return new d(e,0>b?-a:a)},exp:function(){var a=Math.exp(this.re);return new d(a*Math.cos(this.im),a*Math.sin(this.im))},expm1:function(){var a=this.re,b=this.im,c=Math.expm1(a)*Math.cos(b);var e=Math.PI/4;b<-e||b>e?e=Math.cos(b)-1:(e=b*b,e*=-.5+e*(1/24+e*(-1/720+e*(1/40320+e*(-1/3628800+e*(1/4790014600+e*(-1/87178291200+ | ||
1/20922789888E3*e)))))));return new d(c+e,Math.exp(a)*Math.sin(b))},log:function(){var a=this.re,b=this.im;return new d(p(a,b),Math.atan2(b,a))},abs:function(){var a=this.re;var b=this.im,c=Math.abs(a),e=Math.abs(b);3E3>c&&3E3>e?a=Math.sqrt(c*c+e*e):(c<e?(c=e,e=a/b):e=b/a,a=c*Math.sqrt(1+e*e));return a},arg:function(){return Math.atan2(this.im,this.re)},sin:function(){var a=this.re,b=this.im;return new d(Math.sin(a)*g(b),Math.cos(a)*k(b))},cos:function(){var a=this.re,b=this.im;return new d(Math.cos(a)* | ||
g(b),-Math.sin(a)*k(b))},tan:function(){var a=2*this.re,b=2*this.im,c=Math.cos(a)+g(b);return new d(Math.sin(a)/c,k(b)/c)},cot:function(){var a=2*this.re,b=2*this.im,c=Math.cos(a)-g(b);return new d(-Math.sin(a)/c,k(b)/c)},sec:function(){var a=this.re,b=this.im,c=.5*g(2*b)+.5*Math.cos(2*a);return new d(Math.cos(a)*g(b)/c,Math.sin(a)*k(b)/c)},csc:function(){var a=this.re,b=this.im,c=.5*g(2*b)-.5*Math.cos(2*a);return new d(Math.sin(a)*g(b)/c,-Math.cos(a)*k(b)/c)},asin:function(){var a=this.re,b=this.im, | ||
c=(new d(b*b-a*a+1,-2*a*b)).sqrt();a=(new d(c.re-b,c.im+a)).log();return new d(a.im,-a.re)},acos:function(){var a=this.re,b=this.im,c=(new d(b*b-a*a+1,-2*a*b)).sqrt();a=(new d(c.re-b,c.im+a)).log();return new d(Math.PI/2-a.im,a.re)},atan:function(){var a=this.re,b=this.im;if(0===a){if(1===b)return new d(0,Infinity);if(-1===b)return new d(0,-Infinity)}var c=a*a+(1-b)*(1-b);a=(new d((1-b*b-a*a)/c,-2*a/c)).log();return new d(-.5*a.im,.5*a.re)},acot:function(){var a=this.re,b=this.im;if(0===b)return new d(Math.atan2(1, | ||
a),0);var c=a*a+b*b;return 0!==c?(new d(a/c,-b/c)).atan():(new d(0!==a?a/0:0,0!==b?-b/0:0)).atan()},asec:function(){var a=this.re,b=this.im;if(0===a&&0===b)return new d(0,Infinity);var c=a*a+b*b;return 0!==c?(new d(a/c,-b/c)).acos():(new d(0!==a?a/0:0,0!==b?-b/0:0)).acos()},acsc:function(){var a=this.re,b=this.im;if(0===a&&0===b)return new d(Math.PI/2,Infinity);var c=a*a+b*b;return 0!==c?(new d(a/c,-b/c)).asin():(new d(0!==a?a/0:0,0!==b?-b/0:0)).asin()},sinh:function(){var a=this.re,b=this.im;return new d(k(a)* | ||
@@ -23,4 +23,4 @@ Math.cos(b),g(a)*Math.sin(b))},cosh:function(){var a=this.re,b=this.im;return new d(g(a)*Math.cos(b),k(a)*Math.sin(b))},tanh:function(){var a=2*this.re,b=2*this.im,c=g(a)+Math.cos(b);return new d(k(a)/c,Math.sin(b)/c)},coth:function(){var a=2*this.re,b=2*this.im,c=g(a)-Math.cos(b);return new d(k(a)/c,-Math.sin(b)/c)},csch:function(){var a=this.re,b=this.im,c=Math.cos(2*b)-g(2*a);return new d(-2*k(a)*Math.cos(b)/c,2*g(a)*Math.sin(b)/c)},sech:function(){var a=this.re,b=this.im,c=Math.cos(2*b)+g(2*a); | ||
b*b;return 0!==c?(new d(a/c,-b/c)).acosh():(new d(0!==a?a/0:0,0!==b?-b/0:0)).acosh()},inverse:function(){if(this.isZero())return d.INFINITY;if(this.isInfinite())return d.ZERO;var a=this.re,b=this.im,c=a*a+b*b;return new d(a/c,-b/c)},conjugate:function(){return new d(this.re,-this.im)},neg:function(){return new d(-this.re,-this.im)},ceil:function(a){a=Math.pow(10,a||0);return new d(Math.ceil(this.re*a)/a,Math.ceil(this.im*a)/a)},floor:function(a){a=Math.pow(10,a||0);return new d(Math.floor(this.re* | ||
a)/a,Math.floor(this.im*a)/a)},round:function(a){a=Math.pow(10,a||0);return new d(Math.round(this.re*a)/a,Math.round(this.im*a)/a)},equals:function(a,b){var c=new d(a,b);return Math.abs(c.re-this.re)<=d.EPSILON&&Math.abs(c.im-this.im)<=d.EPSILON},clone:function(){return new d(this.re,this.im)},toString:function(){var a=this.re,b=this.im,c="";if(this.isNaN())return"NaN";if(this.isInfinite())return"Infinity";if(0===b)return c+a;0!==a?(c=c+a+" ",0>b?(b=-b,c+="-"):c+="+",c+=" "):0>b&&(b=-b,c+="-");1!== | ||
b&&(c+=b);return c+"i"},toVector:function(){return[this.re,this.im]},valueOf:function(){return 0===this.im?this.re:null},isNaN:function(){return isNaN(this.re)||isNaN(this.im)},isZero:function(){return 0===this.im&&0===this.re},isFinite:function(){return isFinite(this.re)&&isFinite(this.im)},isInfinite:function(){return!(this.isNaN()||this.isFinite())}};d.ZERO=new d(0,0);d.ONE=new d(1,0);d.I=new d(0,1);d.PI=new d(Math.PI,0);d.E=new d(Math.E,0);d.INFINITY=new d(Infinity,Infinity);d.NAN=new d(NaN,NaN); | ||
d.EPSILON=1E-16;"function"===typeof define&&define.amd?define([],function(){return d}):"object"===typeof exports?(Object.defineProperty(d,"__esModule",{value:!0}),d["default"]=d,d.Complex=d,module.exports=d):q.Complex=d})(this); | ||
a)/a,Math.floor(this.im*a)/a)},round:function(a){a=Math.pow(10,a||0);return new d(Math.round(this.re*a)/a,Math.round(this.im*a)/a)},equals:function(a,b){var c=new d(a,b);return Math.abs(c.re-this.re)<=d.EPSILON&&Math.abs(c.im-this.im)<=d.EPSILON},clone:function(){return new d(this.re,this.im)},toString:function(){var a=this.re,b=this.im,c="";if(this.isNaN())return"NaN";if(this.isInfinite())return"Infinity";Math.abs(a)<d.EPSILON&&(a=0);Math.abs(b)<d.EPSILON&&(b=0);if(0===b)return c+a;0!==a?(c=c+a+ | ||
" ",0>b?(b=-b,c+="-"):c+="+",c+=" "):0>b&&(b=-b,c+="-");1!==b&&(c+=b);return c+"i"},toVector:function(){return[this.re,this.im]},valueOf:function(){return 0===this.im?this.re:null},isNaN:function(){return isNaN(this.re)||isNaN(this.im)},isZero:function(){return 0===this.im&&0===this.re},isFinite:function(){return isFinite(this.re)&&isFinite(this.im)},isInfinite:function(){return!(this.isNaN()||this.isFinite())}};d.ZERO=new d(0,0);d.ONE=new d(1,0);d.I=new d(0,1);d.PI=new d(Math.PI,0);d.E=new d(Math.E, | ||
0);d.INFINITY=new d(Infinity,Infinity);d.NAN=new d(NaN,NaN);d.EPSILON=1E-15;"function"===typeof define&&define.amd?define([],function(){return d}):"object"===typeof exports?(Object.defineProperty(d,"__esModule",{value:!0}),d["default"]=d,d.Complex=d,module.exports=d):q.Complex=d})(this); |
@@ -6,3 +6,3 @@ { | ||
"title": "complex.js", | ||
"version": "2.0.12", | ||
"version": "2.0.13", | ||
"description": "A complex number library", | ||
@@ -9,0 +9,0 @@ "keywords": [ |
@@ -6,722 +6,741 @@ var assert = require("assert"); | ||
var functionTests = [{ | ||
set: Complex.I, | ||
fn: "mul", | ||
param: Complex(Math.PI).exp(), | ||
expect: "23.140692632779267i" | ||
}, { | ||
set: new Complex(1, 4), | ||
fn: "mul", | ||
param: 3, | ||
expect: "3 + 12i" | ||
}, { | ||
set: "4 + 3i", | ||
fn: "add", | ||
param: "-3 - 2i", | ||
expect: "1 + i" | ||
}, { | ||
set: "3i", | ||
fn: "add", | ||
param: "-2i", | ||
expect: "i" | ||
}, { | ||
set: "4", | ||
fn: "add", | ||
param: "-3", | ||
expect: "1" | ||
}, { | ||
set: 9, | ||
fn: "sqrt", | ||
expect: "3" | ||
}, { | ||
set: -9, | ||
fn: "sqrt", | ||
expect: "3i" | ||
}, { | ||
set: "-36", | ||
fn: "sqrt", | ||
expect: "6i" | ||
}, { | ||
set: "36i", | ||
fn: "sqrt", | ||
expect: "4.242640687119285 + 4.242640687119285i" | ||
}, { | ||
set: Infinity, | ||
fn: "mul", | ||
param: "i", | ||
expect: "Infinity" | ||
}, { | ||
set: "-36i", | ||
fn: "sqrt", | ||
expect: "4.242640687119285 - 4.242640687119285i" | ||
}, { | ||
set: "4 + 2i", | ||
fn: "div", | ||
param: "0", | ||
expect: "Infinity" | ||
}, { | ||
set: "0", | ||
fn: "div", | ||
param: Infinity, | ||
expect: "0" | ||
}, { | ||
set: -Infinity, | ||
fn: "div", | ||
param: 0, | ||
expect: "Infinity" | ||
}, { | ||
set: Infinity, | ||
fn: "div", | ||
param: Infinity, | ||
expect: "NaN" | ||
}, { | ||
set: 0, | ||
fn: "div", | ||
param: 0, | ||
expect: "NaN" | ||
}, { | ||
set: "4 + 2i", | ||
fn: "div", | ||
param: "1 + i", | ||
expect: "3 - i" | ||
}, { | ||
set: "25", | ||
fn: "div", | ||
param: "3 - 4i", | ||
expect: "3 + 4i" | ||
}, { | ||
set: "3 - 2i", | ||
fn: "div", | ||
param: "i", | ||
expect: "-2 - 3i" | ||
}, { | ||
set: "4i", | ||
fn: "mul", | ||
param: "-5i", | ||
expect: "20" | ||
}, { | ||
set: "3 - 6i", | ||
fn: "mul", | ||
param: "i", | ||
expect: "6 + 3i" | ||
}, { | ||
set: Infinity, | ||
fn: "mul", | ||
param: 0, | ||
expect: "NaN" | ||
}, { | ||
set: "3 + 4i", | ||
fn: "add", | ||
param: "5 - 7i", | ||
expect: "8 - 3i" | ||
}, { | ||
set: Infinity, | ||
fn: "add", | ||
param: Infinity, | ||
expect: "NaN" | ||
}, { | ||
set: -Infinity, | ||
fn: "sub", | ||
param: -Infinity, | ||
expect: "NaN" | ||
}, { | ||
set: "6i", | ||
fn: "div", | ||
param: "3 - 12i", | ||
expect: "-0.47058823529411764 + 0.11764705882352941i" | ||
}, { | ||
set: "36 + 36i", | ||
fn: "sqrt", | ||
expect: "6.59210468080686 + 2.730539163373364i" | ||
}, { | ||
set: "36 - 36i", | ||
fn: "sqrt", | ||
expect: "6.59210468080686 - 2.730539163373364i" | ||
}, { | ||
set: "-36 + 36i", | ||
fn: "sqrt", | ||
expect: "2.730539163373364 + 6.59210468080686i" | ||
}, { | ||
set: "-36 - 36i", | ||
fn: "sqrt", | ||
expect: "2.730539163373364 - 6.59210468080686i" | ||
}, { | ||
set: "0", | ||
fn: "sqrt", | ||
expect: "0" | ||
}, { | ||
set: Math.E, | ||
fn: "log", | ||
expect: "1" | ||
}, { | ||
set: 0, | ||
fn: "log", | ||
expect: "Infinity" | ||
}, { | ||
set: Infinity, | ||
fn: "mul", | ||
param: 3, | ||
expect: "Infinity" | ||
}, { | ||
set: "-1", | ||
fn: "log", | ||
expect: Math.PI + "i" | ||
}, { | ||
set: "i", | ||
fn: "log", | ||
expect: (Math.PI / 2) + "i" | ||
}, { | ||
set: "3 + 2i", | ||
fn: "log", | ||
expect: Math.log(13) / 2 + " + " + Math.atan2(2, 3) + "i" | ||
}, { | ||
set: "3 - 2i", | ||
fn: "log", | ||
expect: Math.log(13) / 2 + " - " + Math.atan2(2, 3) + "i" | ||
}, { | ||
set: 1, | ||
fn: "exp", | ||
expect: "" + Math.E | ||
}, { | ||
set: "i", | ||
fn: "exp", | ||
expect: Math.cos(1) + " + " + Math.sin(1) + "i" | ||
}, { | ||
set: "i", | ||
fn: "mul", | ||
param: "i", | ||
expect: "-1" | ||
}, { | ||
set: "3 + 2i", | ||
fn: "exp", | ||
expect: "-8.358532650935372 + 18.263727040666765i" | ||
}, { | ||
set: "3 - 2i", | ||
fn: "exp", | ||
expect: "-8.358532650935372 - 18.263727040666765i" | ||
}, { | ||
set: "3 - 2i", | ||
fn: "expm1", | ||
expect: "-9.358532650935372 - 18.263727040666765i" | ||
}, { | ||
set: "0", | ||
fn: "expm1", | ||
expect: "0" | ||
}, { | ||
set: "1e-6", | ||
fn: "expm1", | ||
expect: "0.0000010000005000001665" | ||
}, { | ||
set: "1e-5 + 5i", | ||
fn: "expm1", | ||
expect: "-0.716334977900736 - 0.9589338639538314i" | ||
}, { | ||
set: "1.2e-7 - 2e-6i", | ||
fn: "expm1", | ||
expect: "1.1999800719976027e-7 - 0.000002000000239998681i" | ||
}, { | ||
set: "3", | ||
fn: "pow", | ||
param: "3", | ||
expect: "27" | ||
}, { | ||
set: "0", | ||
fn: "pow", | ||
param: "1+i", | ||
expect: "0" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "0", | ||
expect: "1" | ||
}, { | ||
set: "87", | ||
fn: "pow", | ||
param: "3", | ||
expect: "658503" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "1", | ||
expect: "i" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "2", | ||
expect: "-1" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "3", | ||
expect: "-i" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "4", | ||
expect: "1" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "5", | ||
expect: "i" | ||
}, { | ||
set: 7, | ||
fn: "pow", | ||
param: 2, | ||
expect: 49 | ||
}, { | ||
set: 0, | ||
fn: "pow", | ||
param: 2, | ||
expect: 0 | ||
}, { | ||
set: "3i", | ||
fn: "pow", | ||
param: "3i", | ||
expect: "-0.008876640735623678 - 0.0013801328997494863i" | ||
}, { | ||
set: {re: 3, im: 4}, | ||
fn: "abs", | ||
expect: "5" | ||
}, { | ||
set: {re: 10, im: 24}, | ||
fn: "abs", | ||
expect: "26" | ||
}, { | ||
set: "+++++--+1 + 4i", | ||
fn: "mul", | ||
param: "3 + 2i", | ||
expect: "-5 + 14i" | ||
}, { | ||
set: "4 + 16i", | ||
fn: "div", | ||
param: "4.0000", | ||
expect: "1 + 4i" | ||
}, { | ||
set: {re: -7.1, im: 2.5}, | ||
fn: "neg", | ||
expect: "7.1 - 2.5i" | ||
}, { | ||
set: {re: 1, im: 1}, | ||
fn: "div", | ||
param: {re: 3, im: 4}, | ||
expect: 7 / 25 + " - " + 1 / 25 + "i" | ||
}, { | ||
set: new Complex(-7.1, 2.5), | ||
fn: "neg", | ||
expect: "7.1 - 2.5i" | ||
}, { | ||
set: {re: 1, im: 1}, | ||
fn: "arg", | ||
expect: "" + Math.PI / 4 | ||
}, { | ||
set: {re: -1, im: -1}, | ||
fn: "arg", | ||
expect: "" + -3 * Math.PI / 4 | ||
}, { | ||
set: {re: 0, im: 1}, | ||
fn: "arg", | ||
expect: "" + Math.PI / 2 | ||
}, { | ||
set: {re: 1, im: 0.5 * Math.sqrt(4 / 3)}, | ||
fn: "arg", | ||
expect: "" + Math.PI / 6 | ||
}, { | ||
set: "3 + 4i", | ||
fn: "conjugate", | ||
expect: "3 - 4i" | ||
}, { | ||
set: {re: 99, im: 50}, | ||
fn: "conjugate", | ||
expect: "99 - 50i" | ||
}, { | ||
set: {re: 0, im: 0}, | ||
fn: "conjugate", | ||
expect: "0" | ||
}, { | ||
set: {re: 1, im: 23}, | ||
fn: "conjugate", | ||
expect: "1 - 23i" | ||
}, { | ||
set: "2 + 8i", | ||
fn: "div", | ||
param: new Complex(1, 2), | ||
expect: "3.6 + 0.8i" | ||
}, { | ||
set: "2 + 8i", | ||
fn: "div", | ||
param: "2 + 8i", | ||
expect: "1" | ||
}, { | ||
set: -Infinity, | ||
fn: "div", | ||
param: 3, | ||
expect: "Infinity" | ||
}, { | ||
set: "3+4i", | ||
fn: "add", | ||
param: "5 - i", | ||
expect: "8 + 3i" | ||
}, { | ||
set: {re: 1, im: 2}, | ||
fn: "add", | ||
param: "4 + 6i", | ||
expect: "5 + 8i" | ||
}, { | ||
set: {re: 5, im: 8}, | ||
fn: "sub", | ||
param: "4 + 6i", | ||
expect: "1 + 2i" | ||
},{ | ||
set: "3 + 4i", | ||
fn: "sub", | ||
param: "2 - 5i", | ||
expect: "1 + 9i" | ||
}, { | ||
set: "1 + 2i", | ||
fn: "pow", | ||
param: "2", | ||
expect: "-2.999999999999999 + 4.000000000000001i" | ||
}, { | ||
set: "1 + 2i", | ||
fn: "pow", | ||
param: "1 + 2i", | ||
expect: "-0.22251715680177267 + 0.10070913113607541i" | ||
}, { | ||
set: {re: 1, im: 2}, | ||
fn: "pow", | ||
param: new Complex(3, 4), | ||
expect: "0.1290095940744669 + 0.03392409290517001i" | ||
}, { | ||
fn: "abs", | ||
set: new Complex(3, 4), | ||
expect: "5" | ||
}, { | ||
param: 2, | ||
fn: "pow", | ||
set: new Complex(1, 2), | ||
expect: "-2.999999999999999 + 4.000000000000001i" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: 7, | ||
expect: "-i" | ||
}, { | ||
set: "2+3i", | ||
fn: "mul", | ||
param: "4+5i", | ||
expect: "-7 + 22i" | ||
}, { | ||
set: "3 + 4i", | ||
fn: "mul", | ||
param: "2 - 5i", | ||
expect: "26 - 7i" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: 4, | ||
expect: "1" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: 5, | ||
expect: "i" | ||
}, { | ||
set: "0-0i", | ||
fn: "pow", | ||
param: 2, | ||
expect: "0" | ||
}, { | ||
set: "0-0i", | ||
fn: "pow", | ||
param: 0, | ||
expect: "1" | ||
}, { | ||
set: "1 + 4i", | ||
fn: "sqrt", | ||
expect: "1.600485180440241 + 1.2496210676876531i" | ||
}, { | ||
set: {re: -3, im: 4}, | ||
fn: "sqrt", | ||
expect: "1 + 2i" | ||
}, { | ||
set: {re: 3, im: -4}, | ||
fn: "sqrt", | ||
expect: "2 - i" | ||
}, { | ||
set: {re: -3, im: -4}, | ||
fn: "sqrt", | ||
expect: "1 - 2i" | ||
}, { | ||
set: [-2,0], | ||
fn: "pow", | ||
param: 2, | ||
expect: "4" | ||
}, { | ||
set: {abs: 1, arg: 0}, | ||
fn: "equals", | ||
param: {re: 1, im: 0}, | ||
expect: "true" | ||
}, { | ||
set: -Complex.E.pow(2), | ||
fn: "log", | ||
expect: "2 + 3.141592653589793i" | ||
}, { | ||
set: "4 + 3i", | ||
fn: "log", | ||
expect: "1.6094379124341003 + 0.6435011087932844i" | ||
}, { | ||
set: "4 + 3i", | ||
fn: "exp", | ||
expect: "-54.051758861078156 + 7.704891372731154i" | ||
}, { | ||
set: "1-2i", | ||
fn: "sqrt", | ||
expect: "1.272019649514069 - 0.7861513777574233i" | ||
}, { | ||
set: {re: 1, im: 2}, | ||
fn: "sin", | ||
expect: "3.165778513216168 + 1.9596010414216063i" | ||
}, { | ||
set: "i", | ||
fn: "cos", | ||
expect: "1.5430806348152437" | ||
}, { | ||
set: "i", | ||
fn: "acos", | ||
expect: "1.5707963267948966 - 0.8813735870195428i" | ||
}, { | ||
set: {re: 1, im: 2}, | ||
fn: "cos", | ||
expect: "2.0327230070196656 - 3.0518977991518i" | ||
}, { | ||
set: {re: 1, im: 2}, | ||
fn: "tan", | ||
expect: "0.03381282607989669 + 1.0147936161466335i" | ||
}, { | ||
set: {re: 1, im: 3}, | ||
fn: "sinh", | ||
expect: "-1.1634403637032504 + 0.21775955162215221i" | ||
}, { | ||
set: {re: 1, im: 3}, | ||
fn: "cosh", | ||
expect: "-1.5276382501165433 + 0.1658444019189788i" | ||
}, { | ||
set: {re: 1, im: 3}, | ||
fn: "tanh", | ||
expect: "0.7680176472869112 - 0.059168539566050726i" | ||
}, { | ||
set: {re: 1, im: 3}, | ||
fn: "inverse", | ||
expect: "0.1 - 0.3i" | ||
}, { | ||
set: "3+4i", | ||
fn: "inverse", | ||
expect: "0.12 - 0.16i" // 3/25 - (4/25)i | ||
}, { | ||
set: {re: 0.5, im: -0.5}, | ||
fn: "inverse", | ||
expect: "1 + i" | ||
}, { | ||
set: "1 + i", | ||
fn: "inverse", | ||
expect: "0.5 - 0.5i" | ||
}, { | ||
set: "0", | ||
fn: "inverse", | ||
expect: "Infinity" | ||
}, { | ||
set: Infinity, | ||
fn: "inverse", | ||
expect: "0" | ||
}, { | ||
set: Complex['EPSILON'], | ||
fn: "equals", | ||
param: 1e-16, | ||
expect: "true" | ||
}, { | ||
set: 0, | ||
fn: "equals", | ||
param: "5i", | ||
expect: "false" | ||
}, { | ||
set: 5, | ||
fn: "equals", | ||
param: "5i", | ||
expect: "false" | ||
}, { | ||
set: 5, | ||
fn: "equals", | ||
param: 5, | ||
expect: "true" | ||
}, { | ||
set: "10i", | ||
fn: "equals", | ||
param: "10i", | ||
expect: "true" | ||
}, { | ||
set: "2 + 3i", | ||
fn: "equals", | ||
param: "2 + 3i", | ||
expect: "true" | ||
}, { | ||
set: "2 + 3i", | ||
fn: "equals", | ||
param: "5i", | ||
expect: "false" | ||
}, { | ||
set: "2 + 3i", | ||
fn: "round", | ||
param: "0", | ||
expect: "2 + 3i" | ||
}, { | ||
set: "2.5 + 3.5i", | ||
fn: "round", | ||
param: "1", | ||
expect: "2.5 + 3.5i" | ||
}, { | ||
set: "2.5 + 3.5i", | ||
fn: "sign", | ||
param: null, | ||
expect: "0.5812381937190965 + 0.813733471206735i" | ||
}, { | ||
set: "10 + 24i", | ||
fn: "sign", | ||
param: null, | ||
expect: "0.38461538461538464 + 0.9230769230769231i" | ||
}, { | ||
set: "1e3i", | ||
fn: "add", | ||
param: "3e-3 + 1e2i", | ||
expect: "0.003 + 1100i" | ||
}, { | ||
set: "3.14-4i", | ||
fn: "coth", | ||
expect: "0.9994481238383576 + 0.0037048958915019857i" | ||
}, { | ||
set: "8i-31", | ||
fn: "cot", | ||
expect: "1.6636768291213935e-7 - 1.0000001515864902i" | ||
}, { | ||
set: Complex(1, 1).sub(0, 1), // Distance | ||
fn: "abs", | ||
expect: "1" | ||
}, { | ||
set: Complex(1, 1), // Rotate around center | ||
fn: "mul", | ||
param: {abs: 1, arg: Math.PI / 2}, | ||
expect: "-0.9999999999999999 + i" | ||
}, { | ||
set: Complex(1, 1).sub(0, 1).mul({abs: 1, arg: Math.PI / 2}), // Rotate around another point | ||
fn: "add", | ||
param: "i", | ||
expect: "6.123233995736766e-17 + 2i" | ||
} | ||
set: Complex.I, | ||
fn: "mul", | ||
param: Complex(Math.PI).exp(), | ||
expect: "23.140692632779267i" | ||
}, { | ||
set: new Complex(1, 4), | ||
fn: "mul", | ||
param: 3, | ||
expect: "3 + 12i" | ||
}, { | ||
set: "4 + 3i", | ||
fn: "add", | ||
param: "-3 - 2i", | ||
expect: "1 + i" | ||
}, { | ||
set: "3i", | ||
fn: "add", | ||
param: "-2i", | ||
expect: "i" | ||
}, { | ||
set: "4", | ||
fn: "add", | ||
param: "-3", | ||
expect: "1" | ||
}, { | ||
set: 9, | ||
fn: "sqrt", | ||
expect: "3" | ||
}, { | ||
set: -9, | ||
fn: "sqrt", | ||
expect: "3i" | ||
}, { | ||
set: "-36", | ||
fn: "sqrt", | ||
expect: "6i" | ||
}, { | ||
set: "36i", | ||
fn: "sqrt", | ||
expect: "4.242640687119285 + 4.242640687119285i" | ||
}, { | ||
set: Infinity, | ||
fn: "mul", | ||
param: "i", | ||
expect: "Infinity" | ||
}, { | ||
set: "-36i", | ||
fn: "sqrt", | ||
expect: "4.242640687119285 - 4.242640687119285i" | ||
}, { | ||
set: "4 + 2i", | ||
fn: "div", | ||
param: "0", | ||
expect: "Infinity" | ||
}, { | ||
set: "0", | ||
fn: "div", | ||
param: Infinity, | ||
expect: "0" | ||
}, { | ||
set: -Infinity, | ||
fn: "div", | ||
param: 0, | ||
expect: "Infinity" | ||
}, { | ||
set: Infinity, | ||
fn: "div", | ||
param: Infinity, | ||
expect: "NaN" | ||
}, { | ||
set: 0, | ||
fn: "div", | ||
param: 0, | ||
expect: "NaN" | ||
}, { | ||
set: "4 + 2i", | ||
fn: "div", | ||
param: "1 + i", | ||
expect: "3 - i" | ||
}, { | ||
set: "25", | ||
fn: "div", | ||
param: "3 - 4i", | ||
expect: "3 + 4i" | ||
}, { | ||
set: "3 - 2i", | ||
fn: "div", | ||
param: "i", | ||
expect: "-2 - 3i" | ||
}, { | ||
set: "4i", | ||
fn: "mul", | ||
param: "-5i", | ||
expect: "20" | ||
}, { | ||
set: "3 - 6i", | ||
fn: "mul", | ||
param: "i", | ||
expect: "6 + 3i" | ||
}, { | ||
set: Infinity, | ||
fn: "mul", | ||
param: 0, | ||
expect: "NaN" | ||
}, { | ||
set: "3 + 4i", | ||
fn: "add", | ||
param: "5 - 7i", | ||
expect: "8 - 3i" | ||
}, { | ||
set: Infinity, | ||
fn: "add", | ||
param: Infinity, | ||
expect: "NaN" | ||
}, { | ||
set: -Infinity, | ||
fn: "sub", | ||
param: -Infinity, | ||
expect: "NaN" | ||
}, { | ||
set: "6i", | ||
fn: "div", | ||
param: "3 - 12i", | ||
expect: "-0.47058823529411764 + 0.11764705882352941i" | ||
}, { | ||
set: "36 + 36i", | ||
fn: "sqrt", | ||
expect: "6.59210468080686 + 2.730539163373364i" | ||
}, { | ||
set: "36 - 36i", | ||
fn: "sqrt", | ||
expect: "6.59210468080686 - 2.730539163373364i" | ||
}, { | ||
set: "-36 + 36i", | ||
fn: "sqrt", | ||
expect: "2.730539163373364 + 6.59210468080686i" | ||
}, { | ||
set: "-36 - 36i", | ||
fn: "sqrt", | ||
expect: "2.730539163373364 - 6.59210468080686i" | ||
}, { | ||
set: "0", | ||
fn: "sqrt", | ||
expect: "0" | ||
}, { | ||
set: Math.E, | ||
fn: "log", | ||
expect: "1" | ||
}, { | ||
set: 0, | ||
fn: "log", | ||
expect: "Infinity" | ||
}, { | ||
set: Infinity, | ||
fn: "mul", | ||
param: 3, | ||
expect: "Infinity" | ||
}, { | ||
set: "-1", | ||
fn: "log", | ||
expect: Math.PI + "i" | ||
}, { | ||
set: "i", | ||
fn: "log", | ||
expect: (Math.PI / 2) + "i" | ||
}, { | ||
set: "3 + 2i", | ||
fn: "log", | ||
expect: Math.log(13) / 2 + " + " + Math.atan2(2, 3) + "i" | ||
}, { | ||
set: "3 - 2i", | ||
fn: "log", | ||
expect: Math.log(13) / 2 + " - " + Math.atan2(2, 3) + "i" | ||
}, { | ||
set: 1, | ||
fn: "exp", | ||
expect: "" + Math.E | ||
}, { | ||
set: "i", | ||
fn: "exp", | ||
expect: Math.cos(1) + " + " + Math.sin(1) + "i" | ||
}, { | ||
set: "i", | ||
fn: "mul", | ||
param: "i", | ||
expect: "-1" | ||
}, { | ||
set: "3 + 2i", | ||
fn: "exp", | ||
expect: "-8.358532650935372 + 18.263727040666765i" | ||
}, { | ||
set: "3 - 2i", | ||
fn: "exp", | ||
expect: "-8.358532650935372 - 18.263727040666765i" | ||
}, { | ||
set: "3 - 2i", | ||
fn: "expm1", | ||
expect: "-9.358532650935372 - 18.263727040666765i" | ||
}, { | ||
set: "0", | ||
fn: "expm1", | ||
expect: "0" | ||
}, { | ||
set: "1e-6", | ||
fn: "expm1", | ||
expect: "0.0000010000005000001665" | ||
}, { | ||
set: "1e-5 + 5i", | ||
fn: "expm1", | ||
expect: "-0.716334977900736 - 0.9589338639538314i" | ||
}, { | ||
set: "1.2e-7 - 2e-6i", | ||
fn: "expm1", | ||
expect: "1.1999800719976027e-7 - 0.000002000000239998681i" | ||
}, { | ||
set: "3", | ||
fn: "pow", | ||
param: "3", | ||
expect: "27" | ||
}, { | ||
set: -2, | ||
fn: "pow", | ||
param: 1.5, | ||
expect: "-2.82842712474619i" | ||
}, { | ||
set: -8, | ||
fn: "pow", | ||
param: 1 / 3, | ||
expect: "1 + 1.732050807568877i" | ||
}, { | ||
set: -25, | ||
fn: "sqrt", | ||
expect: "5i" | ||
}, { | ||
set: -25, | ||
fn: "pow", | ||
param: 0.5, | ||
expect: "5i" | ||
}, { | ||
set: "0", | ||
fn: "pow", | ||
param: "1+i", | ||
expect: "0" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "0", | ||
expect: "1" | ||
}, { | ||
set: "87", | ||
fn: "pow", | ||
param: "3", | ||
expect: "658503" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "1", | ||
expect: "i" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "2", | ||
expect: "-1" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "3", | ||
expect: "-i" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "4", | ||
expect: "1" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: "5", | ||
expect: "i" | ||
}, { | ||
set: 7, | ||
fn: "pow", | ||
param: 2, | ||
expect: '49' | ||
}, { | ||
set: 0, | ||
fn: "pow", | ||
param: 2, | ||
expect: '0' | ||
}, { | ||
set: "3i", | ||
fn: "pow", | ||
param: "3i", | ||
expect: "-0.008876640735623678 - 0.0013801328997494863i" | ||
}, { | ||
set: { re: 3, im: 4 }, | ||
fn: "abs", | ||
expect: "5" | ||
}, { | ||
set: { re: 10, im: 24 }, | ||
fn: "abs", | ||
expect: "26" | ||
}, { | ||
set: "+++++--+1 + 4i", | ||
fn: "mul", | ||
param: "3 + 2i", | ||
expect: "-5 + 14i" | ||
}, { | ||
set: "4 + 16i", | ||
fn: "div", | ||
param: "4.0000", | ||
expect: "1 + 4i" | ||
}, { | ||
set: { re: -7.1, im: 2.5 }, | ||
fn: "neg", | ||
expect: "7.1 - 2.5i" | ||
}, { | ||
set: { re: 1, im: 1 }, | ||
fn: "div", | ||
param: { re: 3, im: 4 }, | ||
expect: 7 / 25 + " - " + 1 / 25 + "i" | ||
}, { | ||
set: new Complex(-7.1, 2.5), | ||
fn: "neg", | ||
expect: "7.1 - 2.5i" | ||
}, { | ||
set: { re: 1, im: 1 }, | ||
fn: "arg", | ||
expect: "" + Math.PI / 4 | ||
}, { | ||
set: { re: -1, im: -1 }, | ||
fn: "arg", | ||
expect: "" + -3 * Math.PI / 4 | ||
}, { | ||
set: { re: 0, im: 1 }, | ||
fn: "arg", | ||
expect: "" + Math.PI / 2 | ||
}, { | ||
set: { re: 1, im: 0.5 * Math.sqrt(4 / 3) }, | ||
fn: "arg", | ||
expect: "" + Math.PI / 6 | ||
}, { | ||
set: "3 + 4i", | ||
fn: "conjugate", | ||
expect: "3 - 4i" | ||
}, { | ||
set: { re: 99, im: 50 }, | ||
fn: "conjugate", | ||
expect: "99 - 50i" | ||
}, { | ||
set: { re: 0, im: 0 }, | ||
fn: "conjugate", | ||
expect: "0" | ||
}, { | ||
set: { re: 1, im: 23 }, | ||
fn: "conjugate", | ||
expect: "1 - 23i" | ||
}, { | ||
set: "2 + 8i", | ||
fn: "div", | ||
param: new Complex(1, 2), | ||
expect: "3.6 + 0.8i" | ||
}, { | ||
set: "2 + 8i", | ||
fn: "div", | ||
param: "2 + 8i", | ||
expect: "1" | ||
}, { | ||
set: -Infinity, | ||
fn: "div", | ||
param: 3, | ||
expect: "Infinity" | ||
}, { | ||
set: "3+4i", | ||
fn: "add", | ||
param: "5 - i", | ||
expect: "8 + 3i" | ||
}, { | ||
set: { re: 1, im: 2 }, | ||
fn: "add", | ||
param: "4 + 6i", | ||
expect: "5 + 8i" | ||
}, { | ||
set: { re: 5, im: 8 }, | ||
fn: "sub", | ||
param: "4 + 6i", | ||
expect: "1 + 2i" | ||
}, { | ||
set: "3 + 4i", | ||
fn: "sub", | ||
param: "2 - 5i", | ||
expect: "1 + 9i" | ||
}, { | ||
set: "1 + 2i", | ||
fn: "pow", | ||
param: "2", | ||
expect: "-2.999999999999999 + 4.000000000000001i" | ||
}, { | ||
set: "1 + 2i", | ||
fn: "pow", | ||
param: "1 + 2i", | ||
expect: "-0.22251715680177267 + 0.10070913113607541i" | ||
}, { | ||
set: { re: 1, im: 2 }, | ||
fn: "pow", | ||
param: new Complex(3, 4), | ||
expect: "0.1290095940744669 + 0.03392409290517001i" | ||
}, { | ||
fn: "abs", | ||
set: new Complex(3, 4), | ||
expect: "5" | ||
}, { | ||
param: 2, | ||
fn: "pow", | ||
set: new Complex(1, 2), | ||
expect: "-2.999999999999999 + 4.000000000000001i" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: 7, | ||
expect: "-i" | ||
}, { | ||
set: "2+3i", | ||
fn: "mul", | ||
param: "4+5i", | ||
expect: "-7 + 22i" | ||
}, { | ||
set: "3 + 4i", | ||
fn: "mul", | ||
param: "2 - 5i", | ||
expect: "26 - 7i" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: 4, | ||
expect: "1" | ||
}, { | ||
set: "i", | ||
fn: "pow", | ||
param: 5, | ||
expect: "i" | ||
}, { | ||
set: "0-0i", | ||
fn: "pow", | ||
param: 2, | ||
expect: "0" | ||
}, { | ||
set: "0-0i", | ||
fn: "pow", | ||
param: 0, | ||
expect: "1" | ||
}, { | ||
set: "1 + 4i", | ||
fn: "sqrt", | ||
expect: "1.600485180440241 + 1.2496210676876531i" | ||
}, { | ||
set: { re: -3, im: 4 }, | ||
fn: "sqrt", | ||
expect: "1 + 2i" | ||
}, { | ||
set: { re: 3, im: -4 }, | ||
fn: "sqrt", | ||
expect: "2 - i" | ||
}, { | ||
set: { re: -3, im: -4 }, | ||
fn: "sqrt", | ||
expect: "1 - 2i" | ||
}, { | ||
set: [-2, 0], | ||
fn: "pow", | ||
param: 2, | ||
expect: "4" | ||
}, { | ||
set: { abs: 1, arg: 0 }, | ||
fn: "equals", | ||
param: { re: 1, im: 0 }, | ||
expect: "true" | ||
}, { | ||
set: -Complex.E.pow(2), | ||
fn: "log", | ||
expect: "2 + 3.141592653589793i" | ||
}, { | ||
set: "4 + 3i", | ||
fn: "log", | ||
expect: "1.6094379124341003 + 0.6435011087932844i" | ||
}, { | ||
set: "4 + 3i", | ||
fn: "exp", | ||
expect: "-54.051758861078156 + 7.704891372731154i" | ||
}, { | ||
set: "1-2i", | ||
fn: "sqrt", | ||
expect: "1.272019649514069 - 0.7861513777574233i" | ||
}, { | ||
set: { re: 1, im: 2 }, | ||
fn: "sin", | ||
expect: "3.165778513216168 + 1.9596010414216063i" | ||
}, { | ||
set: "i", | ||
fn: "cos", | ||
expect: "1.5430806348152437" | ||
}, { | ||
set: "i", | ||
fn: "acos", | ||
expect: "1.5707963267948966 - 0.8813735870195428i" | ||
}, { | ||
set: { re: 1, im: 2 }, | ||
fn: "cos", | ||
expect: "2.0327230070196656 - 3.0518977991518i" | ||
}, { | ||
set: { re: 1, im: 2 }, | ||
fn: "tan", | ||
expect: "0.03381282607989669 + 1.0147936161466335i" | ||
}, { | ||
set: { re: 1, im: 3 }, | ||
fn: "sinh", | ||
expect: "-1.1634403637032504 + 0.21775955162215221i" | ||
}, { | ||
set: { re: 1, im: 3 }, | ||
fn: "cosh", | ||
expect: "-1.5276382501165433 + 0.1658444019189788i" | ||
}, { | ||
set: { re: 1, im: 3 }, | ||
fn: "tanh", | ||
expect: "0.7680176472869112 - 0.059168539566050726i" | ||
}, { | ||
set: { re: 1, im: 3 }, | ||
fn: "inverse", | ||
expect: "0.1 - 0.3i" | ||
}, { | ||
set: "3+4i", | ||
fn: "inverse", | ||
expect: "0.12 - 0.16i" // 3/25 - (4/25)i | ||
}, { | ||
set: { re: 0.5, im: -0.5 }, | ||
fn: "inverse", | ||
expect: "1 + i" | ||
}, { | ||
set: "1 + i", | ||
fn: "inverse", | ||
expect: "0.5 - 0.5i" | ||
}, { | ||
set: "0", | ||
fn: "inverse", | ||
expect: "Infinity" | ||
}, { | ||
set: Infinity, | ||
fn: "inverse", | ||
expect: "0" | ||
}, { | ||
set: Complex['EPSILON'], | ||
fn: "equals", | ||
param: 1e-16, | ||
expect: "true" | ||
}, { | ||
set: 0, | ||
fn: "equals", | ||
param: "5i", | ||
expect: "false" | ||
}, { | ||
set: 5, | ||
fn: "equals", | ||
param: "5i", | ||
expect: "false" | ||
}, { | ||
set: 5, | ||
fn: "equals", | ||
param: 5, | ||
expect: "true" | ||
}, { | ||
set: "10i", | ||
fn: "equals", | ||
param: "10i", | ||
expect: "true" | ||
}, { | ||
set: "2 + 3i", | ||
fn: "equals", | ||
param: "2 + 3i", | ||
expect: "true" | ||
}, { | ||
set: "2 + 3i", | ||
fn: "equals", | ||
param: "5i", | ||
expect: "false" | ||
}, { | ||
set: "2 + 3i", | ||
fn: "round", | ||
param: "0", | ||
expect: "2 + 3i" | ||
}, { | ||
set: "2.5 + 3.5i", | ||
fn: "round", | ||
param: "1", | ||
expect: "2.5 + 3.5i" | ||
}, { | ||
set: "2.5 + 3.5i", | ||
fn: "sign", | ||
param: null, | ||
expect: "0.5812381937190965 + 0.813733471206735i" | ||
}, { | ||
set: "10 + 24i", | ||
fn: "sign", | ||
param: null, | ||
expect: "0.38461538461538464 + 0.9230769230769231i" | ||
}, { | ||
set: "1e3i", | ||
fn: "add", | ||
param: "3e-3 + 1e2i", | ||
expect: "0.003 + 1100i" | ||
}, { | ||
set: "3.14-4i", | ||
fn: "coth", | ||
expect: "0.9994481238383576 + 0.0037048958915019857i" | ||
}, { | ||
set: "8i-31", | ||
fn: "cot", | ||
expect: "1.6636768291213935e-7 - 1.0000001515864902i" | ||
}, { | ||
set: Complex(1, 1).sub(0, 1), // Distance | ||
fn: "abs", | ||
expect: "1" | ||
}, { | ||
set: Complex(1, 1), // Rotate around center | ||
fn: "mul", | ||
param: { abs: 1, arg: Math.PI / 2 }, | ||
expect: "-0.9999999999999999 + i" | ||
}, { | ||
set: Complex(1, 1).sub(0, 1).mul({ abs: 1, arg: Math.PI / 2 }), // Rotate around another point | ||
fn: "add", | ||
param: "i", | ||
expect: "2i" | ||
} | ||
]; | ||
var constructorTests = [{ | ||
set: null, | ||
expect: "0" | ||
}, { | ||
set: undefined, | ||
expect: "0" | ||
}, { | ||
set: "foo", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: {}, | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: " + i", | ||
expect: "i" | ||
}, { | ||
set: "3+4i", | ||
expect: "3 + 4i" | ||
}, { | ||
set: "i", | ||
expect: "i" | ||
}, { | ||
set: "3", | ||
expect: "3" | ||
}, { | ||
set: [9, 8], | ||
expect: "9 + 8i" | ||
}, { | ||
set: "2.3", | ||
expect: "2.3" | ||
}, { | ||
set: "2.3", | ||
expect: "2.3" | ||
}, { | ||
set: "0", | ||
expect: "0" | ||
}, { | ||
set: "-0", | ||
expect: "0" | ||
}, { | ||
set: {re: -0, im: 0}, | ||
expect: "0" | ||
}, { | ||
set: {re: 0, im: -0}, | ||
expect: "0" | ||
}, { | ||
set: Infinity, | ||
expect: "Infinity" | ||
}, { | ||
set: -Infinity, | ||
expect: "Infinity" | ||
}, { | ||
set: {re: Infinity, im: 0}, | ||
expect: "Infinity" | ||
}, { | ||
set: {re: -Infinity, im: 0}, | ||
expect: "Infinity" | ||
}, { | ||
set: {re: 0, im: Infinity}, | ||
expect: "Infinity" | ||
}, { | ||
set: {re: 0, im: -Infinity}, | ||
expect: "Infinity" | ||
}, { | ||
set: " + 7 - i + 3i - + + + + 43 + 2i - i4 + - 33 + 65 - 1 ", | ||
expect: "-5" | ||
}, { | ||
set: " + 7 - i + 3i - + + + + 43 + 2i - i4 + - 33 + 65 - 1 + ", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: "-3x + 4", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: "- + 7", | ||
expect: "-7" | ||
}, { | ||
set: "4 5i", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: "-", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: "2.2e-1-3.2e-1i", | ||
expect: "0.22 - 0.32i" | ||
}, { | ||
set: "2.2.", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: {r: 0, phi: 4}, | ||
expect: "0" | ||
}, { | ||
set: {r: 1, phi: 1}, | ||
expect: "0.5403023058681398 + 0.8414709848078965i" | ||
}, { | ||
set: {r: Infinity, phi: 0}, | ||
expect: "Infinity" | ||
}, { | ||
set: {r: Infinity, phi: 2}, | ||
expect: "Infinity" | ||
}, { | ||
set: {r: Infinity, phi: Infinity}, | ||
expect: "NaN" | ||
}, { | ||
set: {r: Infinity, phi: NaN}, | ||
expect: "NaN" | ||
} | ||
set: null, | ||
expect: "0" | ||
}, { | ||
set: undefined, | ||
expect: "0" | ||
}, { | ||
set: "foo", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: {}, | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: " + i", | ||
expect: "i" | ||
}, { | ||
set: "3+4i", | ||
expect: "3 + 4i" | ||
}, { | ||
set: "i", | ||
expect: "i" | ||
}, { | ||
set: "3", | ||
expect: "3" | ||
}, { | ||
set: [9, 8], | ||
expect: "9 + 8i" | ||
}, { | ||
set: "2.3", | ||
expect: "2.3" | ||
}, { | ||
set: "2.3", | ||
expect: "2.3" | ||
}, { | ||
set: "0", | ||
expect: "0" | ||
}, { | ||
set: "-0", | ||
expect: "0" | ||
}, { | ||
set: { re: -0, im: 0 }, | ||
expect: "0" | ||
}, { | ||
set: { re: 0, im: -0 }, | ||
expect: "0" | ||
}, { | ||
set: Infinity, | ||
expect: "Infinity" | ||
}, { | ||
set: -Infinity, | ||
expect: "Infinity" | ||
}, { | ||
set: { re: Infinity, im: 0 }, | ||
expect: "Infinity" | ||
}, { | ||
set: { re: -Infinity, im: 0 }, | ||
expect: "Infinity" | ||
}, { | ||
set: { re: 0, im: Infinity }, | ||
expect: "Infinity" | ||
}, { | ||
set: { re: 0, im: -Infinity }, | ||
expect: "Infinity" | ||
}, { | ||
set: " + 7 - i + 3i - + + + + 43 + 2i - i4 + - 33 + 65 - 1 ", | ||
expect: "-5" | ||
}, { | ||
set: " + 7 - i + 3i - + + + + 43 + 2i - i4 + - 33 + 65 - 1 + ", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: "-3x + 4", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: "- + 7", | ||
expect: "-7" | ||
}, { | ||
set: "4 5i", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: "-", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: "2.2e-1-3.2e-1i", | ||
expect: "0.22 - 0.32i" | ||
}, { | ||
set: "2.2.", | ||
error: "SyntaxError: Invalid Param" | ||
}, { | ||
set: { r: 0, phi: 4 }, | ||
expect: "0" | ||
}, { | ||
set: { r: 1, phi: 1 }, | ||
expect: "0.5403023058681398 + 0.8414709848078965i" | ||
}, { | ||
set: { r: Infinity, phi: 0 }, | ||
expect: "Infinity" | ||
}, { | ||
set: { r: Infinity, phi: 2 }, | ||
expect: "Infinity" | ||
}, { | ||
set: { r: Infinity, phi: Infinity }, | ||
expect: "NaN" | ||
}, { | ||
set: { r: Infinity, phi: NaN }, | ||
expect: "NaN" | ||
} | ||
]; | ||
@@ -780,6 +799,6 @@ | ||
} catch (e) { | ||
assert.equal(e.toString(), test.error.toString()); | ||
assert.strictEqual(e.toString(), test.error.toString()); | ||
} | ||
} else { | ||
assert.equal(new Complex(test.set)[test.fn](test.param).toString(), test.expect); | ||
assert.strictEqual(new Complex(test.set)[test.fn](test.param).toString(), test.expect); | ||
} | ||
@@ -791,3 +810,3 @@ }); | ||
describe("Complex constructor", function() { | ||
describe("Complex constructor", function () { | ||
@@ -802,6 +821,6 @@ for (var i = 0; i < constructorTests.length; i++) { | ||
} catch (e) { | ||
assert.equal(e.toString(), test.error.toString()); | ||
assert.strictEqual(e.toString(), test.error.toString()); | ||
} | ||
} else { | ||
assert.equal(new Complex(test.set).toString(), test.expect); | ||
assert.strictEqual(new Complex(test.set).toString(), test.expect); | ||
} | ||
@@ -816,8 +835,8 @@ }); | ||
it("should work with different params", function () { | ||
assert.equal(Complex(1, -1).toString(), "1 - i"); | ||
assert.equal(Complex(0, 0).toString(), "0"); | ||
assert.equal(Complex(0, 2).toString(), "2i"); | ||
assert.equal(Complex.I.toString(), "i"); | ||
assert.equal(Complex(0, -2).toString(), "-2i"); | ||
assert.equal(Complex({re: 0, im: -2}).toString(), "-2i"); | ||
assert.strictEqual(Complex(1, -1).toString(), "1 - i"); | ||
assert.strictEqual(Complex(0, 0).toString(), "0"); | ||
assert.strictEqual(Complex(0, 2).toString(), "2i"); | ||
assert.strictEqual(Complex.I.toString(), "i"); | ||
assert.strictEqual(Complex(0, -2).toString(), "-2i"); | ||
assert.strictEqual(Complex({ re: 0, im: -2 }).toString(), "-2i"); | ||
}); | ||
@@ -829,31 +848,31 @@ | ||
assert.equal(zero.toString(), "0"); | ||
assert.equal(one.toString(), "1 + i"); | ||
assert.strictEqual(zero.toString(), "0"); | ||
assert.strictEqual(one.toString(), "1 + i"); | ||
assert(one.neg().equals(Complex(-1, -1))); | ||
assert(one.conjugate().equals(Complex(1, -1))); | ||
assert.equal(one.abs(), Math.SQRT2); | ||
assert.equal(one.arg(), Math.PI / 4); | ||
assert.equal(one.add(one).toString(), two.toString()); | ||
assert.equal(one.sub(one).toString(), zero.toString()); | ||
assert.equal(one.mul(2).toString(), two.toString()); | ||
assert.equal(one.mul(one).toString(), Complex(0, 2).toString()); | ||
assert.equal(one.div(2).toString(), "0.5 + 0.5i"); | ||
assert.equal(one.div(one).toString(), "1"); | ||
assert.equal(one.div(0).toString(), "Infinity"); | ||
assert.equal(one.exp().toString(), "1.4686939399158851 + 2.2873552871788423i"); | ||
assert.equal(one.log().toString(), "0.34657359027997264 + 0.7853981633974483i"); | ||
assert.equal(one.pow(one).toString(), "0.2739572538301211 + 0.5837007587586147i"); | ||
assert.equal(one.pow(zero).toString(), "1"); | ||
assert.equal(one.sqrt().toString(), "1.09868411346781 + 0.45508986056222733i"); | ||
assert.equal(one.sin().toString(), "1.2984575814159773 + 0.6349639147847361i"); | ||
assert.equal(one.cos().toString(), "0.8337300251311491 - 0.9888977057628651i"); | ||
assert.equal(one.tan().toString(), "0.27175258531951174 + 1.0839233273386948i"); | ||
assert.equal(one.asin().toString(), "0.6662394324925153 + 1.0612750619050355i"); | ||
assert.equal(one.acos().toString(), "0.9045568943023813 - 1.0612750619050355i"); | ||
assert.equal(one.atan().toString(), "1.0172219678978514 + 0.40235947810852507i"); | ||
assert.strictEqual(one.abs(), Math.SQRT2); | ||
assert.strictEqual(one.arg(), Math.PI / 4); | ||
assert.strictEqual(one.add(one).toString(), two.toString()); | ||
assert.strictEqual(one.sub(one).toString(), zero.toString()); | ||
assert.strictEqual(one.mul(2).toString(), two.toString()); | ||
assert.strictEqual(one.mul(one).toString(), Complex(0, 2).toString()); | ||
assert.strictEqual(one.div(2).toString(), "0.5 + 0.5i"); | ||
assert.strictEqual(one.div(one).toString(), "1"); | ||
assert.strictEqual(one.div(0).toString(), "Infinity"); | ||
assert.strictEqual(one.exp().toString(), "1.4686939399158851 + 2.2873552871788423i"); | ||
assert.strictEqual(one.log().toString(), "0.34657359027997264 + 0.7853981633974483i"); | ||
assert.strictEqual(one.pow(one).toString(), "0.2739572538301211 + 0.5837007587586147i"); | ||
assert.strictEqual(one.pow(zero).toString(), "1"); | ||
assert.strictEqual(one.sqrt().toString(), "1.09868411346781 + 0.45508986056222733i"); | ||
assert.strictEqual(one.sin().toString(), "1.2984575814159773 + 0.6349639147847361i"); | ||
assert.strictEqual(one.cos().toString(), "0.8337300251311491 - 0.9888977057628651i"); | ||
assert.strictEqual(one.tan().toString(), "0.27175258531951174 + 1.0839233273386948i"); | ||
assert.strictEqual(one.asin().toString(), "0.6662394324925153 + 1.0612750619050355i"); | ||
assert.strictEqual(one.acos().toString(), "0.9045568943023813 - 1.0612750619050355i"); | ||
assert.strictEqual(one.atan().toString(), "1.0172219678978514 + 0.40235947810852507i"); | ||
assert.equal(Complex(3, 4).abs(), "5"); | ||
assert.strictEqual(Complex(3, 4).abs(), 5); | ||
assert.equal(Complex("5i + 3").log().exp().toString(), "3 + 5i") | ||
assert.equal(Complex("-2i - 1").log().exp().toString(), "-1 - 2i") | ||
assert.strictEqual(Complex("5i + 3").log().exp().toString(), "3 + 5i") | ||
assert.strictEqual(Complex("-2i - 1").log().exp().toString(), "-1 - 2i") | ||
}); | ||
@@ -869,3 +888,3 @@ | ||
assert.equal(r1.toString(), r2.toString()); | ||
assert.strictEqual(r1.toString(), r2.toString()); | ||
}); | ||
@@ -878,3 +897,3 @@ | ||
assert.equal(t.toString(), "-9.405287417451663e-14 - 511.9999999999995i"); | ||
assert.strictEqual(t.toString(), "-9.405287417451663e-14 - 511.9999999999995i"); | ||
}); | ||
@@ -906,53 +925,53 @@ | ||
it('should handle get real part', function () { | ||
assert.equal(Complex({abs: 1, arg: Math.PI / 4}).re, Math.SQRT2 / 2); | ||
assert.strictEqual(Complex({ abs: 1, arg: Math.PI / 4 }).re, Math.SQRT2 / 2); | ||
}); | ||
it('should handle get complex part', function () { | ||
assert.equal(Complex({abs: 1, arg: Math.PI / 4}).im, "0.7071067811865475"); | ||
assert.strictEqual(Complex({ abs: 1, arg: Math.PI / 4 }).im, 0.7071067811865475); | ||
}); | ||
it('should handle sum', function () { | ||
assert.equal(Complex({abs: 1, arg: 0}).add({abs: 1, arg: Math.PI / 2}).abs(), Math.SQRT2); | ||
assert.equal(Complex({abs: 1, arg: 0}).add({abs: 1, arg: Math.PI / 2}).arg(), Math.PI / 4); | ||
assert.strictEqual(Complex({ abs: 1, arg: 0 }).add({ abs: 1, arg: Math.PI / 2 }).abs(), Math.SQRT2); | ||
assert.strictEqual(Complex({ abs: 1, arg: 0 }).add({ abs: 1, arg: Math.PI / 2 }).arg(), Math.PI / 4); | ||
}); | ||
it('should handle conjugate', function () { | ||
assert.equal(Complex({abs: 1, arg: Math.PI / 4}).conjugate().toString(), Complex({abs: 1, arg: -Math.PI / 4}).toString()); | ||
assert.strictEqual(Complex({ abs: 1, arg: Math.PI / 4 }).conjugate().toString(), Complex({ abs: 1, arg: -Math.PI / 4 }).toString()); | ||
}); | ||
it('should handle substract', function () { | ||
assert.equal(Complex({abs: 1, arg: 0}).sub({abs: 1, arg: Math.PI / 2}).abs().toString(), "1.414213562373095"); | ||
assert.equal(Complex({abs: 1, arg: 0}).sub({abs: 1, arg: Math.PI / 2}).arg().toString(), "-0.7853981633974484"); | ||
assert.strictEqual(Complex({ abs: 1, arg: 0 }).sub({ abs: 1, arg: Math.PI / 2 }).abs().toString(), "1.414213562373095"); | ||
assert.strictEqual(Complex({ abs: 1, arg: 0 }).sub({ abs: 1, arg: Math.PI / 2 }).arg().toString(), "-0.7853981633974484"); | ||
}); | ||
it('should handle arg for the first quadrant', function () { | ||
assert.equal(Complex({re: 1, im: 1}).arg(), Math.PI / 4); | ||
assert.strictEqual(Complex({ re: 1, im: 1 }).arg(), Math.PI / 4); | ||
}); | ||
it('should handle arg for the second quadrant', function () { | ||
assert.equal(Complex({re: -1, im: 1}).arg(), 3 * Math.PI / 4); | ||
assert.strictEqual(Complex({ re: -1, im: 1 }).arg(), 3 * Math.PI / 4); | ||
}); | ||
it('should handle arg for the third quadrant', function () { | ||
assert.equal(Complex({re: -1, im: -1}).arg(), -3 * Math.PI / 4); | ||
assert.strictEqual(Complex({ re: -1, im: -1 }).arg(), -3 * Math.PI / 4); | ||
}); | ||
it('should handle arg for the fourth quadrant', function () { | ||
assert.equal(Complex({re: 1, im: -1}).arg(), -Math.PI / 4); | ||
assert.strictEqual(Complex({ re: 1, im: -1 }).arg(), -Math.PI / 4); | ||
}); | ||
it('should handle arg for the fourth and first quadrant', function () { | ||
assert.equal(Complex({re: 1, im: 0}).arg(), 0); | ||
assert.strictEqual(Complex({ re: 1, im: 0 }).arg(), 0); | ||
}); | ||
it('should handle arg for first and second quadrant', function () { | ||
assert.equal(Complex({re: 0, im: 1}).arg(), Math.PI / 2); | ||
assert.strictEqual(Complex({ re: 0, im: 1 }).arg(), Math.PI / 2); | ||
}); | ||
it('should handle arg for the second and third quadrant', function () { | ||
assert.equal(Complex({re: -1, im: 0}).arg(), Math.PI); | ||
assert.strictEqual(Complex({ re: -1, im: 0 }).arg(), Math.PI); | ||
}); | ||
it('should handle arg for the third and fourth quadrant', function () { | ||
assert.equal(Complex({re: 0, im: -1}).arg(), -Math.PI / 2); | ||
assert.strictEqual(Complex({ re: 0, im: -1 }).arg(), -Math.PI / 2); | ||
}); | ||
@@ -966,3 +985,3 @@ | ||
assert.equal(c.toString(), '-24 - 10i'); | ||
assert.strictEqual(c.toString(), '-24 - 10i'); | ||
}); | ||
@@ -974,3 +993,3 @@ | ||
assert.equal(a.toString(), '1'); | ||
assert.strictEqual(a.toString(), '1'); | ||
}); | ||
@@ -981,5 +1000,5 @@ | ||
assert.equal(n.toString(), '2 + ' + 4 * Math.PI + "i"); | ||
assert.strictEqual(n.toString(), '2 + ' + 4 * Math.PI + "i"); | ||
}); | ||
}); |
2145
71329