mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
* Add `.js` extension to source file imports * Specify package `exports` in `package.json` Specify package type as `commonjs` (It's good to be specific) * Move all compiled scripts into `lib` directory Remove ./number.js (You can use the compiled ones in `./lib/*`) Tell node that the `esm` directory is type `module` and enable tree shaking. Remove unused files from packages `files` property * Allow importing of package.json * Make library ESM first * - Fix merge conflicts - Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs` - Refactor unit tests to be able to run with plain nodejs (no transpiling) - Fix browser examples * Fix browser and browserstack tests * Fix running unit tests on Node 10 (which has no support for modules) * Fix node.js examples (those are still commonjs) * Remove the need for `browserBundle.cjs` * Generate minified bundle only * [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963) Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.** - [Release notes](https://github.com/bitinn/node-fetch/releases) - [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md) - [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * Cleanup console.log * Add integration tests to test the entry points (commonjs/esm, full/number only) * Create backward compatibility error messages in the files moved/removed since v8 * Describe breaking changes in HISTORY.md * Bump karma from 5.2.1 to 5.2.2 (#1965) Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2. - [Release notes](https://github.com/karma-runner/karma/releases) - [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md) - [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
86 lines
2.5 KiB
JavaScript
86 lines
2.5 KiB
JavaScript
import { factory } from '../../utils/factory.js'
|
|
import { flatten } from '../../utils/array.js'
|
|
|
|
const name = 'hypot'
|
|
const dependencies = [
|
|
'typed',
|
|
'abs',
|
|
'addScalar',
|
|
'divideScalar',
|
|
'multiplyScalar',
|
|
'sqrt',
|
|
'smaller',
|
|
'isPositive'
|
|
]
|
|
|
|
export const createHypot = /* #__PURE__ */ factory(name, dependencies, ({ typed, abs, addScalar, divideScalar, multiplyScalar, sqrt, smaller, isPositive }) => {
|
|
/**
|
|
* Calculate the hypotenusa of a list with values. The hypotenusa is defined as:
|
|
*
|
|
* hypot(a, b, c, ...) = sqrt(a^2 + b^2 + c^2 + ...)
|
|
*
|
|
* For matrix input, the hypotenusa is calculated for all values in the matrix.
|
|
*
|
|
* Syntax:
|
|
*
|
|
* math.hypot(a, b, ...)
|
|
* math.hypot([a, b, c, ...])
|
|
*
|
|
* Examples:
|
|
*
|
|
* math.hypot(3, 4) // 5
|
|
* math.hypot(3, 4, 5) // 7.0710678118654755
|
|
* math.hypot([3, 4, 5]) // 7.0710678118654755
|
|
* math.hypot(-2) // 2
|
|
*
|
|
* See also:
|
|
*
|
|
* abs, norm
|
|
*
|
|
* @param {... number | BigNumber | Array | Matrix} args A list with numeric values or an Array or Matrix.
|
|
* Matrix and Array input is flattened and returns a
|
|
* single number for the whole matrix.
|
|
* @return {number | BigNumber} Returns the hypothenusa of the input values.
|
|
*/
|
|
return typed(name, {
|
|
'... number | BigNumber': _hypot,
|
|
|
|
Array: function (x) {
|
|
return this.apply(this, flatten(x))
|
|
},
|
|
|
|
Matrix: function (x) {
|
|
return this.apply(this, flatten(x.toArray()))
|
|
}
|
|
})
|
|
|
|
/**
|
|
* Calculate the hypotenusa for an Array with values
|
|
* @param {Array.<number | BigNumber>} args
|
|
* @return {number | BigNumber} Returns the result
|
|
* @private
|
|
*/
|
|
function _hypot (args) {
|
|
// code based on `hypot` from es6-shim:
|
|
// https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1619-L1633
|
|
let result = 0
|
|
let largest = 0
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
const value = abs(args[i])
|
|
if (smaller(largest, value)) {
|
|
result = multiplyScalar(result,
|
|
multiplyScalar(divideScalar(largest, value), divideScalar(largest, value)))
|
|
result = addScalar(result, 1)
|
|
largest = value
|
|
} else {
|
|
result = addScalar(result, isPositive(value)
|
|
? multiplyScalar(divideScalar(value, largest), divideScalar(value, largest))
|
|
: value)
|
|
}
|
|
}
|
|
|
|
return multiplyScalar(largest, sqrt(result))
|
|
}
|
|
})
|