- Recurrence relations and a series expansion for ArcSin
- Mathematica memory management
- Some problems with series expansions
- Naming conventions in Mathematica

Recurrence relations and a series expansion for ArcSin

#### Question:

Can **Mathematica** generate a series expansion for the arcsin? Can it find a recurrence relation for the terms in the series?

#### Answer:

The

Series

function generates a power series expansion of a function. The general usage of

Series

is:

Series[ f[x], {x, x0, n}]

This generates a series expansion of the function f about x = x0 of order n in (x - x0) [**The Mathematica Book 5th Edition,** pp 100-101]. The expansion of ArcSin[x] about x0 = 0 to order 13 is therefore:

Series[ArcSin[x], {x, 0, 13}]

3 5 7 9 11 13

x 3 x 5 x 35 x 63 x 231 x 14

x + -- + ---- + ---- + ----- + ------ + ------- + O[x]

6 40 112 1152 2816 13312

Examining these terms leads to the conclusion that each term is of the form:

2 (2n)! (2n+1)

------------- (x/2)

(n!)^2 (2n+1)

Test this hypothesis by using the **Mathematica** function SymbolicSum. The SymbolicSum function

SymbolicSum[expression, {i, imin, imax}]

attempts to find a value for:

imax

Sum(expression_{i})

i=imin

`SymbolicSum` is not a built in part of **Mathematica**, but is part of the standard Algebra package, read it in:

Needs["Algebra`SymbolicSum`"]

Now use SymbolicSum to compute:

2 (2n)!

infinity

Sum ------------- (x/2)^{(2n+1)}

n=0

(n!)^2 (2n+1)

SymbolicSum[2 (2n)!/((n!)^2 (2n+1)) (x/2)^(2n+1), {n, 0, Infinity}]

ArcSin[x]

This shows that the series is a representation for the arcsin of x.

A recursion relation expresses a relationship between successive members of a series. The general expression for the nth term in this series is known from the above. This can be used to express the nth term as a function of the`(n -1)`^{th} term.

Obtain an expression for the`n`^{th} term:

termn = 2 (2n)!/((n!)^2 (2n+1)) (x/2)^(2n+1)

1 1 + 2 (1 + n) 1 + 2 (1 + n)

2 (-) x (2 (1 + n))!

2

----------------------------------------------

2

(1 + 2 (1 + n)) (1 + n)!

And one for the`(n - 1)`^{th} term

termnm1 = termn /. n->n - 1

1 1 + 2 (-1 + n) 1 + 2 (-1 + n)

2 (-) x (2 (-1 + n))!

2

------------------------------------------------

2

(1 + 2 (-1 + n)) (-1 + n)!

Then divide the`n`^{th} term by the`(n - 1)`^{th} term to find the relationship between the two.

ratio = termn / termnm1

Out[53]=

1 -2 (-1 + n) + 2 n -2 (-1 + n) + 2 n

((-) (1 + 2 (-1 + n)) x

2

2 2

(-1 + n)! (2 n)!) / ((1 + 2 n) (2 (-1 + n))! n!

Try to simplify it:

Out[56]=

2 2

(-1 + 2 n) x (-1 + n)! (2 n)!

-------------------------------

2

4 (1 + 2 n) (2 (-1 + n))! n!

**Mathematica** does not seem to do very well with simplifying expressions involving factorials. Provide it with some help by explicitly supplying some substitutions.

Simplify[% /. { n! -> n (n-1)!, (2n)! -> (2n)(2n-1) (2(n-1))! }]

2 2

(-1 + 2 n) x

--------------

2

2 n + 4 n

Hence:

2 2

(-1 + 2 n) x

term = -------------- term

n 2 n-1

2 n + 4 n

With this relationship, all the terms in the series can be generated once one is given. To make use of this, start by defining the first term:

term[0, x_] := x

Then define a function that, given the`(n - 1)`^{th} term, returns the`n`^{th} term.

term[n_, x_] := term[n, x] =

(2n-1)^2 x^2 / (2n (2n+1)) t[n-1, x]

The use of`term[n_, x_] := term[n, x] = ... `causes the value of the function evaluated at a given n and x to be stored. Otherwise they would have to be recomputed every time they were used. This method makes it possible to compute the series expansion to arbitrary order, and produces a significant savings in time.

Sum[term[n, x], {n, 0, 6}]

3 5 7 9 11 13

x 3 x 5 x 35 x 63 x 231 x

x + -- + ---- + ---- + ----- + ------ + -------

6 40 112 1152 2816 13312

Timing[Sum[termn, {n, 0, 600}];]

{6.53 Second, Null}

And then use the recurrence relation:

Timing[Sum[term[n, x], {n, 0, 600}];]

{1.94 Second, Null}

Mathematica Memory Management

#### Question:

I keep running out of memory, what can I do?

#### Answer:

The rapidly increasing computational capabilities of personal computers are making them more attractive for solving larger and more complex problems. However, many components of these systems are not keeping pace with the advances in CPU speed. Specifically, many personal computers are lacking in memory or memory management capabilities. The techniques discussed below allow more and larger problems to be addressed with limited memory.

##### Removing Unused Variables

The **ClearAll** command removes all information associated with a variable. To clear an unused variable x, use:

In[10]:=

ClearAll[x]

Wild card patterns can also be used with **ClearAll**. Most user defined variables and functions are in the Global` context. All of the variables and functions in the Global` context can be removed from memory with:

In[6]:=

ClearAll["Global`*"]

Values for derivatives, i.e. f'[x] = -3Cos[3x], are cleared with:

In[7]:=

Clear[Derivative]

##### Clearing the command history

All commands and their results are stored in the arrays In[n] and Out[n] respectively. This history can be cleared with the commands

In[50]:=

Unprotect[ In, Out ]

Out[50]=

{In, Out}

In[51]:=

Clear[ In, Out ]

In[52]:=

Protect[ In, Out ]

Out[52]=

{In, Out}

The % notation can not be used to refer to results cleared by this procedure.

##### An example

The session below uses the MemoryInUse function to display the amount of memory **Mathematica** is using. The initial amount of memory in use could be slightly reduced by removing some of the packages from the **Mathematica** StartUp directory. A large array is allocated as an example of how memory might be consumed by **Mathematica**. Finally, the memory is cleared.

On startup check how much memory, in bytes, we are using. This example was run on a 64 bit Unix system.

In[1]:=

MemoryInUse[]

Out[1]=

904928

Allocate a large array. This is indicative of the memory intensive operations that can be done in * Mathematica*.

In[2]:=

Array[BigArray, {1000, 1000}];

In[3]:=

MemoryInUse[]

Out[3]=

60950688

Attempting a second, similar, operation would exceed the available memory. First clear the variable BigArray.

In[4]:=

Clear[BigArray]

In[5]:=

MemoryInUse[]

Out[5]=

60952032

The memory has not been freed. This is because BigArray is still referenced by the Out[] array. Clearing Out[] removes the final reference to BigArray and allows Mathematica to reclaim the memory.

In[6]:=

Unprotect[ In, Out ]

Out[6]=

{In, Out}

In[7]:=

Clear[ In, Out ]

In[8]:=

Protect[ In, Out ]

Out[8]=

{In, Out}

In[9]:=

MemoryInUse[]

Out[9]=

908248

Once Out[]

is cleared, almost all of the memory is reclaimed.

##### The Cleanslate package

The Cleanslate package provides an automatic procedure for clearing large amounts of memory from Mathematica. There are 3 functions exported from the package: CleanSlate, CleanSlateExcept, and ClearInOut.

ClearInOut[]

Clears the In[] and Out[] values (as shown above), and resets the $Line number to 1 (so new input begins as In[1]).

CleanSlate[]

Tries to purge everything that has happened since the CleanSlate package was read in. You can also specify specific contexts for purging with CleanSlate["Context1`","Context2`", ...]. Only the listed contexts will be affected.

CleanSlateExcept["Context1`","Context2`", ...]

CleanSlateExcept allows you to specify a set of contexts to be spared from purging. Everything other than what you list will be purged.

##### The Share Command

The Share command causes * Mathematica* to conserve memory by sharing subexpressions. This command should be reissued periodically.

##### The Front End

Mathematica is divided into two parts. The front end handles interactions with the user such as displaying plots. The kernel carries out the mathematical computations. When you start Mathematica you actually start only the front end. The front end will in turn start the kernel only when a calculation is required. Except for a slight delay in the first calculation this should be transparent. The other occasion when the separation betwee n the front end and the kernel is likely to become evident is if your computer has enough memory to start the front end, but not enough to start the kernel. In this case you will receive a message that the kernel was unable to start.

##### Write the notebook to disk

Graphics consume most of the front end's memory. Normally, all of the graphics produced during a session must be stored in memory. However, if the notebook containing the graphics has been saved since the graphics was produced, the front end will flush the graphics from memory, and recover it from disk when needed. This makes it highly desirable to save the notebook to disk after every graphics intensive calculation. Indeed, the *Auto Save after Each Result* option, located in the *Action* pulldown menu, causes the front end to automatically save the notebook after every calculation.

##### Clear the clipboard

Whenever possible use **Paste and Discard** rather than **Paste**.

##### Suppress the display of intermediate results

Appending a semicolon to **Mathematica** input suppresses the display of the corresponding *Out[n]* expression. This saves more time and memory than one might expect because formatting calculations are not done for expressions that are not displayed.

##### Memory use and timing for a normal Mathematica session

The session below shows the memory and time used when no effort is made to conserve memory.

In[1]:=

MemoryInUse[]

Out[1]=

567976

In[2]:=

MyExp = Expand[(x+ 2xy + y)^40]

Out[2]

40 39 38 2 37 3

x + 80x xy + 3120*x xy + 79040x xy +

36 4 35 5

1462240x xy + 21056256x xy +

34 6 + 33 7

245656320x xy + 2386375680x xy +

.

.

.

In[3]:=

MemoryInUse[]

Out[3]=

723884

In[4]:=

TimeUsed[]

Out[4]=

33.9

##### Memory use and timing for a Mathematica session with output suppressed

This session shows how even a small effort can produce a significant savings in both time and memory.

In[1]:=

MemoryInUse[]

Out[1]=

567548

In[2]:=

MyExp = Expand[(x + 2xy + y)^40];

In[3]:=

MemoryInUse[]

Out[3]=

656812

In[4]:=

TimeUsed[]

Out[4]=

23.3667

##### Suppressing graphics output

Graphics are one of the most memory intensive aspects of Mathematica. To suppress the display of graphical output, use the

DisplayFunction->Identity

option to the command generating the graphics.

MyGraph = Plot[Sin[x], {x, 0, 2Pi}, DisplayFunction->Identity]

The plot can then be manipulated through the variable

MyGraph

. The following command displays the plot.

Show[MyGraph, DisplayFunction -> $DisplayFunction]

The a postscript version of the plot can also be written directly to disk.

Display["MyGraph.ps", MyGraph]

Such a PostScript file can be read back into **Mathematica** with the !! command:

!!MyGraph.ps

This loads the PostScript into a cell as text. To display the PostScript as a **Mathematica** graphic, first select the cell containing the PostScript, then choose the **Graphics** style from the **Cell Style** submenu of the **Style** pulldown menu.A postscript file written in this way can also be displayed directly by the front end, without the need to startup a kernel. Use the** Open...** option from the **File** pulldown menu to open the PostScript file. Then use the procedure described above to display the PostScript as a **Mathematica** graphic.

##### Mac Specific Hints

The Mac front end keeps both a PostScript and PICT version of the graphics that it displays. Usually the PostScript version can be discarded. This is done by first select the graphics, then select *Convert to PICT...* from the graph menu.

Clear the clipboard. From the **Edit** pulldown menu select **Convert Clipboard...** then click on **Empty**. This clears any text or graphics from the clipboard. This can be combined with a paste operation by selecting **Paste and Discard.**