From 754b33b983db59363ebd2c4d49d1b2d4ce5ee4d2 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 4 Apr 2014 01:21:08 +0200 Subject: [PATCH] Add fromPolar and math.complex with polar input #76 --- lib/function/construction/complex.js | 5 ++++ lib/type/Complex.js | 31 ++++++++++++++++++++++ test/function/construction/complex.test.js | 5 ++++ test/type/Complex.test.js | 31 +++++++++++++++++++++- 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/lib/function/construction/complex.js b/lib/function/construction/complex.js index 74dafb0e3..9db363b54 100644 --- a/lib/function/construction/complex.js +++ b/lib/function/construction/complex.js @@ -76,6 +76,11 @@ module.exports = function (math) { return collection.deepMap(arg, complex); } + if(typeof arg === 'object' && 'r' in arg && 'phi' in arg) { + // polar coordinates + return Complex.fromPolar(arg.r, arg.phi); + } + throw new TypeError('Two numbers or a single string expected in function complex'); case 2: diff --git a/lib/type/Complex.js b/lib/type/Complex.js index 35f5e374a..34138b57c 100644 --- a/lib/type/Complex.js +++ b/lib/type/Complex.js @@ -1,7 +1,9 @@ var util = require('../util/index'), + Unit = require('./Unit'), number = util.number, isNumber = util.number.isNumber, + isUnit = Unit.isUnit, isString = util.string.isString; /** @@ -272,6 +274,35 @@ Complex.parse = function parse (str) { return null; }; +/** + * Create a complex number from polar coordinates + * @return {Complex} + */ +Complex.fromPolar = function fromPolar(args) { + switch (arguments.length) { + case 1: + var arg = arguments[0]; + if(typeof arg === 'object') { + return Complex.fromPolar(arg.r, arg.phi); + } + throw new TypeError('Input has to be an object with r and phi keys.'); + case 2: + var r = arguments[0], + phi = arguments[1]; + if(isNumber(r)) { + if(isNumber(phi) || (isUnit(phi) && phi.hasBase(Unit.BASE_UNITS.ANGLE))) { + return new Complex(r * math.cos(phi), r * math.sin(phi)); + } else { + throw new TypeError('Phi is not a number nor an angle unit.'); + } + } else { + throw new TypeError('Radius r is not a number.'); + } + default: + throw new math.error.ArgumentsError('fromPolar', arguments.length, 1, 2); + } +} + /** * Create a copy of the complex value * @return {Complex} clone diff --git a/test/function/construction/complex.test.js b/test/function/construction/complex.test.js index 82e9a781e..c25b9c3b1 100644 --- a/test/function/construction/complex.test.js +++ b/test/function/construction/complex.test.js @@ -39,6 +39,11 @@ describe('complex', function() { assert.deepEqual(complex([2, 1, complex(2, 3)]), result); }); + it('should accept polar coordinates as input', function() { + var polar = complex({r: 1, phi: 1}); + assert.deepEqual(polar, new math.type.Complex.fromPolar(1, 1)); + }); + it('should throw an error if called with a string', function() { assert.throws(function () {complex('no valid complex number')}, SyntaxError); }); diff --git a/test/type/Complex.test.js b/test/type/Complex.test.js index 760109db6..c46575705 100644 --- a/test/type/Complex.test.js +++ b/test/type/Complex.test.js @@ -1,7 +1,8 @@ // test data type Complex var assert = require('assert'), - Complex = require('../../lib/type/Complex'); + Complex = require('../../lib/type/Complex'), + math = require('../../index')(); describe('Complex', function () { @@ -182,4 +183,32 @@ describe('Complex', function () { }); + describe('fromPolar', function() { + it('should save polar coordinates input correctly', function() { + var complex1 = Complex.fromPolar({r: 0, phi: 4}); + var complex2 = Complex.fromPolar({r: 5, phi: 0}); + var complex3 = Complex.fromPolar({r: 1, phi: math.pi}); + var complex4 = Complex.fromPolar({r: 3, phi: math.pi / 2}); + assertComplex(complex1, 0, 0); + assertComplex(complex2, 5, 0); + assert.equal(complex3.re, -1); + assert.equal(complex4.im, 3); + }); + + it('should have the same value for the different import ways', function() { + var way1 = Complex.fromPolar(1, 1); + var way2 = Complex.fromPolar({r: 1, phi: 1}); + assert(way1.equals(way2)); + }); + + it('should accept angle units for phi properly', function() { + var fromDeg = Complex.fromPolar(1, math.unit('90deg')), + fromRad = Complex.fromPolar(1, math.unit('0rad')), + fromGrad = Complex.fromPolar(1, math.unit('100grad')); + assert.equal(fromDeg.im, 1); + assert.equal(fromGrad.im, 1); + assert.equal(fromRad.im, 0); + + }); + }); }); \ No newline at end of file