add.js (4232B)
1 import { factory } from '../../utils/factory.js'; 2 import { extend } from '../../utils/object.js'; 3 import { createAlgorithm01 } from '../../type/matrix/utils/algorithm01.js'; 4 import { createAlgorithm04 } from '../../type/matrix/utils/algorithm04.js'; 5 import { createAlgorithm10 } from '../../type/matrix/utils/algorithm10.js'; 6 import { createAlgorithm13 } from '../../type/matrix/utils/algorithm13.js'; 7 import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js'; 8 var name = 'add'; 9 var dependencies = ['typed', 'matrix', 'addScalar', 'equalScalar', 'DenseMatrix', 'SparseMatrix']; 10 export var createAdd = /* #__PURE__ */factory(name, dependencies, _ref => { 11 var { 12 typed, 13 matrix, 14 addScalar, 15 equalScalar, 16 DenseMatrix, 17 SparseMatrix 18 } = _ref; 19 var algorithm01 = createAlgorithm01({ 20 typed 21 }); 22 var algorithm04 = createAlgorithm04({ 23 typed, 24 equalScalar 25 }); 26 var algorithm10 = createAlgorithm10({ 27 typed, 28 DenseMatrix 29 }); 30 var algorithm13 = createAlgorithm13({ 31 typed 32 }); 33 var algorithm14 = createAlgorithm14({ 34 typed 35 }); 36 /** 37 * Add two or more values, `x + y`. 38 * For matrices, the function is evaluated element wise. 39 * 40 * Syntax: 41 * 42 * math.add(x, y) 43 * math.add(x, y, z, ...) 44 * 45 * Examples: 46 * 47 * math.add(2, 3) // returns number 5 48 * math.add(2, 3, 4) // returns number 9 49 * 50 * const a = math.complex(2, 3) 51 * const b = math.complex(-4, 1) 52 * math.add(a, b) // returns Complex -2 + 4i 53 * 54 * math.add([1, 2, 3], 4) // returns Array [5, 6, 7] 55 * 56 * const c = math.unit('5 cm') 57 * const d = math.unit('2.1 mm') 58 * math.add(c, d) // returns Unit 52.1 mm 59 * 60 * math.add("2.3", "4") // returns number 6.3 61 * 62 * See also: 63 * 64 * subtract, sum 65 * 66 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to add 67 * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to add 68 * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Sum of `x` and `y` 69 */ 70 71 return typed(name, extend({ 72 // we extend the signatures of addScalar with signatures dealing with matrices 73 'DenseMatrix, DenseMatrix': function DenseMatrixDenseMatrix(x, y) { 74 return algorithm13(x, y, addScalar); 75 }, 76 'DenseMatrix, SparseMatrix': function DenseMatrixSparseMatrix(x, y) { 77 return algorithm01(x, y, addScalar, false); 78 }, 79 'SparseMatrix, DenseMatrix': function SparseMatrixDenseMatrix(x, y) { 80 return algorithm01(y, x, addScalar, true); 81 }, 82 'SparseMatrix, SparseMatrix': function SparseMatrixSparseMatrix(x, y) { 83 return algorithm04(x, y, addScalar); 84 }, 85 'Array, Array': function ArrayArray(x, y) { 86 // use matrix implementation 87 return this(matrix(x), matrix(y)).valueOf(); 88 }, 89 'Array, Matrix': function ArrayMatrix(x, y) { 90 // use matrix implementation 91 return this(matrix(x), y); 92 }, 93 'Matrix, Array': function MatrixArray(x, y) { 94 // use matrix implementation 95 return this(x, matrix(y)); 96 }, 97 'DenseMatrix, any': function DenseMatrixAny(x, y) { 98 return algorithm14(x, y, addScalar, false); 99 }, 100 'SparseMatrix, any': function SparseMatrixAny(x, y) { 101 return algorithm10(x, y, addScalar, false); 102 }, 103 'any, DenseMatrix': function anyDenseMatrix(x, y) { 104 return algorithm14(y, x, addScalar, true); 105 }, 106 'any, SparseMatrix': function anySparseMatrix(x, y) { 107 return algorithm10(y, x, addScalar, true); 108 }, 109 'Array, any': function ArrayAny(x, y) { 110 // use matrix implementation 111 return algorithm14(matrix(x), y, addScalar, false).valueOf(); 112 }, 113 'any, Array': function anyArray(x, y) { 114 // use matrix implementation 115 return algorithm14(matrix(y), x, addScalar, true).valueOf(); 116 }, 117 'any, any': addScalar, 118 'any, any, ...any': function anyAnyAny(x, y, rest) { 119 var result = this(x, y); 120 121 for (var i = 0; i < rest.length; i++) { 122 result = this(result, rest[i]); 123 } 124 125 return result; 126 } 127 }, addScalar.signatures)); 128 });