import { factory } from '../../utils/factory.js'; import { createMatAlgo02xDS0 } from '../../type/matrix/utils/matAlgo02xDS0.js'; import { createMatAlgo03xDSf } from '../../type/matrix/utils/matAlgo03xDSf.js'; import { createMatAlgo05xSfSf } from '../../type/matrix/utils/matAlgo05xSfSf.js'; import { createMatAlgo11xS0s } from '../../type/matrix/utils/matAlgo11xS0s.js'; import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js'; import { modNumber } from '../../plain/number/index.js'; import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js'; var name = 'mod'; var dependencies = ['typed', 'matrix', 'equalScalar', 'DenseMatrix']; export var createMod = /* #__PURE__ */factory(name, dependencies, _ref => { var { typed, matrix, equalScalar, DenseMatrix } = _ref; var matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar }); var matAlgo03xDSf = createMatAlgo03xDSf({ typed }); var matAlgo05xSfSf = createMatAlgo05xSfSf({ typed, equalScalar }); var matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar }); var matAlgo12xSfs = createMatAlgo12xSfs({ typed, DenseMatrix }); var matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix }); /** * Calculates the modulus, the remainder of an integer division. * * For matrices, the function is evaluated element wise. * * The modulus is defined as: * * x - y * floor(x / y) * * See https://en.wikipedia.org/wiki/Modulo_operation. * * Syntax: * * math.mod(x, y) * * Examples: * * math.mod(8, 3) // returns 2 * math.mod(11, 2) // returns 1 * * function isOdd(x) { * return math.mod(x, 2) != 0 * } * * isOdd(2) // returns false * isOdd(3) // returns true * * See also: * * divide * * @param {number | BigNumber | Fraction | Array | Matrix} x Dividend * @param {number | BigNumber | Fraction | Array | Matrix} y Divisor * @return {number | BigNumber | Fraction | Array | Matrix} Returns the remainder of `x` divided by `y`. */ return typed(name, { 'number, number': modNumber, 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) { if (y.isNeg()) { throw new Error('Cannot calculate mod for a negative divisor'); } return y.isZero() ? x : x.mod(y); }, 'Fraction, Fraction': function FractionFraction(x, y) { if (y.compare(0) < 0) { throw new Error('Cannot calculate mod for a negative divisor'); } // Workaround suggested in Fraction.js library to calculate correct modulo for negative dividend return x.compare(0) >= 0 ? x.mod(y) : x.mod(y).add(y).mod(y); } }, matrixAlgorithmSuite({ SS: matAlgo05xSfSf, DS: matAlgo03xDSf, SD: matAlgo02xDS0, Ss: matAlgo11xS0s, sS: matAlgo12xSfs })); });