UNPKG

1.35 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2import { deepMap } from '../../utils/collection'
3import { sechNumber } from '../../plain/number'
4
5const name = 'sech'
6const dependencies = ['typed', 'BigNumber']
7
8export const createSech = /* #__PURE__ */ factory(name, dependencies, ({ typed, BigNumber }) => {
9 /**
10 * Calculate the hyperbolic secant of a value,
11 * defined as `sech(x) = 1 / cosh(x)`.
12 *
13 * For matrices, the function is evaluated element wise.
14 *
15 * Syntax:
16 *
17 * math.sech(x)
18 *
19 * Examples:
20 *
21 * // sech(x) = 1/ cosh(x)
22 * math.sech(0.5) // returns 0.886818883970074
23 * 1 / math.cosh(0.5) // returns 0.886818883970074
24 *
25 * See also:
26 *
27 * cosh, csch, coth
28 *
29 * @param {number | Complex | Unit | Array | Matrix} x Function input
30 * @return {number | Complex | Array | Matrix} Hyperbolic secant of x
31 */
32 return typed(name, {
33 number: sechNumber,
34
35 Complex: function (x) {
36 return x.sech()
37 },
38
39 BigNumber: function (x) {
40 return new BigNumber(1).div(x.cosh())
41 },
42
43 Unit: function (x) {
44 if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) {
45 throw new TypeError('Unit in function sech is no angle')
46 }
47 return this(x.value)
48 },
49
50 'Array | Matrix': function (x) {
51 return deepMap(x, this)
52 }
53 })
54})