15,312,400 members
Articles / Programming Languages / XML
Article
Posted 10 Mar 2019

13.1K views
15 bookmarked

# Drawing and Evaluating Math Formulae in WPF Applications

Rate me:
The article describes a simple way of realizing math formula evaluation and drawing in WPF applications.

## Introduction

Engineering and math modeling applications are an important part of modern software. These applications use extremely complicated math and require presenting results in the form of charts, schemes, 3D models, formulae. The result presentation must be simple to perceive and understand. Therefore, developing math and engineering applications with appropriate GUI is an essential task. WPF technology is one of the suitable GUI developing frameworks for solving the task. There are many free and commercial components for presenting scientific results in beautiful and proper form.

Working with math formula is one of the possible requirements for scientific applications. There are several common scenarios of dealing with formulae. The most frequently used are: evaluating formula value and draw formulae in natural math form.

The article describes a simple way of realizing math formula evaluation and drawing in WPF applications using two available C# libraries. The first library is used for drawing formulae in natural math form. The second evaluates formula value. Using the libraries together allows creating simple ‘formula calculator’ which evaluates string expressions and shows the result as math formula.

## Drawing Math Formulae

We will use the WPF Math project for drawing math formula. This .NET library realizes rendering math expressions, written in TeX format. The TeX format is widely used for creating scientific articles and documents. For drawing math formula, there is one simple WPF component ‘FormulaControl’ with string property ‘Formula’. Assigning value to the property the formula is rendered on the component.

For example, assigning this value ‘\frac{(2n-1)!}{2^{n+1}} \sqrt{\frac{\pi}{a^{2n+1}}}’ to the property draws math formula as follows:

$\frac{(2n-1)!}{2^{n+1}} \sqrt{\frac{\pi}{a^{2n+1}}}$

Another example is the next TeX expression ‘{A}\cdot{sin\left({n\cdot x}\right)}+\frac{B}{2} \cdot {e^{m \cdot y}}’ which produces the following formula in math form:

${A}\cdot{sin\left({n\cdot x}\right)}+\frac{B}{2} \cdot {e^{m \cdot y}}$

As one can see from the examples above, all formulae drawn in natural math form that is easy to read and understand. One small drawback of such way of writing formula for drawing is that it requires knowing of TeX expression format. The format is like scripting language and uses special commands and lexemes to define math symbols like fraction, square root, sum, product, integral, derivative and so on. The advantage of the format is that all formula can be written as plain text without using any ‘non-keyboard’ symbols and special editors.

## Evaluating Math Expressions

We will use Analytics project (http://sergey-l-gladkiy.narod.ru/index/analytics/0-13) for evaluating math expressions. The ANALYTICS framework is a general purpose symbolic library. The main functionality of the library is evaluating symbolic (analytical) math expressions. For example, there is a simple code for dealing with math expressions:

C#
Translator t = new Translator();

string f = "A*ln(Pi*x)";
double y1 = (double)t.Calculate(f);
t.Variables["x"].Value = 27.3;
double y2 = (double)t.Calculate(f);

Here, the Translator is one of the main classes of the Analytics framework that realizes most of the symbolic capabilities. The code adds two variables to the Translator instance: ‘A’ and ‘x’. Then it evaluates the symbolic expression using current variable values, changes the value of the ‘x’ variable and evaluates the same expression again.

As can be seen from the example above, the analytics library provides simple interface to evaluate math expressions. The expressions written as simple C# strings and can contain literals (constants), variables, math operators, functions. The framework supports all algebraic operators (addition, multiplication, power and so on), comparison operators (equality, inequality, less than, greater than and so on) and logical operators (not, and, or). Some special math operators also supported, such as sum (), product (), square root () and other. These special operators written in expressions as Unicode symbols and their notation is close to the equivalent math formula. One drawback of using these special symbols is that they must be provided by special editor.

The format of expressions in the Analytics framework is closer to math expressions than the TeX expressions are. Nevertheless, this is not natural format math formulae should be written with. For example, power operation for the Analytics expression must be written as ‘x^y’, which is not what we expect for presenting equivalent math formula  xy .

## Realizing Simple Formula Calculator

Therefore, we have one .NET library for evaluating math expressions and one WPF component for drawing math expressions as natural math formula. And we would like to build simple ‘formula calculator’ that would evaluate math expression and show the result as math formula.

The only problem left is that the evaluation library Analytics works with expressions written with its own format and the drawing library WPF Math requires formula in TeX format. Fortunately, the Analytics library contains special classes to convert internal formulae into the TeX format. Here is an example code for such conversion:

C#
string f = "A*sin(n*x)+B/2*e^(m*y)";
AnalyticsConverter converter = new AnalyticsTeXConverter();
string texf = "";
try
{
texf = converter.Convert(f);
}
catch (Exception ex)
{
// Show exception message...
}
// Using string in TeX format...

In the code above, an instance of the AnalyticsTeXConverter class created. This class has the Convert method for converting symbolic expressions to the TeX format. For the expression, result string in TeX format is the following:

{{{A}\cdot{{sin}\left({{n}\cdot{x}}\right)}}+{{\frac{B}{2}}\cdot{{e}^{{m}\cdot{y}}}}}

Now we have all ingredients for realizing our simple ‘formula calculator’. The main functionality of formulae evaluation and drawing can be realized with one method as follows:

C#
string f = inputTextBox.Text;
try
{
if (translator.CheckSyntax(f))
{
object v = translator.Calculate(f);

string texf = converter.Convert(f);
string vs = Utilities.SafeToString(v);

formulaControl.Formula = texf + " = {" + vs + "}";
}
}
catch (Exception ex)
{
formulaControl.Formula = ex.Message;
}

The simple WPF application for formulae evaluation and drawing is presented in the picture below:

This application allows input math expression written in Analytics format, evaluate its value and draw the result as math formula.

There are some other examples of formulae that can be evaluated and drawn with the application.

• Multiplication of fractions ‘(x-2)/2*(x+1)/A/B*(x-A)*(x+B/2)’:

• Parametric (logarithmic) functions ‘A*log{3}(x+m)-B/log{2}(x+n)’:

• Complicated formula ‘A*x^2/(m-1)!+B*e^sin(y+1)/n!+C*log{n+m}(z^2)

As one can see from the examples above, all formulae drawn in natural math format. The presented approach can be used in various engineering and math applications where results of evaluated expressions must be shown as math formulae.

It should be noted here that the same approach was used in early versions of Computer Algebra Systems (CAS). For example, Maple system used ‘console’ input, where the expressions must be written as plain text, but the result of evaluation was presented as math formula. Now CAS systems use interactive editors to write all math formulae in natural form. Realization of such interactive editor could be the next developments of the libraries used here.

## Conclusions

This article introduced one approach for realizing formulae evaluation and drawing in WPF applications. Two libraries were used: one for math expression evaluation and one for formulae drawing. The approach demonstrated with simple WPF application which allows input math expression as plain text and then evaluate value of the expression and draw the result as math formulae. The same approach can be used in other math end engineering applications where results of math formulae evaluation should be presented in proper and readable form.

## History

• 10th March, 2019: Initial version

## Share

EDUCATION:

Master’s degree in Mechanics.

PhD degree in Mathematics and Physics.

PROFESSIONAL EXPERIENCE:

15 years’ experience in developing scientific programs
(C#, C++, Delphi, Java, Fortran).

SCIENTIFIC INTERESTS:

Mathematical modeling, symbolic computer algebra, numerical methods, 3D geometry modeling, artificial intelligence, differential equations, boundary value problems.

 First Prev Next
 Helpful Member 135126018-Mar-21 18:51 Member 13512601 8-Mar-21 18:51
 Thank you
 drawing formula as a string not image Member 1045598613-May-20 16:46 Member 10455986 13-May-20 16:46
 Re: drawing formula as a string not image Sergey L. Gladkiy31-Jul-20 4:57 Sergey L. Gladkiy 31-Jul-20 4:57
 helpful oyy_ou25-Mar-19 2:36 oyy_ou 25-Mar-19 2:36
 Very Useful Matthew C Schmidt11-Mar-19 12:00 Matthew C Schmidt 11-Mar-19 12:00
 Last Visit: 31-Dec-99 18:00     Last Update: 26-May-22 14:24 Refresh 1