79 lines
2.1 KiB
JavaScript

'use strict';
module.exports = function (math) {
var util = require('../../util/index'),
BigNumber = math.type.BigNumber,
Complex = require('../../type/Complex'),
collection = require('../../type/collection'),
isNumber = util.number.isNumber,
isBoolean = util['boolean'].isBoolean,
isComplex = Complex.isComplex,
isCollection = collection.isCollection;
/**
* Calculate the inverse tangent function with two arguments, y/x.
* By providing two arguments, the right quadrant of the computed angle can be
* determined.
*
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.atan2(y, x)
*
* Examples:
*
* math.atan2(2, 2) / math.pi; // returns number 0.25
*
* var angle = math.unit(60, 'deg'); // returns Unit 60 deg
* var x = math.cos(angle);
* var y = math.sin(angle);
*
* math.atan(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i
*
* See also:
*
* tan, atan, sin, cos
*
* @param {Number | Boolean | Complex | Array | Matrix} y Second dimension
* @param {Number | Boolean | Complex | Array | Matrix} x First dimension
* @return {Number | Complex | Array | Matrix} Four-quadrant inverse tangent
*/
math.atan2 = function atan2(y, x) {
if (arguments.length != 2) {
throw new math.error.ArgumentsError('atan2', arguments.length, 2);
}
if (isNumber(y)) {
if (isNumber(x)) {
return Math.atan2(y, x);
}
}
// TODO: support for complex computation of atan2
if (isCollection(y) || isCollection(x)) {
return collection.deepMap2(y, x, atan2);
}
if (isBoolean(y)) {
return atan2(+y, x);
}
if (isBoolean(x)) {
return atan2(y, +x);
}
// TODO: implement bignumber support
if (y instanceof BigNumber) {
return atan2(y.toNumber(), x);
}
if (x instanceof BigNumber) {
return atan2(y, x.toNumber());
}
throw new math.error.UnsupportedTypeError('atan2', math['typeof'](y), math['typeof'](x));
};
};