mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-12-19 06:58:15 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@@ -1,37 +1,45 @@
|
||||
/*
|
||||
*
|
||||
* @file
|
||||
* @title Composite Simpson's rule for definite integral evaluation
|
||||
* @author: [ggkogkou](https://github.com/ggkogkou)
|
||||
* @brief Calculate definite integrals using composite Simpson's numerical method
|
||||
*
|
||||
* @details The idea is to split the interval in an EVEN number N of intervals and use as interpolation points the xi
|
||||
* for which it applies that xi = x0 + i*h, where h is a step defined as h = (b-a)/N where a and b are the
|
||||
* first and last points of the interval of the integration [a, b].
|
||||
*
|
||||
* We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:
|
||||
* I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
|
||||
*
|
||||
* That means that the first and last indexed i f(xi) are multiplied by 1,
|
||||
* the odd indexed f(xi) by 4 and the even by 2.
|
||||
*
|
||||
* N must be even number and a<b. By increasing N, we also increase precision
|
||||
*
|
||||
* More info: [Wikipedia link](https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule)
|
||||
*
|
||||
*/
|
||||
*
|
||||
* @file
|
||||
* @title Composite Simpson's rule for definite integral evaluation
|
||||
* @author: [ggkogkou](https://github.com/ggkogkou)
|
||||
* @brief Calculate definite integrals using composite Simpson's numerical method
|
||||
*
|
||||
* @details The idea is to split the interval in an EVEN number N of intervals and use as interpolation points the xi
|
||||
* for which it applies that xi = x0 + i*h, where h is a step defined as h = (b-a)/N where a and b are the
|
||||
* first and last points of the interval of the integration [a, b].
|
||||
*
|
||||
* We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:
|
||||
* I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
|
||||
*
|
||||
* That means that the first and last indexed i f(xi) are multiplied by 1,
|
||||
* the odd indexed f(xi) by 4 and the even by 2.
|
||||
*
|
||||
* N must be even number and a<b. By increasing N, we also increase precision
|
||||
*
|
||||
* More info: [Wikipedia link](https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule)
|
||||
*
|
||||
*/
|
||||
|
||||
function integralEvaluation (N, a, b, func) {
|
||||
function integralEvaluation(N, a, b, func) {
|
||||
// Check if N is an even integer
|
||||
let isNEven = true
|
||||
if (N % 2 !== 0) isNEven = false
|
||||
|
||||
if (!Number.isInteger(N) || Number.isNaN(a) || Number.isNaN(b)) { throw new TypeError('Expected integer N and finite a, b') }
|
||||
if (!isNEven) { throw Error('N is not an even number') }
|
||||
if (N <= 0) { throw Error('N has to be >= 2') }
|
||||
if (!Number.isInteger(N) || Number.isNaN(a) || Number.isNaN(b)) {
|
||||
throw new TypeError('Expected integer N and finite a, b')
|
||||
}
|
||||
if (!isNEven) {
|
||||
throw Error('N is not an even number')
|
||||
}
|
||||
if (N <= 0) {
|
||||
throw Error('N has to be >= 2')
|
||||
}
|
||||
|
||||
// Check if a < b
|
||||
if (a > b) { throw Error('a must be less or equal than b') }
|
||||
if (a > b) {
|
||||
throw Error('a must be less or equal than b')
|
||||
}
|
||||
if (a === b) return 0
|
||||
|
||||
// Calculate the step h
|
||||
@@ -58,7 +66,11 @@ function integralEvaluation (N, a, b, func) {
|
||||
|
||||
result *= temp
|
||||
|
||||
if (Number.isNaN(result)) { throw Error("Result is NaN. The input interval doesn't belong to the functions domain") }
|
||||
if (Number.isNaN(result)) {
|
||||
throw Error(
|
||||
"Result is NaN. The input interval doesn't belong to the functions domain"
|
||||
)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user