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

How to Create Calling a Complex Function from Visual Studio to Wolfram Mathematica

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
21 Sep 2020CPOL3 min read 5.3K   2   5
Integration of VS and Wolfram Mathematica
This article will show you the integration of Visual Studio and Wolfram Mathematica using a WF application as an example, explaining what the technology is, and then invoking the kernel from Visual Studio.

Introduction

Nowadays, we don't have programming languages that would equally well solve all problems of software systems. A number of languages already exist and new ones continue to appear. Outdated languages are in no hurry to leave our technologies, programs have been written with a lot of solutions in them, and they continue to work. Projects using multiple programming languages open up new possibilities for designing software systems. In such systems, for different tasks for different parts, the programming language is chosen, by means of which the best result is achieved. The synergistic effect of this approach is achieved by using the advantages of the language in those parts for which the chosen language will be most effective and compensating for the shortcomings in those where it is best to use another language.

How It Can Create

In my last article, I described the simplest example of integrating Wolfram Mathematica and Visual Studio. However, it has a number of problems that I ran into after. I often use Visual Studio to develop the interface, and Wolfram Mathematica to handle computations. The pros is that complex mathematical calculations can be written in Wolram Mathematica. The cons is that you have to use two development environments for development.

If we make a copy-paste of code from Wolfram Mathematica, the problem arises that a rather large code should be transferred along the lines in the Visual Studio. It is undoubtedly possible to make this, but it can take a long time. Let's take a large function and result from Wolfram Mathematica as an example in image 1:

Image 1

Image 1 - A Wolram Mathematica's function

If we paste this function in Visual Studio, we will have a result. It is shown in Image 2.

Image 2

Image 2 - The result of copy-past

As the picture shows, the result was not well. Yes, we can use a formatted string. Moreover, there is an easier method for inserting large functions from Wolfram Mathematica. An executable file containing the required functions can be integrated into the Mathematica kernel load. To do this, we need to edit the init.m boot file. It is usually located in the path c: \ Users \ * Username \ AppData \ Roaming \ Mathematica \ Kernel \. For the simplest instance, let's consider a function created in Wolfram Mathematica (Image 3 ) and a result (Image 4).

Image 3

Image 3 - A Mathematica's function

Image 4

Image 4 - A result of function

Let's insert the code into init.m. By doing this, we initialize the loading of functions when the kernel is loaded and can be called from mathematics immediately.

Then we can call it from Visual Studio. For instance, using the following code:

C#
mathKernel1.Compute("NORMDISTR[]");
if (mathKernel1.Graphics.Length > 0) pictureBox1.Image = mathKernel1.Graphics[0];    
mathKernel1.Dispose();

After executing the code, we will have the following result in Image 5. As you can see, the function worked in the same way.

Image 5

Image 5 - A result of function in Visual Studio

We can also call more complex functions, for example, we add a rather complex function that calls another function for execution (Image 6) in init.m file.

Image 6

Image 6 - A complex function

The result of complex function shown in image 7.

Image 7

Image 7 - The result of complex function in Wolfram Mathematica

Also, we can call it from Visual Studio. For instance, using the following code:

C#
mathKernel1.Compute("Nbin[500^2, 1000, 0, 0, 0.8, 0.6, 0.98, 100]");//sent function to Kernel
          textBox1.Text = mathKernel1.Result.ToString();            //convert result to string

After executing the code, we will have the following result in Image 8.

Image 8

Image 9 - The result in Visual Studio

Conclusion

Although Wolfram Mathematica is a highly specialized tool for researching, it contains sufficient integration mechanisms with other development environments. This approach allows us to develop integrate complex calculations in Visual Studio.

History

  • 20th September, 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

 
QuestionVery interesting but what about Wolfram Alpha? Pin
asiwel22-Sep-20 5:52
professionalasiwel22-Sep-20 5:52 
AnswerRe: Very interesting but what about Wolfram Alpha? Pin
Uladzislau Baryshchyk26-Sep-20 16:30
Uladzislau Baryshchyk26-Sep-20 16:30 
GeneralRe: Very interesting but what about Wolfram Alpha? Pin
Uladzislau Baryshchyk4-Oct-20 5:47
Uladzislau Baryshchyk4-Oct-20 5:47 
GeneralRe: Very interesting but what about Wolfram Alpha? Pin
asiwel6-Oct-20 8:31
professionalasiwel6-Oct-20 8:31 
I saw and read and bookmarked your new article demonstrating how to interface with Wolfram Alpha API using C#. I thought it was great, both narrative and code, and was all excited about trying that out today. And I thought it would be very popular, interesting and useful for a lot of people.

But then today as I was just about to get started, CodeProject now says that your article has been deleted?? Why would that have happened??
GeneralRe: Very interesting but what about Wolfram Alpha? Pin
Uladzislau Baryshchyk19-Oct-20 15:22
Uladzislau Baryshchyk19-Oct-20 15:22 

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.