# kepempem/Math-PKG

Fixed Zeta and Eta Function, Improved Riemann Sums by adding a length…

`… argument and improved documentation`
kepempem committed Jul 18, 2019
1 parent 94ee0bb commit c090c15737c6b9c49647dcaaa6451440bfa924ab
Showing with 81 additions and 44 deletions.
1. +40 −9 code/Functions.js
2. +15 −8 code/RiemannSum.js
3. +8 −0 docs/BaseToolbox.md
4. +9 −1 docs/Functions.md
5. +7 −7 docs/Integration.md
6. +1 −1 package.json
7. +1 −18 test.js
 @@ -4,6 +4,7 @@ const Complex = require("./Complex"); const Fk = Math.pow(10, 4); const FupperBound = Math.pow(10, 6); const eps = Math.pow(10, -10); const realUnit = new Complex(1, 0); const imaginaryUnit = new Complex(0, 1); @@ -41,15 +42,34 @@ const Prod = (a, b, f, step = 1)=>{ const digamma = x=>FactorialDerivative(x-1)/gamma(x); const eta = s=>{ if(s < 0){ return NaN; }else if(s == 0){ return 1/2; }else{ return RiemannSum.avg(s>0?0:eps, 100, t=>(Math.pow(t, s)/Math.pow(Math.cosh(t),2)))/(Math.pow(2,1-s)*gamma(s+1)); } }; const zeta = s=>{ if(s>1){ if(s == 1){ return Infinity; }else if(s > 0 && s < 2){ return eta(s)/(1-Math.pow(2,1-s)) }else if(s>=2){ let rs = RiemannSum.avg(Math.pow(10,-8),100,t=>{ return Math.pow(t,s-1)/(Math.exp(t)-1); }); return (1/gamma(s)) * rs; }else{ return NaN; }else if(s == 0){ return -1/2; }else if(s == -1){ return -1/12; }else if(s < 0 && Math.abs(s) % 2 == 0){ return 0; } return NaN; }; const FactorialDerivative = (z)=>{ @@ -63,13 +83,23 @@ const erf = x=>(2/Math.sqrt(Math.PI)) * RiemannSum.avg(0, x, t=>Math.exp(-1 * Ma const Ei = x=>li(Math.exp(x)); const li = x=>{ let rema = 0; let start = Math.pow(10, -8); if(x >= 2){ rema = li2; start = 2; const g = t=>1/ln(t).Re; if(x == 0){ return 0; }else if(x < 0){ return NaN; }else if(x == 1){ return -Infinity; }else if(x < 2){ let h = Math.pow(10, -6); if(x > 1){ return RiemannSum.avg(eps,1-eps,g,0,h) + RiemannSum.avg(1+eps, x, g,0,h); }else{ return RiemannSum.avg(eps, x, g,0,h); } }else{ return li2 + RiemannSum.avg(2, x, g); } return rema+RiemannSum.avg(start, x, t=>1/ln(t).Re); }; const root = (r,p)=>Math.pow(r, 1/p); @@ -285,6 +315,7 @@ module.exports={ Sum, Prod, digamma, eta, zeta, FactorialDerivative, erf,
 @@ -1,7 +1,11 @@ const riemannSum = (a, b, f, p = 0, isLeftSum=true)=>{ const DEFAULT_RECT_LENGTH = Math.pow(10, -3); const riemannSum = (a, b, f, p = 0, l=0, isLeftSum=true)=>{ if(a == b){ return 0; } let n = p; if(n <= 0){ n = Math.pow(10,3) * (b - a); n = Math.ceil((1/l) * (b - a)); } let dt = ((b-a)/n); let sumStart = 0; @@ -16,21 +20,24 @@ const riemannSum = (a, b, f, p = 0, isLeftSum=true)=>{ }; const avgRiemannSum = (a, b, f, p=0)=>{ const avgRiemannSum = (a, b, f, p=0, l=DEFAULT_RECT_LENGTH)=>{ if(a == b){ return 0; } let n = p; if(n <= 0){ n = Math.ceil(Math.pow(10, 3) * (b - a)); n = Math.ceil((1/l) * (b - a)); } let dt = (b-a)/n; return (dt/2) * (f(a) + f(b)) + dt * Sum(1,n-1, (i)=>{ return f(a + i*dt); }); }; const {Sum, Prod} = require("./Functions"); const {Sum} = require("./Functions"); module.exports={ left:(a,b,f,n=0)=>riemannSum(a,b,f,n.true), right:(a,b,f,n=0)=>riemannSum(a,b,f,n,false), avg:(a,b,f,n=0)=>avgRiemannSum(a,b,f,n) left:(a,b,f,n=0,l=DEFAULT_RECT_LENGTH)=>riemannSum(a,b,f,n,l,true), right:(a,b,f,n=0,l=DEFAULT_RECT_LENGTH)=>riemannSum(a,b,f,n,l,false), avg:(a,b,f,n=0,l=DEFAULT_RECT_LENGTH)=>avgRiemannSum(a,b,f,n,l) };
 @@ -14,6 +14,7 @@ + [BaseNumber](#basenumber) + [BaseNumber.toString()](#basenumbertostring) + [BaseNumber.to(b)](#basenumbertob) + [BaseNumber.toBase10()](#basenumbertobase10) + [BaseNumber.digits](#basenumberdigits) + [BaseNumber.base](#basenumberbase) + [BaseNumber.sign](#basenumbersign) @@ -103,6 +104,13 @@ let n1 = new BaseToolbox.BaseNumber("1011010110",b2); let n2 = n1.to(b16).toString(); // Output: "2D6" ``` ### BaseNumber.toBase10() Turns the `BaseNumber` into a base 10 number and returns the result (As an instance of `Number`, not `BaseNumber`). ``` let n1 = new BaseToolbox.BaseNumber("A12", base16); let n2 = n1.toBase10(); // = 2578 ``` ### BaseNumber.digits One may use ``BaseNumber.digits`` to either get or set the digits of the number. When setting the digits, One may set them as a String, An array of string digits or a number.
 @@ -8,6 +8,7 @@ + [Sum(a, b, f[, step = 1])](#suma-b-f-step--1) + [Prod(a, b, f[, step=1])](#proda-b-f-step--1) + [digamma(x)](#digammax) + [eta(s)](#etas) + [zeta(s)](#zetas) + [FactorialDerivative(x)](#factorialderivativex) + [erf(x)](#erfx) @@ -61,9 +62,16 @@ The digamma function. Defined as ``digamma(x) = gamma'(x) / gamma(x)`` where ``g let dg = Functions.digamma(5); // ~ 1.506117 ``` ## eta(s) The Dirichlet Eta Function. `s` must be a real number. The function is only able to calculate values for `s >= 0`. ``` let ln2 = Function.eta(1); // ~ 0.6931471 ``` ## zeta(s) The Riemann Zeta function. `s` must be a real number. `s` must be a real number. The function is only able to calculate values for `s >= 0` and is undefined at `s = 1`. ``` let psos = Functions.zeta(2); // ~ 1.64493 ```
 @@ -13,31 +13,31 @@ + [Constants](./Constants.md) MathPKG has no capability of finding antiderivatives of a function, so to integrate, one may use MathPKG to calculate the area under a curve using Riemann Summation. The RiemannSum functions are located inside ``MathPKG.RiemannSum``. For convenience, one may define: The Riemann Summation functions are located inside ``MathPKG.RiemannSum``. For convenience, one may define: ``` const {RiemannSum} = MathPKG; ``` ## left(a, b, f[, n = 0]) ## left(a, b, f[, n = 0, l = 10 ^ -3]) ![Left Riemann Sum](../media/left.gif) Calculates the left side Riemann sum of `f(x)` from `x=a` to `x=b`. In the geometric sense, `n` is the number of "rectangles", the bigger `n` is, the more accurate the calculation will be. When `n = 0`, the function sets `n` so that the length of one rectangle on the `x-axis` is `10^-3`, making the calculation extremly acurate. By default, `n` is `0`. The left side sum is usually a little bit less than the actual area. Calculates the left side Riemann sum of `f(x)` from `x=a` to `x=b`. In the geometric sense, `n` is the number of "rectangles", the bigger `n` is, the more accurate the calculation will be. When `n = 0`, the function sets `n` so that the length of one rectangle on the x-axis is `l`. By default, `n` is `0` and `l` is `10 ^ -3`, making the calculation extremly accurate. The left side sum is usually a little bit less than the actual area. ``` let area = RiemannSum.left(0,10,x=>x); // = 49.995000000000005 ``` ## right(a, b, f[, n = 0]) ## right(a, b, f[, n = 0, l = 10 ^ -3]) ![Right Riemann Sum](../media/right.gif) Calculates the right side Riemann sum of `f(x)` from `x=a` to `x=b`. In the geometric sense, `n` is the number of "rectangles", the bigger `n` is, the more accurate the calculation will be. When `n = 0`, the function sets `n` so that the length of one rectangle on the `x-axis` is `10^-3`, making the calculation extremly acurate. By default, `n` is `0`. The right side sum is usually a little bit more than the actual area. Calculates the right side Riemann sum of `f(x)` from `x=a` to `x=b`. In the geometric sense, `n` is the number of "rectangles", the bigger `n` is, the more accurate the calculation will be. When `n = 0`, the function sets `n` so that the length of one rectangle on the x-axis is `l`. By default, `n` is `0` and `l` is `10 ^ -3`, making the calculation extremly accurate. The right side sum is usually a little bit more than the actual area. ``` let area = RiemannSum.right(0,10,x=>x); // = 50.005 ``` ## avg(a, b, f[, n = 0]) Calculates the average of the left side and right side Riemann sums of `f(x)` from `x=a` to `x=b`. In the geometric sense, `n` is the number of "rectangles", the bigger `n` is, the more accurate the calculation will be. When `n = 0`, the function sets the `n` so that the length of one rectangle on the `x-axis` is `10^-3`, making the calculation extremly acurate. By default, `n` is `0`. This is the most accurate method. ## avg(a, b, f[, n = 0, l = 10 ^ -3]) Calculates the average of the left side and right side Riemann sums of `f(x)` from `x=a` to `x=b`. In the geometric sense, `n` is the number of "rectangles", the bigger `n` is, the more accurate the calculation will be. When `n = 0`, the function sets the `n` so that the length of one rectangle on the x-axis is `l`. By default, `n = 0` and `l = 10 ^ -3`, making the calculation extremly accurate. This is the most accurate method. ``` let area = RiemannSum.avg(0,10,x=>x); // = 50 ```
 @@ -1,6 +1,6 @@ { "name": "math-pkg-cmplx", "version": "1.0.7", "version": "1.0.8", "description": "Math Library with Complex Number Functionality, Integration functions and conversion functions between numerical bases.", "main": "index.js", "scripts": {
19 test.js
 @@ -16,21 +16,4 @@ console.log( .pow(pi.times(i)) .round(10) .toString() ); // e ^ i pi let b16 = BaseToolbox.generateSimpleBase(16); let b2 = BaseToolbox.generateSimpleBase(2); console.log( new BaseToolbox.BaseNumber("1001101",b2) .to(b16) .toString() ); let c = new Complex(5, 8); console.log( Functions.cis(c.Arg) .times(Functions.abs(c)) .round(10) .toString() ); console.log(Functions.sin(new Complex(4, 7)).toString()); console.log(Functions.li(100)); ); // e ^ i pi