2015-04-30 17:51:04 -04:00

131 lines
3.4 KiB
JavaScript

'use strict';
function factory (type, config, load, typed) {
var matrix = load(require('../construction/matrix'));
var pow = load(require('./pow'));
var elementWiseOperations = load(require('../../type/matrix/util/elementWiseOperations'));
/**
* Calculates the power of x to y element wise.
*
* Syntax:
*
* math.dotPow(x, y)
*
* Examples:
*
* math.dotPow(2, 3); // returns Number 8
*
* var 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 | Boolean | Complex | Unit | Array | Matrix | null} x The base
* @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} 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,
'Matrix, Matrix': function (x, y) {
// result
var c;
// process matrix storage
switch (x.storage()) {
case 'sparse':
switch (y.storage()) {
case 'sparse':
// sparse .^ sparse
c = elementWiseOperations.algorithm7(x, y, pow, false);
break;
default:
// sparse .^ dense
c = elementWiseOperations.algorithm3(y, x, pow, true);
break;
}
break;
default:
switch (y.storage()) {
case 'sparse':
// dense .^ sparse
c = elementWiseOperations.algorithm3(x, y, pow, false);
break;
default:
// dense .^ dense
c = elementWiseOperations.algorithm11(x, y, pow, false);
break;
}
break;
}
return c;
},
'Array, Array': function (x, y) {
// use matrix implementation
return dotPow(matrix(x), matrix(y)).valueOf();
},
'Array, Matrix': function (x, y) {
// use matrix implementation
return dotPow(matrix(x), y);
},
'Matrix, Array': function (x, y) {
// use matrix implementation
return dotPow(x, matrix(y));
},
'Matrix, any': function (x, y) {
// result
var c;
// check storage format
switch (x.storage()) {
case 'sparse':
c = elementWiseOperations.algorithm9(x, y, dotPow, false);
break;
default:
c = elementWiseOperations.algorithm12(x, y, dotPow, false);
break;
}
return c;
},
'any, Matrix': function (x, y) {
// result
var c;
// check storage format
switch (y.storage()) {
case 'sparse':
c = elementWiseOperations.algorithm10(y, x, dotPow, true);
break;
default:
c = elementWiseOperations.algorithm12(y, x, dotPow, true);
break;
}
return c;
},
'Array, any': function (x, y) {
// use matrix implementation
return elementWiseOperations.algorithm12(matrix(x), y, dotPow, false).valueOf();
},
'any, Array': function (x, y) {
// use matrix implementation
return elementWiseOperations.algorithm12(matrix(y), x, dotPow, true).valueOf();
}
});
return dotPow;
}
exports.name = 'dotPow';
exports.factory = factory;