complex-expression-parser
v2.1.0
Published
Parses and evaluates expressions over the complex numbers
Downloads
11
Maintainers
Readme
Complex Expression Parser
Complex Expression Parser parses mathematical expression strings over the field of complex numbers.
Installation
If you want to use it in a browser:
- Just include
es5/expression.js
ores5/expression.min.js
before your scripts. - Include a shim such as es6-shim if
your target browser does not support common ES6 features such as extra methods
on
Array
orNumber
.
For node.js
just run
npm install complex-expression-parser
If you are running in an environment that supports ES6 then you may begin using
Expression
in your code using
import Complex from './es6/complex.js';
import Expression from './es6/expression.js';
ES6 environments can use the Complex and ComplexMath classes directly by importing their respective files.
Usage
Expression
has a single method: evaluate
.
Construction
Expression
takes a single string as an argument to its constructor. This
string should be a valid mathematical expression. Any symbol which is not known
to the parser is interpreted as a variable. If the expression is not
syntactically valid then the constructor throws an exception.
// Create an expression for the equation 2x^2 + 3x - i where i is the
// imaginary constant
const expression = new Expression('2x * x + 3x - i');
evaluate
evaluate
takes a single, optional dictionary of symbols and their values and
returns the value of the expression as a Complex
. If the expression has any
unset variables then this method throws an exception.
const expression = new Expression('2x + i');
const valueA = expression.evaluate({'x': new Complex(2, 4)}); // valueA is 6 + 9i
const valueB = expression.evaluate({'x': 2}); // valueB is 4 + i
const noValue = expression.evaluate(); // throws exception
Naming Symbols
Symbols can be any number of alphabetic characters followed by any number of digits or can be a single Unicode character.
Example symbols:
x
yy
M104
☃
\uD83D\uDE80
Example non-symbols:
i
- Known constant2x
- Interpreted as2 * x
M104M
- Interpreted asM104 * M
☃☃
- Interpreted as☃ * ☃
\uD83D\uDE80\uD83D\uDE80
- Interpreted as\uD83D\uDE80 * \uD83D\uDE80
Supported Functions and Constants
Constants
e
: Euler's constanti
: The imaginary unitpi
: The ratio of a circle's circumference to its diameter
Functions
Arithmetic
+
(unary): The identity function+
(binary): Addition-
(unary): Negation-
(binary): Subtraction*
: Multiplication/
: Division
Algebraic
abs(x)
: The magnitude ofx
arg(x)
: The phase ofx
ceil(x)
: The ceiling ofx
conj(x)
: The conjugate ofx
exp(x)
: The exponential ofx
floor(x)
: The floor ofx
frac(x)
: The fractional part ofx
imag(x)
: The imaginary part ofx
ℑ(x)
: The imaginary part ofx
lg(x)
: The log base 2 ofx
ln(x)
: The natural log ofx
log(base, x)
: The log basebase
ofx
log10(x)
: The log base 10 ofx
mod(x, y)
:x
mody
nint(x)
: The nearest integer ofx
norm(x)
: The norm ofx
pow(base, power)
:base
raised to the power ofpower
real(x)
: The real part ofx
ℜ(x)
: The real part ofx
sqrt(x)
: The square root ofx
Trigonometric
arccos(x)
: The inverse cosine ofx
arccosh(x)
: The inverse hyperbolic cosine ofx
arccot(x)
: The inverse cotangent ofx
arccoth(x)
: The inverse hyperbolic cotangent ofx
arccsc(x)
: The inverse cosecant ofx
arccsch(x)
: The inverse hyperbolic cosecant ofx
arcsec(x)
: The inverse secant ofx
arcsech(x)
: The inverse hyperbolic secant ofx
arcsin(x)
: The inverse sine ofx
arcsinh(x)
: The inverse hyperbolic sine ofx
arctan(x)
: The inverse tangent ofx
arctanh(x)
: The inverse hyperbolic tangent ofx
cos(x)
: The cosine ofx
cosh(x)
: The hyperbolic cosine ofx
cot(x)
: The cotangent ofx
coth(x)
: The hyperbolic cotangent ofx
csc(x)
: The cosecant ofx
csch(x)
: The hyperbolic cosecant ofx
sec(x)
: The secant ofx
sech(x)
: The hyperbolic secant ofx
sin(x)
: The sine ofx
sinh(x)
: The hyperbolic sine ofx
tan(x)
: The tangent ofx
tanh(x)
: The hyperbolic tangent ofx
Special
gamma(x)
: The gamma ofx
Γ(x)
: The gamma ofx
Contributing
Submit a pull request and mail [email protected].
Development should occur against the ES6 files. Build and test using
npm run prepare
All changes must include appropriate tests.
License
Complex Expression Parser is open-sourced software licensed under the MIT license