mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
93 lines (88 loc) • 3.25 kB
JavaScript
;
function factory(type, config, load, typed) {
var matrix = load(require('../../type/matrix/function/matrix'));
var pow = load(require('./pow'));
var latex = require('../../utils/latex');
var algorithm03 = load(require('../../type/matrix/utils/algorithm03'));
var algorithm07 = load(require('../../type/matrix/utils/algorithm07'));
var algorithm11 = load(require('../../type/matrix/utils/algorithm11'));
var algorithm12 = load(require('../../type/matrix/utils/algorithm12'));
var algorithm13 = load(require('../../type/matrix/utils/algorithm13'));
var algorithm14 = load(require('../../type/matrix/utils/algorithm14'));
/**
* Calculates the power of x to y element wise.
*
* Syntax:
*
* math.dotPow(x, y)
*
* Examples:
*
* math.dotPow(2, 3) // returns number 8
*
* const a = [[1, 2], [4, 3]]
* math.dotPow(a, 2) // returns Array [[1, 4], [16, 9]]
* math.pow(a, 2) // returns Array [[9, 8], [16, 17]]
*
* See also:
*
* pow, sqrt, multiply
*
* @param {number | BigNumber | Complex | Unit | Array | Matrix} x The base
* @param {number | BigNumber | Complex | Unit | Array | Matrix} y The exponent
* @return {number | BigNumber | Complex | Unit | Array | Matrix} The value of `x` to the power `y`
*/
var dotPow = typed('dotPow', {
'any, any': pow,
'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) {
return algorithm07(x, y, pow, false);
},
'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) {
return algorithm03(y, x, pow, true);
},
'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) {
return algorithm03(x, y, pow, false);
},
'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) {
return algorithm13(x, y, pow);
},
'Array, Array': function ArrayArray(x, y) {
// use matrix implementation
return dotPow(matrix(x), matrix(y)).valueOf();
},
'Array, Matrix': function ArrayMatrix(x, y) {
// use matrix implementation
return dotPow(matrix(x), y);
},
'Matrix, Array': function MatrixArray(x, y) {
// use matrix implementation
return dotPow(x, matrix(y));
},
'SparseMatrix, any': function SparseMatrixAny(x, y) {
return algorithm11(x, y, dotPow, false);
},
'DenseMatrix, any': function DenseMatrixAny(x, y) {
return algorithm14(x, y, dotPow, false);
},
'any, SparseMatrix': function anySparseMatrix(x, y) {
return algorithm12(y, x, dotPow, true);
},
'any, DenseMatrix': function anyDenseMatrix(x, y) {
return algorithm14(y, x, dotPow, true);
},
'Array, any': function ArrayAny(x, y) {
// use matrix implementation
return algorithm14(matrix(x), y, dotPow, false).valueOf();
},
'any, Array': function anyArray(x, y) {
// use matrix implementation
return algorithm14(matrix(y), x, dotPow, true).valueOf();
}
});
dotPow.toTex = {
2: "\\left(${args[0]}".concat(latex.operators['dotPow'], "${args[1]}\\right)")
};
return dotPow;
}
exports.name = 'dotPow';
exports.factory = factory;