math-analysis
v0.2.3
Published
Tools for Mathematical Analysis
Downloads
10
Maintainers
Readme
MATH-ANALYSIS
MATH-ANALYSIS can be used to perform various operations pertaining to mathematical analysis such as (symbolic) differentiation, (numeric and symbolic) integration etc.
Installation
$ npm install math-analysis
Usage
var ma = require('math-analysis');
ma
exposes the following functions:
createFunction (fctSpec)
Creates a function (object) from the given string specification. The specification follows standard syntax, using the following built-in functions:
sqrt(x)
: square root of xlog(x)
: natural logarithm (basis e)exp(x)
: e to the power of xpow(x,y)
: x to the power of ysin(x)
: trigonometric function sinecos(x)
: trigonometric function cosinePI
: constant π
Note: all built-in functions are computed through the respective function offered by Math
, i.e.
Math.sqrt
, Math.log
etc.
Example:
var f = ma.createFunction('2.1 * sqrt(x + 1/x)');
The function object created, henceforth denoted by f, has the following methods:
eval(x)
Returns the function value f(x) for the given x
.
differentiate()
Returns a function (object) that represents the (symbolically) differentiated f or the derivative of f.
integral(a,b,s)
Computes the integral of f over the interval [a;b].
a
and b
must be provided and must be numbers. Otherwise the return value is undefined.
The integral is computed as F(b) - F(a) for the antiderivative F of f if such
an F can be determined (see integrate). Otherwise a numeric approximation of the integral is computed.
In the case of a numeric approach
parameter s
defines the number of sub-intervals into which [a;b] is "chopped"
to approximate the integral. This parameter is an optional integer number
not less than 1. If it is not supplied or invalid the default 1000 is used. The bigger the value
of s
the more accurate the approximation. It will nonetheless remain an approximation. So there is
no guarantee that the approximation and the exact value will be the same.
Note that the value of the approximation will be undefined if NaN
was encountered
when computing the approximation. Keep in mind, however, that the existence of an x in [a;b]
for which the function is undefined does not necessarily mean that NaN
is indeed encountered.
Depending on a and parameter s
such an x may be "skipped".
integrate()
Returns the antiderivative of f (as a function). Computation of an antiderivative may not always succeed. If it fails the return value is undefined, which does not necessarily mean that there is no (known) antiderivative. The procedure for computing the antiderivative implemented here has limitations that will be lifted to a certain extent with future versions, but will (or can) never be fully removed. (See also integral).
rewrite()
Returns a function that is a simplified version of f. For instance,
x + 0
becomes x
and (1+2)*x - x
becomes 2 * x
etc.
Note that rewriting may modify f. That is, rewriting does not
return a rewritten copy of f, but rather operates on f itself.
Thus, the return value can be ignored as it is merely a pointer to f itself
that may come in handy for method-chaining.
Hence it is always true that f === f.rewrite()
.
If for some reason you want to retain f as it was before rewriting simply store
its string representation and, if required, apply ma.createFunction
to the string
to obtain a function object again.
Example
var f = ma.createFunction('x + 0');
var s1 = f.toString();
f.rewrite();
var s2 = f.toString();
Having executed the above code the two string variables s1
and s2
hold the
strings 'x + 0'
and 'x'
, respectively.
toString()
Returns the string representation of the function.
lookupTable (fct, from, to, step)
Creates a look-up table for the given function fct
with values for x
ranging from from
to to
using the given step size step
.
fct
must have been created with createFunction
. The other
three parameters are also mandatory and must be of type number
.
Furthermore, step
must be greater than 0
and from
must not
be greater than to
. If any of these prerequisites are not met
the return value of this function is undefined.
Example:
var f = ma.createFunction('2*x+1');
var t = ma.lookupTable(f,-1,2.1,0.5);
As a result t
is
[{x:-1,y:-1},{x:-0.5,y:0},{x:0,y:1},{x:0.5,y:2},{x:1,y:3},{x:1.5,y:4},{x:2,y:5}]
.