greenkeeper[bot] c5971b371a Update standard to the latest version 🚀 (#1226)
* chore(package): update standard to version 12.0.0

* update to new lint version with --fix

I believe this mainly adds whitespace to `{}`'s.

* Replace assert.equal with assert.strictEqual

This breaks a lot of tests which I will endevour to fix in the next
commits.

* Fix most errors due to assert.strictEquals

Some instances of `strictEquals` are replaced by `deepEquals`.
`toString` has been used to make some string comparisions explicit.
Tests will still fail untill #1236 and #1237 are fixed.

* Fix assertion erros due to -0

With node 10, assert.strictEqual no longer considers `0 === -0`.
I missed these first time round as I was using node 8.

* Put toString correct side of bracket

I was converting the constructor to a string rather
than the result of the computation. Oops.

* Fixed #1236: quantileSeq has inconsistant return

* Update package-lock

* Fixed #1237: norm sometimes returning a complex number instead of number

* Fix cli tests

* More changes for standardjs, and fixes in unit tests
2018-09-08 16:33:58 +02:00

81 lines
1.9 KiB
JavaScript

'use strict'
const deepMap = require('../../utils/collection/deepMap')
function factory (type, config, load, typed) {
/**
* Calculate the square root of a value.
*
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.sqrt(x)
*
* Examples:
*
* math.sqrt(25) // returns 5
* math.square(5) // returns 25
* math.sqrt(-4) // returns Complex 2i
*
* See also:
*
* square, multiply, cube, cbrt, sqrtm
*
* @param {number | BigNumber | Complex | Array | Matrix | Unit} x
* Value for which to calculate the square root.
* @return {number | BigNumber | Complex | Array | Matrix | Unit}
* Returns the square root of `x`
*/
const sqrt = typed('sqrt', {
'number': _sqrtNumber,
'Complex': function (x) {
return x.sqrt()
},
'BigNumber': function (x) {
if (!x.isNegative() || config.predictable) {
return x.sqrt()
} else {
// negative value -> downgrade to number to do complex value computation
return _sqrtNumber(x.toNumber())
}
},
'Array | Matrix': function (x) {
// deep map collection, skip zeros since sqrt(0) = 0
return deepMap(x, sqrt, true)
},
'Unit': function (x) {
// Someday will work for complex units when they are implemented
return x.pow(0.5)
}
})
/**
* Calculate sqrt for a number
* @param {number} x
* @returns {number | Complex} Returns the square root of x
* @private
*/
function _sqrtNumber (x) {
if (isNaN(x)) {
return NaN
} else if (x >= 0 || config.predictable) {
return Math.sqrt(x)
} else {
return new type.Complex(x, 0).sqrt()
}
}
sqrt.toTex = { 1: `\\sqrt{\${args[0]}}` }
return sqrt
}
exports.name = 'sqrt'
exports.factory = factory