utlogo
Home Software FAQs Mathematica
Mathematica PDF Print E-mail
  1. Recurrence relations and a series expansion for ArcSin
  2. Mathematica memory management
  3. Some problems with series expansions
  4. 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(expressioni)
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 nth 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 nth 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:

  Simplify[%]

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 nth 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

Compare the timings for the two different methods. First compute the series using the explicit term by term definition:

  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.

supress 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 supressed
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
Supressing 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.

Some Problems with Series Expansions

Question:

Are there any known bugs in Mathematica's Series function?

Answer:

There are a number of minor problems with the Series function. Below is a discussion of all of them that I am aware of. This information was extracted from a series of discussions on the newsgroup comp.soft-sys.math.mathematica, primarily between David Withoff of Wolfram and Peter Arnold at the University of Washington.

    In[2]:= Series[ Log[x(1-x)],{x,0,1}]

2

Out[2]= Log[x] + O[x]

The bug is that the Log of a series expansion around zero with exactly one linear coefficient returns an order term that is bigger than it should be. The bug is specific to this example. I don't know of any other manifestations. This bug has been fixed in the development version of Mathematica; contacting Wolfram technical support ( This e-mail address is being protected from spambots. You need JavaScript enabled to view it ) might produce a workaround for later versions.

    In[3]:= E^( (a - b x) (c - Log[d]) )

(a - b x) (c - Log[d])

Out[3]= E


In[4]:= Series[%,{x,0,1}]


Series::esss: Essential singularity encountered in

2

Exp[a (c - Log[d]) - b (c + ) x + O[x] ].

(a - b x) (c - Log[d])

Out[4]= Series[E, {x, 0, 1}]

Series mistakenly thinks that anything with Log in a non-constant exponent has an essential singularity. The workaround is to replace these logs with something else. This has also been fixed in the upcomming version of Mathematica.

The cases where it doesn't return the expansion to the order requested are also a potential source of trouble because one frequently wants to take the Normal of a series before processing it. If one isn't aware that Mathematica may not return the series to the order requested, mistakes can easily be made.

WRI has responded, by the way, that the fact that Series doesn't always return the order you request is *not* an error in Series; it is instead an error in the documentation.

    In[2]:= Series[Gamma[x],{x,0,0}]

1

Series::sbyc: Warning: Division by a series with no coefficients in -----.

1

O[x]

Pi

Out[2]= -----

1

O[x]

and

    In[3]:= Series[Gamma[x],{x,0,2}]

1

Out[3]= - - EulerGamma + O[x]

x

The current algorithm used by Series is unable (at least as a practical matter) to predict the order that will appear in the result. A nearly identical problem comes up in numerical computations, where cancellations and numerical instability can give low-precision output even for high precision inputs. There are several ways to solve this problem (both in series and in numerical computations), but it has not been solved in Version 2.2 of Mathematica. Since the documentation says that "Series[f, {x, x0, n}] generates a power series expansion ... to order (x - x0)^n", this behavior does not agree with the documentation. Whether this is an error in the code or in the documentation is matter of semantics.

The next example is something that doesn't work and is documented as such. It should, though, fail more gracefully.

    In[1]:= Series[Exp[x]/x^4, {x,0,-4}]

-4 -3

Out[1]= x + O[x]

In[2]:= % (x^4)^x
4

SeriesData::slnc: Argument O[x] in Log is a series with no coefficients.

$IterationLimit::itlim: Iteration limit of 4096 exceeded.

Out[2]= $Aborted

Wolfram has promised that this will be fixed in upcoming versions of Mathematica.

The problems people seem to encounter with Series are special cases of the examples listed above.

 

Naming Conventions in Mathematica

Question:

What do things like Global` or Graphics` mean?

Answer:

The full name of an object, usually a variable or a function, in Mathematica consists of two parts, a context and a short name. The full name of an object is then context`short name, where the ` is a backquote. The context is frequently a Mathematica package. This prevents local variables in one package from interfearing with local variables in another package.

When an object is referred to in Mathematica, but the context is not specified, Mathematica searches the current context then the contexts listed in $ContextPath for object.

The current context is available from the variable $Context. This shows that the default context is Global`

 In[1]:=

$Context

Out[1]=

Global`

Most user defined variables and functions are in the Global` context.

The $ContextPath by default contains:

  In[2]:=

$ContextPath

Out[2]=

{Global`, System`}

When additional packages are loaded, their contexts are automatically added to $ContextPath.

  In[3]:=

<
In[4]:=

$ContextPath

Out[4]=

{Algebra`Trigonometry`, Global`, System`}

Functions, such as TrigReduce, that are part of the Algebra`Trigonometry` package, are automatically found when called because their context is included in $ContextPath.

In most cases this process is transparent to the user. However, explicit knowledge of this mechanism is useful when writing Mathematica packages, loading packages with name conflicts, or clearing unused packages and variables from Mathematica's memory.

For additional details, see Mathematica: A system for Doing Mathematics by Computer, PP332-338.
 
College of Natural Sciences