forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcos.js
More file actions
56 lines (50 loc) · 1.43 KB
/
cos.js
File metadata and controls
56 lines (50 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { factory } from '../../utils/factory.js'
import { deepMap } from '../../utils/collection.js'
const name = 'cos'
const dependencies = ['typed']
export const createCos = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
/**
* Calculate the cosine of a value.
*
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.cos(x)
*
* Examples:
*
* math.cos(2) // returns number -0.4161468365471422
* math.cos(math.pi / 4) // returns number 0.7071067811865475
* math.cos(math.unit(180, 'deg')) // returns number -1
* math.cos(math.unit(60, 'deg')) // returns number 0.5
*
* const angle = 0.2
* math.pow(math.sin(angle), 2) + math.pow(math.cos(angle), 2) // returns number ~1
*
* See also:
*
* cos, tan
*
* @param {number | BigNumber | Complex | Unit | Array | Matrix} x Function input
* @return {number | BigNumber | Complex | Array | Matrix} Cosine of x
*/
return typed(name, {
number: Math.cos,
Complex: function (x) {
return x.cos()
},
BigNumber: function (x) {
return x.cos()
},
Unit: function (x) {
if (!x.hasBase(x.constructor.BASE_UNITS.ANGLE)) {
throw new TypeError('Unit in function cos is no angle')
}
return this(x.value)
},
'Array | Matrix': function (x) {
return deepMap(x, this)
}
})
})