Numbers

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

Accuracy  
Append  
Apply  
Array  
Base  
BaseForm  
Chop  
Complement  
ComposeList  
Count  
Delete  
Drop  
First  
FixedPoint  
FixedPointList  
Flatten  
FlattenAt  
FoldList  
Head  
Insert  
Integer`exact  
Intersection  
Join  
Last  
Lists`combining  
Lists`constructing  
Lists`functions  
Lists`grouping  
Lists`modifying  
Lists`nested`rearranging  
Lists`pieces  
Lists`rearranging  
Lists`searching  
MantissaExponent  
Map  
N  
Nest  
NestList  
Number`approximate  
Number`complex  
Part  
Partition     
Position  
Precision  
Precision`arbitrary  
Precision`machine  
Prepend  
Rationalize  
ReplacePart  
Rest  
Reverse  
RotateLeft    
RotateRight  
Sort  
Table  
Take  
Thread  
Transpose  
Union

Numbers

Syntax (see page 534 )

Exact Integer

123  is an exact integer.

Approximate Real Number

123.  is an approximate real number.

123.00000000000000000000000  is an approximate real number with a certain precision.

Complex Number

123.+ 0I   is a complex number with approximate real number components.

Head

Head[numbr ] gives the type of a number.

N

N[x, n ] converts x  to an approximate real number with at most n digits of precision.

Rationalize

Rationalize[x ] gives a rational approximation to x .

Number in Base b

b  ^^nnnn   gives a number in base b.

BaseForm

BaseForm[x, b ] prints with x  in base b.

MantissaExponent

MantissaExponent[x ] gives a list containing the mantissa and exponent of x..

Precision

Precision[x ] gives the total number of significant decimal digits in x.

Accuracy

Accuracy[x ] gives the number of significant digits to the right of the decimal point in x..

N -Digit Precision

N[expr, n ] evaluates expr  numerically using numbers with n -digit precision.

N[expr ] or expr //N   evaluates expr  numerically using machine-precision numbers.

Chop

Chop[expr ] replaces all approximate real numbers in expr  with magnitude less than 10^(-10) by 0.

Chop[expr, dx ] replaces all approximate real numbers with magnitude less than dx  by 0.

Finding the Type of a Number

Head[ 123 ]

Head[ 123. ]

Head[123.000000000 ]

Head[ 123. + 0 I ]

Head[ 123. + 0. I ]

Head[ 123. + 3 I ]

Changing the Precision of a Number

N[ 3/ 7, 30]

N[%, 20]

Finding a Rational Approximation

Rationalize[.428571428571428]

Rationalize[ N[Pi] ]

Converting to Base 2

The number 37 in base 10 is 100101 in base 2.

BaseForm[37, 2]

Converting to Base 10

The number 100101 in base 2 is 37 in base 10.

2^^100101

Mantissa and Exponent

MantissaExponent[3.45 10^125]

Precision and Accuracy

xacc= 4311237365141527287.393872671651

Precision[ xacc ]

Accuracy[ xacc ]

Evaluating with Specified Precision

N[Pi^25, 30]

A Note on Exact, Arbitrary-Precision, and Machine-Precision Numbers

When you type in an integer like 7 or a fraction like 3/7, Mathematica  assumes it is exact. If you type in a number like 4.5, with an explicit decimal point, Mathematica  assumes that it is an approximate real number that is accurate only to a fixed number of decimal places.

In general, Mathematica  distinguishes two kinds of approximate real numbers: arbitrary-precision ones and machine-precision ones. Arbitrary-precision numbers can contain any number of digits, and their precision is adjusted during computations. Machine-precision numbers, on the other hand, contain a fixed number of digits, and their precision remains unchanged during computations. Machine-precision numbers work by making direct use of the numerical capabilities of your underlying computer system. As a result, computations with them can be done quickly. However, machine-precision numbers are much less flexible than arbitrary-precison ones. For more information, see page 534.

Chopping Numbers near Zero

Exp[ N[ 2 Pi I] ]

Chop[ % ]

Lists

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

Accuracy  
Append  
Apply  
Array  
Base  
BaseForm  
Chop  
Complement  
ComposeList  
Count  
Delete  
Drop  
First  
FixedPoint  
FixedPointList  
Flatten  
FlattenAt  
FoldList  
Head  
Insert  
Integer`exact  
Intersection  
Join  
Last  
Lists`combining  
Lists`constructing  
Lists`functions  
Lists`grouping  
Lists`modifying  
Lists`nested`rearranging  
Lists`pieces  
Lists`rearranging  
Lists`searching  
MantissaExponent  
Map  
N  
Nest  
NestList  
Number`approximate  
Number`complex  
Part  
Partition     
Position  
Precision  
Precision`arbitrary  
Precision`machine  
Prepend  
Rationalize  
ReplacePart  
Rest  
Reverse  
RotateLeft    
RotateRight  
Sort  
Table  
Take  
Thread  
Transpose  
Union

Constructing Lists

Syntax (see page 118)

Table

Table[f, {imax }] gives a list of imax  values of x.

Table[f, {i, imax }] gives a list of values of f  as i  runs from 1 to imax.

Table[f, {i, imin, imax }] gives a list of values with i  running from imin  to imax.

Table[f, {i, imin, imax, di }] uses steps of di.

Table[f, {i, imin, imax }, {j, jmin, jmax }, ... ] generates a multidimensional table.

Array

Array[a, {m, n }] builds an  m  by n   symbolic array with elements a [i, j ].

Making a List

Note that if the starting value of the index is not given, it is assumed to be 1.

Table[i^2, {i, 6}]

Table[ Sin[ n Pi/5], {n, 0, 4}]

Displaying in Tabular Form

Table[ Sqrt[x], {x, 0, 1, 0.25}]

TableForm[ % ]

3 by 3 Table

Table[ x^i + y^j, {i, 3}, {j, 3}]

Table with a Repeated Element

Table[x, {4} ]

Nested List

Table[ i j^2 k^3, {i, 2}, {j, 2}, {k, 2}]

Assigning Values

Note that the values of the elements of the array can be assigned in any order. Values that are not assigned remain in symbolic form.

arr = Array[ a, {2, 3}]

a[1, 3] = 5

a[2, 2] = 3

arr

Getting Pieces of Lists

Syntax (see page 125)

Part

list [[ n ]] or Part[list, n ] gives the n th sublist in list.

list [[ - n ]] or Part[list, -n ] gives the n th sublist from the end.

list [[{n1, n2, ...}]] or Part[list, {n1, n2, ...}] gives a list of the n1 th, n2 th, ...  parts of list.

list [[n, m, ... ]] or Part[list, n, m, ...] gives the part of the list corresponding to list [[ n ]] [[ m ]]....

First

First[list ] gives the first element in list.

Last

Last[list ] gives the last element.

Take

Take[list, n ] gives the first n  elements in list.

Take[list, -n ] gives the last n  elements in list.

Take[list, {m, n }] gives elements m  through n  (inclusive).

Rest

Rest[list ] gives list  with the first element dropped.

Drop

Drop[list, n ] gives list  with its first n  elements dropped.

Drop[list, -n ] gives list  with its last n  elements dropped.

Drop[list, {m, n }] gives list  with elements m  through n  dropped.

Using Part

t = {{a, b, c}, {d, e, f}}

t[[1]]

t[[1, 2]]

t[[1]][[2]]

t[[{2, 2, 1}]]

t[[{2, 2, 1}, {2, 3}]]

Using First and Last

s = {a, b, c, d, e, f, g}

First[ s ]

Last[ s]

Using Take

s = {a, b, c, d, e, f, g}

Take[ s, 3]

Take[ s, -3]

Take[ s, {2, 5}]

Using Rest and Drop

s = {a, b, c, d, e, f, g}

Rest[ s ]

Drop[ s, 3]

Drop[ s, {3, 3}]

Searching List Elements

Syntax (see page 127)

Position

Position[list, form ] gives the positions at which form  occurs in   list .

Count

Count[list, form ] gives the number of times form  appears as an element of list .

Positions of a Specified Form

Position[{a, b, c, a, b}, a]

Number of Times a Specified Form Appears

Count[{a, b, c, a, b}, a]

Positions in a Matrix

m = IdentityMatrix[3]

Position[m, 0]

Adding, Removing and Modifying List Elements

Syntax (see page 128)

Prepend

Prepend[list, element ] adds element  at the beginning of list .

Append

Append[list, element ] adds element  at the end of list .

Insert

Insert[list, element, i ] inserts element  at position i  in list .

Insert[list, element, -i ] inserts at position i  counting from the end of list .

Insert[list, element, {i, j, ...}] inserts at position i, j, .. in list.

Delete

Delete list, i ] deletes the element at position i  in list .

Delete[list,{i, j, ...}] deletes at position i, j, ... in list .

ReplacePart

ReplacePart[list, element, i ] replaces the element at position i  in list .

ReplacePart[list, element, -i  ] replaces  at position i  counting from the end.

ReplacePart[list, element, {i, j, ...}] replaces list [[i, j, ...]].

ReplacePart[list, element, {{i1, j1, ...},{i2,...}, ...}] replaces all parts list [[ik, jk, ...]] with the same element.

Using Prepend and Append

Prepend[{a,b,c}, x]

Append[{a,b,c}, x]

Using Insert

Insert[{a,b,c}, x, 2]

Insert[{a,b,c}, x, -2]

Using Delete

Delete[{a, b, c, d}, 3]

Replacing a Part

ReplacePart[{a, b, c, d}, x, 3]

ReplacePart[{a, b, c, d}, x, {{1}, {4}}]

Replacing in a Matrix

IdentityMatrix[3]

ReplacePart[%, x, {2,2}]

Combining Lists

Syntax (see page 129)

Join

Join[list1, list2, ...] concatenates the lists together.

Union

Union[list1, list2, ...] combines the lists, removing repeated elements and sorting the result.

Intersection

Intersection[list1, list2, ...] gives a list of elements that are common to the lists.

Complement

Complement[universal, list1, ...] gives a list of the elements that are in universal , but not in any of the lists.

Joining Lists

Join[ {a, b, c}, {x, y}, {c, {d, e}, a}]

Taking the Union

Union[{a, b, c}, {c, a, d}, {a, d}]

Taking the Intersection

Intersection[{a, c, b}, {b, a, d, a}]

Finding the Complement

Complement[ {a, b, c, d}, {a, d}]

Rearranging Lists

Syntax (see page 0)

Sort

Sort[list ] sorts the elements of list  in a standard order.

Union

Union[list ]  sorts elements, removing any duplicates.

Reverse

Reverse[list ] reverses the order of the elements in list.

RotateLeft

RotateLeft[list, n ] rotates the elements of list  n  places to the left.

RotateRight

RotateRight[list, n ] rotates the elements of list  n  places to the right.

RotateLeft[list ] and RotateRight[list ] rotate by one position.

Sorting the Elements

Sort[ {b, a, c, a, b}]

Sorting and Removing Duplicates

Union[ {b, a, c, a, b}]

Reversing a List

Reverse[{a, b, c, d}]

Rotating to the Left

RotateLeft[{a, b, c, d, e}, 2]

RotateRight[{a, b, c, d, e}, -2]

Rotating to the Right

RotateRight[{a, b, c, d, e}, 2]

Rearranging Nested Lists

Syntax (see page 131)

Transpose

Transpose[list ] interchanges the top two levels of lists.

Transpose[list, n ] interchanges the top level with the n th level.

Flatten

Flatten[list ] flattens out all levels in list.

Flatten[list, n ] flattens out the top n  levels in list.

FlattenAt

FlattenAt[list, i ] flattens out a sublist that appears at position i.

FlattenAt[list, {{i1, j1, ...}, {i2, ...}, ...}] flattens out several sublists.

RotateLeft

RotateLeft[list, {n1, n2, ...}] rotates successive levels by ni  places.

Partition

Partition[list, {n1, n2, ...}, {d1, d2, ...}] partitions successive levels into ni  element pieces with offsets di.

Using Transpose

t = {{a, b}, {c, d}, {e, f}}

Transpose[ t ]

Using Flatten

tt = Table[ i^2 +j^2 + k^2, 
                 {i, 2}, {j, 2}, {k, 2}]

Flatten[ tt ]

Flatten[ tt, 1 ]

Using FlattenAt

FlattenAt[{{a, b}, {c, d}}, 2]

A Note on Nested Lists

You will encounter nested lists if you use matrices or generate multidimensional arrays and tables. Lists are some of the most flexible and powerful objects in Mathematica .  Functional operations on lists provide one of the most conceptually and practically efficient ways to use Mathematica..  Rearranging nested lists, however, can be a complicated affair, and you will often have to experiment to get the right combination of commands.

Grouping Together Elements in Lists

Syntax (see page 131)

Partition

Partition[list, n ] partitions list  into n  element pieces.

Partition[list, n, d ] uses offset d  for successive pieces.

Partitioning a List

t = {a, b, c, d, e, f, g}

Partition[t, 2]

Partition[t, 3]

Partitioning with an Offset

t = {a, b, c, d, e, f, g}

Partition[t, 3, 1]

Functions and  Lists

Syntax (see pages 131 and  200)

Apply

Apply[f, {a, b, ...}] applies f   to a list, giving f [a, b, ...].

Map

Map[f, {a, b, ...}] applies f   to each element in a list, giving             {f  [a ], f [b ], ...}.

Thread

Thread[f [{a1, a2 }, {b1, b2 }]] threads f  over lists to give           {f [a1, b1 ], f [a2, b2 ]}.

NestList

NestList[ f, x, n ]  generates the list {x, f [x ], f [f [x ]],...}, where f  is nested up to n  deep.

Nest

Nest[f, x, n ]  gives the last element in NestList[ f, x, n ].

FixedPointList

FixedPointList[f, x ] generates the list                                        {x, f [x ], f [f [x ]],...}, stopping when the elements no longer change.

FixedPoint

FixedPoint[f, x ] gives the last element in                     FixedPointList[f, x ].

FoldList

FoldList[f, x,{a, b, ...}] creates the list                                      {x, f [x, a ], f [f [x, a ], b ], ...}.

ComposeList

ComposeList[{f1, f2, ...}, x ] generates the list                            {x, f1 [x ], f2 [f1 [x ]], ...}.

Applying a Function to a List

Apply[Plus, {a, b, c, d}]

Applying a Function to Elements of a List

Map[f, {a, b, c}]

Using Map with a Function Definition

take2[list_] := Take[list, 2]

Map[take2,{{1, 3, 4}, {5, 6, 7}, {2, 1, 6, 6}}]

Using Thread

Thread[f[{a1, a2}, {b1, b2}]]

Thread[Equal[{x, y}, {2, 3}]]

A Note on Lists as Function Arguments

Most mathematical functions in Mathematica  are set up to apply themselves separately to each element in a list.  This is true in particular of all functions that carry the attribute Listable. In other cases, such as functions that you have defined, you can control the action of the function using Apply, Map, Thread, and other operations. For more information, see pages 204, 217, and 272.

Using Nest and NestList

hump[x_] = 4 x (1 - x)

Nest[ hump, .123, 6]

NestList[ hump, .123, 6]

Using FixedPoint and FixedPointList

newton3[x_] := N[ 1/2 (x + 3/x ) ]

FixedPoint[newton3, 1.0]

FixedPointList[newton3, 1.0]

Using FoldList

FoldList[ Plus, 0, {a, b, c}]

Using ComposeList

ComposeList[ {f, g, h }, x]