Skip to content
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

Implementation of discrete summations #3104

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/factoriesAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export { createSign } from './function/arithmetic/sign.js'
export { createSqrt } from './function/arithmetic/sqrt.js'
export { createSquare } from './function/arithmetic/square.js'
export { createSubtract } from './function/arithmetic/subtract.js'
export { createSummation } from './function/arithmetic/summation.js'
export { createXgcd } from './function/arithmetic/xgcd.js'
export { createInvmod } from './function/arithmetic/invmod.js'
export { createDotMultiply } from './function/arithmetic/dotMultiply.js'
Expand Down
1 change: 1 addition & 0 deletions src/factoriesNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const createSign = /* #__PURE__ */ createNumberFactory('sign', signNumber
export const createSqrt = /* #__PURE__ */ createNumberFactory('sqrt', sqrtNumber)
export const createSquare = /* #__PURE__ */ createNumberFactory('square', squareNumber)
export const createSubtract = /* #__PURE__ */ createNumberFactory('subtract', subtractNumber)
export { createSummation } from './function/arithmetic/summation.js'
export const createXgcd = /* #__PURE__ */ createNumberFactory('xgcd', xgcdNumber)
export const createDivideScalar = /* #__PURE__ */ createNumberFactory('divideScalar', divideNumber)
export const createPow = /* #__PURE__ */ createNumberFactory('pow', powNumber)
Expand Down
45 changes: 45 additions & 0 deletions src/function/arithmetic/summation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { isInteger } from '../../utils/number.js'
import { factory } from '../../utils/factory.js'

const name = 'summation'
const dependencies = ['typed', 'numeric']

export const createSummation = /* #__PURE__ */ factory(name, dependencies, ({ typed, numeric }) => {
/**
* Takes two integer values start, and end, and a functrion.
* Calculates the summation of the function over the start to end range.
*
* Syntax:
*
* math.summation(start, end, func))
*
* Examples:
*
* // Summation of squares from 1 to 5: 1^2 + 2^2 + 3^2 + 4^2 + 5^2
* math.sum(1, 5, x => x ** 2) // returns 55
*
* This function does not support collections (Array or Matrix).
*
* @param {number} start Start
* @param {number} end Denominator
* @param {function} func f(n)
* @return {number | BigNumber | Fraction} Sum of f(n) for n from [start, end]
*/
return typed(name, {
'number, number, function': function (start, end, func) {
if (!isInteger(start) || !isInteger(end)) {
throw new Error('For summations, start and end must be integers')
}
if (start > end) {
throw new Error('For summations, start must be greater than, or equal to end')
}

let sum = 0
for (let i = start; i <= end; i++) {
sum += func(i)
}

return sum
}
})
})
22 changes: 22 additions & 0 deletions test/unit-tests/function/arithmetic/summation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from 'assert'

import math from '../../../../src/defaultInstance.js'

const summation = math.summation

describe('summation', function () {
it('should produce the sum of a function over a range from start to end', function () {
assert.deepEqual(summation(1, 5, x => x ** 2), 55)
assert.deepEqual(summation(1, 4, x => x ** 3, 100))
})

it('should throw an error if start or end are not integers', function () {
assert.throws(function () { summation(1.1, 2.2, x => x ** 2) })
assert.throws(function () { summation(1.1, 2, x => x ** 2) })
assert.throws(function () { summation(1, 2.4, x => x ** 2) })
})

it('should throw an error if end is less than start', function () {
assert.throws(function () { summation(2, 1, x => x ** 2) })
})
})