Title: Mathematica stuff
status: hidden
slug: mathematica
On this page I collected some [Mathematica](http://www.wolfram.com/)-related projects. This page is still under construction until everything is migrated from [the old site](http://web.ift.uib.no/~szhorvat/mmatricks-o.php).
Be sure to also check out the [Mathematica-related blog posts](../tag/mathematica.html)!
# Packages
### MATLink
An advanced interface for seamlessly calling MATLAB functions from Mathematica. Please go to [http://matlink.org/](http://matlink.org/).
### BoolEval
This is a package that allows a simple notation for doing MATLAB-style vectorized computations on numerical arrays. For example, to count the number of array elements greater than 0.5, one might write `Count[arr, x_ /; x > 0.5]`. This is clean and readable, but there's an orders of magnitude faster way to do the same thing when working with a large [packed](http://mathematica.stackexchange.com/questions/3496/what-is-a-mathematica-packed-array) numerical array:
Total@UnitStep[0.5 - arr]
Unfortunately this is not vey readable, and for complex conditions and comparisons it becomes very unwieldy to write. The package provides an alternative notation,
BoolCount[arr > 0.5]
There are functions for element selection, counting, and other similar operations, e.g.
BoolPick[arr, 0.5 < arr < 0.6]
which would take the much more complex `Pick[arr, (1 - UnitStep[0.5 - arr]) UnitStep[0.6 - arr], 1]` to write by hand.
Get the code [here](https://bitbucket.org/szhorvat/booleval).
### IGraphR
This package makes it easy to call the igraph graph manipulation library from Mathematica. This is useful both for graph operations that Mathematica doesn't support and verifying results obtained with Mathematica.
See the [blog post]({static}/igraph.md) and get the code [here](https://github.com/szhorvat/IGraphR).
### IGraph/M
IGraph/M is another Mathematica interface to igraph which does not depend on R and is more tightly integrated with Mathematica. IGraph/M does not cover all igraph functions, but it is much faster and more reliable then IGraphR due to not having to depend on RLink. If the function you need is present in IGraph/M, I recommend you use it instead of IGraphR.
IGraph/M is an ongoing effort and contributions are welcome.
See the [blog post](http://szhorvat.net/mathematica/IGraphM) and get the package [here](https://github.com/szhorvat/IGraphM).
### MaTeX
This is a package for generating $\mathsf{\LaTeX}$-formatted labels within Mathematica.
::mma
MaTeX[Sin[x] + O[x]^6]
See the [blog post](http://szhorvat.net/mathematica/MaTeX) and get the code [here](https://github.com/szhorvat/MaTeX).
### LTemplate
LTemplate is a Mathematica package that simplifies writing [LibraryLink](https://reference.wolfram.com/language/LibraryLink/tutorial/Overview.html) code. It automatically generates much of the necessary boilerplate code from "templates" that specify a class interface. A tutorial is included with the package.
Get the code [here](https://github.com/szhorvat/LTemplate).
### Mathematica on the Notre Dame CRC cluster
This package greatly simplifies running parallel Mathematica jobs on [Notre Dame's HPC cluster](http://crc.nd.edu/). Just include the package, which auto-detects the number of cores and hosts usable by the jobs and sets up Mathematica's parallel tools appropriately.
Get the code [here](https://bitbucket.org/szhorvat/crc).
# Links and tricks
* See my blog posts tagged with [`mathematica`](../tag/mathematica.html).
* [PackageData.net](http://packagedata.net) is a new Mathematica package repository that makes it easy to submit new package links or edit old ones. Submit your packages and discover new ones!
* [Mathematca at StackExchange](http://mathematica.stackexchange.com/) -- for help and questions about working with Mathematica.
- [A large collection of tutorials, guides, tips and tricks](http://mathematica.stackexchange.com/q/18/12), for all levels of expertise.
- [A medley of useful and interesting programming techniques](http://stackoverflow.com/questions/4198961/what-is-in-your-mathematica-tool-bag/695132).
- [Some interesting and popular Mathematica packages](http://meta.mathematica.stackexchange.com/questions/428/what-third-party-packages-do-you-use/12)
* These are some of my favourite programming tricks and techniques, recommended for the intermediate Mathematica programmer:
- [The "Villegas-Gayley trick"](http://stackoverflow.com/a/5149656/695132) is a clever way for wrapping built-in functions to add arbitrary functionality.
- How to transform parts of held expressions using replacement rules? Use the [Trott-Strzebonski in-place evaluation technique](http://mathematica.stackexchange.com/q/29317/12).
- [Creating closures](http://mathematica.stackexchange.com/a/4137/12), functions with an internal state.
* [**Mathematica plugin for IntelliJ IDEA**](http://wlplugin.halirutan.de) -- this makes [IntelliJ IDEA](https://www.jetbrains.com/idea/) into an excellent Mathematica code editor with smart syntax highlighting, auto-completion and source navigation.
* The [**Spelunking package**](https://github.com/szhorvat/Spelunking) makes it very easy to read the definitions of in-memory symbols. It can be used to see how some builtin or package functions work. I often use it on package functions even when the package source is available.
* [**SciDraw**](http://scidraw.nd.edu/) by [Mark Caprio](http://www.nd.edu/~mcaprio) is a package for creating publication quality figures. It makes it easy to create figure grids and subfigures and provides fine grained control over many aspects of the figure. The tradeoff is that it takes more code to create figures than standard Mathematica and it takes a while to learn the syntax. Highly recommended for publication figures or situations where output quality is important, but not for everyday visualization.
* One of the first things I do when installing or upgrading Mathematica is **set up shortcut keys for typing** 〚 **and** 〛. Find the file `SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr` within the `$InstallationDirectory` and add the following right after the line `EventTranslations[{`. Do make a backup before editing this file and make sure not to create additional files with a `.tr` extension in that directory!
::mma
Item[KeyEvent["[", Modifiers -> {Control}],
FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
"\[LeftDoubleBracket]", After]}]],
Item[KeyEvent["]", Modifiers -> {Control}],
FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
"\[RightDoubleBracket]", After]}]],
After restarting Mathematica it will be possible to type 〚 and 〛 using `Control`-`[` and `Control`-`]`, which I find much more convenient than the default key sequence of `esc` `[` `esc`. Using these special brackets makes the code much easier to read than `[[` and `]]`.
On OS X I used to use `Command`-`[`, but in version 10.2 that was assigned to hyperlink navigation (back and forward). So if you are wondering why Command doesn't seem to work in 10.2, this is the reason.