UNPKG

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