CxTranslator.Net.x64
4.0.101
Math parser evaluator for complex numbers expressions
Install-Package CxTranslator.Net.x64 -Version 4.0.101
dotnet add package CxTranslator.Net.x64 --version 4.0.101
<PackageReference Include="CxTranslator.Net.x64" Version="4.0.101" />
paket add CxTranslator.Net.x64 --version 4.0.101
CxTranslator.Net is fast and compact math parser/evaluator with pre-build feature and user-friendly
interface. Implementing simple data type to cover complex numbers: real and imaginary parts
it uses recursive descent and operator precedence rules to parse and evaluate run-time
defined strings as math expressions.
Imag part in math expression should have postfix i or j. For example: 3.5j
The valid math expressions should consist of:
� Variables should start from letterand and be no more than 32 characters.The math
expression may have unlimited number of variables.
� Arithmetic operations : +, -, *, / , ^.
� Functions - set of predefined functions : abs, acos, acosh, arg, asin, asinh, atan, atanh,
conj, cos, cosh, exp, imag, log, log10, norm, proj, real, sin, sinh, sqrt,
tan, tanh.
Also, users can define their own function to improve performance and extend functionality.
The main characteristics of CxTranslator:
� Extremely fast.
� Friendly interface and error handling mechanism support.
� Safe Multithreading.
� Supports unlimited number of variables.
� Can be extended by user defined functions.
using System;
using System.Numerics;
using EMS.Math.Cx; //Namespace to use CxTranslator
namespace Cx.Net_Demo
{
class Program
{
//User defined function
static TCALC pow3(TCALC x)
{
//Must convert to Complex first
Complex cmplx = new Complex(x.real, x.imag);
//Do calculation
Complex tmp = cmplx * cmplx * cmplx;
//Transform result to TCalc data type
TCALC result;
result.real = tmp.Real;
result.imag = tmp.Imaginary;
return result;
}
unsafe static void Main(string[] args)
{
string mathExp = "x+2.3i*(1-sin(y/3.14)^2)-z";
string varList = "x,y,z";
TCALC[] data = new TCALC[3];
data[0].real = 1.2; data[0].imag = 2.3;
data[1].real = -3; data[1].imag = 0.005;
data[2].real = 0.5; data[2].imag = -0.123;
TCALC res = default(TCALC);
//Initialize CxTranslator algorithm before we can use any function.
Translator cx = new Translator();
try
{
/*********************************************************
Evaluate math expression for a given point "data"
Arguments:
mathExpression – math expression as string to be evaluated.
varList – coma separated list of named variable. For example”
“x,y,z”.
arguments – array of values for a given varList. First element
in the array corresponds to first named variable in varList,
second to second...
Return:
Result of evaluation.
*********************************************************/
res = cx.Evaluate(mathExp, varList, data);
data[0].real = 2.3; data[0].imag = 1.3;
data[1].real = -5; data[1].imag = 0.123;
res = cx.Evaluate("x/y+5-3.1j+(x^2-y^2)", "x,y", data);
/*********************************************************
Add user defined function to EquTranslator.
Arguments:
funcName - name of the function as string. This name must
be used in math expression.
pFunction - pointer to user defined function.
*********************************************************/
cx.AddFunction("p3", pow3);
//Calculate a set of points for one math expression
//Build it first
/*********************************************************
The function prepares math expression but doesn’t evaluate it.
It should be used when the same math expression must be evaluated
for a large set of points. If number of points is small
(not more than 10) function “Evaluate()” may work faster.
Arguments:
mathExpression – math expression as string to be parsed.
varList – coma separated list of named variable.
For example” “x,y,z”.
*********************************************************/
cx.Build("3.14*p3(x)+sin(y)-sqrt(z+x)", "x,y,z");
for (int i = 0; i < 100; i++)
{
data[0].real = data[0].real + 1; data[0].imag = i;
data[1].real = i / 200; data[1].imag = 0.005 * i;
data[2].real = -5; data[2].imag = data[0].real * 0.123;
//Do calculation for a given point
/*********************************************************
The function evaluates math expression prepared by “Build(…)”
for the given values.
Arguments:
arguments – array of values for a varList in “Build(…)”.
Return:
Result of evaluation.
*********************************************************/
res = cx.CalcFor(data);
}
//*********************************************************
// Memory for data must be pinned in order to use
// "Compile" and "Run" functions.
// Also, the code must defined as unsafe
TCALC* pinData = stackalloc TCALC[3];
/*********************************************************
The function prepares math expression but doesn’t evaluate it.
This method should be used when the same math expression must be
evaluated for a large set of points. If number of points is small
(not more than 5) function “Evaluate()” may work faster.
Arguments:
mathExpression – math expression as string to be parsed.
varList – coma separated list of named variable. For example” “x,y,z”.
arguments – array of values for a varList.
*********************************************************/
cx.Compile(mathExp, varList, pinData);
res = default(TCALC);
for (int i = 0; i < 10000; i++)
{
pinData[0].real = pinData[0].real + 0.0001; pinData[0].imag = i;
pinData[1].real = i / 2000; pinData[1].imag = i / 10000;
pinData[2].real = -5; pinData[2].imag = pinData[0].real * 0.0123;
//Do calculation for a given point
res = cx.Run();
}
//*********************************************************
//Negative test
/*********************************************************
Remove user defined function from EquTranslator.
Arguments:
funcName - name of the function as a string.
*********************************************************/
cx.RemoveFunction("p3");
res = cx.Evaluate("3.14*p3(x)+sin(y)-sqrt(z+x)", "x,y,z", data);
}
catch (TranslatorException ex)
{
Console.WriteLine(ex.ErrorCode);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ErrorPosition);
}
}
}
}
CxTranslator.Net is fast and compact math parser/evaluator with pre-build feature and user-friendly
interface. Implementing simple data type to cover complex numbers: real and imaginary parts
it uses recursive descent and operator precedence rules to parse and evaluate run-time
defined strings as math expressions.
Imag part in math expression should have postfix i or j. For example: 3.5j
The valid math expressions should consist of:
� Variables should start from letterand and be no more than 32 characters.The math
expression may have unlimited number of variables.
� Arithmetic operations : +, -, *, / , ^.
� Functions - set of predefined functions : abs, acos, acosh, arg, asin, asinh, atan, atanh,
conj, cos, cosh, exp, imag, log, log10, norm, proj, real, sin, sinh, sqrt,
tan, tanh.
Also, users can define their own function to improve performance and extend functionality.
The main characteristics of CxTranslator:
� Extremely fast.
� Friendly interface and error handling mechanism support.
� Safe Multithreading.
� Supports unlimited number of variables.
� Can be extended by user defined functions.
using System;
using System.Numerics;
using EMS.Math.Cx; //Namespace to use CxTranslator
namespace Cx.Net_Demo
{
class Program
{
//User defined function
static TCALC pow3(TCALC x)
{
//Must convert to Complex first
Complex cmplx = new Complex(x.real, x.imag);
//Do calculation
Complex tmp = cmplx * cmplx * cmplx;
//Transform result to TCalc data type
TCALC result;
result.real = tmp.Real;
result.imag = tmp.Imaginary;
return result;
}
unsafe static void Main(string[] args)
{
string mathExp = "x+2.3i*(1-sin(y/3.14)^2)-z";
string varList = "x,y,z";
TCALC[] data = new TCALC[3];
data[0].real = 1.2; data[0].imag = 2.3;
data[1].real = -3; data[1].imag = 0.005;
data[2].real = 0.5; data[2].imag = -0.123;
TCALC res = default(TCALC);
//Initialize CxTranslator algorithm before we can use any function.
Translator cx = new Translator();
try
{
/*********************************************************
Evaluate math expression for a given point "data"
Arguments:
mathExpression – math expression as string to be evaluated.
varList – coma separated list of named variable. For example”
“x,y,z”.
arguments – array of values for a given varList. First element
in the array corresponds to first named variable in varList,
second to second...
Return:
Result of evaluation.
*********************************************************/
res = cx.Evaluate(mathExp, varList, data);
data[0].real = 2.3; data[0].imag = 1.3;
data[1].real = -5; data[1].imag = 0.123;
res = cx.Evaluate("x/y+5-3.1j+(x^2-y^2)", "x,y", data);
/*********************************************************
Add user defined function to EquTranslator.
Arguments:
funcName - name of the function as string. This name must
be used in math expression.
pFunction - pointer to user defined function.
*********************************************************/
cx.AddFunction("p3", pow3);
//Calculate a set of points for one math expression
//Build it first
/*********************************************************
The function prepares math expression but doesn’t evaluate it.
It should be used when the same math expression must be evaluated
for a large set of points. If number of points is small
(not more than 10) function “Evaluate()” may work faster.
Arguments:
mathExpression – math expression as string to be parsed.
varList – coma separated list of named variable.
For example” “x,y,z”.
*********************************************************/
cx.Build("3.14*p3(x)+sin(y)-sqrt(z+x)", "x,y,z");
for (int i = 0; i < 100; i++)
{
data[0].real = data[0].real + 1; data[0].imag = i;
data[1].real = i / 200; data[1].imag = 0.005 * i;
data[2].real = -5; data[2].imag = data[0].real * 0.123;
//Do calculation for a given point
/*********************************************************
The function evaluates math expression prepared by “Build(…)”
for the given values.
Arguments:
arguments – array of values for a varList in “Build(…)”.
Return:
Result of evaluation.
*********************************************************/
res = cx.CalcFor(data);
}
//*********************************************************
// Memory for data must be pinned in order to use
// "Compile" and "Run" functions.
// Also, the code must defined as unsafe
TCALC* pinData = stackalloc TCALC[3];
/*********************************************************
The function prepares math expression but doesn’t evaluate it.
This method should be used when the same math expression must be
evaluated for a large set of points. If number of points is small
(not more than 5) function “Evaluate()” may work faster.
Arguments:
mathExpression – math expression as string to be parsed.
varList – coma separated list of named variable. For example” “x,y,z”.
arguments – array of values for a varList.
*********************************************************/
cx.Compile(mathExp, varList, pinData);
res = default(TCALC);
for (int i = 0; i < 10000; i++)
{
pinData[0].real = pinData[0].real + 0.0001; pinData[0].imag = i;
pinData[1].real = i / 2000; pinData[1].imag = i / 10000;
pinData[2].real = -5; pinData[2].imag = pinData[0].real * 0.0123;
//Do calculation for a given point
res = cx.Run();
}
//*********************************************************
//Negative test
/*********************************************************
Remove user defined function from EquTranslator.
Arguments:
funcName - name of the function as a string.
*********************************************************/
cx.RemoveFunction("p3");
res = cx.Evaluate("3.14*p3(x)+sin(y)-sqrt(z+x)", "x,y,z", data);
}
catch (TranslatorException ex)
{
Console.WriteLine(ex.ErrorCode);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ErrorPosition);
}
}
}
}
Dependencies
This package has no dependencies.
Used By
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version History
Version | Downloads | Last updated |
---|---|---|
4.0.101 | 188 | 11/16/2019 |