UNPKG

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