How to use dotMath


The following page is focused on understanding how to use dotMath in your application and get up and running in the shortest amount of time.

If you are using the compiled assembly library, be sure to create a reference to the .Math library and include the library DLL in your distribution package.

Scenario 1: Evaluating an Expression

In order to take an expression without variables stored in a string – sExpression – and evaluate its value we need the following set of code.

sExpression = “4+3”;
EqCompiler oCompiler = new EqCompiler( sExpression, true);
oCompiler.Compile();
double dValue = oCompiler.Calculate();



If you do not perform the ‘.Compile()’ step, it will be executed automatically the first time you perform ‘.Calculate();’. Wherever the compile step is performed it is important to place a try..catch block around it to capture any compilation errors. All errors raised by the compilers are ApplicationExceptions.


Scenario 2: Evaluating an Expression with Variables



Taking advantage of expressions with variables involves discovering the variables and setting them before performing the .Calculate() step.

sExpression = “a+b”;
EqCompiler oCompiler = new EqCompiler( sExpression, true);
oCompiler.Compile();

string[] asVars = oCompiler.GetVariableList();

// At this point we have the variable list and we can do what we want.
// Here, we’ll set the values of each variable using the interface…

double dValue = 1.0;
foreach (string sVar in asVars)
{
       oCompiler.SetVariable( sVar, dValue );
       dValue = dValue +1.5;    
}

// at this point we’re ready to calculate the expression with our declared variable values.

double dValue = oCompiler.Calculate();


Scenario 3: Adding user-defined functions to the compiler’s syntax.



The .Math library allows the user to extend the syntax of functions by creating classes and registering them with the compiler. In this case, we’ll create a function called Pythag(a,b) where given the length of side a and the length of side b, we’ll return side c (assuming we’re dealing with a right triangle).

First, we’ll create our class:

using dotMath;
/// <summary>
/// CTanh class: Returns the hyperbolic tangent of the supplied angle.
/// </summary>

public class CPythag : EqCompiler.CFunction 
{
         ArrayList m_alValues;

         public CPythag( )       
         {

         }
 
         /// <summary>
         /// CreateInstance: this function is used to create
         /// and return an object for evaluation of Pythag(a,b)
         /// </summary>
         public override EqCompiler.CFunction CreateInstance( ArrayList alValues ) 
         {
                  CPythag oPythag = new CPythag();
                  oPythag.SetValue( alValues );

                  return oPythag;
         }
 
         /// <summary>
         /// SetValue(ArrayList): this function accepts an arraylist
         /// of CValue objects that represent the parameters passed.
         /// </summary>

         public override void SetValue( ArrayList alValues )
         {
                  /// CheckParms is a CFunction member that validates
                  // the number of parameters.  It throws an exception
                  // formatted for the client if a mismatch occurs.
                  CheckParms(alValues,2);
                  m_alValues = alValues;
         }

         /// <summary>
         /// GetValue(): returns the evaluated function value during
         /// the .Calculate process.
         /// </summary>

         public override double GetValue()
         {
                  EqCompiler.CValue oValue1 = (EqCompiler.CValue) m_alValues[0];
                  EqCompiler.CValue oValue2 = (EqCompiler.CValue) m_alValues[1];
                  return Math.Sqrt(Math.Pow(oValue1.GetValue(),2) + Math.Pow(oValue2.GetValue(),2));       }
 
         public override string GetFunction()
         {
                  return "pythag";
         }
}



When we’re ready to evaluate the function, the following code enables the function:

string sFunction = “pythag(2,4)”;
EqCompiler eq = new EqCompiler( txtFunction.Text.Trim(),true);
eq.AddFunction( new CPythag() );

Last edited Oct 26, 2006 at 2:49 AM by sdhebert, version 3

Comments

No comments yet.