{"id":285,"date":"2016-07-22T05:27:00","date_gmt":"2016-07-22T05:27:00","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=285"},"modified":"2016-09-11T15:20:24","modified_gmt":"2016-09-11T15:20:24","slug":"using-julia-to-interact-with-mathematica","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2016\/07\/22\/using-julia-to-interact-with-mathematica\/","title":{"rendered":"Using Julia to Interact with Mathematica"},"content":{"rendered":"<p><a href=\"https:\/\/www.wolfram.com\/mathematica\/\" target=\"_blank\">Mathematica<\/a> is a powerful environment for symbolic and numerical computation. I have been using it for many years now. In this <a href=\"http:\/\/www.rangakrish.com\/index.php\/2015\/09\/28\/controlling-cloudbit-using-raspberry-pi-2\/\" target=\"_blank\">post<\/a>\u00a0I had explained how we can use Mathematica bundled with Raspberry distribution to control littleBits devices.<\/p>\n<p>When I saw that there is support in Julia for interacting with Mathematica, I decided to investigate it further. Please note that for this to work, you must have a local copy of Mathematica; Julia does not replace Mathematica.<\/p>\n<p>The package <a href=\"https:\/\/github.com\/MikeInnes\/Mathematica.jl\" target=\"_blank\"><strong>Mathematica.jl<\/strong><\/a> is required for this interaction.\u00a0I found out that this package has dependency on a couple of other files (<strong>MathLink<\/strong>) that\u00a0are not automatically installed by Julia\u2019s package manager. The files are located here: <a href=\"https:\/\/github.com\/MikeInnes\/MathLink.jl\">https:\/\/github.com\/MikeInnes\/MathLink.jl<\/a>.\u00a0So I manually downloaded the ZIP file and extracted the SRC files to my local directory.<\/p>\n<p>For convenience in launching Julia from the Terminal on my iMac (that is where I have my Mathematica installation), I set up an alias:<\/p>\n<p style=\"padding-left: 30px;\"><strong>alias julia=&#8221;\/Applications\/Julia-0.4.6.app\/Contents\/Resources\/julia\/bin\/julia&#8221;<\/strong><\/p>\n<p>Launch Julia on the Terminal by typing <strong>julia<\/strong>.<\/p>\n<p>We have to install the <strong>Mathematica.jl<\/strong> package just once through Julia\u2019s package manager. In my case, I had already installed it in an earlier session. The next important step is to tell Julia where it can locate the <strong>MathLink<\/strong> dependency files. See the following image:<\/p>\n<figure id=\"attachment_286\" aria-describedby=\"caption-attachment-286\" style=\"width: 661px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"286\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2016\/07\/22\/using-julia-to-interact-with-mathematica\/julia-1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png\" data-orig-size=\"661,336\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Julia Session\" data-image-description=\"&lt;p&gt;Julia Session&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Julia Session&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png\" class=\"size-full wp-image-286\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png?resize=661%2C336\" alt=\"Julia Session\" width=\"661\" height=\"336\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png?w=661&amp;ssl=1 661w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/07\/Julia-1.png?resize=300%2C152&amp;ssl=1 300w\" sizes=\"(max-width: 661px) 100vw, 661px\" \/><\/a><figcaption id=\"caption-attachment-286\" class=\"wp-caption-text\">Julia Session<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Now we are all set to interact with Mathematica. The nice thing is that we need not launch Mathematica manually for this interaction to work.<\/p>\n<p>Let us start by calculating the Nth prime number, where N is 10^10:<\/p>\n<p style=\"padding-left: 30px;\"><b>julia&gt; <\/b><b>Prime(10^10)<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>252097800623<\/b><\/p>\n<p>Note that the answer is coming from Mathematica, not Julia.<\/p>\n<p>Next, let us create an array of Lucas numbers for N = 10 to 20:<\/p>\n<p style=\"padding-left: 30px;\"><b>julia&gt; <\/b><b>res = [LucasL(n) for n = 10:20]<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>11-element Array{Any,1}:<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0\u00a0 123<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0\u00a0 199<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0\u00a0 322<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0\u00a0 521<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0\u00a0 843<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0 1364<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0 2207<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0 3571<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0 5778<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a0 9349<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>\u00a015127<\/b><\/p>\n<p>What is interesting\u00a0is that the result is a Julia expression, so we can access the elements of the returned array.<\/p>\n<p style=\"padding-left: 30px;\"><b>julia&gt; <\/b><b>res[1] + res[2]<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>322<\/b><\/p>\n<p>Because the syntax of Mathematica and Julia are different, one way to execute an actual Mathematica expression in Julia is to represent it as a String and call Mathematica\u2019s <strong>ToExpression<\/strong> function on the String.<\/p>\n<p>As an example, let us use Mathematica to play some music &#8211; the notes C4 and D4, each lasting 1 second:<\/p>\n<p style=\"padding-left: 30px;\"><b>julia&gt; <\/b><b>ToExpression(&#8220;&#8221;&#8221;EmitSound[Sound[{SoundNote[&#8220;C4&#8221;], SoundNote[&#8220;D4&#8243;]}]]&#8221;&#8221;&#8221;)<\/b><\/p>\n<p>When you execute the above code, you can hear Mathematica playing music.<\/p>\n<p>A slight variation of the above is to play a Chord using notes C4-E4-G4, for 0.5 second using Violin:<\/p>\n<p style=\"padding-left: 30px;\"><b>julia&gt; <\/b><b>ToExpression(&#8220;&#8221;&#8221;EmitSound[Sound[SoundNote[{&#8220;C4&#8221;, &#8220;E4&#8221;, &#8220;G4&#8221;}, 0.5, &#8220;Violin&#8221;]]]&#8221;&#8221;&#8221;)<\/b><\/p>\n<p>As the final example, I can define my own Mathematica function and execute it:<\/p>\n<p style=\"padding-left: 30px;\"><b>julia&gt; <\/b><b>ToExpression(&#8220;MyFunction[a_] = a ^ 34;MyFunction[2]&#8221;)<\/b><\/p>\n<p style=\"padding-left: 30px;\"><b>17179869184<\/b><\/p>\n<p>So it is quite remarkable\u00a0that we can interact with Mathematica and take advantage of its vast computational power from within Julia. I am sure that as Julia evolves, further enhancements to the <strong>Mathematica.jl<\/strong> package will be made.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mathematica is a powerful environment for symbolic and numerical computation. I have been using it for many years now. In this post\u00a0I had explained how we can use Mathematica bundled with Raspberry distribution to control littleBits devices. When I saw that there is support in Julia for interacting with Mathematica, I decided to investigate it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[63,72,17],"tags":[64,43],"class_list":["post-285","post","type-post","status-publish","format-standard","hentry","category-julia","category-mathematica","category-programming","tag-julia","tag-mathematica"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-4B","jetpack-related-posts":[{"id":2947,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/24\/using-julia-from-mathematica\/","url_meta":{"origin":285,"position":0},"title":"Using Julia from Mathematica","author":"admin","date":"November 24, 2022","format":false,"excerpt":"In an earlier article, I had shown how it is possible to interact with Mathematica from Julia. In today\u2019s article, I will share the details of how to interact with Julia from within Mathematica. Why would somebody want to execute Julia code inside Mathematica? Although Mathematica is a great symbolic\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Installing Julia","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":304,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/08\/20\/cuda-and-mathematica\/","url_meta":{"origin":285,"position":1},"title":"CUDA and Mathematica","author":"admin","date":"August 20, 2016","format":false,"excerpt":"Recently I purchased a high-end desktop computer for my image processing project. Since many computations tend to take several hours to execute, I wanted to accelerate the calculations to the extent possible by adding a GPU. I chose NIVIDA's GeForce GTX 1080\u00a0processor-based card. Although I will be using C++ for\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"Basic CUDA Check","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/08\/Mathematica-1-1024x771.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/08\/Mathematica-1-1024x771.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/08\/Mathematica-1-1024x771.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/08\/Mathematica-1-1024x771.png?resize=700%2C400 2x"},"classes":[]},{"id":1594,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/06\/08\/using-nodejs-in-mathematica-12\/","url_meta":{"origin":285,"position":2},"title":"Using NodeJS in Mathematica 12","author":"admin","date":"June 8, 2019","format":false,"excerpt":"In an earlier article, I had described Python integration in Mathematica 12. In addition to Python, NodeJS is also supported as a default \u201cexternal\u201d language. In today\u2019s article, I will focus on NodeJS integration. By the way, NodeJS support was introduced in Mathematica 11.2. Before using NodeJS with Mathematica 12,\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"NodeJS Session Continued","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/06\/Session2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/06\/Session2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/06\/Session2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/06\/Session2.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1560,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/05\/05\/python-integration-in-mathematica-12\/","url_meta":{"origin":285,"position":3},"title":"Python Integration in Mathematica 12","author":"admin","date":"May 5, 2019","format":false,"excerpt":"Mathematica has had Python support since ver 11.2 through ExternalEvaluate[]. In ver 11.3 it was possible to input Python expression in a cell by beginning with \u201c>\u201d character. The good news is that Mathematica 12 has significantly enhanced this integration. Python Cells Let us start with the simplest way to\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"Function References","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/05\/External-Evaluate-4.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3626,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/02\/21\/using-openai-from-mathematica-part-3\/","url_meta":{"origin":285,"position":4},"title":"Using OpenAI from Mathematica: Part-3","author":"admin","date":"February 21, 2025","format":false,"excerpt":"Let us continue our discussion on using Mathematica to interact with OpenAI (you may want to go through the earlier article as well). The simplest function to interact with the LLM is LLMSynthesize[]. As you might have guessed, this is a \u201csync\u201d (non-streaming) call. What if you expect a long\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"Basic LLMSynthesize","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/02\/fig1-300x21.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/02\/fig1-300x21.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/02\/fig1-300x21.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":328,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/09\/11\/natural-language-processing-in-mathematica\/","url_meta":{"origin":285,"position":5},"title":"Natural Language Processing in Mathematica","author":"admin","date":"September 11, 2016","format":false,"excerpt":"Welcome back. Today I am going to share with you some of the nice capabilities of Mathematica in the area of Natural Language Processing (NLP). Let us start with words. What if we wish to know\u00a0the various definitions of the word image?\u00a0Here is the answer. Mathematica gives the various senses\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"Word Definition","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/09\/word-data1-1024x238.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/09\/word-data1-1024x238.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/09\/word-data1-1024x238.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/09\/word-data1-1024x238.png?resize=700%2C400 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=285"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/285\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}