# Three-Dimensional Mathematica Graphics (Section 6.1)

### Extract from Mathematica Graphics: An Intensive Tutorial

#### by Tom Wickham-Jones

This HTML document is based on Mathematica Graphics: An Intensive Tutorial by Tom Wickham-Jones. It was adapted by Martin Kraus for non-commercial use.

All other product names mentioned are trademarks of their producers.

Copyright 1992 by Wolfram Research, Inc.

All rights reserved. No part of this document may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the copyright holder.

## 6.1 Introduction to Mathematica Programming

One of the most powerful aspects of Mathematica graphics is the availability of the rest of the system. This provides many capabilities and functions which are not normally present in a graphics application. One can use the built-in Mathematica functions which implement a wide range of numerical techniques such as root-finding, function minimization, numerically solving differential equations or quadrature, and then graph the results. Further still one can write code to construct primitives which draw a special type of picture.

For more information on Mathematica programming consider the book Mathematica: A System for Doing Mathematics by Computer, Third Edition, by Stephen Wolfram, Mathematica conference notes which are available from Wolfram Research or books on the subject such as Programming in Mathematica, Third Edition by Roman Maeder or Mathematica: A Practical Approach by Nancy Blachman.

### Mathematica Patterns and Rules

One of the unique things about Mathematica programming is the ease and economy with which rules are given to describe how a function should behave. If you do not like the way it works, add a rule to make it work just as you desire.

 f matches exactly f f[_] matches f[ thing ] f[x_] matches f[ thing ] and gives thing a name
Some simple Mathematica patterns.
 This gives a rule for f. In:= f := foo This evaluates since a rule matches the input. In:= f Out= foo This does not evaluate since there is no rule to match it. In:= f Out= f This will match f[ thing ]. In:= f[_] := bar Now this matches a pattern and the result is returned. In:= f Out= bar This does not match since a pattern has only been given for f of one argument. In:= f[2, 3] Out= f[2, 3] This will clear all the rules of f. In:= Clear[f] This gives thing a name, namely x. In:= f[x_] := x^3 - x^2 This matches. In:= f Out= 100 And this matches the pattern as well. In:= f[(a+b)] Out= -(a + b)^2 + (a + b)^3

More complex patterns can be given to match multiple arguments, to refer to particular types of inputs and to accept conditions. The book Mathematica: A System for Doing Mathematics by Computer, Third Edition describes patterns in considerably more detail.

### Delayed Evaluation

Often in these notes we see = and := which are the assignments functions. The first evaluates the right-hand side before the assignment is made. The second, := which is delayed assignment, does not evaluate the right-hand side until the left-hand side is evaluated.

 = evaluate and then assign := assign but do not evaluate
 This evaluates before assignment. In:= f1 = Random[] Out= 0.0560708 These are all the same since f1 has been set to the value of the random number. In:= Table[f1, {5}] Out= {0.0560708, 0.0560708, 0.0560708, 0.0560708, 0.0560708} This makes a delayed assignment. In:= f2 := Random[] We now see a list of different and random numbers. This is because the evaluation was delayed. In:= Table[f2, {5}] Out= {0.6303, 0.359894, 0.871377, 0.858645, 0.584579} In:= ??f1 Global`f1 f1 = 0.05607079483486153 The settings of the ?? clearly show the difference. In:= ??f2 Global`f2 f2 := Random[]

Delayed assignment avoids evaluating the right-hand side. There are a number of other functions which do not evaluate their arguments. An example is ParametricPlot3D which possesses the attribute HoldAll delaying the evaluation of its arguments. An attribute is a property of a symbol which affects it behavior. In the example here the attribute HoldAll controls expressions of which the symbol is the head. Attributes are discussed in further detail in The Mathematica Book.

 HoldAll a HoldAll function will delay argument evaluation Evaluation[ expr ] evaluates this even if in a HoldAll function Hold[ expr ] do not evaluate expr ReleaseHold[ expr ] remove Hold if present
Other ways to delay evaluation.
 This shows that ParametricPlot3D delays evaluation of the arguments. In:= Attributes[ParametricPlot3D] Out= {HoldAll, Protected} This would not plot three functions without the Evaluate. In:= ParametricPlot3D[Evaluate[Table[{x, x^n, x^(2 n)}, {n, 3}]], {x, 0, 1}] Out= - Graphics3D - This does not evaluate due to Hold. In:= Hold[1 + 5] Out= Hold[1 + 5] ReleaseHold removes the Hold and evaluation proceeds. In:= ReleaseHold[%] Out= 6

### Working with Lists

Mathematica lists are fundamental to the whole system. A good understanding of the power of list manipulations is a key to an understanding of Mathematica itself. There are many functions which work with lists and here we review some of the most basic.

 Table construct a list Map[f, list] travel down list applying f to each element Apply[f, list] make f the head of list Transpose[{list1, list2}] transpose {list1, list2}
Some useful commands for working with lists.
 Make a list. In:= list = Table[i, {i, 5}] Out= {1, 2, 3, 4, 5} This travels down the list applying g to everything. It returns a list as the result. In:= Map[g, list] Out= {g, g, g, g, g} In:= Map[Sqrt, list] Out= {1, Sqrt, Sqrt, 2, Sqrt} This exchanges the head of list with g. In:= Apply[g, list] Out= g[1, 2, 3, 4, 5] Of course the result may evaluate further. Using Times will multiply all the numbers together. In:= Apply[Times, list] Out= 120 In:= list1 = {a, b, c} Out= {a, b, c} In:= list2 = {1, 2, 3} Out= {1, 2, 3} This is an extremely useful way to collect things together. In:= Transpose[{list1, list2}] Out= {{a, 1}, {b, 2}, {c, 3}}

### Pure Functions

A command like Map will march down a list and apply a function to each element of the list. The examples were all of things like Sqrt and were deliberately chosen to be functions which only took one argument which was used to build an expression. For example Sqrt would build things of the type Sqrt which will evaluate further. But what do you do if you have a function which takes two arguments or one which uses its argument at a deeper level?

For an example let us say we want to go down a list and record whether the elements are greater or less than some number. We could make a function to do this.

 This is my function. In:= newfun[x_] := Greater[x, 5] This does the job. In:= Map[newfun, {2, 4, 6, 8}] Out= {False, False, True, True}]

However this is not very satisfactory. I may want to compare my list with many different numbers and not just 5 as in this example. One solution is to use a pure function. This is a function which will put its argument at any level.

 This is a pure function. It is labeled as a pure function by the & and the # shows where the argument will go. In:= Greater[#, 5]& Out= #1 > 5 & This applies the pure function. When this is evaluated the 10 will replace the #, called the slot, and this evaluates. In:= Greater[#, 5]&  Out= True This combines the pure function with Map. In:= Map[Greater[#, 5]&, {2, 4, 6, 8}] Out= {False, False, True, True}]

### Blocks and Modules

If you have lines of code with a common purpose and wish to group them together this may be done with either Block or Module. These allow you to collect commands together and provide tools for local variables.

 The result of Block is the last evaluation. In:= blockfun[n_] := Block[{i, res}, res = Table[i, {i, n}]; res = Partition[res, 2]; Transpose[res]] In:= blockfun Out= {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}} Module is very similar to Block. The difference is the treatment of local variables. In:= modfun[n_] := Module[{i, res}, res = Table[i, {i, n}]; res = Partition[res, 2]; Transpose[res]] In:= modfun Out= {{1, 3, 5, 7, 9, 11}, {2, 4, 6, 8, 10, 12}}