82 lines
2.0 KiB
JavaScript

'use strict';
module.exports = function (math) {
var util = require('../../util/index'),
BigNumber = math.type.BigNumber,
Complex = require('../../type/Complex'),
Unit = require('../../type/Unit'),
collection = require('../../type/collection'),
isNumber = util.number.isNumber,
isBoolean = util['boolean'].isBoolean,
isComplex = Complex.isComplex,
isUnit = Unit.isUnit,
isCollection = collection.isCollection,
bigAcoth = util.bignumber.atanh_acoth;
/**
* Calculate the hyperbolic arccotangent of a value,
* defined as `acoth(x) = (ln((x+1)/x) + ln(x/(x-1))) / 2`.
*
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.acoth(x)
*
* Examples:
*
* math.acoth(0.5); // returns 0.8047189562170503
*
* See also:
*
* acsch, asech
*
* @param {Number | Boolean | Complex | Unit | Array | Matrix | null} x Function input
* @return {Number | Complex | Array | Matrix} Hyperbolic arccotangent of x
*/
math.acoth = function acoth(x) {
if (arguments.length != 1) {
throw new math.error.ArgumentsError('acoth', arguments.length, 1);
}
if (isNumber(x)) {
return isFinite(x) ? (Math.log((x+1)/x) + Math.log(x/(x-1))) / 2 : 0;
}
if (isComplex(x)) {
if (x.re == 0 && x.im == 0) {
return new Complex(0, 1.5707963267948966);
}
// acoth(z) = -i*atanh(1/z)
var den = x.re*x.re + x.im*x.im;
if (den != 0) {
x.re = x.re / den;
x.im = -x.im / den;
} else {
x.re = (x.re != 0) ? (x.re / 0) : 0;
x.im = (x.im != 0) ? -(x.im / 0) : 0;
}
return math.atanh(x);
}
if (isCollection(x)) {
return collection.deepMap(x, acoth);
}
if (isBoolean(x) || x === null) {
return (x) ? Infinity : NaN;
}
if (x instanceof BigNumber) {
return bigAcoth(x, true);
}
throw new math.error.UnsupportedTypeError('acoth', math['typeof'](x));
};
};