Where can I find examples of good Mathematica programming practice?

562

773

I consider myself a pretty good Mathematica programmer, but I'm always looking out for ways to either improve my way of doing things in Mathematica, or to see if there's something nifty that I haven't encountered yet. Where (books, websites, etc.) do I look for examples of good (best?) practices of Mathematica programming?

J. M.'s ennui

Posted 2012-01-17T21:51:01.890

Reputation: 115 520

Answers

492

Here's a collection of resources that I started on Mathgroup (a collection of Mathematica learning resources) and updated here at Stack Overflow. As this site is dedicated to Mathematica it makes more sense to maintain it here. This represents a huge amount of information; of course it's not exhaustive so feel free to improve it! Also, don't hesitate to share it and suggest other interesting links! Remember, you can always search the online Documentation Center of Mathematica, that is identical to the built-in help of the latest software version.

Links to more advanced aspects of the program that you can start to appreciate once you understand the basics are provided in separate answers (below) as this post became too large.

Introduction

Basic advices for people new to Mathematica

Functional style

Avoid iterative programming using loops like For or Do, use instead functional programming functions Map, Scan, MapThread, Fold, FoldList, ... and pure functions. This makes the code cleaner and faster.

Transpose and dimensions

  • Something not easy to guess alone at the beginning: if you have x={1,2} and y={3,4}, doing Transpose[{x,y}] or {x,y}ESC tr ESC in the front end will produce {{1,3},{2,4}} (format compatible with ListPlot). This animation helps understand why.

  • You can also use the second argument of Transpose to reorder the indices of a multidimensional list.

  • Don't forget to regularly control the output of the lists you generate using Dimensions.

Get familiar with shorthand syntax (@, &, ##, /@, /., etc.)

Programming easily

  • Getting help: Execute ?Map for example for a short description of a function, or press F1 on a function name for more details and examples about it. You can solve many problems by adapting examples to your needs.

  • Auto-completion: Start typing the name of a function and (in Mathematica 9+) select from the pop-up auto-completion menu, or press Ctrl+k to get a list of functions which names start with what has already been entered. Once the name of the function is written completely press Ctrl+Shift+k (on Mac, Cmd+k) to get a list of its arguments.

  • Function templates: In Mathematica 9, after typing a function name, press Ctrl+Shift+k (on Mac, Cmd+Shift+k) and click on the desired form from the pop-up menu to insert a template with named placeholders for the arguments.

    Other useful shortcuts are described in the post Using the Mathematica front-end efficiently for editing notebooks.

  • Use palettes in the Palettes menu especially when you're beginning.

  • In Mathematica 8, use the natural input capability of Wolfram Alpha, for example type "= graph 2 x + 1 between 0 and 3" without the quotes and see the command associated with the result.

Tutorials

FAQ

Books

Wolfram Websites

Learn

Examples

Resources

Blogs

Other related sites

Virtual conferences

Mathematica one-liner competition

Wolfram technology conferences

faysou

Posted 2012-01-17T21:51:01.890

Reputation: 10 549

In the introduction this link does not work: Introductory videos of various applications of Mathematica – MOON – 2014-11-09T17:25:13.707

This link seems to be broken: http://math.georgiasouthern.edu/math/computer/ABGMATHs1.pdf (in section "Tutorials").

– Peter Mortensen – 2015-03-09T21:27:01.507

Ok, I've found another source for the doc and updated the list. – faysou – 2015-03-09T22:00:25.687

67Apparently, this is the most edited post by a single person across all of the SE network. You've really done a remarkable job at maintaining it! – rm -rf – 2012-07-17T19:36:29.877

13Thanks, maintaining this list is a good way also to keep track of interesting posts on this site that summarize well some aspects of Mathematica. – faysou – 2012-07-18T11:50:56.373

Is there a way to download the videos from wolfram.com/broadcast? – becko – 2012-10-18T14:37:08.793

1@becko it's not too hard - like downloading YouTube videos, there are various ways. On Mac Safari, I set the user-agent to iPad before going to the page, then Download Linked File: this gets the mp4 download link. (No Flash for me!) – cormullion – 2012-11-07T15:22:54.757

@cormullion Got any tricks that work on Windows? I usually download videos from Youtube using a little program called Free Youtube Download. But that doesn't work on wolfram.com/broadcast (except for a few videos at wolfram.com/broadcast that are actually hosted on Youtube). – becko – 2012-11-07T18:15:07.013

@becko I'm not a Windows-er, but perhaps you could investigate switching useragent somehow - there's a tool for the Chrome browser (User Agent Switcher for Chrome) that might work...? – cormullion – 2012-11-07T19:55:49.653

@cormullion Thanks! I found the option to change the user agent and it works! – becko – 2012-11-07T20:41:00.207

Karl Unterkofler (https://homepages.fhv.at/ku/karl/mma.html) has collected a list of Mathematica timing benchmarks on various hardware platforms (Mac's + PC's). The list also shows an interesting history: from Mathematica version 2.2 up to 8.0.

– Romke Bontekoe – 2013-05-21T19:15:10.653

153

My original post above became too big so I'm splitting it into two posts. The tips and tricks section that I used to keep track of interesting new posts mostly on this site was big so here it is.

Note the tags at the end of each question on this site in order to read related questions.

I'm further dividing this post. See my third answer on Advanced evaluation, patterns, and neat algorithms below.

Tips and tricks

Top posts

General tricks

Tips for writing faster code

Reap and Sow

Compile

Using links to other languages

Packed arrays

Debugging

Data Structures

Objects

Traversal of an expression

Graphs

Optional Values

Accessing data in different ways

Association/Dataset

Rules and replacement (the backbone of Mathematica, more advanced)

Getting ideas from Lisp

Memoization/Caching

Undocumented features

IDEs and packages

Mathematica functions and environment

faysou

Posted 2012-01-17T21:51:01.890

Reputation: 10 549

8Now this is really useful, big +1. The better we categorize all these resources, the more effective this page will be. – Leonid Shifrin – 2012-07-25T10:55:14.627

Hi, @faysou, I'd like to see "Data structures and efficient algorithms, from Daniel Lichtblau", but this resources is not available anymore. Do you have a copy? – matheorem – 2016-08-31T04:32:53.293

@matheorem no I don't, unfortunately. – faysou – 2017-11-25T21:32:41.440

The Data Structures presentation is back online. – faysou – 2017-11-30T07:55:42.510

5This could be the Table of Contents of Mathematica.SE. – István Zachar – 2013-03-11T16:54:46.063

2This is such a fabulous resource, thank you for taking the time to compile it. – Keshav Saharia – 2013-06-03T08:11:46.117

55

Third part of my collection of links, dedicated to advanced evaluation, pattern matching and neat algorithms (which is quite arbitrary but contains a lot of interesting Q&As in my opinion).

Advanced evaluation of expressions

Pattern matching

A pattern is a way to quickly describe the structure of expressions and do computations on them, using /. or Cases for example.

Neat algorithms

faysou

Posted 2012-01-17T21:51:01.890

Reputation: 10 549

1That's incredible! Thanks for keeping up a great work, this is very useful for all of us, much appreciated. – Leonid Shifrin – 2013-09-17T13:42:52.913

You're welcome, you're the hero of many of these posts. – faysou – 2013-09-17T14:52:03.990

1@Leonid I've collected some of your answers where you use a parsing approach, if there are other such posts could you please add them to the list ? – faysou – 2014-01-18T22:30:52.770

2

Thanks. It's good that this technique is now systematically represented here, I think it is an important one. I have added this one, and my answer for the code formatter question (since in a sense formatter is also an expression parser, and in fact a pretty sophisticated one), which are the only ones that comes to mind right now.

– Leonid Shifrin – 2014-01-19T12:56:21.300

2I find the most difficult part of Mathematica is controlling evaluation (because there isn't just a single way to do it), and I think it's very important for the future of Mathematica, where macros (or code for rewriting code) is the horizon. The post above is extremely valuable. – Reb.Cabin – 2014-12-27T12:26:11.273

41

I highly recommend examining the included packages under your Mathematica installation directory:

\AddOns\ExtraPackages

\AddOns\LegacyPackages

\AddOns\Packages

\AddOns\Applications

You can also find examples of good practice, framework guidelines, and insider methods in the presentations from various Mathematica conferences. A mere sampling:

1999 Mathematica Developer Conference

2003 Mathematica Developer Conference

2007 Wolfram Technology Conference

Many, many more.

Mr.Wizard

Posted 2012-01-17T21:51:01.890

Reputation: 259 163

38

Besides the documentation, which I find very helpful, I also like the following resources:

  • The Wolfram Demonstrations Project is a fantastic resource, where you can draw up previously successful programs and learn some best practices. Their utility has varied, but I've certainly learned a lot by seeing great code in practice.
  • In a similar vein, I also follow the 'Wolfram Blog'. It's not all universally relevant for my own interests, but following it on Twitter is a good way to dip in here and there.
  • Finally, as a new user, I also draw on the Mathematica Cookbook for some nifty examples.

As noted above, however, I normally use the documentation and look through examples of uses, as that's my best way of learning.

canadian_humanist

Posted 2012-01-17T21:51:01.890

Reputation: 3 674

1+1 for the cookbook but I agree with @Nasser on the readability of the demonstrations (plus, doing everything in one giant code block as demonstrations seem to organize things is probably not going to scale very well for anything nontrivial) – acl – 2012-01-19T13:26:37.667

30

In my opinion, rules and pattern matching are central to mastering Mathematica. I strongly recommend Demystifying Rules by Nancy Blachman published in The Mathematica Journal, Volume 8, Issue 4, for a solid grounding in this area. It is available on-line at The Mathematica Journal

m_goldberg

Posted 2012-01-17T21:51:01.890

Reputation: 104 223

29

For neat tips and tricks, there is a daily tip posted to the MathematicaTip twitter page.

tlehman

Posted 2012-01-17T21:51:01.890

Reputation: 2 004

1This is a pretty good resource. It has lots of small, easy to digest snippets. – Mike Bailey – 2012-01-18T04:44:51.197

28

The Mathematica GuideBook by Michael Trott always gives me lots of inspirations. Beside of it (and other places been mentioned above), I like exploring the SystemFiles folder. Some interesting tricks (especially about interface and FrontEnd) are hiding there.

Silvia

Posted 2012-01-17T21:51:01.890

Reputation: 25 336

The SystemFiles suggestion is a good one. Do you have any particular examples to share? – Verbeia – 2012-01-18T02:28:08.037

1@Verbeia Some undocumented things can be learned from the system files. such as "SyntaxColoringReasons" for CurrentValue which can be seen in WhyTheColoring.nb (Dynamic[CurrentValue[InputNotebook[], "SyntaxColoringReasons"]]), system fonts classification which can be seen in UnicodeFontMapping.tr, some inappropriate converting when import GBK encoded Chinese text can be corrected by manipulating CP936.m, etc. – Silvia – 2012-01-18T11:49:26.350

26

Nobody's mentioned the packages that come with Mathematica. There's a heap of great coding examples in there, especially the later packages.

dwa

Posted 2012-01-17T21:51:01.890

Reputation: 1 859

26

The 'Mathematica GuideBook' series by Michael Trott has tons of good examples that go much further than typical 'toy-examples'. I found it a very valuable and thorough ressource for learning the ins and outs of the Mathematica language.

Thies Heidecke

Posted 2012-01-17T21:51:01.890

Reputation: 8 489

24

Fourth fork of the list of links.

Manipulate/Dynamic

Palettes

Finance (but not only)

Correlation

Proba/Stats

Misc

faysou

Posted 2012-01-17T21:51:01.890

Reputation: 10 549

16

Not strictly a Mathematica blog but Rip’s Applied Mathematics Blog is a very nice resource for advanced Mathematica problem solving. Rip makes regular weekly posts on whatever interests him that week and they usually include some neat implementation in Mathematica.

And another very good reference Mathematica blog by Kris Carlson with interesting methods and examples:

R Hall

Posted 2012-01-17T21:51:01.890

Reputation: 4 190

The author of this blog has passed away late last year. – R Hall – 2014-01-25T14:10:45.900

1

If bewildered one should look into examples http://rip94550.wordpress.com/2012/04/16/trusses-snow-load-on-howe-fink-and-pratt-trusses/

– Artes – 2012-04-24T12:03:27.847

1Thanks this blog is very interesting. – faysou – 2012-05-01T14:17:50.970

13

I'm not sure this has already been posted but I found these tutorials really helpful as a beginner. They are problem based (similar to the Euler Problems) and the author takes you through the solutions in a step wise fashion. Hope it helps.

John_dydx

Posted 2012-01-17T21:51:01.890

Reputation: 391

7

Blogs

Personal websites

Calculus

Resources on other languages

Forums

MathGroup

Stack Exchange sites

Links to some packages

Packages for preparing publication-quality scientific figures

  • LevelScheme by Mark Caprio (latest version: 3.52, Sep 2011, for Mathematica 6 and higher)
  • Presentations and other packages by David Park (latest version: 25 Aug 2011)
  • A WorkLife Framework by Scientific Arts LLC (extendable and customizable toolset that broadens Mathematica's scope across many aspects of daily work)
  • FeynArts by Thomas Hahn (latest version: 3.7, 27 Mar 2012, package for generation and visualization of Feynman diagrams and amplitudes)
  • Writing and Publishing a Book with Mathematica by Paul R. Wellin (2005, available from the Wolfram Library Archive)

Useful non-free tools for development, deployment, distribution, linking, etc.

faysou

Posted 2012-01-17T21:51:01.890

Reputation: 10 549

6

Stephen Wolfram: An Elementary introduction to Wolfram Language

Also, this does not belong 100% here but it is an overshelming list of reference to be missed. A Bibliography of Publications about the Mathematica Symbolic Algebra Language

enter image description here

Dimitris

Posted 2012-01-17T21:51:01.890

Reputation: 4 594

4

I'm a beginner, and I find it interesting to explore the documentation. Some of the commands I've read about have given me interesting ideas for programs. Also, I'm not sure if this has been mentioned, but the Wolfram Demonstrations Project has a bunch of amazing programs you can play with and download the code from.

heather

Posted 2012-01-17T21:51:01.890

Reputation: 485

4

ApplicationMaker shows how to create packages that look like native functionality, including integration into the Documentation Center.

masterxilo

Posted 2012-01-17T21:51:01.890

Reputation: 5 447

4

I am not sure if it has already be mentioned.

Adding Object-Oriented Capabilities to Mathematica

Dimitris

Posted 2012-01-17T21:51:01.890

Reputation: 4 594

2

I have been adding some new content here (mostly following along with the elementary introduction book and doing explorations in machine learning):

https://www.livecoding.tv/learn/wolfram/

These 'live coding' sessions are recorded so you can watch them. They are a little bit different from pre-recorded video tutorials, because there is a chat window where 'live watchers' can ask questions (which you can not do when you watch the recording, of course).

Arnoud Buzing

Posted 2012-01-17T21:51:01.890

Reputation: 9 213