Skip to content

added subtractScalar #3081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,7 @@ BuildTools <[email protected]>
Anik Patel <[email protected]>
Vrushaket Chaudhari <[email protected]>
Praise Nnamonu <[email protected]>
Vincent Tam <[email protected]>
vrushaket <[email protected]>

# Generated by tools/update-authors.js
1 change: 1 addition & 0 deletions src/factoriesAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export { createUnaryPlus } from './function/arithmetic/unaryPlus.js'
export { createAbs } from './function/arithmetic/abs.js'
export { createApply } from './function/matrix/apply.js'
export { createAddScalar } from './function/arithmetic/addScalar.js'
export { createSubtractScalar } from './function/arithmetic/subtractScalar.js'
export { createCbrt } from './function/arithmetic/cbrt.js'
export { createCeil } from './function/arithmetic/ceil.js'
export { createCube } from './function/arithmetic/cube.js'
Expand Down
1 change: 1 addition & 0 deletions src/factoriesNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const createUnaryMinus = /* #__PURE__ */ createNumberFactory('unaryMinus'
export const createUnaryPlus = /* #__PURE__ */ createNumberFactory('unaryPlus', unaryPlusNumber)
export const createAbs = /* #__PURE__ */ createNumberFactory('abs', absNumber)
export const createAddScalar = /* #__PURE__ */ createNumberFactory('addScalar', addNumber)
export const createSubtractScalar = /* #__PURE__ */ createNumberFactory('subtractScalar', subtractNumber)
export const createCbrt = /* #__PURE__ */ createNumberFactory('cbrt', cbrtNumber)
export { createCeilNumber as createCeil } from './function/arithmetic/ceil.js'
export const createCube = /* #__PURE__ */ createNumberFactory('cube', cubeNumber)
Expand Down
30 changes: 9 additions & 21 deletions src/function/arithmetic/subtract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ const dependencies = [
'matrix',
'equalScalar',
'addScalar',
'subtractScalar',
'unaryMinus',
'DenseMatrix',
'concat'
]

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

const matAlgo01xDSid = createMatAlgo01xDSid({ typed })
Expand Down Expand Up @@ -63,33 +64,20 @@ export const createSubtract = /* #__PURE__ */ factory(name, dependencies, ({ typ
return typed(
name,
{
'number, number': (x, y) => x - y,
'Complex, Complex': (x, y) => x.sub(y),
'BigNumber, BigNumber': (x, y) => x.minus(y),
'Fraction, Fraction': (x, y) => x.sub(y),
'any, any': addScalar,

'Unit, Unit': typed.referToSelf(self => (x, y) => {
if (x.value === null) {
throw new Error('Parameter x contains a unit with undefined value')
}

if (y.value === null) {
throw new Error('Parameter y contains a unit with undefined value')
}
'any, any, ...any': typed.referToSelf(self => (x, y, rest) => {
let result = self(x, y)

if (!x.equalBase(y)) {
throw new Error('Units do not match')
for (let i = 0; i < rest.length; i++) {
result = self(result, rest[i])
}

const res = x.clone()
res.value =
typed.find(self, [res.valueType(), y.valueType()])(res.value, y.value)
res.fixPrefix = false

return res
return result
})
},
matrixAlgorithmSuite({
elop: subtractScalar,
SS: matAlgo05xSfSf,
DS: matAlgo01xDSid,
SD: matAlgo03xDSf,
Expand Down
52 changes: 52 additions & 0 deletions src/function/arithmetic/subtractScalar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { factory } from '../../utils/factory.js'
import { subtractNumber } from '../../plain/number/index.js'

const name = 'subtractScalar'
const dependencies = ['typed']

export const createSubtractScalar = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
/**
* Subtract two scalar values, `x - y`.
* This function is meant for internal use: it is used by the public function
* `subtract`
*
* This function does not support collections (Array or Matrix).
*
* @param {number | BigNumber | Fraction | Complex | Unit} x First value
* @param {number | BigNumber | Fraction | Complex} y Second value to be subtracted from `x`
* @return {number | BigNumber | Fraction | Complex | Unit} Difference of `x` and `y`
* @private
*/
return typed(name, {

'number, number': subtractNumber,

'Complex, Complex': function (x, y) {
return x.sub(y)
},

'BigNumber, BigNumber': function (x, y) {
return x.minus(y)
},

'Fraction, Fraction': function (x, y) {
return x.sub(y)
},

'Unit, Unit': typed.referToSelf(self => (x, y) => {
if (x.value === null || x.value === undefined) {
throw new Error('Parameter x contains a unit with undefined value')
}
if (y.value === null || y.value === undefined) {
throw new Error('Parameter y contains a unit with undefined value')
}
if (!x.equalBase(y)) throw new Error('Units do not match')

const res = x.clone()
res.value =
typed.find(self, [res.valueType(), y.valueType()])(res.value, y.value)
res.fixPrefix = false
return res
})
})
})
1 change: 1 addition & 0 deletions src/utils/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export function createSnapshotFromFactories (factories) {
'equalScalar',
'apply',
'addScalar',
'subtractScalar',
'multiplyScalar',
'print',
'divideScalar',
Expand Down
2 changes: 1 addition & 1 deletion test/node-tests/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('lib/browser', function () {
'compile', 'parse', 'parser', // TODO: add embedded docs for compile, parse, and parser?
'reviver', 'replacer', // TODO: add embedded docs for reviver and replacer?
'apply', // FIXME: apply is not supported right now because of security concerns
'addScalar', 'divideScalar', 'multiplyScalar', 'equalScalar'
'addScalar', 'subtractScalar', 'divideScalar', 'multiplyScalar', 'equalScalar'
]

// test whether all functions are documented
Expand Down
2 changes: 1 addition & 1 deletion test/node-tests/doc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function checkExpectation (want, got) {
}

const OKundocumented = new Set([
'addScalar', 'divideScalar', 'multiplyScalar', 'equalScalar',
'addScalar', 'subtractScalar', 'divideScalar', 'multiplyScalar', 'equalScalar',
'docs', 'FibonacciHeap',
'IndexError', 'DimensionError', 'ArgumentsError'
])
Expand Down
1 change: 0 additions & 1 deletion test/unit-tests/function/arithmetic/subtract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ describe('subtract', function () {

it('should throw an error in case of invalid number of arguments', function () {
assert.throws(function () { subtract(1) }, /TypeError: Too few arguments/)
assert.throws(function () { subtract(1, 2, 3) }, /TypeError: Too many arguments/)
})

it('should throw an in case of wrong type of arguments', function () {
Expand Down
Loading