complex-expression-parser
v2.1.0
Published
Parses and evaluates expressions over the complex numbers
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.jsores5/expression.min.jsbefore your scripts. - Include a shim such as es6-shim if
your target browser does not support common ES6 features such as extra methods
on
ArrayorNumber.
For node.js just run
npm install complex-expression-parserIf 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 exceptionNaming Symbols
Symbols can be any number of alphabetic characters followed by any number of digits or can be a single Unicode character.
Example symbols:
xyyM104☃\uD83D\uDE80
Example non-symbols:
i- Known constant2x- Interpreted as2 * xM104M- 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 ofxarg(x): The phase ofxceil(x): The ceiling ofxconj(x): The conjugate ofxexp(x): The exponential ofxfloor(x): The floor ofxfrac(x): The fractional part ofximag(x): The imaginary part ofxℑ(x): The imaginary part ofxlg(x): The log base 2 ofxln(x): The natural log ofxlog(base, x): The log basebaseofxlog10(x): The log base 10 ofxmod(x, y):xmodynint(x): The nearest integer ofxnorm(x): The norm ofxpow(base, power):baseraised to the power ofpowerreal(x): The real part ofxℜ(x): The real part ofxsqrt(x): The square root ofx
Trigonometric
arccos(x): The inverse cosine ofxarccosh(x): The inverse hyperbolic cosine ofxarccot(x): The inverse cotangent ofxarccoth(x): The inverse hyperbolic cotangent ofxarccsc(x): The inverse cosecant ofxarccsch(x): The inverse hyperbolic cosecant ofxarcsec(x): The inverse secant ofxarcsech(x): The inverse hyperbolic secant ofxarcsin(x): The inverse sine ofxarcsinh(x): The inverse hyperbolic sine ofxarctan(x): The inverse tangent ofxarctanh(x): The inverse hyperbolic tangent ofxcos(x): The cosine ofxcosh(x): The hyperbolic cosine ofxcot(x): The cotangent ofxcoth(x): The hyperbolic cotangent ofxcsc(x): The cosecant ofxcsch(x): The hyperbolic cosecant ofxsec(x): The secant ofxsech(x): The hyperbolic secant ofxsin(x): The sine ofxsinh(x): The hyperbolic sine ofxtan(x): The tangent ofxtanh(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 prepareAll changes must include appropriate tests.
License
Complex Expression Parser is open-sourced software licensed under the MIT license
