mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
84 lines
2.0 KiB
JavaScript
84 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
var bigArcSin = require('../../util/bignumber').arcsin_arccsc;
|
|
|
|
function factory (type, config, load, typed) {
|
|
var collection = load(require('../../type/collection'));
|
|
var complexSqrt = load(require('../arithmetic/sqrt')).signatures['Complex'];
|
|
var complexLog = load(require('../arithmetic/log')).signatures['Complex'];
|
|
|
|
/**
|
|
* Calculate the inverse sine of a value.
|
|
*
|
|
* For matrices, the function is evaluated element wise.
|
|
*
|
|
* Syntax:
|
|
*
|
|
* math.asin(x)
|
|
*
|
|
* Examples:
|
|
*
|
|
* math.asin(0.5); // returns Number 0.5235987755982989
|
|
* math.asin(math.sin(1.5)); // returns Number ~1.5
|
|
*
|
|
* math.asin(2); // returns Complex 1.5707963267948966 -1.3169578969248166 i
|
|
*
|
|
* See also:
|
|
*
|
|
* sin, atan, acos
|
|
*
|
|
* @param {Number | BigNumber | Boolean | Complex | Array | Matrix | null} x Function input
|
|
* @return {Number | BigNumber | Complex | Array | Matrix} The arc sine of x
|
|
*/
|
|
var asin = typed('asin', {
|
|
'number': function (x) {
|
|
if (x >= -1 && x <= 1) {
|
|
return Math.asin(x);
|
|
}
|
|
else {
|
|
return _complexAsin(new type.Complex(x, 0));
|
|
}
|
|
},
|
|
|
|
'Complex': _complexAsin,
|
|
|
|
'BigNumber': function (x) {
|
|
return bigArcSin(x, type.BigNumber, false);
|
|
},
|
|
|
|
'Array | Matrix': function (x) {
|
|
// deep map collection, skip zeros since asin(0) = 0
|
|
return collection.deepMap(x, asin, true);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Calculate asin for a complex value
|
|
* @param {Complex} x
|
|
* @returns {Complex}
|
|
* @private
|
|
*/
|
|
function _complexAsin(x) {
|
|
// asin(z) = -i*log(iz + sqrt(1-z^2))
|
|
var re = x.re;
|
|
var im = x.im;
|
|
var temp1 = new type.Complex(
|
|
im * im - re * re + 1.0,
|
|
-2.0 * re * im
|
|
);
|
|
var temp2 = complexSqrt(temp1);
|
|
var temp3 = new type.Complex(
|
|
temp2.re - im,
|
|
temp2.im + re
|
|
);
|
|
var temp4 = complexLog(temp3);
|
|
|
|
return new type.Complex(temp4.im, -temp4.re);
|
|
}
|
|
|
|
return asin;
|
|
}
|
|
|
|
exports.name = 'asin';
|
|
exports.factory = factory;
|