UNPKG

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