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)
protected
  function GetFunctionName(): string; override;
  function BaseDerivative(argument: TBaseExpression): TBaseExpression; override;
public
  class function IsRealized: boolean; override;
end;

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

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

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

  result:= TProductExpression.MakeProduct(ge, dge);
end;
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


Bookmarks: 

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);

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

      if FTransDeriv.CheckSyntax(df) then
      begin
        for i := -xrange to +xrange do
        begin
          // 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
          AChart.Series[0].AddXYPoint(i,v.AsExtended);

          // 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
          AChart.Series[1].AddXYPoint(i,v.AsExtended);
        end;
      end;
    end;
  finally
    FTransMain.Free;
    FTransDeriv.Free;
  end;
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


Bookmarks: 

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:
1/(x-1)+(2*x-1)/(x+1)^2

Result:
(-(1)/(x-1)^2)+(2*(x+1)^2-(2*(x+1)^(2-1))*(2*x-1))/((x+1)^2)^2

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

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

   try
      if Translator.CheckSyntax(f) then
      begin
        df:=Translator.Derivative(f,'x');
        ShowMessage('Derivative function :' + df);
      end;
   except
      ShowMessage('Error');
   end;
Let's take another example with some trigonometric mathematical functions:

Task: calculate the derivative function of:
(sin(x)+cos(x))/x

Result:
((cos(x)+(-sin(x)))*x-(sin(x)+cos(x)))/x^2

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

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

   try
      Translater.Add('x', 2.0); 
      if Translator.CheckSyntax(df) then
      begin
        v :=Translator.Calculate(df);
        ShowMessage('Result :' + s:=TUtilities.SafeToString(v));
      end;
   except
      ShowMessage('Error');
   end;
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


Bookmarks: 

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




Previous  |  Next  |  Index