Wolfram’s Mathematica 101

Original Author: Angelo Pesce / C0de517e

This will appear in a longer, more rambling form, on C0de517e.

It’s main section is inspired by LearnXinYminutes


Games and rendering are becoming increasingly “data driven”, in a sense that we use big amounts of “offline” data, either through acquisition or simulation, that we have to fit into our games via forms of approximation. These can be derived analytically, through simplifying assumptions, numerically, by fitting functions to data (learning), or using both methods.

Mathematica can help with all these steps:

  • Being a Computer Algebra System, it aids with experimentation on analytic forms
  • It has great support for a wide range of numerical solvers, allowing to write simulations
  • It has strong visualization and interactive manipulation abilities, which makes reasoning about the data easier
  • It’s a prototyping, experimention language, with an interactive REPL and very coincise, powerful functional primitives

Mathematica’s language

It stands to reason that a CAS is built on top of a symbolic language that supports programmatic manipulation of its own programs (code as data, a.k.a. homoiconicity), and indeed this is the case here. The most famous homoiconic language is Lisp, and indeed you’re familiar with the Lisp family of languages, Mathematica won’t be too far off, but there are a few notable differences.

While in Lisp everything is a list, in Mathematica, everything is an expression tree. Also, expressions in mathematica can have different forms, that is, input (or display) versions of the same internal expression node. This allows you for example to have equations entered in the standard mathematical notation (TraditionalForm) via equation editors or in a textual form that can be typed without auxiliary graphical symbols (InputForm) and so on. Mathematica’s environment, the notebook, is not a purely textual one, but supports graphics, so even images and graphs can be displayed as output or input, inside equations, while still maintaining the same internal representation.

Mathematica is an interactive environment, but it’s not a standard REPL (read-eval-print loop), instead it relies on the concept of “notebooks” which are a collection of “cells”. Each cell can be evaluated (shift-enter) and it will yield an output cell underneath them, thus allowing for changes and re-evaluation of cells in any order. Cells can also be marked as not containing Mathematica code but just text, thus the notebook is a mix of code and documentation which enables a sort of “literary programming” style.

For completeness it’s worth noticing that Mathematica also has a traditional text-only interface that can be invoked by running the Kernel outside the notebook environment, which has only textual input and output and has only the standard REPL you would expect, but there’s little reason to use it. There is also a more “programming” oriented environment called the Workbench, an optional product that can make your life easier if you write lots of Mathematica code and need to profile, debug and so on.

Mathematica by example

If you don’t have Mathematica, you can try Mathics which is an OpenSource implementation based on SciPy and Sage. It’s obviously far from being as complete as Wolfram’s solution, but it has a lot of the basic language covered and also has an online interface! I expect most of the code below to work on it.

(* This is a comment, if you're entering this in a notebook remember that to evaluate the content of a cell you need to use shift-enter or the numeric pad enter *)
  (* Basic math is as expected, but it's kept at arbitrary precision unless you use machine numbers *)
  (* % refers to the last computed value *)
  (* Functions are invoked passing parameters in square braces, all built-in functions start with capitals*)
  (* N[] forces evaluation to machine numbers, using machine numbers makes evaluation faster, but will defeat many CAS functions *)
  (* Infix and postfix operators all have a functional form, use FullForm to show *)
  (* Each expression in a cell will yield an output in a separate output cell. Expressions can be terminated with ; if we don't want them to emit output, which is useful when doing intermediate assignments that would yield large outputs otherwise *)
  (* Assigning a symbol to an expression *)
  x = 10
  (* If a symbol is not yet defined, it will be kept in its symbolic version as the evaluation can't proceed further *)
  y = x*w
  (* This will recursively expand z until it reaches expansion limit and errors out *)
  z = z+1
  (* Clears the previous assignments. It's not wise to assign as globals such common symbols, we use these here for brevity and will clear as needed *)
  (* Evaluation is controlled by symbols attributes *)
  x = 10
  (* y will be equal to "x*2", not 20 as := is the infix version of the function SetDelayed, which doesn't evaluate the right hand...*)
  y := x*2 
  (* …that's because SetDelayed has attribute HoldAll, which tells the evaluator to not evaluate any of its arguments. HoldAll and HoldFirst attributes are one of the "tricky" parts, and a big difference from Lisp where you should explicitly quote to stop evaluation *)
  (* As many functions in Mathematica are supposed to deal with symbolic expressions and not their evaluated version, you'll find that many of them have HoldAll or HoldFirst, for example Plot has HoldFirst to not evaluate its first argument, that is the expression that we want to graph *)
  Plot[Sin[x], {x, 0, 6*Pi}]
  (* The Hold function can be used to stop evaluation, and the Evaluate function can be used to counter-act HoldFirst or HoldAll *)
  (* A neat usage of SetDelayed is for memoization of computations, the following pi2, the first time it will be evaluated, will set itself to the numerical value of Pi*Pi to 50 decimal points *)
  (* Defining functions can be done with the Function function, which has attributes HoldAll *)
  fn=Function[{x,y}, x*y];
  (* As many Mathematica built-ins, Function has multiple input forms, the following is a shorthand with unnamed parameters #1 and #2, ended with the & postfix *)
  (* Third version, infix notation. Note that [Function] is a textual representation of a graphical symbol that can be more easily entered in Mathematica with the key presses: esc f n esc, many symbols can be similarly entered, try for example esc theta esc *)
  (* A second, very common way of defining functions is to use pattern matching and delayed evaluation, the following defines the fn4 symbol to evaluate the expression x*y when it's encountered with two arguments that will matched to the symbols x and y *)
  (* _ or Blank[] can match any Mathematica expression, _h matches only expressions with the Head[] h *)
  (* A symbol can have multiple matching rules *)
  fn6[0] = 1;
  fn6[x_Integer] := x*fn6[x - 1]
  (* In general pattern matching is more powerful than Function as it's really an evaluation rule, but it's slower to evaluate, thus not the best if a function has to be applied over large datasets *)
  (*Note that pattern matching can be used also with=,not only:=,but beware that=evaluates RHS,in the following fnWrong will multiply y by 3,not by the value matching test at "call" site,as test*y gets fully evaluated and test doesn't "stay" a symbol,it evaluates to its global value*)
  (* Lists are defined with {} *)
(* Elements are accessed with [[index]], indices are one-based, negative wrap-around *)
  (* Ranges are expressed with ;; or Span *)
  (* From the beginning to the second last *)
  (* Vectors and matrices are just appropriately sized lists and lists of lists *)
  (* . is the product for vector, matrices, and tensors *)
  (* Expression manipulation and CAS. ReplaceAll or /. applies rules to an expression *)
  (* Rules can contain patterns, the following will match only the x symbols that appear to a power, match the expression of the power and replace it *)
  1+x+x^2 +x^(t+n)/.{x^p_->f[p]}
  (* Mathematica has lots of functions that deal with expressions, Integrate, Limit, D, Series, Minimize, Reduce, Refine, Factor, Expand and so on. We'll show only some basic examples. Solve finds solution to systems of equations or inequalities *)
  Solve[x^2+a*x+1==0, x]
  (* It returns results as list of replacement rules that we can replace into the original equation *)
  sol=Solve[eq==0, x]
  (* Simplifying neweq yields true as the equation is satisfied *)
  (* Assumptions on the variables can be made *)
  Simplify[Sqrt[x^2], Assumptions -> x < 0]
  (* fn7 will compute the Integral and Derivative every time it's evaluated, as Function is HoldAll, fn8, using Evaluate, will force the definition to be equal to the simplified version which yields correctly back the original equation *)
  (* Many procedural programming primitives are supported *)
  For[i = 0,i < 4,i++,Print[i]]
  n=1; While[n < 4,Print[n];n++]
  (* Boolean operators are C-like for the most, only Xor is not ^ which means Power instead *)
  (* Equality tests can be chained *)
  (* == compares the result of the evaluation on both sides, === is true only if the expression are identical *)
  (* Boolean values are False and True. No output is Null *)
  (* With, Block and Module can be used to set symbols to temporary values in an expression *)
  With[{x = Sin[y]}, x*y]
  Block[{x = Sin[y]}, x*y]
  Module[{x = Sin[y]}, x*y]
  (* The difference is subtle. With acts as a replacement rule. Block temporarily assigns the value to a symbol and the restores the previous definition. Module creates an unique, temporary symbol, which affects only the occurrences in the inner scope. *)
  Block[{i = a}, i + m]
  Module[{i = a}, i + m]
  (* In general prefer Block or With, which are faster than Module. Module implements lexical scoping, Block does dynamic scoping *)
  (* Data operations. Table generates data from expressions *)
  (* Table can generate multi-dimensional arrays, i.e. matrices *)
  (* List elements can be manipulated using functional programming primitives, like Map which applies a function over a list *)
  (* Short-hand, infix notation of Map[] is /@ *)
  (* You can use MapIndexed to operate in parallel across two lists, it passes to the mapped function *)
  addLists[list1_,list2_]:=MapIndexed[Function[{element,indexList},element + list2[[indexList[[1]]]] ], list1]
  (* A more complete version of the above that is defined only on lists and asserts if the two lists are not equal size. Note the usage of ; to compound two expressions and the need of parenthesis *)
  addListsAssert[list1_List,list2_List]:=(Assert[Length[list1]==Length[list2]]; MapIndexed[Function[{element,indexList},element + list2[[indexList[[1]]]] ], list1])
  (* Or Thread can be used, which "zips" two or more lists together *)
  (* There are many functional list manipulation primitives, in general, using these is faster than trying to use procedural style programming. Extract from a list of the first 100 integers, the ones divisible by five *)
  (* Group together all integers from 1...100 in the same equivalence class modulo 5 *)
  (* Fold repeatedly applies a function to each element of a list and the result of the previous fold *)
  (* Another way of redefining Total is to use Apply, which calls a function with as arguments, the elements of a list. The infix shorthand of Apply is @@ *)
  (* Mathematica's CAS abilities also help with numerical algorithms, as Mathematica is able to infer some information from the equations passed in order to select or optimize the numerical methods *)
  (* NMinimize does constrained and unconstrained minimization, linear and nonlinear, selecting among different algorithms as needed *)
  NMinimize[{x^2-(y-1)^2, x^2+y^2<=4}, {x,y}]
  (* NIntegrate does numerical definite integrals. Uses Monte Carlo methods for many-dimensional integrands *)
  NIntegrate[Sin[Sin[x]], {x,0,2}]
  (* NSum approximates discrete summations, even to infinites *)
  (* Many other analytic operators have numerical counterparts, like NLimit, ND and so on... *)
  (* Mathematica's plots produce Graphics and Graphics3D outputs, which the notebook shows in a graphical interface *)
  (* Graphics are objects that can be further manipulated, Show combines different graphics together into a single one *)
  (* GraphicsGrid on the other hand takes a 2d matrix of Graphics objects and displays them on a grid *)
  (* Graphics and Graphics3D can also be used directly to create primitives *)
  (* Most Mathematica functions accept a list of options as the last argument. For Plots an useful one is to override the automatic range. Show by default uses the range of the first Graphics so it will cut the second plot here: *)
  (* Forcing to show all the plotted data *)
  Show[Plot[x^2,{x,0,1}],Plot[x^3,{x,1,2}], PlotRange->All]
  (* Very handy for explorations is the ability of having parametric graphs that can manipulated. Manipulate allows for a range of widgets to be displayed next to the output of an expression *)
  (* Manipulate output is a Dynamic cell, which is special as it get automatically re-evaluated if any of the symbols it capture changes. That's why you can see Manipulate output behaving "weirdly" if you change symbols that are used to compute its output. This allows for all kind of "spreadsheet-like" computations and interactive applications. *)
  (* Debugging functional programs can be daunting. Mathematica offers a number of primitives that to a degree help. Monitor generates a temporary output that shows the computation in progress. Here the temporary output is a ProgressIndicator graphical object. Evaluations can be aborted with Alt+. *)
  Monitor[Table[FactorInteger[2^(2*n)+1],{n,1,100}], ProgressIndicator[n, {1,100}]]
  (* Another example, we assign the value of the function to be minimized to a local symbol, so we can display how it changes as the algorithm progresses *)
  (* Print forces an output from intermediate computations *)
  (* Mathematica also supports reflection, via Names, Definition, Information and more *)
  (* Performance tuning. A first common step is to reduce the number of results Mathematica will keep around for % *)
  (* Evaluate current memory usage *)
  (* Share[] can sometimes shrink the memory usage by making Mathematica realize that certain subexpressions can be shared, it prints the amount of bytes saved *)
  (* Reflection can be used to know which symbols are taking the most memory *)
  (* Timing operations is simple with AbsoluteTiming *)
  (* Mathematica's symbolic evaluation is relatively slow. Machine numbers operations are faster, but slow compared to other languages. In general Mathematica is not made for high-performance, and if that's needed it's best to directly go to one of the ways it supports external compilation: LibraryLink, CudaLink, and OpenCLLink *)
  (* On the upside, many list-based operations are trivially parallelizable via Parallelize *)
  (* The downside is that only a few functions seems to be natively parallelized, mostly image-related, and many others require manual parallelization via domain-splitting. E.G. integrals *)
  (* Even plotting ca be parallelized, see http://mathematica.stackexchange.com/questions/30391/parallelize-plotting. Intra-thread communication is expensive, beware of the amount of data you move! *)
  (* There is a Compile functionality that can translate -some- Mathematica expressions into bytecode or C code, even parallelizing, but it'http://mathematica.stackexchange.com/questions/1096/list-of-compilable-functions *)