Numerical Methods at work

Disclaimer:
Permission to use, copy, and distribute this software, and It’s documentation for any non-commercial purpose is hereby granted without fee, provided: THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL Henrik Vestermark, BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Javascript Libraries for Numerical computation

Below is a list of powerful JavaScript libraries for numerical calculation. These libraries work on top of the build in Math, Array and String function generally available in any browser. Furthermore, it automatically accepts some of the JavaScript extension and only provide a function if this is not available in the current browser version. There is several different libraries available.

  • The first library is a library for Complex arithmetic that runs on top of the Math library. It also extend the basic Math libraries with Hyperbolic functions as well as Gamma and Beta functions if not available in the current browser.
  • The second library extend the build in floating point arithmetic with arbitrary precisions functions.
  • The third library extend complex arithmetic to also work on Arbitrary precision numbers.
  • The fourth library is a Polynomial class library that works on polynomial arithmetic and zeros for polynomials.
  • The fifth library is a library for fraction arithmetic. It supports both regular Javascript Numbers, but also the new type BigInt.
  • The sixth is a library for interval arithmetic.
  • The seventh is a library for calculation financial parameters for mortgage or investment.

Complex - Javascript library
JavaScript Complex Class Library contains several methods to handle complex arithmetic and complex functions.

  • Complex arithmetic: +,-,*,/,conj(),real(),imag(),
  • Complex functions: arg(),norm(),polar(),negate(),abs(),log(),log10(),exp(),pow(),sqrt()
  • Complex comparison: equal(),notequal()
  • Complex trigonometric: sin(),cos(),tan(),asin(),acos(),atan(),atan2()
  • Complex hyperbolic: sinh(),cosh(),tanh(),asinh(),acosh(),atanh()
  • Complex special function: gamma(), lgamma(), beta()
  • Other functions: toString(), toExponential(), toFixed(), toPrecision(), valueOf(), parseComplex()
Download Library ver 1.14
Download User Manual...

Example: Evaluate a polynomial at a complex point z. this.c[] contains the coefficients of the polynomial

//Evaluate f(z). z can be a complex number or real number.
//Polynomial can be either with complex or real coefficients
Polynomial.prototype.value=function(z)
 { var i, fz;
 // Polynomial evaluation Real point Complex point
 // Real coefficients Real Complex
 // Complex coefficients Complex Complex
  if(this.isReal()) // Evaluate f(z). Horner. Real coefficients only
  {
   if(z instanceof Complex)
   {// Real coefficients and complex z
   var p,q,s,r,t;
   p=-2.0*z.real(); q=z.norm(); s=0; r=this.c[0];
   for(i=1;i<this.c.length-1;i++) { t=this.c[i]-p*r-q*s; s=r; r=t; }
   fz=new Complex(this.c[this.c.length-1]+z.real()*r-q*s, z.imag()*r);
   return fz; // Return Complex
   }
  else
   {// Real coefficients and real z
   fz=this.c[0];
   for(i=1;i<this.c.length;i++)  fz=fz*z+this.c[i];
   return fz; // Return real;
   }
  }
 else // Evaluate a polynomial complex coefficients, at a real or complex number z using Horner,
   // return complex function value
  {var zz;
  fz=Complex(this.c[0]); zz=Complex(z);
  for(i=1;i<this.c.length;i++) {fz=Complex.add(Complex.mul(fz,zz),Complex(this.c[i]));}
  return fz; // return Complex
  }
 }
October
2018

New Tool

BigFloat - Arbitrary precision Javascript library
JavaScript BigFloat Class Library. This arbitrary precision library is a full implementation of arbitrary precision library that contains all relevant functions available with arbitrary precision. Compare to other libraries like bigfloat (Smaller case), or BigNumber this libraries contains more than just the arithmetic operators, abs() and sqrt(). Furthermore, the number of decimal precisions can be controlled per BigFloat variable and not just a global constant setting. This mean you can mix freely variable with different decimal precisions and round mode. Making it ideal for a BigFloat interval class to be available later. Also the BigFloat constant PI, LN2, LN10 or E is available in arbitrary precision.

  • BigFloat arithmetic: add(),sub(),mul(),div(),minus(),floor(),ceil()
  • BigFloat functions: abs(),log(),log10(),exp(),pow(),sqrt()
  • BigFloat comparison: equal(), notequal(), less(), lessequal(), greater(), greatereual()
  • BigFloat trigonometric: sin(),cos(),tan(),asin(),acos(),atan(),atan2()
  • BigFloat hyperbolic: sinh(),cosh(),tanh(),asinh(),acosh(),atanh()
  • BigFloat Special function: gamma(), lgamma(), beta(), erf()
  • BigFloat constant: PI(),LN2(),LN10(),E()

Download Library ver 1.26
Download User Manual...

Example:


function PolynomialBigFloat(coeff,x)
  // coeff is an BigFloat Array of polynomial coefficients,
  // x is a BigFloat number of the real point for evaluation
  {var i,fz;
  fz=new BigFloat(oeff[0],x.precision()); // Do the evaluation using the same precision as x
  for(i=1;i < coeff.length; i++)
   fz.assign(BigFloat.add(BigFloat.mul(fz,x),coeff[i]));
  return fz; // Return real;
  }

There is also a small web tool where you can see how different multiplication algorithm works for arbitrary precision using this Javascript library. It support a number of different methods, see below:

  • Karatsuba (2 way splitting).
  • Toom-cook3 (3 way splitting).
  • Schönhage-Strassen with Linear convolution.
  • Fast Fourier Multiplication.
  • School Book multiplication.
Click Here for the multiplication calculator
Revised
October
2019

New Tool

ComplexBigFloat - Complex Arbitrary precision Javascript library
JavaScript Complex Class Library for arbitrary precision math (BigFloat library) contains several methods to handle complex arithmetic and complex functions. Similar to the Complex library using the build in floating point type

  • Complex arithmetic: +,-,*,/,conj(),real(),imag(),
  • Complex functions: arg(),norm(),polar(),negate(),abs(),log(),log10(),exp(),pow(),sqrt()
  • Complex comparison: equal(),notequal()
  • Complex trigonometric: sin(),cos(),tan(),asin(),acos(),atan(),atan2()
  • Complex hyperbolic: sinh(),cosh(),tanh(),asinh(),acosh(),atanh()
  • Complex special function: gamma(), lgamma(), beta()
  • Other functions: toString(), toExponential(), toFixed(), toPrecision()
Download Library ver 1.01
Download User Manual...
December
2019

New Tool

Polynomial Arithmetic - Polynomial Arithmetic Javascript library
JavaScript Polynomial Class Library for Polynomial Arithmetic.

  • Polynomial arithmetic: +,-,*,/,rem
  • Polynomial functions: deflate(),compositedeflate(),derivative(),value(),pow(),zeros()
  • Polynomial other functions: array(),join(), degree(),getcoeff(),setcoeff(),isComplex(),isReal(),normalize(),scale(),shift(),simplify(),monic()
  • Other functions: toString(), toExponential(), toFixed(), toPrecision()
Download Library ver 1.26
Download User Manual...

Example:

 // Find the zeros and reconstruct the error polynomial from the zeros
 var pp=new Polynomial(),i,px,solutions;
 pp=parsePolynomial("x^3-2x+1");
 solution=pp.zeros();
 for(i=pp.degree();i>0;i--)
  {
  var px=new Polynomial(1,solutions[i].negate());
  if(i==pp.degree()) pp.px=px; else pp.px=Polynomial.mul(pp.px,px);
  }
February
2020

New Tool

Fraction Arihtmetic - Fraction Arithmetic Javascript library
JavaScript Fraction Class Library for Fraction Arithmetic.

  • Fraction arithmetic: +,-,*,/,
  • Fraction functions: ,abs(),gcd(),typeof(),whole(),reduce(),negate(),inverse()
  • Fraction comparison: equal(),notequal(),greater(),greaterequal(),less(),lessequal()
  • Other functions: toString(), toExponential(), toFixed(), toPrecision(), parseFraction()
Download Library ver 1.0
Download User Manual...

Example:

 // Example of using continuous fraction for finding π.
 function pi(i) // i is the number of iterations
 {var cf=Fraction(0);
  for(;i>0;--i)
  {cf=Fraction.add(cf,Fraction(i*2+1));
  cf=Fraction.div(Fraction(i*i),cf);}
 return Fraction.div(Fraction(4),Fraction.add(cf,Fraction(1)));}
 var cf=pit(10);
February
2020

New Tool

Interval Arihtmetic - Interval Arithmetic Javascript library
JavaScript Interval Class Library for Interval Arithmetic

  • Interval arithmetic: +,-,*,/,%
  • Interval functions: ,abs(),log(),log10(),exp(),pow(),sqr(),sqrt()
  • Interval comparison: equal(),notequal(),less(),lessequal(),greater(),greaterequal()
  • Interval trigonometric: sin(),cos(),tan(),asin(),acos(),atan(),atan2()
  • Interval hyperbolic: sinh(),cosh(),tanh(),asinh(),acosh(),atanh()
  • Interval logical: interior(),precedes(),intersection(),union(),subset()
  • Other functions: toString(), toExponential(), toFixed(), toPrecision(),parseInterval(),center(),width(),isEmpty(), isProper(), isImproper(), isPoint(), inf(), sup(), in()
Download Library ver 2.03
Download User Manual...

Example:

 // Example of using interval arithmetic to calculate the result of exp(x).
 {var low,high,t;
 function intervalexp(x)
  {var i,k,sign=1,isum,ix,ixp,ifac,idelta;
  if(x<0) {x=-x; sign=-1;}
  // Argument reduction
  for(k=0;x>0.5 &&k<=16;++k) x*=0.5;
  ix=Interval(x); ixp=Interval(1); isum=Interval(1); ifac=Interval(1);
  for(i=1;;++i)
   {
   ifac=Interval.mul(ifac,Interval(i));
   ixp=Interval.mul(ixp,ix);
   idelta=Interval.div(ixp,ifac);
   if(isum.center()+idelta.center()==isum.center()) break;
   isum=Interval.add(isum,idelta);
   }
  // Reverse reduction
  for(;k>0;--k) isum=Interval.mul(isum,isum);
  if(sign<0) {isum=Interval.div(Interval(1),isum);}
  return isum;
  }
 alert(intervalexp(1.5).toString());
Revised
August
2024

New Tool

Financial equation - Financial calculation Javascript library
JavaScript Financial Class Library for mortgage, investment etc. This Financial calculator employs the general financial formula: (PV + PMT(1 + c·IR)/IR)((1 + IR)NP - 1) + PV + FV = 0, to calculate one of the 5 parameters when 4 parameters are known.

  • PV (Present Value): The current value of a future sum of money or stream of cash flows given a specified rate of return.
  • FV (Future Value): The value of an asset or cash at a specified date in the future that is equivalent in value to a specified sum today.
  • PMT (Periodic Payment): The amount paid per period in a financial transaction.
  • NP (Number of Periods): The total number of payment periods in an investment or loan.
  • IR (Interest Rate): The yearly rate charged by a lender to a borrower or paid by an investor.
  • c (beginning of period payments): is one if Begining of period of payments is selected otherwise 0.
Download Library ver 1.01
Download User Manual...

Example:

// Initialize Financial object
const finance = new Financial(1000, 5000, null, 10, 5);
// Calculate Present Value
let pvResult = finance.calcPV();
if (pvResult.success) { console.log('Present Value:', pvResult.value);
console.log('Details:', pvResult.verbose); }
// Get Payment Plan as String
let paymentPlan = finance.getPaymentPlanAsString
console.log(paymentPlan); // Plot Payments
finance.plotPayments(document.getElementById('paymentCanvas'));
// Plot Financial Overview
finance.plotFinancialOverview(document.getElementById('overviewCanvas'));
May
2024

New Tool