Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C#
Tip/Trick

How to Create a Simple Call from Visual Studio to Wolfram Mathematica

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
30 Aug 2020CPOL3 min read 7.7K   4   1
An overview of how to integrate Wolfram engine to your .NET application
This article will show you the simplest integration of Visual Studio and Wolfram Mathematica using a WF application as an example, explaining what the technology is, and then invoking the Kernel kernel from Visual Studio.

Introduction

Nowadays, when computer technology is developing rapidly, it is not always possible to create a complex application using one programming language. Different languages have their own advantages and disadvantages, and as a rule, none of them does not satisfy the requirements of the developed application program. The way out of this situation is to use several programming languages. This method is often used in software development for scientific research, industrial control, and other commercial applications. In this case, it is necessary to solve the problems of interaction between components written in different programming languages. The components that implement the graphical user interface, database management, and real-time data acquisition and processing are usually integrated into one application.

In this article, I will give an example of integrating Visual Studio with the Wolfram Mathematica computer algebra system. Wolfram has a protocol for communicating data between Mathematica and other programs ­­– MathLink. MathLink allows external programs to call Mathematica and be called from Mathematica1. Using MathLink, you can, for instance, treat Mathematica as a subprogram in an external program. Moreover, you can create an interface that implements your own user interface and interacts with the Mathematica core via MathLink. Besides, you can use MathLink to let Mathematica call individual functions within an external program. At least, you can even write code for MathLink itself, but in C. The disadvantage of this protocol is that the preinstalled Wolfram Mathematica environment is required for operation, although the library itself is in one file (Wolfram.NETLink.dll).

How It Can Create

In this example, I will show you how to create the simplest connect to Wolfram Mathematica from the Visual Studio using Windows Form Application. As described earlier, you need the Wolfram Mathematica package installed, and then you need to include the Wolfram.NETLink.dll library in your project. Just add to references in your project (Image 1).

Image 1

Image 1 – Add References to project

This library is usually located at C: \ Program Files \ Wolfram Research \ Mathematica \ 12.1 \ SystemFiles \ Links \ NETLink.

Then add to Toolbox the "MathKernel" component from the Wolfram.NETLink namespace (Image 2).

Image 2

Image 2 – Add to ToolBox "MathKernel" component

Then add in form “MathKernel” component and turn on "CaptureGraphics" property. As a result, we have a form like this with a button, a pictureBox (for graphics) and a textBox (for results) (Image 3).

Image 3

Image 3 – The form for test

For instance, consider the function “PDF[NormalDistribution[-5,3],x]” from Wolfram Mathematica. The result should be Image 4.

Just add action to the button:

C#
// This launches the Mathematica kernel:
mathKernel1.Compute("PDF[NormalDistribution[-5, 3], x]"); //sent function to Kernel         
textBox1.Text = mathKernel1.Result.ToString(); //convert result to string
mathKernel1.Dispose();

Then we have a result on Image 4.

Image 5

Image 4 – The result calling Mathematica

We can add graphic at the form. Let's add code in the button action:

C#
if (pictureBox1.Image != null)
            {
                pictureBox1.Image = null;
            }
            mathKernel1.GraphicsHeight = pictureBox1.Height;
            mathKernel1.GraphicsWidth = pictureBox1.Width;

            mathKernel1.Compute("Plot[PDF[NormalDistribution[0, 1], x] , 
                                 {x, -5, 5}, Filling -> Axis]");
            if (mathKernel1.Graphics.Length > 0) 
                    pictureBox1.Image = mathKernel1.Graphics[0];    //
            mathKernel1.Dispose();

The result is a graph (Image 5).

Image 6

Image 5 – Final result

It is fast and simple.

Conclusion

In this article, we looked at the easiest way to call from Visual Studio Wolfram Mathematica. This mechanism is suitable for projects with a lot of mathematical calculations. I think .NET / Link can be a good tool for masters, graduate students and other developers in science or science related.

References

History

  • 30th August, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Stylianos Polychroniadis31-Aug-20 5:20
Stylianos Polychroniadis31-Aug-20 5:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.