How to use the cos function from mathjs

Find comprehensive JavaScript mathjs.cos code examples handpicked from public code repositorys.

mathjs.cos is a function in the Math.js library used to calculate the cosine of a number in radians.

4
5
6
7
8
9
10
11
12
13
console.log(math.tan(0.5));  
console.log(math.sin(0.5) / math.cos(0.5));  
console.log(math.cot(2));   
console.log(1 / math.tan(2));  
console.log(math.sec(2));  
console.log(1 / math.cos(2));   
console.log(math.csc(2));   
console.log(1 / math.sin(2));   
console.log(math.asin(0.5));  
console.log(math.atan(0.5));  
fork icon19
star icon29
watch icon6

+ 11 other calls in file

125
126
127
128
129
130
131
132
133
134
    return erf_large(z);
}

const K = math.exp(-z.re*z.re)/Math.PI;
const q = 4*z.re*z.re;
const a = math.cos(2*z.re*z.im);
const b = math.sin(2*z.re*z.im);

const series = [math.erf(z.re), cmul(K/(2*z.re), math.complex(1-a, b))];
for (let k = 1; k < 65; k++) {
fork icon8
star icon29
watch icon3

+ 3 other calls in file

How does mathjs.cos work?

mathjs.cos is a function provided by the Math.js library used to calculate the cosine of a number in radians. When mathjs.cos is called, it takes as input a number representing an angle in radians. It then returns the cosine of that angle as a number. The cos function is part of the larger family of trigonometric functions, which relate the angles and sides of triangles. In particular, the cosine function relates the adjacent and hypotenuse sides of a right triangle. In JavaScript, angles are typically measured in radians, which are defined as the angle subtended by an arc of a circle equal in length to the radius of the circle. One complete revolution around a circle corresponds to an angle of 2π radians. The mathjs.cos function can be used in a variety of applications, such as computer graphics, physics, and engineering. By providing an easy-to-use interface for calculating cosine values, Math.js makes it easier to perform mathematical operations in JavaScript.

49
50
51
52
53
54
55
56
57
58
sphereCarthesianCoordinates() {
    const sphereCoord = this.sphereAngleCoordinates();

    const ax = math.sin(sphereCoord.teta) * math.cos(sphereCoord.phi);
    const ay = math.sin(sphereCoord.teta) * math.sin(sphereCoord.phi);
    const az = math.cos(sphereCoord.teta);

    return {
        x: ax,
        y: ay,
fork icon2
star icon4
watch icon3

105
106
107
108
109
110
111
112
113
},

gauss: function gauss(slength) {
  var _samples_length = slength !== undefined ? Number(slength) : defaults.slength;
  var _samples = new Array(_samples_length).fill(0).map(function (x, i) { 
    return math.cos(2 * math.PI * math.random()) * math.sqrt(-2 * math.log(math.random())); 
  });
  return _samples;
}
fork icon0
star icon2
watch icon2

Ai Example

1
2
3
4
5
const math = require("mathjs");

const x = math.cos(0); // Returns 1
const y = math.cos(math.pi); // Returns -1
const z = math.cos(math.pi / 4); // Returns approximately 0.7071

In this example, we first import the mathjs library. We then call math.cos three times to calculate the cosine of different angles in radians. In the first call to cos, we pass in 0 as the angle, which corresponds to a cosine of 1. In the second call to cos, we pass in pi as the angle, which corresponds to a cosine of -1. Finally, in the third call to cos, we pass in pi / 4 as the angle, which corresponds to a cosine of approximately 0.7071. By using mathjs.cos, we can easily calculate the cosine of angles in radians in our JavaScript applications, making it easier to perform mathematical operations.

100
101
102
103
104
105
106
107
108
109
        }
}

function isWithinMiles(origin, other, dist_miles) {

        num =  math.cos( toRads(origin.lat) ) * math.cos( toRads( other.lat ) ) * math.cos( toRads( other.lng ) - toRads(origin.lng) ) + math.sin( toRads(origin.lat) ) * math.sin( toRads( other.lat ) )
        distance = ( 3959 * math.acos( num ) )

//      if(distance <= dist_miles){
//              console.log(other.lat + ', ' + other.lng)
fork icon2
star icon1
watch icon7

36
37
38
39
40
41
42
43
44
45
width,
height,
depth,
orientation,
{
    x: originX + (width / 2) * cos((orientation / 180) * Math.PI),
    y: originY + (width / 2) * sin((orientation / 180) * Math.PI)
},
reality,
debug
fork icon2
star icon1
watch icon2

+ 13 other calls in file

719
720
721
722
723
724
725
726
727
728
main(){ 
    let find = "**Find:** ";
    let answer = "**Answer:**\n";
    // If user has distance, only thing to have is to get is time
    // First find vx and vy
    this.vx.actual = multiply(this.vi, cos(this.angle))
    this.vx.rounded = clone(this.vx.actual);
    this.vx.rounded.value = SigFig(this.vx.rounded.value, this.sf)
    this.equationInLatex.push(`v_{x} = v_{i}cos(\\theta) \\implies ${this.vi.toString()} \\cdot cos(${this.angle.toString()}) = ${this.vx.rounded.toString()}`)
    find += "Horizontal Velocity (vx), "
fork icon0
star icon1
watch icon0

26
27
28
29
30
31
32
33
34
     result.should.be.approximately(-0.717344150, 10e-7);
 });

it('1st Derivate O(h^4)', function () {
    var x =  [0.78, 0.79, 0.8, 0.81, 0.82];
    var y = math.cos(x);
    var result = Derivate.derivate(x, y, 2, 1, 0.01, 4);
    result.should.be.approximately(-0.717356108, 10e-7);
});
fork icon0
star icon1
watch icon13

+ 111 other calls in file

148
149
150
151
152
153
154
155
156
buildFunctionsArray(){
  let sin = function(x){
    return Mathjs.sin(x);
  }
  let cos = function(x){
    return Mathjs.cos(x);
  }
  let sigmoidSuck = function(x){
    let result = 1.0/(1.0 + Mathjs.exp(-1 * x));
fork icon0
star icon1
watch icon1

+ 7 other calls in file

2746
2747
2748
2749
2750
2751
2752
2753
2754

// Haversine formula
let dlon = lon2 - lon1;
let dlat = lat2 - lat1;
let a = mathjs.pow(mathjs.sin(dlat / 2), 2)
         + mathjs.cos(lat1) * mathjs.cos(lat2)
         * mathjs.pow(mathjs.sin(dlon / 2), 2);
       
let c = 2 * mathjs.asin(mathjs.sqrt(a));
fork icon0
star icon0
watch icon1

+ 2 other calls in file

118
119
120
121
122
123
124
125
126
127
128
}


function epicycloid(r0, k, theta) {
  const kp1 = k + 1;
  return [
    r0 * kp1 * math.cos(theta) - r0 * math.cos(kp1 * theta),
    r0 * kp1 * math.sin(theta) - r0 * math.sin(kp1 * theta),
  ];
}

fork icon0
star icon0
watch icon0

+ 6 other calls in file

79
80
81
82
83
84
85
86
87
88
math.sqrt(c);                   // 2 - i

math.sqrt(-4);                  // 2i

var f = math.unit(60, 'deg');   // 60 deg
var g = math.cos(f);            // 0.5
```


## Parser
fork icon0
star icon0
watch icon2

+ 15 other calls in file

80
81
82
83
84
85
86
87
88
89
// invert magnetometer input according to iphone axis
m_y = -m_y;
m_z = -m_z;

// build formula for yaw (accelerometer + magnetometer)
nom = cos(phi_hat)*m_x-sin(phi_hat)*m_z;
denom = cos(theta_hat)*m_y+sin(phi_hat)*sin(theta_hat)*m_x+cos(phi_hat)*sin(theta_hat)*m_z;

// estimated yaw
psi_hat = psi_old + q * dt;
fork icon0
star icon0
watch icon2

+ 7 other calls in file