mod.js (4912B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createMod = void 0; 7 8 var _factory = require("../../utils/factory.js"); 9 10 var _algorithm = require("../../type/matrix/utils/algorithm02.js"); 11 12 var _algorithm2 = require("../../type/matrix/utils/algorithm03.js"); 13 14 var _algorithm3 = require("../../type/matrix/utils/algorithm05.js"); 15 16 var _algorithm4 = require("../../type/matrix/utils/algorithm11.js"); 17 18 var _algorithm5 = require("../../type/matrix/utils/algorithm12.js"); 19 20 var _algorithm6 = require("../../type/matrix/utils/algorithm13.js"); 21 22 var _algorithm7 = require("../../type/matrix/utils/algorithm14.js"); 23 24 var _index = require("../../plain/number/index.js"); 25 26 var name = 'mod'; 27 var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix']; 28 var createMod = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 29 var typed = _ref.typed, 30 matrix = _ref.matrix, 31 equalScalar = _ref.equalScalar, 32 DenseMatrix = _ref.DenseMatrix; 33 var algorithm02 = (0, _algorithm.createAlgorithm02)({ 34 typed: typed, 35 equalScalar: equalScalar 36 }); 37 var algorithm03 = (0, _algorithm2.createAlgorithm03)({ 38 typed: typed 39 }); 40 var algorithm05 = (0, _algorithm3.createAlgorithm05)({ 41 typed: typed, 42 equalScalar: equalScalar 43 }); 44 var algorithm11 = (0, _algorithm4.createAlgorithm11)({ 45 typed: typed, 46 equalScalar: equalScalar 47 }); 48 var algorithm12 = (0, _algorithm5.createAlgorithm12)({ 49 typed: typed, 50 DenseMatrix: DenseMatrix 51 }); 52 var algorithm13 = (0, _algorithm6.createAlgorithm13)({ 53 typed: typed 54 }); 55 var algorithm14 = (0, _algorithm7.createAlgorithm14)({ 56 typed: typed 57 }); 58 /** 59 * Calculates the modulus, the remainder of an integer division. 60 * 61 * For matrices, the function is evaluated element wise. 62 * 63 * The modulus is defined as: 64 * 65 * x - y * floor(x / y) 66 * 67 * See https://en.wikipedia.org/wiki/Modulo_operation. 68 * 69 * Syntax: 70 * 71 * math.mod(x, y) 72 * 73 * Examples: 74 * 75 * math.mod(8, 3) // returns 2 76 * math.mod(11, 2) // returns 1 77 * 78 * function isOdd(x) { 79 * return math.mod(x, 2) != 0 80 * } 81 * 82 * isOdd(2) // returns false 83 * isOdd(3) // returns true 84 * 85 * See also: 86 * 87 * divide 88 * 89 * @param {number | BigNumber | Fraction | Array | Matrix} x Dividend 90 * @param {number | BigNumber | Fraction | Array | Matrix} y Divisor 91 * @return {number | BigNumber | Fraction | Array | Matrix} Returns the remainder of `x` divided by `y`. 92 */ 93 94 return typed(name, { 95 'number, number': _index.modNumber, 96 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { 97 if (y.isNeg()) { 98 throw new Error('Cannot calculate mod for a negative divisor'); 99 } 100 101 return y.isZero() ? x : x.mod(y); 102 }, 103 'Fraction, Fraction': function FractionFraction(x, y) { 104 if (y.compare(0) < 0) { 105 throw new Error('Cannot calculate mod for a negative divisor'); 106 } // Workaround suggested in Fraction.js library to calculate correct modulo for negative dividend 107 108 109 return x.compare(0) >= 0 ? x.mod(y) : x.mod(y).add(y).mod(y); 110 }, 111 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { 112 return algorithm05(x, y, this, false); 113 }, 114 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { 115 return algorithm02(y, x, this, true); 116 }, 117 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { 118 return algorithm03(x, y, this, false); 119 }, 120 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { 121 return algorithm13(x, y, this); 122 }, 123 'Array, Array': function ArrayArray(x, y) { 124 // use matrix implementation 125 return this(matrix(x), matrix(y)).valueOf(); 126 }, 127 'Array, Matrix': function ArrayMatrix(x, y) { 128 // use matrix implementation 129 return this(matrix(x), y); 130 }, 131 'Matrix, Array': function MatrixArray(x, y) { 132 // use matrix implementation 133 return this(x, matrix(y)); 134 }, 135 'SparseMatrix, any': function SparseMatrixAny(x, y) { 136 return algorithm11(x, y, this, false); 137 }, 138 'DenseMatrix, any': function DenseMatrixAny(x, y) { 139 return algorithm14(x, y, this, false); 140 }, 141 'any, SparseMatrix': function anySparseMatrix(x, y) { 142 return algorithm12(y, x, this, true); 143 }, 144 'any, DenseMatrix': function anyDenseMatrix(x, y) { 145 return algorithm14(y, x, this, true); 146 }, 147 'Array, any': function ArrayAny(x, y) { 148 // use matrix implementation 149 return algorithm14(matrix(x), y, this, false).valueOf(); 150 }, 151 'any, Array': function anyArray(x, y) { 152 // use matrix implementation 153 return algorithm14(matrix(y), x, this, true).valueOf(); 154 } 155 }); 156 }); 157 exports.createMod = createMod;