How to use the sqrt function from mathjs

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

mathjs.sqrt is a function that computes the square root of a number or an array of numbers.

4455
4456
4457
4458
4459
4460
4461
4462
4463
4464

if(value) {
	if(prob == 0) {
		U[1][0] = 1;
	} else {
		U[1][1] = math.sqrt(1 / prob);
	}
} else {
	if(prob == 0) {
		U[0][1] = 1;
fork icon43
star icon207
watch icon14

+ 315 other calls in file

6
7
8
9
10
11
12
13
14
15
const cdiv = math.divide;
const cmul = math.multiply;
const csin = math.sin;
const cexp = math.exp;
const clog = math.log;
const csqrt = math.sqrt;
const cpow = math.pow;
const csquare = z => cmul(z, z);

const gamma_right = math.gamma;
fork icon8
star icon0
watch icon0

+ 15 other calls in file

How does mathjs.sqrt work?

mathjs.sqrt is a function provided by the Math.js library that returns the positive square root of a given number, or the square root of each element of an array or matrix if passed as an argument. If a negative number is passed as the argument, the function will return the complex square root of that number.

149
150
151
152
153
154
155
156
157
158
var math = require('mathjs')

var a = math.complex(3, -1)
//=> { re: 3, im: -1 }

var b = math.sqrt(-1)
//=> { re: 0, im: -1 }

console.log(math.multiply(a, b).toString())
//=> '1 + 3i'
fork icon0
star icon39
watch icon12

+ 13 other calls in file

113
114
115
116
117
118
119
120
121
function erf_large(z) {
    const k = cmul_i(creciprocal(z));
    const k2 = csquare(k);
    const corrections = cdiv(
        cmul(k, cadd(1, cmul(k2, cadd(0.5, cmul(0.75, k2))))),
        math.sqrt(Math.PI)
    );
    return cadd(1, cmul_i(cmul(cexp(csquare(cmul_i(z))), corrections)));
}
fork icon8
star icon29
watch icon3

+ 7 other calls in file

Ai Example

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

const num = 25;
const sqrtNum = math.sqrt(num);

console.log(sqrtNum); // Output: 5

In this example, we first import the mathjs library using require. We then define a variable num with the value of 25, and use math.sqrt to find its square root, which is assigned to the variable sqrtNum. Finally, we log the value of sqrtNum to the console, which should output 5.

44
45
46
47
48
49
50
51
52
53
  <script src="math.js" type="text/javascript"></script>
</head>
<body>
  <script type="text/javascript">
    // use math.js
    math.sqrt(-4); // 2i
  </script>
</body>
</html>
```
fork icon0
star icon5
watch icon28

+ 15 other calls in file

166
167
168
169
170
171
172
173
174

  const uniquePixelsPow = pow(uniquePixelsSub, 2);

  const colorDifferenceMatrix = uniquePixelsPow.map(row => row.map(col => math.sum(col)));

  this.colorDistanceMatrix = math.sqrt(colorDifferenceMatrix);

  this.exponentialColorDistanceMatrix = math.exp(math.multiply(math.divide(colorDifferenceMatrix, (2 * sigmac * sigmac)), -1));
}
fork icon0
star icon5
watch icon2

162
163
164
165
166
167
168
169
170
171
<p>With <a href="https://rollupjs.org/guide/en/">rollup.js</a>, we can run CommonJS (CJS) and NodeJS (NPM) modules in WasmEdge too. The <a href="https://github.com/second-state/wasmedge-quickjs/blob/main/example_js/simple_common_js_demo/npm_main.js">simple_common_js_demo/npm_main.js</a> demo shows how it works. It utilizes the third-party <code>md5</code> and <code>mathjs</code> modules.</p>
<pre><code class="language-javascript">const md5 = require('md5');
console.log('md5(message)=', md5('message'));

const {sqrt} = require('mathjs');
console.log('sqrt(-4)=', sqrt(-4).toString());
</code></pre>
<p>In order to run it, we must first use the <a href="https://rollupjs.org/guide/en/">rollup.js</a> tool to build all dependencies into a single file. In the process, <code>rollup.js</code> converts CommonJS modules into <a href="es6.html">WasmEdge-compatible ES6 modules</a>. The build script is <a href="https://github.com/second-state/wasmedge-quickjs/blob/main/example_js/simple_common_js_demo/rollup.config.js">rollup.config.js</a>.</p>
<pre><code class="language-javascript">const {babel} = require('@rollup/plugin-babel');
const nodeResolve = require('@rollup/plugin-node-resolve');
fork icon3
star icon4
watch icon2

50
51
52
53
54
55
56
57
58
let oros = maths.chain(jx).dotDivide(q).dotDivide(maths.dotPow(bes, 2)).done();

const j2 = maths.complex('i');
var dz = [];
for (var i = 0; i < this.f.length; i++) {
  var p2 = maths.sqrt(maths.chain(q)
                .dotMultiply(q)
                .add(maths.dotMultiply(j2,omega[i]*Math.PI*4.0e-7*this.mr2*this.con2))
                .done());
fork icon2
star icon3
watch icon0

+ 3 other calls in file

41
42
43
44
45
46
47
48
49
50
math.log(a) // ln
math.log(a, 2) // log base 2
math.mod(a, b) // modulus (remainder)
math.multiply(a, b) // multiply
math.pow(a, b) // a ** b
math.sqrt(a) // square root
math.subtract(a, b) // subtract

// abs
math.abs(a); // absolute value
fork icon0
star icon3
watch icon0

+ 6 other calls in file

46
47
48
49
50
51
52
53
54
55
  return this.findAllDivisorsLessThan(source, source)
}

static findAllDivisorsLessThan(source, target) {
  let numbers = []
  let sqrt = math.sqrt(source)
  for (let i = 1; i < sqrt; i++) {
    if (source % i === 0) {
      if (source / i <= target) {
        numbers.push(source / i)
fork icon1
star icon0
watch icon0

22
23
24
25
26
27
28
29
30
31
	}
	return a / b
}

sqrt(a) {
	return sqrt(a)
}

cbrt(a) {
	return cbrt(a)
fork icon2
star icon0
watch icon0

505
506
507
508
509
510
511
512
513
514
515
516
	const z = math.subtract(x, y);
	return RMS(z);
}


function RMS(x) {
	return math.sqrt(math.sum(math.square(x)));
}	


function sr_quartic(s) {
    // notice using math.js evaluate we can use +, -
fork icon1
star icon0
watch icon0

+ 2 other calls in file

21
22
23
24
25
26
27
28
29
30
                return NaN
        }
        return a / b
}

sqrt(a) {
        return sqrt(a)
}

cbrt(a) {
fork icon1
star icon0
watch icon3

112
113
114
115
116
117
118
119
120
  },
  {
    'b': [0, 0],
    'w': [
      -math.sqrt(6 / (dimension[0] + dimension[1])),
      math.sqrt(6 / (dimension[0] + dimension[1]))
    ]
  }
];
fork icon1
star icon0
watch icon0

166
167
168
169
170
171
172
173
174
175
  let result = ( Mathjs.exp(-1 * x*x));

  return isNaN(result) ? 1.0 : result;
}
let sqrRoot = function (x){
  let result = Mathjs.sqrt(x);
    return isNaN(result) ? 1.0 : result;
}
let exp = function(x){
  let result = Mathjs.exp(x);
fork icon0
star icon1
watch icon1

+ 7 other calls in file

197
198
199
200
201
202
203
204
205
206
var Ap = math.multiply(A, p);
var alpha = rsold / math.dot(p, Ap);
x = math.add(x, math.multiply(alpha, p));
r = math.subtract(r, math.multiply(alpha, Ap));
var rsnew = math.dot(r, r);
var er = math.sqrt(rsnew);
err.push(er);
console.log(x);
console.log(math.flatten(x));
if(Sizematrix == 2){
fork icon0
star icon1
watch icon0

63
64
65
66
67
68
69
70
71
72
this.height.actual = unit(this.height.actual)
this.height.rounded = clone(this.height.actual);
this.height.rounded.value = SigFig(this.height.rounded.value, this.roundToSigFig) 
// First find final velocity (vf) where formula is: d = \frac{vf^{2} - vi^{2}}{2g}
// Derived formula is: \sqrt{vi^{2} + 2gd} = vf
this.finalVelocity.actual = sqrt(add(square(this.initialVelocity.actual), multiply(multiply(2, gravity), this.height.actual)))
this.finalVelocity.rounded = clone(this.finalVelocity.actual);
this.finalVelocity.rounded.value = SigFig(this.finalVelocity.rounded.value, this.roundToSigFig) 
this.equationInLatex.push(`v_{f} = \\sqrt{{v_{i}}^{2} - 2gd} \\implies \\sqrt{(${this.initialVelocity.actual.toString()})^{2} - 2(${gravity.toString()})(${this.height.actual.toString()})} = ${this.finalVelocity.rounded.toString()}`)
// Next find the time (use the computed final velocity)
fork icon0
star icon1
watch icon0

+ 10 other calls in file

60
61
62
63
64
65
66
67
68
69
}
map(func) {
    return new Point(func(this.x), func(this.y));
}
get magntitude() {
    return math.sqrt(math.add(math.pow(this._x, "2"), math.pow(this._y, "2")));
}
get array() {
    return [this._x, this._y];
}
fork icon1
star icon0
watch icon1

107
108
109
110
111
112
113
114
115
116

let nl = 9; // Number of Lines multiplier
let N = 4999; // number of steps in a random walk per stage: (8 * nl)^2 +1 to make the number odd
let numStages = nl + 1; // TODO: confirm how this differs to nl...
let entropyBytesLen = math.ceil((N * numStages)/8); // byte size of total number of MMI bits to get from the QRNG
let stddev = math.sqrt(N);
let resolution = 35906101; // total number of crawled URLs to find an index in

axios
.get(`http://medfarm.fp2.dev:3333/api/randbytes?deviceId=QWR4E002&length=${entropyBytesLen}`, {responseType: 'arraybuffer'})
fork icon0
star icon0
watch icon0

+ 5 other calls in file

53
54
55
56
57
58
59
60
61
62
63
//.....................sqrt...........................//
app.get('/app/sqrt/:num1', async (req, res, next) => {
    res.send(


        {
            sqrt: sqrt(Number(req.params.num1))
        }
    )
});

fork icon0
star icon0
watch icon0

+ 2 other calls in file