Rapid Prototyping of Industrial Applications with the Wolfram Language

Stefan Janecek, 
uni software plus GmbH

ABSTRACT

The Wolfram language is the perfect tool for rapidly building prototypes for industrial applications: its symbolic nature allows to quickly capture almost any problem, concept or new idea in a succinct, unified way, making it available for computation. The wide range of functions and built-in knowledge allows the developer to concentrate on the goals he wants to achieve, without wasting time on technical details. Mathematica’s notebook interface and visualization capabilities foster iterative development and short feedback loops.

In this talk, we will show real-world examples of industrial application prototypes we developed during the past few years in very different areas, such as electrical engineering (designing transformers), material science (density functional theory) and computational finance. Our focus is on highlighting the path from the idea to the solution, with surprisingly few lines of code.

This Talk

Showcase real prototypes we (uni software plus GmbH) have done for customers

I decided against "dumbing down" the code for presentation

Mathematica provides a unified way to talk about problems

About

Who we are: uni software plus GmbH

Company Facts

Based in upper Austria (close to Linz)

19 employees (3 theoretical physicists, 1 experimental physicist, 10+ software engineers, 1 mechanical engineer)

About

Business Areas

Industrial mathematics (simulation and modeling), data science (“big data”), industry automation

Financial mathematics (UnRisk product line)

Austrian Wolfram Reseller: Austrian Universities, Fraunhofer Gesellschaft Germany, Uni Zürich

Local Mathematica Evangelists

Support Experience

Beginnings are hard...

Beginners: how do I get started solving problem X?

Beginnings are hard...

Please keep in mind Mathematica is very simple!

a lot simpler than, e.g., C++ or Java

try not to blindly translate concepts learned in other languages

Solving Problems

No. 1 advice how to solve problems
 (not only in Mathematica, btw):

Don' t start with the solution, start by clearly describing the problem!

The Wolfram language is a symbolic language and perfect for that task!

Solving Problems

... go on by collecting "facts and relations you know about the problem"

In Mathematica, this is done in the form of Mathematica functions. make sure each function can "stand on its own" and describes one (just one!) piece of information about your problem

Expressions

Everything is an expression

In Mathematica, everything is an expression. Functions, mathematical formulas, lists, graphics, and even notebooks are represented and handled in a uniform way: all these things - and everything else in the world of Mathematica - is an expression.

Note there is no distinction between “a program” and “data” in Mathematica: the functions that make up your “program” are in every way expressions as the data imported from a .csv file, and treated on equal footing

Expressions

Basic structure of expressions

Every expression consists of a head and a sequence of elements in square brackets:

Head[(Subscript[e, 1]),(Subscript[e, 2]), (Subscript[e, 3]),...]

Describe your Problem

Expressions take care of the "describe your problem as clearly as you can" part

Yes, everything...

I'm not kidding:

FullForm[(Sqrt[x])]

FullForm[]

Functions and Replacement Rules

Collect known facts...

Replacement rules (aka functions) take care of the "collect known facts and relations about your problem"

Mathematica functions are actually replacement rules:

greenVersion[Graphics[List[c_,o_],s___]]:=Graphics[List[RGBColor[0,1,0],o],s]

greenVersion[]

greenVersion[]

Example 1: Trajectories

Calculate trajectories of interacting particles in an external field.

Particles have: mass, position, momentum

Particles exert forces on each other (example: Gravitation)

Given positions and momenta of all particles at a certain time, physics tells us positions and momenta a small time step later

Phase-Space Points

Position and momentum of all particles together are called a "phase space point" in physics. It is easy to

ToPhaseSpacePoint[{xs:{__}..},{ps:{__}..}]:={{xs},{ps}}

Positions[{xs:{{__}..},ps:{{__}..}}]:=xs

Momenta[{xs:{{__}..},ps:{{__}..}}]:=ps

AddParticle[{xs:{{__}..},ps:{{__}..}},x_,p_]:={Append[xs,x],Append[ps,p]}

Forces between particles

Two-body force between two particles at x1 and x2 of masses m1, m2

F12[x1_,m1_,x2_,m2_,G_]:=G ((m1 m2 )/((Norm[x2-x1])^3))(x2-x1);

Force excerted on a particle at position x of mass m by a point mass M centered at the origin (aka “the sun”). Serves the purpose of making code below more readable.

Fsun[x_,m_,M_,G_]:=F12[x,m,{0,0},M,G];

Example: Two unit mass particles, a distance of 1 along the x-axis

F12[{0,0},1,{1,0},1,G]

Vector of gravitational forces on the particles at positions xs with masses ms, with an additional “sun” of mass M at the origin

FGrav[xs_,ms_,M_,G_]:=Table[
Module[{fs,fp,ft},
fs=Fsun[xs[[i]],ms[[i]],M,G];
fp=MapThread[F12[xs[[i]],ms[[i]],#1,#2,G]&,{Drop[xs,{i}],Drop[ms,{i}]}];
ft={fs,fp};
fs+Total[fp]
],
{i,1,Length[xs]}]

Moving forward in time

The integration step performs the following transformation:

{t,{ (Subscript[x, 1])(t),...,(Subscript[x, n])(t)},{(Subscript[p, 1])(t)},...,(Subscript[p, n])(t)}} ->
{t+h,{(Subscript[x, 1])(t+h),...,(Subscript[x, n])(t+h)}{(Subscript[p, 1])(t+h),...,(Subscript[p, n])(t+h)}}

We'll use a so-called "symplectic integrator"

Sym2Step[{t0_,u:PSPointPattern},h_,ms_,F_]:=
Module[{q0=Positions[u],p0=Momenta[u],qm,fs,q1,p1},
(* intermediate positions (Subscript[qm, i]) = (Subscript[q, i])+(h/(Subscript[m, i]))(Subscript[p, i]) *)
qm = MapThread[(#1+(h/(2#3))#2)&,{q0,p0,ms}];
(* forces at intermediate positions *)
fs = F[qm];
(* new positions and momenta *)
q1=MapThread[(#1+(h/(#3))#2+((h^2)/(2 #3))#4)&,{q0,p0,ms,fs}];
p1=MapThread[(#1 + h #2)&,{p0,fs}];
(* Assemble result *)
{t0+h,ToPSPoint[q1,p1]}
]

Show me! - Live Demo

Example 2: Quantum Chemistry

Density Functional Theory Calculations in Mathematica

What the heck is Density Functional Theory ???

Used to calculate material properties "from first principles", that is, from quantum mechanics. Used in:

Chemistry

Pharmacy (e.g., drug design)

Molecular Biology

Material Science (e.g., surfaces of steel)

Semiconductor technology

Quantum Chemistry

Why Mathematica?

Prototyping: try out new simulation concepts (a special way to do DFT with FEM in this case)

Background: semiconductors

Can you run realistic calculations in Mathematica?

Time for solving the Kohn-Sham equations of DFT

OK: Show me what it can do! - Live Demo

Example 3: FOREX Trading

Prototype for backtesting FOREX trading strategies in Mathematica

User wants:

Define simple "trading strategy" in Mathematica

Try out strategy on historical data

Optimize the strategy's parameters on historical data

What is a trading strategy?

Receives as tick-by-tick input:

market data

indicators: quantities calculated from market data seen so far 
(moving averages, MACD = "moving average convergence divergence, ...")

Decides when to enter/exit positions (i.e.: buy or sell the underlying financial product)

Mathematica Prototype

Allow user to add a wide variety of indicators (Mathematica offers already tons of such indicators!)

Give user an easy language to define "events" where something happened 
(important: have certain barriers been crossed by the price)

Allow user to define conditions whether to buy or sell

when to go long?

when to go short?

when to stop out?

Show me! - Live Demo

Thank you for your Attention!

Questions?