mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
Add fromPolar and math.complex with polar input #76
This commit is contained in:
parent
721f21574e
commit
754b33b983
@ -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:
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
@ -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);
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user