UNPKG

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