Exploring parametric ODE with TMS Analytics and Physics numerical tool

Thursday, July 13, 2017

Ordinary differential equations (ODE) used in many applications for physical, economical and other problem solution. The equations commonly describe the time evolution of some system from known initial state – initial value problem (https://en.wikipedia.org/wiki/Initial_value_problem).

The TMS Analytics and Physics pack contains the ODE solution tool of solving initial value problems for systems of ODEs. This tool is totally integrated with the symbolic capabilities of the library. This allows solving parametric problems and exploring how the parameter value influences the system behavior.

Let us consider the following simple parametric ODE:

with initial condition y(0)=1 on the time interval t=[0, 10]. The problem is parametric because there are parameters ‘A’ and ‘b’ in the equation. Our goal is exploring how the solution (function y(t)) depends on the value of ‘b’ parameter.

First we create the instance of analytical ODE object, here is the code:

  A, b: TVariable;
  prms: TArray<TVariable>;
  fv, equations: TArray<string>;
  ode: TAnalyticalODE;
  solver: TODESolverClass;
  y0: TArray<TFloat>;
  t: TArray<TFloat>;
  y: TArray<TArray<TFloat>>;
  i: Integer;
  A:= TRealVariable.Create('A', 1.0);    // 1
  b:= TRealVariable.Create('b', 0.0);    // 2
  prms:= TArray<TVariable>.Create(A, b); // 3
  fv:= TArray<string>.Create('y');       // 4
  equations:= TArray<string>.Create('sin(t)+A*t/y^b'); // 5
  ode:= TScalarODE.Create('t', fv, equations, prms);   // 6
Line 1: Create the variable for ‘A’ parameter.
Line 2: Create the variable for ‘b’ parameter.
Line 3: Create array of parameters.
Line 4: Create array of function names (one name required for the problem).
Line 5: Create array of equations (one equation).
Line 6: Create the instance of analytical ODE system with specified equation and parameters.

After the instance of the analytical ODE system created we must select appropriate solver and specify initial condition for the problem. We will use the Runge-Kutta direct solver of the 4-th order. The code continued:

solver:= TRungeKutta4Solver;          // 7
y0:= TArray.Create(1.0);      // 8
And finally we must solve the initial value problem for various values of ‘b’ parameter, say b=1..8:

for i:=1 to 8 do
  ode.Parameters['b'].Value:= TValue.From(i); // 9
  y:= solver.Solve(ode, y0, 10.0, 1000, t);           // 10
  // Code for using the result data ‘y’ and ‘t’.
Line 9: Change value of ‘b’ parameter using the array property ‘Parameters’ of the analytical ODE class. Line 10: Solving the specified initial value problem for current value of ‘b’ parameter, on the time interval t=[0, 10], with 1000 discretization steps.

The solution result for changing value of ‘b’ parameter presented on the picture below.

TMSFNCChart cross-platform / cross-framework chart displaying the result

As can be seen from the code above, there is no need creating new analytical ODE instance for exploring the parametric problem. As the analytical ODE is parametric, it is solved for the current parameter values and they can be easily changed via parametric interface.

Other advantages of using the numerical ODE tool with the symbolic calculus are:
- Minimal code writing, because there is no need to write special classes for the function method references (delegates) as in other numerical libraries.
- Convenient data representation for the developer and user as math expressions, not as programming code.
- Easily using the user input data (string data) for manipulations in programming code.
- Easily transfer input data via network as strings, easily storing and serializing the data.

The TMS Analytics and Physics pack includes the library with 5 ready-to-use numerical tools totally integrated with analytical features, such as symbolic derivatives. The trial version of TMS Analytics and Physics pack can be downloaded here. Total source code of the example application of ODE tool can be downloaded here.

Bruno Fierens


This blog post has not received any comments yet. Add a comment.

Function approximation with TMS Analytics and Physics numerical tools

Monday, June 19, 2017

The problem of data approximation (curve fitting, surface fitting) arises from many areas: economics, physics, informatics and many others. There are many libraries, realizing numerical tools for using the approximation in software applications. The TMS Analytics and Physics pack contains the approximation numerical tool which has one unique feature – it is totally integrated with symbolic calculus. Using symbolic expressions with the numerical approximation gives the developer many advantages.

Let us consider a simple case of using the approximation tool for curve fitting (one-dimensional approximation). For this case we have a set of points Pi=(xi,yi), i=1..N. From the programming point of view we just have two arrays of real values, say X and Y. The approximation problem is to build a function y=f(x) which is close to the set of points. The most popular approach of the approximation is the linear least squares (https://en.wikipedia.org/wiki/Linear_least_squares_(mathematics)). With TMS approximation tool the problem can be solved in five lines of code:

  approximator: TLinearApproximator;
  basis: TLinearBasis;
  variables: TArray;
  functions: TArray;
  cValues: TArray;
  variables:= TArray.Create('x'); // 1
  functions:= TArray.Create('e^x', 'sin(x)', 'e^-x', 'x^2', '3^x',   'sinh(2*x)'); // 2
  basis:= TLinearScalarBasis.Create(variables, 'C', nil, functions); // 3
  approximator:= TLinearLeastSquares.Create; // 4
  cValues:= approximator.Approximate(basis, X, Y); // 5
  // Using the basis with optimal coefficient values.
Line 1: Create the array of variable names, as this is 1D approximation, there is one variable name ‘x’ in the array.
Line 2: Create the array of approximation functions (basis functions). The functions only depend on the ‘x’ variable. The type and the number of the basis functions can be arbitrary.
Line 3: Create the basis instance with the specified arguments. The ‘C’ argument specifies the name of approximation coefficients. The ‘nil’ argument tells that there is no parameters in the functions.
Line 4: Create the appropriate approximator instance. For the linear least squares approximation it is ‘TLinearLeastSquares’;
Line 5: Calculate the approximation coefficients for the specified basis and arrays of points ‘X’ and ‘Y’.

After the optimal coefficients found, we can evaluate the basis in any point and make other analytical manipulations. As example, here is the plotted approximation function with the original data points on the picture.

The main feature of the TMS numerical tools is they are totally integrated with the symbolic calculus. It brings the following advantages to the developer:
- Minimal code writing, because there is no need to write special classes for the function method references (delegates) as in other numerical libraries.
- Convenient data representation for the developer and user as math expressions, not as programming code.
- Easily using the user input data (string data) for manipulations in programming code.
- Almost unlimited choice of math expressions for manipulations, including special functions.
- Easily transfer input data and the output results via network as strings, easily storing and serializing the data.

The result of the approximation can be easily used with other numerical tools. For example, we can analyze the approximation function for the roots and special points – extremums. The code for the analysis is the following:
  f: string;
  sf1D: TSymbolicFunction1D;
  opt: TSolverOptions;
  points: TArray;
  f:= basis.Evaluate(cValues); // 1 
  sf1D:= TSymbolicFunction1D.Create('x', f); // 2 
  opt:= TSolverOptions.Create(true);  // 3
  points:= TFunctionAnalyser.Analyse(sf1D, TNewton, opt, -2, 3, 10); // 4 
  // Using the points array.

Line 1: Make the function expression with optimal coefficient values.
Line 2: Create the symbolic function instance for the analysis.
Line 3: Create default options for the nonlinear solver (which used for the function analysis).
Line 4: Finding all function's special points on the interval [-2..3]. The Newton’s method used for finding roots and extremums of the function which specified with the second argument.

The result of the analysis – function’s roots and extremums, presented on the picture below.

The TMS Analytics and Physics pack includes the library with 5 ready-to-use numerical tools totally integrated with analytical features, such as symbolic derivatives. The version can be downloaded from the product page. There is the source code example for all numerical tools with the new version download.

Bruno Fierens


This blog post has received 2 comments. Add a comment.

Introducing functional derivatives is easy

Monday, March 20, 2017

The unique feature of Analytics library is symbolic derivatives calculation. The library provides the simplest way to get a symbolic derivative expression for almost any complicated expression. There are predefined derivative for all standard functions: trigonometric, hyperbolic, logarithmic and exponential, inverse trigonometric and hyperbolic, and other.

Another fine thing is that any developer can easily define derivative for any other function. Let us consider how to define derivative for special Gamma function (https://en.wikipedia.org/wiki/Gamma_function#General). Its derivative is defined via another special function – Digamma (Polygamma function of order 0, https://en.wikipedia.org/wiki/Polygamma_function). Hereafter the Gamma function denoted as ‘G’ and the Digamma (and Polygamma) as ‘Y’. Then, the derivative formula is: d(G(x))/dx = G(x)*Y(x).

To introduce new functional derivative new class must be implemented. In our case, the class is inherited from ‘TSimpleFunctionalDerivative’ because the function has one argument only. Here is the code of the realized class:

TGammaDerivative = class sealed(TSimpleFunctionalDerivative)
  function GetFunctionName(): string; override;
  function BaseDerivative(argument: TBaseExpression): TBaseExpression; override;
  class function IsRealized: boolean; override;

function TGammaDerivative.GetFunctionName: string;
  result:= 'G';

class function TGammaDerivative.IsRealized: boolean;
  result:= true;

function TGammaDerivative.BaseDerivative(argument: TBaseExpression): TBaseExpression;
  ge: TBaseExpression;
  dge: TBaseExpression;
  ge:= TFunctionExpression.CreateSimple('G', argument);
  dge:= TFunctionExpression.CreateSimple('Y', argument);

  result:= TProductExpression.MakeProduct(ge, dge);
As can be seen from the code, there are only three methods to override. The GetFunctionName method defines that the derivative is for function with name ‘G’. The IsRealized functions returns ‘true’, that means the class is totally realized and can be used by the Analytics system. And the last method defines the derivation algorithm itself. Namely, it creates the Gamma and Digamma functions with the same argument and, according to the derivative formula above, their product.

Now we can calculate derivatives for expressions with the Gamma function. For the simplest case with input string ‘G(x)’ we get the expected output string ‘G(x)*Y(x)’. No surprise here, because we just wrote the code, which produces the output.

But the amazing thing is when we trying to calculate the derivative expression for the Gamma function with a composite argument. As example, for the input string ‘G(x^2+1)’ it produces the output string ‘(G(x^2+1)*Y(x^2+1))*(2*x)’ which is totally correct for this case. And the output is correct for all the expressions with the Gamma function. It is because the Analytics library cares about other things, like implementing the ‘chain rule’ for derivation process.

The source code of the example project is available here.

Nancy Lescouhier


This blog post has not received any comments yet. Add a comment.

Weekend fun with Delphi, math and FNC

Sunday, December 11, 2016

Mathematics is fascinating, Delphi is cool, cross-platform is awesome, cross-framework is the icing on the cake. So, what's better than spending some time with all four on a weekend day?

For some time, we offer our TMS Analytics & Physics package that can do formula evaluation, complex number calculation and most interestingly also expression based derivative function calculation. Also since beginning of 2016, we offer our TMS FNC Chart, which is a chart package that offers a chart UI component that can be used in VCL applications, FMX applications and also LCL (Lazarus/FPC based) applications. This means that with the TMS FNC Chart, we can target Win32, Win64, macOS, iOS, Android, Linux, Raspbian operating systems.

So, I set out this weekend to spend some time creating a simple mathematical formula plotting application (cross platform & cross framework) that could not only display a function but also calculate its derivative function and plot it. This way, we can visually see the effect of calculating a derivative function.
The setup of this application turns out to be amazingly simple with some help of TMS FNC Chart and TMS Analytics & Physics Pack.

The core code is a model class that does chart initialization & formula calculation + derivative calculation by means of two class methods. This model can be used by the VCL, FMX or LCL application as apart from unit references, all code is framework independent. In this model, calculation is central and is nothing more than performing parsing of a mathematical formula and calculating it's derivative and Y-values for a range of X-values and display this in a chart. To do this, we initialize the TTranslator class (part of TMS Analytics) and specify the X is the main variable of the functions we can write and then we check the syntax, calculate the derivative and calculate values for both function and derivative function:
  FTransMain := TTranslator.Create;
  FTransDeriv :=  TTranslator.Create;

  vName := 'x';
  vValue := 1.0;
  FTransMain.Add(vName, vValue);
  FTransDeriv.Add(vName, vValue);

    if FTransMain.CheckSyntax(f) then
      df := FTransMain.Derivative(f,'x');

      if FTransDeriv.CheckSyntax(df) then
        for i := -xrange to +xrange do
          // get Y value of the function for a given X value
          FTransMain.Variables['x'].Value := i;
          v := FTransMain.Calculate(f);
          // add the Y point to the XY chart series 1

          // get Y value of the derivative function for a given X value
          FTransDeriv.Variables['x'].Value := i;
          v := FTransDeriv.Calculate(df);
          // add the Y point to the XY chart series 2
This is the core code responsible for coming to a result for a function like sin(x)+1/4*cos(x*10) that you can think of as mathematical basis for modulation techniques.

FMX version of the Math Explorer app running on macOS

Of course, the intention of the math exploring application is that you can enter any other mathematical function of X and not only see the derivative function but also plot it in the chart. Download & install TMS Analytics & Physics and TMS FNC Chart and then compile and run the VCL or FMX version of the math explorer application you can download with full source code.

Bruno Fierens


This blog post has not received any comments yet. Add a comment.

Introducing Sergey Gladkiy and TMS Analytics & Physics Pack

Monday, November 07, 2016

It's always a privilege and honour to be able to work with very smart people who are not only expert in Delphi but also expert in a specific domain. Earlier this year, Bernard Roussely and Marion Candau joined tmssoftware.com and focus so far on advanced cryptography algorithms with the highest security levels in TMS Cryptography Pack. More tools that will use this underlying strong technology are already in the pipeline. Around the same time, also Roman Yankovsky joined tmssoftware.com and the static code analysis tool TMS FixInsight is the result. With TMS FixInsight, you can bring your Delphi code to a higher quality & safer level with numerous code hints and suggestions it gives.
Today, I'm proud to announce Sergey Gladkiy joins the TMS team for working on products with a strong mathematical background. The first result is the TMS Analytics & Physics Pack. This pack offers a set of (non-visual) platform independent classes to work with mathematical expressions and units of measurements. In TMS Analytics & Physics Pack, you can do evaluation of complex multiparameter mathematical expressions, including expressions with complex numbers. You can do physical measurement conversions of all kinds. But perhaps the most amazing part is symbolic derivative calculations. In this respect, TMS Analytics & Physics Pack brings a math wizard in the form of classes that can be used in VCL and FMX apps. You can discover the power of this library via the included demo but I wanted to share some examples of symbolic derivative calculations that will boggle your mind:

Task: calculate the derivative function of:


The code to achieve this is as simple as:
   f,df: string;

   f := '1/(x-1)+(2*x-1)/(x+1)^2';

      if Translator.CheckSyntax(f) then
        ShowMessage('Derivative function :' + df);
Let's take another example with some trigonometric mathematical functions:

Task: calculate the derivative function of:


The resulting derivative expression can then be evaluated, for example against x=2:
   df: string;
   v: TValue;

   df := '((cos(x)+(-sin(x)))*x-(sin(x)+cos(x)))/x^2';

      Translater.Add('x', 2.0); 
      if Translator.CheckSyntax(df) then
        v :=Translator.Calculate(df);
        ShowMessage('Result :' + s:=TUtilities.SafeToString(v));
The result of this calculation is: -0,786009779256047.

I kindly invite you to explore our new TMS Analytics & Physics Pack. A fully functional trial version is available and the registered versions comes with full source code. We're eager to learn in what kind of exciting and cool applications these powerful classes will find a place!

Bruno Fierens


This blog post has received 6 comments. Add a comment.

Previous  |  Next  |  Index