Introduction to Functions

Math 213 - Math with Mathematica
Christopher Hanusa

Overview

In this tutorial, we ease into getting Mathematica to do what we want it to do, by creating user-defined functions.  
We will visit a few useful resources along the way, which are useful for more than just learning about functions.

Hazrat’s “Mathematica: A Problem-Centered Approach”

Visit: http://link.springer.com.queens.ezproxy.cuny.edu:2048/book/10.1007/978-1-84996-251-3  (If you are off-campus, you will have to log in using your QC library card.)
Download Chapter 2: Defining functions, and read Pages 19 through the top of 21.

Concept Check

1. Explore Problem 2.1 on page 21 (A worked example) by trying it in this notebook. What happens if you nest the f function more times?  What coefficients appear?  Do you see a pattern?

2. Recall the @ and // operators.  Define square[x] to be x^2.  Write the three different ways to calculate 100^2 using the function square.

Using a function repeatedly

Suppose we have defined a function and we want to use the function repeatedly.  There are three important commands that allow you to do this, Map, Apply, and Nest.

Key Command: Map

An extremely important command is the Map command.  What Map[function,list] does is to take the function and evaluates the function at each entry of the list individually and returns the results in a list. For example, if you want to know the values of Sin[x] for multiples of Pi/6, you can Map the Sin function onto the desired list.

piList=Range[0,2Pi,Pi/6]

Map[Sin,piList]

More generically, if your list is {e1,e2,e3} and your function is fcn, then Map will return a list with f evaluated at each entry:

Map[fcn,{e1,e2,e3}]

Ask for help to see the syntax:

?Map

The shorthand for Map is /@ (slash-at).

fcn/@{e1,e2,e3}

Wolfram’s “Elementary Introduction”

A tutorial that goes at a leisurely and in-depth pace is Wolfram’s new “An elementary introduction to the Wolfram Language”.  Read through the section on “Ways to Apply Functions” here: http://www.wolfram.com/language/elementary-introduction/25-ways-to-apply-functions.html

Concept Check

3. Explain the difference in the outputs between Framed @ {x,y,z} and Framed /@ {x,y,z}.

4. Complete Exercise 25.2 from that webpage.

5. Complete Exercise 25.9 from that webpage.

Hint 1. (Open to see.)

Use the command Characters.

Hint 2. (Open to see.)

You may be running into issues if the input to Characters is not a String.

Hint 3. (Open to see.)

Characters["wolfram"]

6. Create a function that takes as an input a word of text and outputs its total of the letter numbers, like in Problem 5.

Apply

A somewhat related command is the Apply command.  Apply is useful when you have a list that you want to serve as the inputs of a function.  For example, suppose you have the list {1,100,3} and you would like to calculate Range[1,100,3].  Then you can use the command

Apply[Range,{1,100,3}]

The shorthand for Apply is @@.

Range@@{1,100,3}

Ask for help to see the syntax:

?Apply

What Apply[function,list] does for us is to take the function and it applies the function to the entire list.  Technically it’s changing the “type” (head) of the object, for instance above the type has changed from List to Range.

Example: Determine the sum of the cube of the digits of the number 156.

Use the function IntegerDigits to determine the digits of a number, then square each digit, and finally apply the function Plus to the result.

digits=IntegerDigits[156]

digits^3

Apply[Plus,digits^3]

Example: Define a function that takes in an integer and outputs the sum of the cube of its digits.

We can let n be the integer that is taken in, and use the above steps to see that the correct output should be Apply[Plus,IntegerDigits[n]].  Wrap this into a function by:

cubeOfDigits[n_]:=Apply[Plus,IntegerDigits[n]^3]

cubeOfDigits[156]

cubeOfDigits[342]

Read more: Hazrat’s “Mathematica: A Problem-Centered Approach”

Visit: http://link.springer.com.queens.ezproxy.cuny.edu:2048/book/10.1007/978-1-84996-251-3  (If you are off-campus, you will have to log in using your QC library card.)
Download Chapter 4: Changing Heads, and read the first two pages (Pages 47 and 48).

Concept Check

7. For the list {i^2,{i,1,10}}, use the Apply and Table commands to get a list of the squares of the numbers from 1 to 10.

list={i^2,{i,1,10}};