Software … powerful tools for your research & development!

How Mathematical Expressions Are Treated in the Script Language of RP Photonics Software

Dr. Rüdiger Paschotta

Today, I would like to give you some insight on how our software works internally in the context of mathematical expressions, handled by our script language. Although as a user you do not need to know such things, it may be interesting to look behind the scenes.

When you generate some plot, you might use some code like the following e.g. in RP Fiber Power:

diagram 1:

"Amplifier Gain vs. Pump Power"

x: 0, 400
"pump power (mW), @x
y: 0, 30
"amplifier gain (dB)", @y

f: (set_P_in(pump, x * mW); 10 * lg(P_out(signal) / P_s_in))

The last line defines the plot, which is based on a mathematical expression. For each <$x$> position, its evaluation will imply that the pump power is set, the new signal output power is calculated, and the gain in decibels is calculated from that power.

But how is that really done? Obviously, the program must be able to read mathematical expressions, for example, understand their structure, store them in some suitable form, and evaluate them later on as needed. What is really does:

tree structure for a mathematical expression
  • When a mathematical expression is read, its details are stored in the form of a tree structure. For example, if the expression is sin(pix), the expression has a node for the sine function. That function, having a single argument, points to a “” node (multiplication). That one has two operands, i.e., points to two other nodes: one with the constant <$\pi$>, another one with the variable x. Here, x is actually a parameter, the value of which will be stored on a stack (rather than the table of global variables). See the illustration on the right side.
  • Later on, when the plot is made, that structure can be evaluated efficiently. For each x value, the corresponding value is put on the stack. Then, the “sin” node is asked to return its result. Of course, it takes the value of is argument, calculates the sine, and returns that. Its argument, the “*” node, itself calls for the two operand values and returns their product.

Of course, that was only a simple example. You can imagine that there are more complicated types of nodes – for example, for functions with multiple arguments and parameters (doing integrations), blocks of expressions with constants and local variables, etc. There are around 130 standard functions for the arithmetics. In addition, there are many additional predefined functions for the physics. In RP Fiber Power, for example, there are 247 functions, for example for defining and modifying a fiber amplifier or laser model and for retrieving calculated results.

You can easily imagine that it takes some time to implement all that – and why our competitors don't just equip their products with a script language as well! It surely takes more than Friday afternoon to do such a thing. However, the script language is the key for absolutely flexible use of our software: the end user can program anything himself or herself, without having to deal with the source code. For example, the user can define any kind of plot – not only select one of those which we anticipated.

You may wonder how the speed of script evaluation compares with direct programming, e.g. with Delphi (the development system we use). When some complex mathematical expressions are evaluated, it is several times slower. However, note that most of the computation time is often spent within predefined functions, where plain Delphi-generated code is executed. For example, one may need some Fourier transforms (FFT algorithm) in a script, and these are of course not implemented with script code, but with a predefined function, which works quite efficiently. Therefore, the speed of execution is in practice often not much lower than if you would implement all the things directly with Delphi, C++ or C#. And your speed and efficiency is certainly much higher. This is what counts in the end, of course.

This article is a posting of the RP Photonics Software News, authored by Dr. Rüdiger Paschotta. You may link to this page, because its location is permanent.

Note that you can also receive the articles in the form of a newsletter or with an RSS feed.

Questions and Comments from Users

Here you can submit questions and comments. As far as they get accepted by the author, they will appear above this paragraph together with the author’s answer. The author will decide on acceptance based on certain criteria. Essentially, the issue must be of sufficiently broad interest.

Please do not enter personal data here. (See also our privacy declaration.) If you wish to receive personal feedback or consultancy from the author, please contact him, e.g. via e-mail.

Spam check:

By submitting the information, you give your consent to the potential publication of your inputs on our website according to our rules. (If you later retract your consent, we will delete those inputs.) As your inputs are first reviewed by the author, they may be published with some delay.


Share this with your network:

Follow our specific LinkedIn pages for more insights and updates: