Introduction to Programming with Mathematica, Third Edition

4.2: Functions for manipulating expressions

4.2 Functions for manipulating expressions

Three of the most powerful and commonly used functions by experienced Mathematica programmers are Map, Apply, and Thread. They provide very sophisticated ways of manipulating expressions in Mathematica. Becoming familiar with them is essential to functional programming in Mathematica. In this section we will discuss their syntax and look at some simple examples of their use. We will also briefly look at some related functions ( Inner and Outer), which will prove useful in manipulating the structure of your expressions. These higher-order functions will be used throughout the rest of this book.

Map

Map applies a function to each element in a list.

<span class="inlinequation"><span class="inlinemediaobject"><img alt="Image from book"> border="0"> height="33"> id="IMG_90"> src="https://images.books24x7.com/bookimages/id_10817/figu78_1.jpg"> title=""> width="229"></span></span>    <i class="emphasis">Out[1]</i>= {Integer, Rational, Symbol}

This is illustrated using an undefined function f and a simple linear list.

    <i class="emphasis">In[2]</i>:= <b class="bold">Map [f, {a, b, c}]</b>    <i class="emphasis">Out[2]</i>= {f [a], f [b], f [c]}

More generally, mapping a function f over the expression g [a,b,c] essentially wraps the function f around each of the elements of g.

    <i class="emphasis">In[3]</i>:= <b class="bold">Map [f, g [a, b, c]]</b>    <i class="emphasis">Out[3]</i>= g [f [a], f [b], f [c]]

So this general computation is identical to Map [f, {a, b, c}], except in that example g is replaced with List

UNLIMITED FREE ACCESS TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Inorganic Chemicals and Compounds
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.