Skip to content

Commit

Permalink
replaced subtract with subtractScalar whereever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
vrushaket committed Oct 18, 2023
1 parent 684e3ce commit 3cddb94
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 54 deletions.
2 changes: 1 addition & 1 deletion examples/advanced/custom_evaluate_using_import.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const divide = (a, b) => a / b

// create a mathjs instance with hardly any functions
// there are some functions created which are used internally by evaluate though,
// for example by the Unit class which has dependencies on addScalar, subtract,
// for example by the Unit class which has dependencies on addScalar, subtractScalar,
// multiplyScalar, etc.
const math = create(evaluateDependencies)

Expand Down
6 changes: 3 additions & 3 deletions src/function/algebra/decomposition/lup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const dependencies = [
'addScalar',
'divideScalar',
'multiplyScalar',
'subtract',
'subtractScalar',
'larger',
'equalScalar',
'unaryMinus',
Expand All @@ -26,7 +26,7 @@ export const createLup = /* #__PURE__ */ factory(name, dependencies, (
addScalar,
divideScalar,
multiplyScalar,
subtract,
subtractScalar,
larger,
equalScalar,
unaryMinus,
Expand Down Expand Up @@ -119,7 +119,7 @@ export const createLup = /* #__PURE__ */ factory(name, dependencies, (
// s = l[i, k] - data[k, j]
s = addScalar(s, multiplyScalar(data[i][k], data[k][j]))
}
data[i][j] = subtract(data[i][j], s)
data[i][j] = subtractScalar(data[i][j], s)
}
}
// row with larger value in cvector, row >= j
Expand Down
10 changes: 5 additions & 5 deletions src/function/algebra/decomposition/qr.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const dependencies = [
'addScalar',
'divideScalar',
'multiplyScalar',
'subtract',
'subtractScalar',
'complex'
]

Expand All @@ -34,7 +34,7 @@ export const createQr = /* #__PURE__ */ factory(name, dependencies, (
addScalar,
divideScalar,
multiplyScalar,
subtract,
subtractScalar,
complex
}
) => {
Expand Down Expand Up @@ -159,7 +159,7 @@ export const createQr = /* #__PURE__ */ factory(name, dependencies, (

if (!isZero(alpha)) {
// first element in vector u
const u1 = subtract(pivot, alpha)
const u1 = subtractScalar(pivot, alpha)

// w = v * u1 / |u| (only elements k to (rows-1) are used)
w[k] = 1
Expand Down Expand Up @@ -198,7 +198,7 @@ export const createQr = /* #__PURE__ */ factory(name, dependencies, (

for (i = k; i < rows; i++) {
Rdata[i][j] = multiplyScalar(
subtract(Rdata[i][j], multiplyScalar(w[i], s)),
subtractScalar(Rdata[i][j], multiplyScalar(w[i], s)),
conjSgn
)
}
Expand All @@ -223,7 +223,7 @@ export const createQr = /* #__PURE__ */ factory(name, dependencies, (

for (j = k; j < rows; ++j) {
Qdata[i][j] = divideScalar(
subtract(Qdata[i][j], multiplyScalar(s, conj(w[j]))),
subtractScalar(Qdata[i][j], multiplyScalar(s, conj(w[j]))),
conjSgn
)
}
Expand Down
8 changes: 4 additions & 4 deletions src/function/algebra/solver/lsolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const dependencies = [
'matrix',
'divideScalar',
'multiplyScalar',
'subtract',
'subtractScalar',
'equalScalar',
'DenseMatrix'
]

export const createLsolve = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtract, equalScalar, DenseMatrix }) => {
export const createLsolve = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtractScalar, equalScalar, DenseMatrix }) => {
const solveValidation = createSolveValidation({ DenseMatrix })

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ export const createLsolve = /* #__PURE__ */ factory(name, dependencies, ({ typed

// loop rows
for (let i = j + 1; i < rows; i++) {
bdata[i] = [subtract(bdata[i][0] || 0, multiplyScalar(xj, mdata[i][j]))]
bdata[i] = [subtractScalar(bdata[i][0] || 0, multiplyScalar(xj, mdata[i][j]))]
}
} else {
// degenerate row, we can choose any value
Expand Down Expand Up @@ -158,7 +158,7 @@ export const createLsolve = /* #__PURE__ */ factory(name, dependencies, ({ typed

for (let k = 0, l = jIndices.length; k < l; k++) {
const i = jIndices[k]
bdata[i] = [subtract(bdata[i][0] || 0, multiplyScalar(xj, jValues[k]))]
bdata[i] = [subtractScalar(bdata[i][0] || 0, multiplyScalar(xj, jValues[k]))]
}

x[j] = [xj]
Expand Down
12 changes: 6 additions & 6 deletions src/function/algebra/solver/lsolveAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const dependencies = [
'matrix',
'divideScalar',
'multiplyScalar',
'subtract',
'subtractScalar',
'equalScalar',
'DenseMatrix'
]

export const createLsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtract, equalScalar, DenseMatrix }) => {
export const createLsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtractScalar, equalScalar, DenseMatrix }) => {
const solveValidation = createSolveValidation({ DenseMatrix })

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ export const createLsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ ty

for (let j = i + 1; j < columns; j++) {
// b[j] -= b[i] * M[j,i]
b[j] = subtract(b[j], multiplyScalar(b[i], M[j][i]))
b[j] = subtractScalar(b[j], multiplyScalar(b[i], M[j][i]))
}
} else if (!equalScalar(b[i], 0)) {
// singular row, nonzero RHS
Expand All @@ -103,7 +103,7 @@ export const createLsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ ty
bNew[i] = 1

for (let j = i + 1; j < columns; j++) {
bNew[j] = subtract(bNew[j], M[j][i])
bNew[j] = subtractScalar(bNew[j], M[j][i])
}

B.push(bNew)
Expand Down Expand Up @@ -162,7 +162,7 @@ export const createLsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ ty

for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
const J = iIndices[j]
b[J] = subtract(b[J], multiplyScalar(b[i], iValues[j]))
b[J] = subtractScalar(b[J], multiplyScalar(b[i], iValues[j]))
}
} else if (!equalScalar(b[i], 0)) {
// singular row, nonzero RHS
Expand All @@ -184,7 +184,7 @@ export const createLsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ ty

for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
const J = iIndices[j]
bNew[J] = subtract(bNew[J], iValues[j])
bNew[J] = subtractScalar(bNew[J], iValues[j])
}

B.push(bNew)
Expand Down
8 changes: 4 additions & 4 deletions src/function/algebra/solver/usolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const dependencies = [
'matrix',
'divideScalar',
'multiplyScalar',
'subtract',
'subtractScalar',
'equalScalar',
'DenseMatrix'
]

export const createUsolve = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtract, equalScalar, DenseMatrix }) => {
export const createUsolve = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtractScalar, equalScalar, DenseMatrix }) => {
const solveValidation = createSolveValidation({ DenseMatrix })

/**
Expand Down Expand Up @@ -90,7 +90,7 @@ export const createUsolve = /* #__PURE__ */ factory(name, dependencies, ({ typed
// loop rows
for (let i = j - 1; i >= 0; i--) {
// update copy of b
bdata[i] = [subtract(bdata[i][0] || 0, multiplyScalar(xj, mdata[i][j]))]
bdata[i] = [subtractScalar(bdata[i][0] || 0, multiplyScalar(xj, mdata[i][j]))]
}
} else {
// zero value at j
Expand Down Expand Up @@ -162,7 +162,7 @@ export const createUsolve = /* #__PURE__ */ factory(name, dependencies, ({ typed

for (let k = 0, lastIndex = jIndices.length; k < lastIndex; k++) {
const i = jIndices[k]
bdata[i] = [subtract(bdata[i][0], multiplyScalar(xj, jValues[k]))]
bdata[i] = [subtractScalar(bdata[i][0], multiplyScalar(xj, jValues[k]))]
}

x[j] = [xj]
Expand Down
12 changes: 6 additions & 6 deletions src/function/algebra/solver/usolveAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const dependencies = [
'matrix',
'divideScalar',
'multiplyScalar',
'subtract',
'subtractScalar',
'equalScalar',
'DenseMatrix'
]

export const createUsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtract, equalScalar, DenseMatrix }) => {
export const createUsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtractScalar, equalScalar, DenseMatrix }) => {
const solveValidation = createSolveValidation({ DenseMatrix })

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ export const createUsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ ty

for (let j = i - 1; j >= 0; j--) {
// b[j] -= b[i] * M[j,i]
b[j] = subtract(b[j], multiplyScalar(b[i], M[j][i]))
b[j] = subtractScalar(b[j], multiplyScalar(b[i], M[j][i]))
}
} else if (!equalScalar(b[i], 0)) {
// singular row, nonzero RHS
Expand All @@ -103,7 +103,7 @@ export const createUsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ ty
bNew[i] = 1

for (let j = i - 1; j >= 0; j--) {
bNew[j] = subtract(bNew[j], M[j][i])
bNew[j] = subtractScalar(bNew[j], M[j][i])
}

B.push(bNew)
Expand Down Expand Up @@ -163,7 +163,7 @@ export const createUsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ ty
// loop upper triangular
for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
const J = iIndices[j]
b[J] = subtract(b[J], multiplyScalar(b[i], iValues[j]))
b[J] = subtractScalar(b[J], multiplyScalar(b[i], iValues[j]))
}
} else if (!equalScalar(b[i], 0)) {
// singular row, nonzero RHS
Expand All @@ -186,7 +186,7 @@ export const createUsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ ty
// loop upper triangular
for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
const J = iIndices[j]
bNew[J] = subtract(bNew[J], iValues[j])
bNew[J] = subtractScalar(bNew[J], iValues[j])
}

B.push(bNew)
Expand Down
3 changes: 1 addition & 2 deletions src/function/arithmetic/subtract.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ const dependencies = [
'typed',
'matrix',
'equalScalar',
'addScalar',
'subtractScalar',
'unaryMinus',
'DenseMatrix',
'concat'
]

export const createSubtract = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, addScalar, subtractScalar, unaryMinus, DenseMatrix, concat }) => {
export const createSubtract = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, subtractScalar, unaryMinus, DenseMatrix, concat }) => {
// TODO: split function subtract in two: subtract and subtractScalar

const matAlgo01xDSid = createMatAlgo01xDSid({ typed })
Expand Down
4 changes: 2 additions & 2 deletions src/function/combinatorics/stirlingS2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const name = 'stirlingS2'
const dependencies = [
'typed',
'addScalar',
'subtract',
'subtractScalar',
'multiplyScalar',
'divideScalar',
'pow',
Expand All @@ -22,7 +22,7 @@ export const createStirlingS2 = /* #__PURE__ */ factory(name, dependencies, (
{
typed,
addScalar,
subtract,
subtractScalar,
multiplyScalar,
divideScalar,
pow,
Expand Down
24 changes: 12 additions & 12 deletions src/function/geometry/distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const name = 'distance'
const dependencies = [
'typed',
'addScalar',
'subtract',
'subtractScalar',
'divideScalar',
'multiplyScalar',
'deepEqual',
'sqrt',
'abs'
]

export const createDistance = /* #__PURE__ */ factory(name, dependencies, ({ typed, addScalar, subtract, multiplyScalar, divideScalar, deepEqual, sqrt, abs }) => {
export const createDistance = /* #__PURE__ */ factory(name, dependencies, ({ typed, addScalar, subtractScalar, multiplyScalar, divideScalar, deepEqual, sqrt, abs }) => {
/**
* Calculates:
* The eucledian distance between two points in N-dimensional spaces.
Expand Down Expand Up @@ -73,9 +73,9 @@ export const createDistance = /* #__PURE__ */ factory(name, dependencies, ({ typ
if (!_2d(y)) { throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument') }
if (!_2d(z)) { throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument') }
if (deepEqual(y, z)) { throw new TypeError('LinePoint1 should not be same with LinePoint2') }
const xCoeff = subtract(z[1], y[1])
const yCoeff = subtract(y[0], z[0])
const constant = subtract(multiplyScalar(z[0], y[1]), multiplyScalar(y[0], z[1]))
const xCoeff = subtractScalar(z[1], y[1])
const yCoeff = subtractScalar(y[0], z[0])
const constant = subtractScalar(multiplyScalar(z[0], y[1]), multiplyScalar(y[0], z[1]))

return _distancePointLine2D(x[0], x[1], xCoeff, yCoeff, constant)
} else {
Expand All @@ -90,9 +90,9 @@ export const createDistance = /* #__PURE__ */ factory(name, dependencies, ({ typ
if (deepEqual(_objectToArray(y), _objectToArray(z))) { throw new TypeError('LinePoint1 should not be same with LinePoint2') }
if ('pointX' in x && 'pointY' in x && 'lineOnePtX' in y &&
'lineOnePtY' in y && 'lineTwoPtX' in z && 'lineTwoPtY' in z) {
const xCoeff = subtract(z.lineTwoPtY, y.lineOnePtY)
const yCoeff = subtract(y.lineOnePtX, z.lineTwoPtX)
const constant = subtract(multiplyScalar(z.lineTwoPtX, y.lineOnePtY), multiplyScalar(y.lineOnePtX, z.lineTwoPtY))
const xCoeff = subtractScalar(z.lineTwoPtY, y.lineOnePtY)
const yCoeff = subtractScalar(y.lineOnePtX, z.lineTwoPtX)
const constant = subtractScalar(multiplyScalar(z.lineTwoPtX, y.lineOnePtY), multiplyScalar(y.lineOnePtX, z.lineTwoPtY))
return _distancePointLine2D(x.pointX, x.pointY, xCoeff, yCoeff, constant)
} else {
throw new TypeError('Key names do not match')
Expand Down Expand Up @@ -270,9 +270,9 @@ export const createDistance = /* #__PURE__ */ factory(name, dependencies, ({ typ
}

function _distancePointLine3D (x, y, z, x0, y0, z0, a, b, c) {
let num = [subtract(multiplyScalar(subtract(y0, y), c), multiplyScalar(subtract(z0, z), b)),
subtract(multiplyScalar(subtract(z0, z), a), multiplyScalar(subtract(x0, x), c)),
subtract(multiplyScalar(subtract(x0, x), b), multiplyScalar(subtract(y0, y), a))]
let num = [subtractScalar(multiplyScalar(subtractScalar(y0, y), c), multiplyScalar(subtractScalar(z0, z), b)),
subtractScalar(multiplyScalar(subtractScalar(z0, z), a), multiplyScalar(subtractScalar(x0, x), c)),
subtractScalar(multiplyScalar(subtractScalar(x0, x), b), multiplyScalar(subtractScalar(y0, y), a))]
num = sqrt(addScalar(addScalar(multiplyScalar(num[0], num[0]), multiplyScalar(num[1], num[1])), multiplyScalar(num[2], num[2])))
const den = sqrt(addScalar(addScalar(multiplyScalar(a, a), multiplyScalar(b, b)), multiplyScalar(c, c)))
return divideScalar(num, den)
Expand All @@ -283,7 +283,7 @@ export const createDistance = /* #__PURE__ */ factory(name, dependencies, ({ typ
let result = 0
let diff = 0
for (let i = 0; i < vectorSize; i++) {
diff = subtract(x[i], y[i])
diff = subtractScalar(x[i], y[i])
result = addScalar(multiplyScalar(diff, diff), result)
}
return sqrt(result)
Expand Down
8 changes: 4 additions & 4 deletions src/function/matrix/det.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { format } from '../../utils/string.js'
import { factory } from '../../utils/factory.js'

const name = 'det'
const dependencies = ['typed', 'matrix', 'subtract', 'multiply', 'divideScalar', 'isZero', 'unaryMinus']
const dependencies = ['typed', 'matrix', 'subtractScalar', 'multiply', 'divideScalar', 'isZero', 'unaryMinus']

export const createDet = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, subtract, multiply, divideScalar, isZero, unaryMinus }) => {
export const createDet = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, subtractScalar, multiply, divideScalar, isZero, unaryMinus }) => {
/**
* Calculate the determinant of a matrix.
*
Expand Down Expand Up @@ -103,7 +103,7 @@ export const createDet = /* #__PURE__ */ factory(name, dependencies, ({ typed, m
} else if (rows === 2) {
// this is a 2 x 2 matrix
// the determinant of [a11,a12;a21,a22] is det = a11*a22-a21*a12
return subtract(
return subtractScalar(
multiply(matrix[0][0], matrix[1][1]),
multiply(matrix[1][0], matrix[0][1])
)
Expand Down Expand Up @@ -133,7 +133,7 @@ export const createDet = /* #__PURE__ */ factory(name, dependencies, ({ typed, m
for (let i = k + 1; i < rows; i++) {
const i_ = rowIndices[i]
for (let j = k + 1; j < rows; j++) {
matrix[i_][j] = divideScalar(subtract(multiply(matrix[i_][j], piv), multiply(matrix[i_][k], matrix[k_][j])), piv_)
matrix[i_][j] = divideScalar(subtractScalar(multiply(matrix[i_][j], piv), multiply(matrix[i_][k], matrix[k_][j])), piv_)
}
}
}
Expand Down
Loading

0 comments on commit 3cddb94

Please sign in to comment.