FNC Conversion Components in TMS Analytics & Physics 3.3

From version 3.3, TMS Analytics & Physics library contains several FNC components for converting math formulae and units of measurement to special text formats. In this article, we’ll consider how to do the conversion and how then use the converted strings in Delphi applications. 

Let’s begin with formula converters:

  • TFNCFormula2PlainConverter – converts a formula to plain text.
  • TFNCFormula2TeXConverter – converts a formula to TeX format.

The TFNCFormula2PlainConverter component has the following published properties:

  • Formula (string) – a formula to convert.
  • PlainFormula (string) – the formula in plain text format (read-only).
  • Valid (boolean) – the formula is valid for conversion (read-only).
  • Error (string) – description of the error if occurred during conversion (read-only).

The component provides a very simple functionality: when the ‘Formula’ property changes, the value is converted to plain text format and assigned to the ‘PlainFormula’ property. 

What is the plain text format and when it could be useful? Plain text format just presents a formula as a string using special Unicode characters to make it closer to natural math notation. For example, the formula ‘x^2’ can be written with the superscript symbol as ‘x2’. In the following picture, an example of such conversion is presented.

TMS Software Delphi  Components

Here we should note that not all formulae can be presented in this format. If a formula is not valid for the conversion, an error message will be shown with the ‘Error’ property.

Now let’s use this converter to make a fine math application. In one of the previous articles, we explained how to use curve fitting FNC math components. In the following picture, we showed an example of data approximated with a Taylor series.
 TMS Software Delphi  Components

On the FNC chart, we showed the formula of the fitted curve in the legend. Let’s use the converter and improve the design of the application. We just need to write a small piece of code:

procedure TForm1.ShowCurveLegend(const formula: string);
begin
  FNCFormula2PlainConverter1.Formula:= formula;
  TMSFNCChart1.Series[1].LegendText:= FNCFormula2PlainConverter1.PlainFormula;
end;
procedure TForm1.FNCApproxFunction1D1FormulaChanged(Sender: TObject; const formula: string);
begin
  ShowCurveLegend(formula);
end;

When a formula of the approximated function is changed, we convert it to plain text format and show the formula on the chart. The resulting form is presented in the picture below.

TMS Software Delphi  Components

As one can see, the formula of the fitted curve looks like a natural math expression, without superfluous symbols, like power operator ‘^’ and indexing brackets ‘[]’. One more example of the curve, fitted with a Fourier series, is shown in the following picture.

 TMS Software Delphi  Components
The full source code of the demo project is available with the latest version of TMS Analytics & Physics library. 

We can use the same approach to present units of measurement in the form of natural physics notation with the TFNCUnit2PlainConverter component. An example of such conversion is shown in the picture below.

 TMS Software Delphi  Components
The next component TFNCFormula2TeXConverter has similar functionality, but it converts formulae to TeX format. This format is not for human reading, so, we need a drawing library to display formulae in natural math notation. We’ll use the MathJax library, as described in this article, together with the TTMSFNCWebBrowser component. Here is a simple code for converting a formula to TeX format and showing it in the browser component:
procedure TForm1.DrawFormula(const formula: string);
var
  texf, htmlcode: string;
begin
  FNCFormula2TeXConverter1.Formula:= formula;
  texf:= FNCFormula2TeXConverter1.TeXFormula;
  htmlcode:= GetTeXHTML(texf);
  DrawTeXInBrowser(htmlcode);
end;

Thus, we have a simple application to display math formulae. An example of a formula drawing is shown in the picture below.

 TMS Software Delphi  Components
Using FNC math components, described in this article, we can evaluate derivatives and integrals of functions and then draw them with our application. The resulting formulae are shown in the following pictures.

 

TMS Software Delphi  Components
TMS Software Delphi  Components
You can download the full source code of the demo project from here . To start developing your own FNC math application you need the latest versions of TMS Analytics & Physics and TMS FNC Chart products.