From f8dc85c31188e06378c17417eaead540043366aa Mon Sep 17 00:00:00 2001 From: Prashal Ruchiranga Date: Mon, 10 Oct 2022 12:48:30 +0530 Subject: [PATCH 1/7] Add an algorithm to find Taylor series approximation of exponential function --- Maths/ExponentialFunction.js | 21 +++++++++++++++++++++ Maths/test/ExponentialFunction.test.js | 8 ++++++++ 2 files changed, 29 insertions(+) create mode 100644 Maths/ExponentialFunction.js create mode 100644 Maths/test/ExponentialFunction.test.js diff --git a/Maths/ExponentialFunction.js b/Maths/ExponentialFunction.js new file mode 100644 index 0000000000..65d4d850a2 --- /dev/null +++ b/Maths/ExponentialFunction.js @@ -0,0 +1,21 @@ +let powerOfX = 1 +let factorial = 1 +/** + * @function exponentialFunction + * @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n + * @param {Integer} power + * @param {Integer} order - 1 + * @returns exponentialFunction(18,4) = 18.4 + * @url https://en.wikipedia.org/wiki/Exponential_function + */ +function exponentialFunction (power, n) { + if (n === 0) { return 1 } + const recursion = exponentialFunction(power, n - 1) + powerOfX = powerOfX * power + factorial = factorial * n + return recursion + powerOfX / factorial +} + +export { + exponentialFunction +} diff --git a/Maths/test/ExponentialFunction.test.js b/Maths/test/ExponentialFunction.test.js new file mode 100644 index 0000000000..d1f52f05e4 --- /dev/null +++ b/Maths/test/ExponentialFunction.test.js @@ -0,0 +1,8 @@ +import { exponentialFunction } from '../ExponentialFunction' + +describe('exponentialFunction', () => { + it('with a power of 5 and order of 21', () => { + const ex = exponentialFunction(5, 20) + expect(ex).toBe(148.4131470673818) + }) +}) From 7b4ff6eb1f9c97f0a49781045789910d89b2f46b Mon Sep 17 00:00:00 2001 From: Prashal Ruchiranga Date: Tue, 11 Oct 2022 13:53:40 +0530 Subject: [PATCH 2/7] Add checks for invalid inputs --- Maths/ExponentialFunction.js | 5 ++++- Maths/test/ExponentialFunction.test.js | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Maths/ExponentialFunction.js b/Maths/ExponentialFunction.js index 65d4d850a2..72304a520c 100644 --- a/Maths/ExponentialFunction.js +++ b/Maths/ExponentialFunction.js @@ -5,10 +5,13 @@ let factorial = 1 * @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n * @param {Integer} power * @param {Integer} order - 1 - * @returns exponentialFunction(18,4) = 18.4 + * @returns exponentialFunction(2,20) = 7.389056098930604 * @url https://en.wikipedia.org/wiki/Exponential_function */ function exponentialFunction (power, n) { + if (isNaN(power) || isNaN(n) || n < 0) { + throw new TypeError('Invalid Input') + } if (n === 0) { return 1 } const recursion = exponentialFunction(power, n - 1) powerOfX = powerOfX * power diff --git a/Maths/test/ExponentialFunction.test.js b/Maths/test/ExponentialFunction.test.js index d1f52f05e4..afa99aff18 100644 --- a/Maths/test/ExponentialFunction.test.js +++ b/Maths/test/ExponentialFunction.test.js @@ -1,7 +1,15 @@ import { exponentialFunction } from '../ExponentialFunction' -describe('exponentialFunction', () => { - it('with a power of 5 and order of 21', () => { +describe('Tests for exponential function', () => { + it('should be a function', () => { + expect(typeof exponentialFunction).toEqual('function') + }) + + it('should throw error for invalid input', () => { + expect(() => exponentialFunction(2, -34)).toThrow() + }) + + it('should return the exponential function of power of 5 and order of 21', () => { const ex = exponentialFunction(5, 20) expect(ex).toBe(148.4131470673818) }) From 418ac6b43bf193b4d85aeadfe6fc36afc502caca Mon Sep 17 00:00:00 2001 From: Prashal Ruchiranga Date: Wed, 12 Oct 2022 18:06:24 +0530 Subject: [PATCH 3/7] make exponentialFunction iterative using a for loop --- Maths/ExponentialFunction.js | 15 ++++++++------- Maths/test/ExponentialFunction.test.js | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Maths/ExponentialFunction.js b/Maths/ExponentialFunction.js index 72304a520c..131c48abe8 100644 --- a/Maths/ExponentialFunction.js +++ b/Maths/ExponentialFunction.js @@ -1,22 +1,23 @@ -let powerOfX = 1 -let factorial = 1 +import { calcFactorial } from './Factorial.js' + /** * @function exponentialFunction * @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n * @param {Integer} power * @param {Integer} order - 1 - * @returns exponentialFunction(2,20) = 7.389056098930604 + * @returns exponentialFunction(2,20) = 7.3890560989301735 * @url https://en.wikipedia.org/wiki/Exponential_function */ function exponentialFunction (power, n) { + let output = 0 if (isNaN(power) || isNaN(n) || n < 0) { throw new TypeError('Invalid Input') } if (n === 0) { return 1 } - const recursion = exponentialFunction(power, n - 1) - powerOfX = powerOfX * power - factorial = factorial * n - return recursion + powerOfX / factorial + for(let i = 0; i < n; i++){ + output += (power ** i) / calcFactorial(i) + } + return output } export { diff --git a/Maths/test/ExponentialFunction.test.js b/Maths/test/ExponentialFunction.test.js index afa99aff18..d1eed4895f 100644 --- a/Maths/test/ExponentialFunction.test.js +++ b/Maths/test/ExponentialFunction.test.js @@ -11,6 +11,6 @@ describe('Tests for exponential function', () => { it('should return the exponential function of power of 5 and order of 21', () => { const ex = exponentialFunction(5, 20) - expect(ex).toBe(148.4131470673818) + expect(ex).toBe(148.4131078683383) }) }) From 4c9a52af236dce5a0837a003eb0981dd3de83328 Mon Sep 17 00:00:00 2001 From: Prashal Ruchiranga Date: Wed, 12 Oct 2022 18:09:58 +0530 Subject: [PATCH 4/7] Update ExponentialFunction.js --- Maths/ExponentialFunction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/ExponentialFunction.js b/Maths/ExponentialFunction.js index 131c48abe8..0fd2bd6e7c 100644 --- a/Maths/ExponentialFunction.js +++ b/Maths/ExponentialFunction.js @@ -14,7 +14,7 @@ function exponentialFunction (power, n) { throw new TypeError('Invalid Input') } if (n === 0) { return 1 } - for(let i = 0; i < n; i++){ + for (let i = 0; i < n; i++) { output += (power ** i) / calcFactorial(i) } return output From b85a2addf948df10b19d4568d5c78e9ea6f2e86c Mon Sep 17 00:00:00 2001 From: Prashal Ruchiranga Date: Wed, 12 Oct 2022 20:09:59 +0530 Subject: [PATCH 5/7] Remove reusing the factorial implementation --- Maths/ExponentialFunction.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Maths/ExponentialFunction.js b/Maths/ExponentialFunction.js index 0fd2bd6e7c..a834820242 100644 --- a/Maths/ExponentialFunction.js +++ b/Maths/ExponentialFunction.js @@ -1,5 +1,3 @@ -import { calcFactorial } from './Factorial.js' - /** * @function exponentialFunction * @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n @@ -10,12 +8,15 @@ import { calcFactorial } from './Factorial.js' */ function exponentialFunction (power, n) { let output = 0 + let fac = 1 if (isNaN(power) || isNaN(n) || n < 0) { throw new TypeError('Invalid Input') } if (n === 0) { return 1 } for (let i = 0; i < n; i++) { - output += (power ** i) / calcFactorial(i) + output += (power ** i) / fac + fac *= ++i + --i } return output } @@ -23,3 +24,5 @@ function exponentialFunction (power, n) { export { exponentialFunction } + +console.log(exponentialFunction(2,20)) \ No newline at end of file From 587a6e277a916fa686c9b315becbaac7512590a6 Mon Sep 17 00:00:00 2001 From: Prashal Ruchiranga Date: Wed, 12 Oct 2022 20:13:48 +0530 Subject: [PATCH 6/7] Update ExponentialFunction.js --- Maths/ExponentialFunction.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/Maths/ExponentialFunction.js b/Maths/ExponentialFunction.js index a834820242..f3cc121a62 100644 --- a/Maths/ExponentialFunction.js +++ b/Maths/ExponentialFunction.js @@ -24,5 +24,3 @@ function exponentialFunction (power, n) { export { exponentialFunction } - -console.log(exponentialFunction(2,20)) \ No newline at end of file From d3e646a8bb53438f9d740a2ab62e0b39d4bf6553 Mon Sep 17 00:00:00 2001 From: Prashal Ruchiranga Date: Wed, 12 Oct 2022 22:26:11 +0530 Subject: [PATCH 7/7] Update ExponentialFunction.js --- Maths/ExponentialFunction.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Maths/ExponentialFunction.js b/Maths/ExponentialFunction.js index f3cc121a62..4212693810 100644 --- a/Maths/ExponentialFunction.js +++ b/Maths/ExponentialFunction.js @@ -15,8 +15,7 @@ function exponentialFunction (power, n) { if (n === 0) { return 1 } for (let i = 0; i < n; i++) { output += (power ** i) / fac - fac *= ++i - --i + fac *= (i + 1) } return output }