mathjs/lib/function/bitwise/rightArithShift.js
2015-05-01 23:38:18 -04:00

151 lines
4.2 KiB
JavaScript

'use strict';
var isInteger = require('../../util/number').isInteger;
var bigRightArithShift = require('../../util/bignumber').rightArithShift;
function factory (type, config, load, typed) {
var matrix = load(require('../construction/matrix'));
var equal = load(require('../relational/equal'));
var zeros = load(require('../matrix/zeros'));
var elementWiseOperations = load(require('../../type/matrix/util/elementWiseOperations'));
/**
* Bitwise right arithmetic shift of a value x by y number of bits, `x >> y`.
* For matrices, the function is evaluated element wise.
* For units, the function is evaluated on the best prefix base.
*
* Syntax:
*
* math.rightArithShift(x, y)
*
* Examples:
*
* math.rightArithShift(4, 2); // returns Number 1
*
* math.rightArithShift([16, -32, 64], 4); // returns Array [1, -2, 3]
*
* See also:
*
* bitAnd, bitNot, bitOr, bitXor, rightArithShift, rightLogShift
*
* @param {Number | BigNumber | Boolean | Array | Matrix | null} x Value to be shifted
* @param {Number | BigNumber | Boolean | null} y Amount of shifts
* @return {Number | BigNumber | Array | Matrix} `x` sign-filled shifted right `y` times
*/
var rightArithShift = typed('rightArithShift', {
'number, number': function (x, y) {
if (!isInteger(x) || !isInteger(y)) {
throw new Error('Integers expected in function rightArithShift');
}
return x >> y;
},
'BigNumber, BigNumber': bigRightArithShift,
'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.algorithm8(x, y, rightArithShift, false);
break;
default:
// sparse & dense
c = elementWiseOperations.algorithm2(y, x, rightArithShift, true);
break;
}
break;
default:
switch (y.storage()) {
case 'sparse':
// dense & sparse
c = elementWiseOperations.algorithm1(x, y, rightArithShift, false);
break;
default:
// dense & dense
c = elementWiseOperations.algorithm12(x, y, rightArithShift);
break;
}
break;
}
return c;
},
'Array, Array': function (x, y) {
// use matrix implementation
return rightArithShift(matrix(x), matrix(y)).valueOf();
},
'Array, Matrix': function (x, y) {
// use matrix implementation
return rightArithShift(matrix(x), y);
},
'Matrix, Array': function (x, y) {
// use matrix implementation
return rightArithShift(x, matrix(y));
},
'Matrix, number | BigNumber': function (x, y) {
// check scalar
if (!equal(y, 0)) {
// result
var c;
// check storage format
switch (x.storage()) {
case 'sparse':
c = elementWiseOperations.algorithm10(x, y, rightArithShift, false);
break;
default:
c = elementWiseOperations.algorithm13(x, y, rightArithShift, false);
break;
}
return c;
}
return x.clone();
},
'number | BigNumber, Matrix': function (x, y) {
// check scalar
if (!equal(x, 0)) {
// result
var c;
// check storage format
switch (y.storage()) {
case 'sparse':
c = elementWiseOperations.algorithm9(y, x, rightArithShift, true);
break;
default:
c = elementWiseOperations.algorithm13(y, x, rightArithShift, true);
break;
}
return c;
}
return zeros(y.size(), y.storage());
},
'Array, number | BigNumber': function (x, y) {
// use matrix implementation
return rightArithShift(matrix(x), y).valueOf();
},
'number | BigNumber, Array': function (x, y) {
// use matrix implementation
return rightArithShift(x, matrix(y)).valueOf();
}
});
return rightArithShift;
}
exports.name = 'rightArithShift';
exports.factory = factory;