UNPKG

1.66 kBJavaScriptView Raw
1'use strict'
2
3const deepMap = require('../../utils/collection/deepMap')
4const sign = require('../../utils/number').sign
5
6function factory (type, config, load, typed) {
7 /**
8 * Calculate the hyperbolic cosecant of a value,
9 * defined as `csch(x) = 1 / sinh(x)`.
10 *
11 * For matrices, the function is evaluated element wise.
12 *
13 * Syntax:
14 *
15 * math.csch(x)
16 *
17 * Examples:
18 *
19 * // csch(x) = 1/ sinh(x)
20 * math.csch(0.5) // returns 1.9190347513349437
21 * 1 / math.sinh(0.5) // returns 1.9190347513349437
22 *
23 * See also:
24 *
25 * sinh, sech, coth
26 *
27 * @param {number | Complex | Unit | Array | Matrix} x Function input
28 * @return {number | Complex | Array | Matrix} Hyperbolic cosecant of x
29 */
30 const csch = typed('csch', {
31 'number': _csch,
32
33 'Complex': function (x) {
34 return x.csch()
35 },
36
37 'BigNumber': function (x) {
38 return new type.BigNumber(1).div(x.sinh())
39 },
40
41 'Unit': function (x) {
42 if (!x.hasBase(type.Unit.BASE_UNITS.ANGLE)) {
43 throw new TypeError('Unit in function csch is no angle')
44 }
45 return csch(x.value)
46 },
47
48 'Array | Matrix': function (x) {
49 return deepMap(x, csch)
50 }
51 })
52
53 csch.toTex = { 1: `\\mathrm{csch}\\left(\${args[0]}\\right)` }
54
55 return csch
56}
57
58/**
59 * Calculate the hyperbolic cosecant of a number
60 * @param {number} x
61 * @returns {number}
62 * @private
63 */
64function _csch (x) {
65 // consider values close to zero (+/-)
66 if (x === 0) {
67 return Number.POSITIVE_INFINITY
68 } else {
69 return Math.abs(2 / (Math.exp(x) - Math.exp(-x))) * sign(x)
70 }
71}
72
73exports.name = 'csch'
74exports.factory = factory