{"id":28459897,"url":"https://github.com/juliainterop/mathlink.jl","last_synced_at":"2025-07-02T12:31:00.957Z","repository":{"id":21165959,"uuid":"24469307","full_name":"JuliaInterop/MathLink.jl","owner":"JuliaInterop","description":"Julia language interface for Mathematica/Wolfram Engine","archived":false,"fork":false,"pushed_at":"2025-06-18T09:04:54.000Z","size":187,"stargazers_count":141,"open_issues_count":15,"forks_count":34,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-07-01T01:36:11.737Z","etag":null,"topics":["julia","mathematica","mathlink","wolfram-engine","wstp"],"latest_commit_sha":null,"homepage":"","language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"zhufengzhufeng/2017node02_homework","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JuliaInterop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2014-09-25T18:05:35.000Z","updated_at":"2025-06-24T03:20:33.000Z","dependencies_parsed_at":"2023-11-20T18:28:58.556Z","dependency_job_id":"f7f3a815-287b-48e8-b0c2-32a38dc297c8","html_url":"https://github.com/JuliaInterop/MathLink.jl","commit_stats":{"total_commits":85,"total_committers":15,"mean_commits":5.666666666666667,"dds":0.5176470588235293,"last_synced_commit":"873050b5aa8f4f94d0c13240a9f294d4393fd119"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/JuliaInterop/MathLink.jl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaInterop%2FMathLink.jl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaInterop%2FMathLink.jl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaInterop%2FMathLink.jl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaInterop%2FMathLink.jl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JuliaInterop","download_url":"https://codeload.github.com/JuliaInterop/MathLink.jl/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuliaInterop%2FMathLink.jl/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263140226,"owners_count":23419838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["julia","mathematica","mathlink","wolfram-engine","wstp"],"created_at":"2025-06-07T01:39:15.261Z","updated_at":"2025-07-02T12:31:00.941Z","avatar_url":"https://github.com/JuliaInterop.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MathLink.jl\n\n[![CI](https://github.com/JuliaInterop/MathLink.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/JuliaInterop/MathLink.jl/actions/workflows/CI.yml)\n\nThis package provides access to Mathematica/Wolfram Engine via the MathLink library, now renamed to [Wolfram Symbolic Transfer Protocol (WSTP)](https://www.wolfram.com/wstp/). \n\n## Installation\n\nThe package requires an installation of either [Mathematica](http://www.wolfram.com/mathematica/) or the free [Wolfram Engine](https://www.wolfram.com/engine/). It will attempt to find the installation at build time; if this fails, please see the [installation troubleshoot](#installation-troubleshoot) below.\n \n \n## Usage\n\nThe main interface consists of the `W\"\"` string macro for specifying symbols. These are call-overloaded for building more complicated expressions. \n\n```julia\njulia\u003e using MathLink\n\njulia\u003e W\"Sin\"\nW\"Sin\"\n\njulia\u003e sin1 = W\"Sin\"(1.0)\nW`Sin[1.0]`\n\njulia\u003e sinx = W\"Sin\"(W\"x\")\nW`Sin[x]`\n```\n\nTo parse an expression in the Wolfram Language, you can use the `W` cmd macro (note the backticks):\n```julia\njulia\u003e W`Sin[1]`\nW`Sin[1]`\n```\n\n`weval` evaluates an expression:\n```julia\njulia\u003e weval(sin1)\n0.8414709848078965\n\njulia\u003e weval(sinx)\nW`Sin[x]`\n\njulia\u003e weval(W\"Integrate\"(sinx, (W\"x\", 0, 1)))\nW`Plus[1, Times[-1, Cos[1]]]`\n```\n\nKeyword arguments can be used to pass local variables\n```julia\njulia\u003e weval(sinx; x=2.0)\n0.9092974268256817\n```\n\n## The algebraic operators\n\nMathLink also overloads the `+`, `-`, `*`, `/`  operations.\n\n```julia\njulia\u003e using MathLink\n\njulia\u003e W\"a\"+W\"b\"\nW`Plus[a, b]`\n\njulia\u003e W\"a\"+W\"a\"\nW`Plus[a, a]`\n\njulia\u003e W\"a\"-W\"a\"\nW`Plus[a, Minus[a]]`\n```\n\nOne can toggle automatic use of `weval`  on-and-off using `set_GreedyEval(x::Bool)`\n\n```julia\njulia\u003e set_GreedyEval(true);\n\njulia\u003e W\"a\"+W\"b\"\nW`Plus[a, b]`\n\njulia\u003e W\"a\"+W\"a\"\nW`Times[2, a]`\n\njulia\u003e W\"a\"-W\"a\"\n0\n```\n\n\n## Fractions and Complex numbers\n \nThe package also contains extensions to handle fractions.\n\n```julia\njulia\u003e weval(1//2)\nW`Rational[1, 2]`\n\njulia\u003e (4//5)*W\"a\"\nW`Times[Rational[4, 5], a]`\n\njulia\u003e W\"a\"/(4//5)\nW`Times[Rational[5, 4], a]`\n```\n\nand complex numbers\n\n```julia\njulia\u003e im*W\"a\"\nW`Times[Complex[0, 1], a]`\n\njulia\u003e im*(im*W\"c\")\nW`Times[-1, c]`\n```\n\n\n## Matrix Multiplication\nSince the arithmetic operators are overloaded, operations such as matrix multiplication are also possible by default.\n\n```julia\njulia\u003e P12 = [ 0 1 ; 1 0 ]\n2×2 Matrix{Int64}:\n 0  1\n 1  0\n\njulia\u003e set_GreedyEval(true)\ntrue\n\njulia\u003e P12 * [W\"a\" W\"b\" ; W`a+b` 2] == [ W\"b\" 2-W\"b\" ; W\"a\" W\"b\"]\ntrue\n```\n\n\n## W2Mstr - Mathematica conversion\nSometimes one wants to be able to read the Julia MathLink expressions back into Mathematica. For that purpose, `W2Mstr` is also supplied. This implementation is currently quite defensive with parentheses, which gives a more verbose output than necessary. Here are a few examples\n\n```julia\njulia\u003e W2Mstr(W`x`)\n\"x\"\n\njulia\u003e W2Mstr(W\"Sin\"(W\"x\"))\n\"Sin[x]\"\n\njulia\u003e W2Mstr(weval(W`a + c + v`))\n\"(a + c + v)\"\n\njulia\u003e W2Mstr(weval(W`a^(b+c)`))\n\"(a^(b + c))\"\n\njulia\u003e W2Mstr(weval(W`e+a^(b+c)`))\n\"((a^(b + c)) + e)\"\n\njulia\u003e W2Mstr(W\"a\"+W\"c\"+W\"v\"+W\"Sin\"(2 +W\"x\" + W\"Cos\"(W\"q\")))\n\"(a + c + v + Sin[(2 + x + Cos[q])])\"\n\njulia\u003e W2Mstr(im*2)\n\"(2*I)\"\n\njulia\u003e W2Mstr(weval(W\"Complex\"(W\"c\",W\"b\")))\n\"(c+b*I)\"\n\njulia\u003e W2Mstr(W\"c\"+im*W\"b\")\n\"(((1*I)*b) + c)\"\n\njulia\u003e W2Mstr(W`b/(c^(a+c))`)\n\"(b*((c^(a + c))^-1))\"\n```\n\n\n\n\n## W2JuliaStruct - Conversion to Julia structures\n`W2JuliaStruct` is designed primarily to convert wolfram structures to Julia structures. This includes conversions of Mathematica lists to Julia vectors and Mathematica associations to Julia dictionaries.\n\nSome examples or tests that will evaluate to true:\n\n```julia\nusing Test\n@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]\n@test W2JuliaStruct([1,2,3]) == [1,2,3]\n@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]\n@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W\"a\",[1,2]]\n@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W\"a\",3]]\n\n@test W2JuliaStruct(Dict( 1 =\u003e \"A\" , \"B\" =\u003e 2)) ==Dict( 1 =\u003e \"A\" , \"B\" =\u003e 2)\n\n@test W2JuliaStruct(W`Association[\"A\" -\u003e \"B\", \"C\" -\u003e \"D\"]`) == Dict( \"A\" =\u003e \"B\" , \"C\" =\u003e \"D\")\n\n@test W2JuliaStruct(W`Association[\"A\" -\u003e {1,a,3}, \"B\" -\u003e \"C\"]`) == Dict( \"A\" =\u003e [1,W\"a\",3] , \"B\" =\u003e \"C\")\n```\n\n`W2JuliaStruct` does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.\n\n\n## LateX printing in JuPyter Notebooks\nPrinting in Jupyter notebooks is, by default, done in latex.\nThis can be turned off with the command `MathLink.set_texOutput(false)`\n\n## Escaping dollars for Mathematica\nThe `$` sign has a special meaning in Julia, but it does not in Mathematica. We can send dollar signs to Mathematica that same way we add them to normal strings. Below are a few examples of how it works: \n\n    using Test\n    x = exp(1)\n    @test W`$x` == x\n    @test W`\\$` == W\"$\"\n    @test W`\\$x` == W\"$x\"\n    @test W`$x +\\$` == x+W\"$\"\n    @test W`\"\\$\"` == \"\\$\"\n    @test W`\"a\"` == \"a\"\n    @test W`{a -\u003e b}` == W\"List\"(W\"Rule\"(W\"a\",W\"b\"))\n    @test W`{\"a\" -\u003e \"b\"}` == W\"List\"(W\"Rule\"(\"a\",\"b\"))\n    @test W`\"a\" -\u003e \"b\"` == W\"Rule\"(\"a\",\"b\")\n    @test W`a -\u003e b` == W\"Rule\"(W\"a\",W\"b\")\n    @test W`\"b(\\$)a\"` == \"b(\\$)a\"\n    @test W`\"b\\\\\\$\"` == \"b\\\\\\$\"\n    @test W`\"b\\$\"` == \"b\\$\"\n    @test W`\"\\$a\"` == \"\\$a\"\n    @test W`\"\\$\" -\u003e \"b\"` == W\"Rule\"(\"\\$\",\"b\")\n    @test W`{\"\\$\" -\u003e \"b\"}` == W\"List\"(W\"Rule\"(\"\\$\",\"b\"))\n    @test W`{\"a\" -\u003e \"\\$\"}` == W\"List\"(W\"Rule\"(\"a\",\"\\$\"))\n    @test W`{a -\u003e \"\\$\"}` == W\"List\"(W\"Rule\"(W\"a\",\"\\$\"))\n\n\n\n## Installation Troubleshoot\nThe package requires an installation of either [Mathematica](http://www.wolfram.com/mathematica/) or the free [Wolfram Engine](https://www.wolfram.com/engine/). It will attempt to find the installation at build time; if this fails, you will need to set the following [environment variables](https://docs.julialang.org/en/v1/manual/environment-variables/):\n- `JULIA_MATHKERNEL`: the path of the MathKernel executable\n- `JULIA_MATHLINK`: the path of the MathLink dynamic library named\n  - `libML64i4.so`/ `libML32i4.so` on Linux\n  - `ml64i4.dll`/`ml32i4.dll`/`libML64.dll`/ `libML32.dll` on Windows\n\nAfter setting, you may need to manually build the package\n```julia\n(@v1.X) pkg\u003e build MathLink\n```\n \nA separate workaround is to directly edit the deps/deps.jl file, which should be located (on Linux) at `~/.julia/packages/MathLink/\u003cversion dependent\u003e/deps/deps.jl`\n \nThe contents of `deps.jl` could for instance, read\n```julia\nconst mlib = \"/usr/local/Wolfram/Mathematica/11.3/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions/libML64i4\"\nconst mker = \"WolframKernel\"\n```\nAfter creating the file `deps.jl` try loading MathLink the usual way\n```julia\n(@v1.X) pkg\u003e using MathLink\n```\nIf you do not have a Mathematica installation at all, the above trick still works, but then you must leave the path blank \n```julia\nconst mlib = \"\"\nconst mker = \"WolframKernel\"\n```\nLoading `MathLink` then proclaims\n```julia\njulia\u003e using MathLink\n[ Info: Precompiling MathLink [18c93696-a329-5786-9845-8443133fa0b4]\n[ Info: Pretending fake installation works\n```\n\n## Relation to other packages\nThe MathLink package is a free standing package with verry few dependencies. However, it can be made to work with e.g. the [Symbolics](https://github.com/JuliaSymbolics/Symbolics.jl) package with the help of the package [SymbolicsMathLink](https://github.com/eswagel/SymbolicsMathLink.jl).\n\nFor documenation of these for packages, see their respective project pages.\n\n\n## Notes\n\n- Mathematica, Wolfram, MathLink are all trademarks of Wolfram Research.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliainterop%2Fmathlink.jl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliainterop%2Fmathlink.jl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliainterop%2Fmathlink.jl/lists"}