<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Mathematica Journal</title>
	<atom:link href="http://www.mathematica-journal.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mathematica-journal.com</link>
	<description></description>
	<lastBuildDate>Fri, 17 Feb 2012 13:50:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Motion of a Spinning Top</title>
		<link>http://www.mathematica-journal.com/2012/02/motion-of-a-spinning-top/</link>
		<comments>http://www.mathematica-journal.com/2012/02/motion-of-a-spinning-top/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 20:46:31 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 14]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=22816</guid>
		<description><![CDATA[Both approximate and exact solutions for the motion of a spinning top are constructed with the help of quaternions.
Quaternion Algebra
A quaternion is a four-dimensional quantity consisting of a scalar, say , and a three-dimensional vector , collectively denoted . Addition of two quaternions is component-wise,




(1)



(we do not need to add quaternions in this article). Their [...]]]></description>
			<content:encoded><![CDATA[<p>Both approximate and exact solutions for the motion of a spinning top are constructed with the help of quaternions.<span id="more-22816"></span></p>
<h3>Quaternion Algebra</h3>
<p>A quaternion is a four-dimensional quantity consisting of a scalar, say <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_1.gif" alt="" width="8" height="12" />, and a three-dimensional vector <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_2.gif" alt="" width="6" height="12" />, collectively denoted <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_3.gif" alt="" width="52" height="12" />. Addition of two quaternions is component-wise,</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_1.gif" alt="" width="156" height="12" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:1">(1)</a></td>
</tr>
</tbody>
</table>
<p>(we do not need to add quaternions in this article). Their multiplication follows the rule</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_2.gif" alt="" width="232" height="12" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:2">(2)</a></td>
</tr>
</tbody>
</table>
<p>It is important to note that such multiplication is <em>associative</em> (even though noncommutative). This can be verified by the following.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_1.gif" alt="" width="474" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_2.gif" alt="" width="434" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_1.gif" alt="" width="89" height="12" /></p>
<p>When the scalar part of a quaternion is zero, it is called a pure quaternion. Since this constitutes an important special case, we will extend our definition of multiplication accordingly.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_3.gif" alt="" width="382" height="12" /></p>
<p>The operation of conjugation simply changes the sign of the vector part of a quaternion.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_4.gif" alt="" width="124" height="15" /></p>
<p>Based on the corresponding Taylor expansion, it is possible to evaluate various functions of a quaternion. Of these, the most important is the exponential function, which, in this article, we need only with pure-quaternion arguments.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_5.gif" alt="" width="471" height="23" /></p>
<h3>Rotation</h3>
<p>Rotating a 3D vector <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_4.gif" alt="" width="6" height="12" /> with respect to an axis through the origin (with a unit direction of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_5.gif" alt="" width="8" height="14" />) by an angle <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_6.gif" alt="" width="6" height="12" /> can be achieved by</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_3.gif" alt="" width="580" height="30" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:3">(3)</a></td>
</tr>
</tbody>
</table>
<p>where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_7.gif" alt="" width="38" height="14" /> (a vector of length <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_8.gif" alt="" width="6" height="12" /> and unit direction <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_9.gif" alt="" width="8" height="14" />), and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_10.gif" alt="" width="11" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_11.gif" alt="" width="13" height="12" /> are the parallel and perpendicular (to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_12.gif" alt="" width="8" height="12" />) parts of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_13.gif" alt="" width="6" height="12" />, respectively [<a href="#Hestenes">1</a>]. Note that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_14.gif" alt="" width="11" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_15.gif" alt="" width="8" height="12" /> commute, while <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_16.gif" alt="" width="13" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_17.gif" alt="" width="8" height="12" /> anticommute.</p>
<p>One can show that a quaternion <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_18.gif" alt="" width="9" height="12" /> has the form of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_19.gif" alt="" width="20" height="15" /> if and only if <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_20.gif" alt="" width="84" height="15" /> (a pure scalar). Alternately, the same <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_21.gif" alt="" width="9" height="12" /> can be parametrized by the three Euler angles (see the next section).</p>
<p>By taking <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_22.gif" alt="" width="37" height="12" />, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_23.gif" alt="" width="4" height="12" /> is time, we achieve constant rotation with an uniform angular velocity <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_24.gif" alt="" width="9" height="12" />. Note that</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_4.gif" alt="" width="59" height="16" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:4">(4)</a></td>
</tr>
</tbody>
</table>
<p>where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_25.gif" alt="" width="55" height="15" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_26.gif" alt="" width="9" height="16" /> denotes its time derivative.</p>
<p>The previous formula can be generalized to any time-dependent <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_27.gif" alt="" width="9" height="12" />, as can be seen by differentiating <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_28.gif" alt="" width="61" height="15" />, thus</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_5.gif" alt="" width="535" height="35" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:5">(5)</a></td>
</tr>
</tbody>
</table>
<p>where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_29.gif" alt="" width="9" height="12" /> is now the instantaneous angular velocity at time <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_30.gif" alt="" width="4" height="12" />, which may change with time. Note that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_31.gif" alt="" width="25" height="16" /> must be a pure quaternion (take the time derivative of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_32.gif" alt="" width="45" height="15" /> to see that). Now, any rotation <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_33.gif" alt="" width="9" height="12" /> can be applied to the axes of the original (inertial) system of coordinates, thus creating the corresponding new, rotating (noninertial) frame. Components of any vector <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_34.gif" alt="" width="6" height="12" /> can then be expressed in either the old, or (often and more conveniently) the new coordinates; the latter will be given by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_35.gif" alt="" width="38" height="15" />. The angular velocity <strong><img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/Omega.gif" alt="" width="8" height="19" /></strong>, in the new coordinates, will thus have components given by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_36.gif" alt="" width="33" height="16" />. This becomes an important and helpful tool: even though the laws of physics are normally valid only in inertial coordinates, the resulting equations often simplify when expressed in the rotating frame.</p>
<h3>Spinning Top</h3>
<p>Let us consider an axially symmetrical body (a &#8220;top&#8221;) of mass <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_37.gif" alt="" width="11" height="12" />, and parallel and perpendicular (with respect to the body&#8217;s symmetry axis) moments of inertia equal to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_38.gif" alt="" width="10" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_39.gif" alt="" width="12" height="12" />, respectively. We make it spin around its axis, place the bottom tip of its (usually tilted) axis on a nonslip horizontal plane (a desk), and let it continue its motion subject to the vertical gravity field. Let us also assume that the top&#8217;s center of mass is at a distance <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_40.gif" alt="" width="5" height="12" /> from its point of contact, that the gravitational acceleration is <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_41.gif" alt="" width="6" height="12" />, and that there is no friction.</p>
<p>To investigate the top&#8217;s motion, we introduce an inertial coordinate system with its origin at the point of contact and its <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_42.gif" alt="" width="5" height="12" /> direction pointing vertically upward. First we position a motionless top vertically (its symmetry axis aligned with the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_43.gif" alt="" width="5" height="12" /> direction) on the desk&#8217;s surface. We then rotate it using a fully general, time-dependent rotation <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_44.gif" alt="" width="9" height="12" />, parametrized by three Euler angles, thus</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_6.gif" alt="" width="119" height="15" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:6">(6)</a></td>
</tr>
</tbody>
</table>
<p>where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_45.gif" alt="" width="7" height="12" /> is a rapidly increasing function of time that represents the actual spinning of the top around its symmetry axis (this rotation is applied first), followed by tilting the spinning top by an angle <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_46.gif" alt="" width="5" height="12" /> (potentially a slowly varying function of time) around the inertial <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_47.gif" alt="" width="6" height="12" /> direction, further followed by slowly rotating the spinning, tilted top around the inertial <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_48.gif" alt="" width="5" height="12" /> direction by an angle <img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/Psi.gif" alt="" width="7" height="19" />, thus creating a so-called precession. Also: <img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/DoubleStruckI.gif" alt="" width="4" height="19" />, <img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/DoubleStruckJ.gif" alt="" width="5" height="19" /> (not used in the last formula), and <img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/DoubleStruckK.gif" alt="" width="7" height="19" /> are unit vectors along the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_49.gif" alt="" width="6" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_50.gif" alt="" width="6" height="12" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_51.gif" alt="" width="5" height="12" /> directions, respectively, in their pure-quaternion form.</p>
<h4>Equations of Motion</h4>
<p>We now use the Lagrange technique to find the corresponding equations for the three Euler angles. The kinetic energy of the top equals one-half of the scalar product of its angular velocity with its angular momentum, as its motion is purely rotational. In the rotating frame (this is when it becomes handy), this equals</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_7.gif" alt="" width="172" height="35" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:7">(7)</a></td>
</tr>
</tbody>
</table>
<p>where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_52.gif" alt="" width="15" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_53.gif" alt="" width="15" height="15" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_54.gif" alt="" width="15" height="14" /> are the rotating-frame coordinates of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_55.gif" alt="" width="8" height="12" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_56.gif" alt="" width="48" height="15" /> (denoted <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_57.gif" alt="" width="6" height="12" /> from now on; similarly, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_58.gif" alt="" width="10" height="12" /> will be called <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_59.gif" alt="" width="11" height="12" />) is the top&#8217;s moment of inertia with respect to a line perpendicular to its symmetry axis, and passing through the origin (which is displaced by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_60.gif" alt="" width="5" height="12" /> from its center of mass). Usually <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_61.gif" alt="" width="31" height="12" /> (a thin circular disk needs <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_62.gif" alt="" width="5" height="12" /> to be bigger than only one-half of its radius to meet this condition), but our results are valid when <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_63.gif" alt="" width="31" height="12" /> as well.</p>
<p>Subtracting the potential energy <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_64.gif" alt="" width="107" height="12" /> (by the definition of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_65.gif" alt="" width="9" height="12" />) yields the resulting Lagrangian.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_6.gif" alt="" width="385" height="67" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_2.gif" alt="" width="519" height="31" /></p>
<p>Since the result is free of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_66.gif" alt="" width="7" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_67.gif" alt="" width="7" height="12" /> (it contains only their time derivatives), the corresponding <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_68.gif" alt="" width="14" height="26" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_69.gif" alt="" width="15" height="26" /> must be constants of motion [<a href="#Goldstein">2</a>]. The remaining equation is then obtained from</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_8.gif" alt="" width="79" height="36" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:8">(8)</a></td>
</tr>
</tbody>
</table>
<p>This yields the following.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_7.gif" alt="" width="303" height="43" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_3.gif" alt="" width="174" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_4.gif" alt="" width="369" height="15" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_5.gif" alt="" width="527" height="18" /></p>
<p>The first two expressions are constants of motion, while the last one must be equal to zero.</p>
<p>Introducing a new variable <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_70.gif" alt="" width="8" height="12" /> by</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_9.gif" alt="" width="127" height="39" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:9">(9)</a></td>
</tr>
</tbody>
</table>
<p>the last three expressions simplify further. It is also important to realize that the transformation (<a href="#eq:9">9</a>) replaces only the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_71.gif" alt="" width="7" height="12" /> parameter (the speed of spinning); if we are interested in the behavior of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_72.gif" alt="" width="7" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_73.gif" alt="" width="5" height="12" /> only (as is usually the case), we get the same solution regardless of whether we use <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_74.gif" alt="" width="7" height="12" /> (in the old set of equations) or <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_75.gif" alt="" width="8" height="12" /> (in the new one).</p>
<p>So, let us see what the new results look like.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_8.gif" alt="" width="480" height="43" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_6.gif" alt="" width="144" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_7.gif" alt="" width="144" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_8.gif" alt="" width="278" height="12" /></p>
<p>The equations are now much simpler, and the number of parameters has been reduced from three to only one (namely <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_76.gif" alt="" width="9" height="12" />, whose dimension is <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_77.gif" alt="" width="27" height="15" />).</p>
<p>We can now easily infer a possibility of what we call a &#8220;steady&#8221; solution, with a constant value of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_78.gif" alt="" width="5" height="12" /> (say <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_79.gif" alt="" width="11" height="12" />) and</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_10.gif" alt="" width="75" height="37" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:10">(10)</a></td>
</tr>
</tbody>
</table>
<p>where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_80.gif" alt="" width="14" height="16" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_81.gif" alt="" width="13" height="18" /> are two constants whose product must be equal to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_82.gif" alt="" width="9" height="12" />. To relate the precession speed <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_83.gif" alt="" width="13" height="18" /> to the actual spinning speed, say <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_84.gif" alt="" width="13" height="18" />, we have to solve (based on (<a href="#eq:9">9</a>))</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_11.gif" alt="" width="156" height="38" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:11">(11)</a></td>
</tr>
</tbody>
</table>
<p>for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_85.gif" alt="" width="13" height="16" />, which yields</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_12.gif" alt="" width="237" height="58" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:12">(12)</a></td>
</tr>
</tbody>
</table>
<p>where the negative sign corresponds to the usually observed &#8220;slow&#8221; precession (assuming, from now on, that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_86.gif" alt="" width="33" height="16" />), whereas the positive sign yields a hard-to-achieve &#8220;fast&#8221; precession. When <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_87.gif" alt="" width="31" height="12" /> (spherically symmetrical top), we get only the &#8220;slow&#8221; solution <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_88.gif" alt="" width="42" height="25" />. For a fast-spinning top, the previous formula yields, to a good approximation, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_89.gif" alt="" width="55" height="26" />; the precession speed is practically independent of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_90.gif" alt="" width="11" height="12" />.</p>
<h4>Approximate Solution</h4>
<p>The actual motion is usually more complicated than a steady solution of the previous section, as <img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/Theta.gif" alt="" width="5" height="19" /> may undergo periodic oscillations, called nutation. Assuming that the amplitude of these is small, we can expand the three dependent variables of our equations around the steady-state solution:</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_13.gif" alt="" width="74" height="73" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:13">(13)</a></td>
</tr>
</tbody>
</table>
<p>where the first term of each right-hand side is a constant, the second term remains time dependent, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_91.gif" alt="" width="5" height="12" /> is small. We substitute these into the three equations, neglecting <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_92.gif" alt="" width="11" height="15" /> and higher powers of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_93.gif" alt="" width="5" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_9.gif" alt="" width="553" height="57" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_9.gif" alt="" width="567" height="65" /></p>
<p>Each of the three expressions must be equal to zero.</p>
<p>We solve the first two equations for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_94.gif" alt="" width="16" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_95.gif" alt="" width="17" height="12" /> and substitute into the last equation.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_10.gif" alt="" width="518" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_10.gif" alt="" width="542" height="46" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_11.gif" alt="" width="254" height="41" /></p>
<p>Solving for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_96.gif" alt="" width="11" height="12" /> (and, subsequently, for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_97.gif" alt="" width="13" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_98.gif" alt="" width="14" height="12" />) is now quite easy. Note that the coefficient of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_99.gif" alt="" width="11" height="12" /> in the last expression is always non-negative.</p>
<p>Also note that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_100.gif" alt="" width="7" height="16" /> will be always positive (assuming &#8220;slow&#8221; precession), whenever the following condition is met:</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_14.gif" alt="" width="116" height="46" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:14">(14)</a></td>
</tr>
</tbody>
</table>
<p>Reversing the above inequality results in so-called looping orbits (in terms of the path of the axis, when displayed on a unit sphere: see below), with <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_101.gif" alt="" width="7" height="16" /> periodically changing direction; making the two sides of (<a href="#eq:14">14</a>) equal to each other results in cuspidal orbits (when <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_102.gif" alt="" width="5" height="12" /> reaches its smallest value, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_103.gif" alt="" width="7" height="16" /> becomes zero).</p>
<p>If desired, one can extend the approximate solution to achieve <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_104.gif" alt="" width="11" height="15" /> (and higher) accuracy.</p>
<h4>Exact Solution</h4>
<p>One can show that the original differential equation for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_105.gif" alt="" width="5" height="12" />, namely</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_DisplayFormulaNumbered_15.gif" alt="" width="122" height="17" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq:15">(15)</a></td>
</tr>
</tbody>
</table>
<p>has the following constant of motion.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_11.gif" alt="" width="466" height="31" /></p>
<p>This can be easily verified.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_12.gif" alt="" width="580" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_12.gif" alt="" width="278" height="12" /></p>
<p>Making <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_106.gif" alt="" width="21" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_107.gif" alt="" width="21" height="12" /> equal to their initial values, we can solve for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_108.gif" alt="" width="7" height="16" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_109.gif" alt="" width="8" height="16" /> and substitute these into <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_110.gif" alt="" width="21" height="12" /> (minus its initial value, so that the resulting expression must be equal to zero).</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_13.gif" alt="" width="580" height="71" /></p>
<p>The resulting differential equation can be simplified by the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_111.gif" alt="" width="66" height="12" /> transformation [<a href="#Whittaker">3</a>].</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_14.gif" alt="" width="580" height="88" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_13.gif" alt="" width="413" height="78" /></p>
<p>To simplify the subsequent solution, we have assumed (without loss of generality) that the initial time has been chosen to coincide with <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_112.gif" alt="" width="5" height="12" /> reaching its maximum value (and, consequently, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_113.gif" alt="" width="32" height="17" />). The above equation can then be solved, using specific initial conditions, which must satisfy <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_114.gif" alt="" width="52" height="18" /> (due to (<a href="#eq:15">15</a>), since <span><img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Image_2.gif" alt="" width="32" height="17" /></span>); here <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_115.gif" alt="" width="15" height="15" /> provides the solution for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_116.gif" alt="" width="12" height="15" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_15.gif" alt="" width="507" height="129" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_14.gif" alt="" width="332" height="15" /></p>
<p>This can be easily transformed back to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_117.gif" alt="" width="5" height="12" />; one can then find the corresponding <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_118.gif" alt="" width="8" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_119.gif" alt="" width="7" height="12" /> by analytically integrating the expressions for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_120.gif" alt="" width="7" height="16" /> and (if desired) <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Math_121.gif" alt="" width="8" height="16" />. Finally, the results can be displayed graphically.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Input_16.gif" alt="" width="580" height="162" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Vrbik_Motion_Output_15.gif" alt="" width="360" height="400" /></p>
<p>One can now compute and display the top&#8217;s motion under various initial conditions, check the accuracy of the approximate solution, and so forth. We will leave it for you to explore. Have fun!</p>
<h3>References</h3>
<table class="ReferenceTable">
<tbody>
<tr>
<td class="Reference"><a name="Hestenes">[1] </a></td>
<td>D. Hestenes, <em>New Foundations for Classical Mechanics</em>, 2nd ed., Dordrecht/Boston/London: Kluwer Academic Publishers, 1999.</td>
</tr>
<tr>
<td class="Reference"><a name="Goldstein">[2] </a></td>
<td>H. Goldstein, <em>Classical Mechanics</em>, 2nd ed., Reading, MA: Addison-Wesley, 1980.</td>
</tr>
<tr>
<td class="Reference"><a name="Whittaker">[3] </a></td>
<td>E. T. Whittaker, <em>A Treatise on the Analytical Dynamics of Particles and Rigid Bodies</em>, 2nd ed., Cambridge: Cambridge University Press, 1917. <a href="http://www.archive.org/details/atreatiseonanal00whitgoog" target="blank">www.archive.org/details/atreatiseonanal00whitgoog</a>.</td>
</tr>
<tr>
<td class="DOIReference" colspan="2">J. Vrbik, &#8220;Motion of a Spinning Top,&#8221; <em>The Mathematica Journal</em>, 2012.<br />
dx.doi.org/doi:10.3888/tmj.14-4.</td>
</tr>
</tbody>
</table>
<h3 class="SectionAboutAuthor">About the Author</h3>
<p class="TextAboutAuthor"><strong>Jan Vrbik</strong><br />
<em>Department of Mathematics, Brock University<br />
500 Glenridge Ave., St. Catharines<br />
Ontario, Canada, L2S 3A1<br />
</em><em><a href="mailto:jvrbik@brocku.ca">jvrbik@brocku.ca</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mathematica-journal.com/2012/02/motion-of-a-spinning-top/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Evaluation of Gaussian Molecular Integrals</title>
		<link>http://www.mathematica-journal.com/2012/02/evaluation-of-gaussian-molecular-integrals/</link>
		<comments>http://www.mathematica-journal.com/2012/02/evaluation-of-gaussian-molecular-integrals/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 20:45:44 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 14]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=22634</guid>
		<description><![CDATA[This article discusses the evaluation of molecular overlap integrals for Gaussian-type functions with arbitrary angular dependence. As an example, we calculate the overlap matrix for the water molecule in the STO-3G basis set.
Introduction
Computational quantum chemistry makes extensive use of various integrals (and their derivatives) of the general form [1, 2, 3]




(1)



where  is an unnormalized [...]]]></description>
			<content:encoded><![CDATA[<p>This article discusses the evaluation of molecular overlap integrals for Gaussian-type functions with arbitrary angular dependence. As an example, we calculate the overlap matrix for the water molecule in the STO-3G basis set.<span id="more-22634"></span></p>
<h3>Introduction</h3>
<p>Computational quantum chemistry makes extensive use of various integrals (and their derivatives) of the general form [<a href="#Szabo">1</a>, <a href="#Helgaker">2</a>, <a href="#Jensen">3</a>]</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_1.gif" alt="" width="105" height="30" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.1">(1)</a></td>
</tr>
</tbody>
</table>
<p>where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_1.gif" alt="" width="27" height="12" /> is an unnormalized Cartesian Gaussian function centered at <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_2.gif" alt="" width="84" height="18" />:</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_2.gif" alt="" width="284" height="23" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.2">(2)</a></td>
</tr>
</tbody>
</table>
<p>where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_3.gif" alt="" width="9" height="12" /> is normally taken at the nucleus, <img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/Alpha.gif" alt="" width="7" height="19" /> is the orbital exponent, and the polynomial represents the angular part, in that the sum of the Cartesian angular momenta <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_4.gif" alt="" width="124" height="14" /> corresponds to functions of type <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_5.gif" alt="" width="5" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_6.gif" alt="" width="6" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_7.gif" alt="" width="6" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_8.gif" alt="" width="5" height="12" />, &#8230;. When the operator <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_9.gif" alt="" width="9" height="12" /> is 1, one simply has the overlap/density integral; otherwise it can be the energy operator for kinetic energy <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_10.gif" alt="" width="37" height="23" />, electron-nuclear attraction <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_11.gif" alt="" width="50" height="17" />, or electron-electron repulsion <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_12.gif" alt="" width="55" height="17" /> (which would involve double integrals). Other molecular properties involving external fields (response functions) or transition moments can also be computed from integrals of this form.</p>
<h4>Gaussian-Type Functions</h4>
<p>Gaussian-type functions are not the most natural choice for expanding the wavefunction. Slater-type functions, where the exponent is <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_13.gif" alt="" width="54" height="14" /> instead, can describe atomic systems more realistically. However, complications quickly arise in molecular calculations, which has led to the use of Gaussian functions in the overwhelming majority of current computational programs. Gaussian functions possess several desirable computational properties [<a href="#Shavitt">4</a>, <a href="#SFBoys">5</a>, <a href="#Taketa">6</a>]. (Much credit is due to S. F. Boys for the introduction of the Cartesian Gaussian function into computational chemistry and its early development in a series of 12 papers under the general title &#8220;Electronic Wavefunctions,&#8221; the first being [<a href="#SFBoys">5</a>].) A given Slater function can be approximated by a linear combination of several Gaussians.</p>
<p>The first useful property is that the product of two Gaussian functions located at <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_14.gif" alt="" width="9" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_15.gif" alt="" width="8" height="12" /> is another Gaussian located at a point <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_16.gif" alt="" width="7" height="12" /> somewhere between <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_17.gif" alt="" width="9" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_18.gif" alt="" width="8" height="12" />. (The proof of this can be found in [<a href="#Shavitt">4</a>].) The product of two Gaussians <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_19.gif" alt="" width="69" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_20.gif" alt="" width="71" height="12" /> is:</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_3.gif" alt="" width="580" height="22" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.3">(3)</a></td>
</tr>
</tbody>
</table>
<p>with <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_21.gif" alt="" width="94" height="26" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_22.gif" alt="" width="59" height="25" />.</p>
<p>A second desirable property is that a derivative of a Gaussian can be expressed as a sum of Gaussians of lower and higher Cartesian angular values.</p>
<h3>Overlap Integrals</h3>
<p>The simplest molecular integral is the overlap integral <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_23.gif" alt="" width="208" height="18" />. We first separate the integral into its orthogonal components:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_1.gif" alt="" width="194" height="18" /></p>
<p>where the notation <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_24.gif" alt="" width="48" height="12" /> expresses its functional dependence on the Cartesian angular components. The <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_25.gif" alt="" width="6" height="12" /> component, for instance, is:</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_4.gif" alt="" width="273" height="30" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.4">(4)</a></td>
</tr>
</tbody>
</table>
<p>Using a binomial expansion in the polynomial part,</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_2.gif" alt="" width="418" height="43" /></p>
<p>yields:</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_5.gif" alt="" width="477" height="43" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.5">(5)</a></td>
</tr>
</tbody>
</table>
<p>Odd values of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_26.gif" alt="" width="30" height="12" /> result in odd functions whose integrals vanish. For even values of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_27.gif" alt="" width="30" height="12" />, a solution [<a href="#Abramowitz">7</a>] for the integrals is given by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_28.gif" alt="" width="121" height="27" />, and, in those cases:</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_6.gif" alt="" width="447" height="51" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.6">(6)</a></td>
</tr>
</tbody>
</table>
<p>We keep in mind that for the summations only terms of even values of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_29.gif" alt="" width="30" height="12" /> survive. We have thus obtained an expression to evaluate the overlap of two Gaussians with arbitrary Cartesian angular factors. Equation (<a href="#Eq.6">6</a>) should be sufficient for programming or even for manual evaluation with small Cartesian angular values. One can, however, further reduce the number of operations by using a recurrence relation, one of the most useful techniques of computational chemistry. Recurrence relations let us efficiently calculate molecular integrations of higher angular values using previously obtained results with lower angular values. Recurrence relations are used in most computational chemistry programs and their application to other molecular integrations will be shown here.</p>
<h4>Recurrence Relations</h4>
<p>The derivation of the following relations involves straightforward algebraic manipulations, but is rather lengthy. Its omission does not impede our understanding of the recurrence relation, but helps maintain a reasonable continuity in our discussion. Interested readers can find a detailed derivation in the Appendix.</p>
<p>We start by defining the function</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_3.gif" alt="" width="161" height="51" /></p>
<p>where we have removed the factor <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_30.gif" alt="" width="64" height="23" /> from <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_31.gif" alt="" width="48" height="12" /> in (<a href="#Eq.6">6</a>). If we take the derivative of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_32.gif" alt="" width="49" height="12" />with respect to the nuclear coordinate <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_33.gif" alt="" width="13" height="12" /> using the definition of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_34.gif" alt="" width="48" height="12" /> in equation (<a href="#Eq.4">4</a>),</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_7.gif" alt="" width="580" height="51" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.7">(7)</a></td>
</tr>
</tbody>
</table>
<p>and combining the result with the same derivative of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_35.gif" alt="" width="47" height="12" />, but using the definition of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_36.gif" alt="" width="48" height="12" /> in equation (<a href="#Eq.6">6</a>) instead,</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_8.gif" alt="" width="580" height="85" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.8">(8)</a></td>
</tr>
</tbody>
</table>
<p>we obtain</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_9.gif" alt="" width="443" height="36" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.9">(9)</a></td>
</tr>
</tbody>
</table>
<p>The same approach in which we combine the derivatives of (<a href="#Eq.4">4</a>) and (<a href="#Eq.6">6</a>) with respect to the nuclear coordinate <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_37.gif" alt="" width="12" height="12" /> gives</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_10.gif" alt="" width="440" height="36" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.10">(10)</a></td>
</tr>
</tbody>
</table>
<p>For <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_38.gif" alt="" width="31" height="12" />, we have the index recursion relation</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_11.gif" alt="" width="296" height="33" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.11">(11)</a></td>
</tr>
</tbody>
</table>
<p>and combining equations (<a href="#Eq.7">7</a>) and (<a href="#Eq.8">8</a>) yields the transfer equation</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_12.gif" alt="" width="261" height="12" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.12">(12)</a></td>
</tr>
</tbody>
</table>
<p>Starting with the initial values <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_39.gif" alt="" width="57" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_40.gif" alt="" width="105" height="12" />, the recurrence relation and the transfer equation, we can build up the overlap of functions of higher Cartesian angular values from lower ones. This is particularly useful for contracted Gaussian basis primitives with different functions sharing the same exponent.</p>
<h4>Implementation</h4>
<p>The function <tt>Ov</tt> evaluates the overlap integral of two Gaussian functions; here <tt>alpha</tt>, <tt>beta</tt>, <tt>RA</tt>, <tt>RB</tt>, <tt>LA</tt>, and <tt>LB</tt> are <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_41.gif" alt="" width="7" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_42.gif" alt="" width="7" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_43.gif" alt="" width="9" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_44.gif" alt="" width="8" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_45.gif" alt="" width="6" height="12" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_46.gif" alt="" width="7" height="12" /> as defined earlier.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_4.gif" alt="" width="580" height="358" /></p>
<h3>Examples</h3>
<p>The published contracted Gaussian basis sets (see, for example, [<a href="#Ref:8">8</a>]) are usually not normalized; in our first example, we will calculate the normalization factor of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_47.gif" alt="" width="5" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_48.gif" alt="" width="6" height="12" /> Cartesian Gaussian functions that we will need later on. The normalization factor is simply the inverse square root of the overlap integral. Here we calculate the overlap between two <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_49.gif" alt="" width="5" height="12" /> functions with the Cartesian angular vector <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_50.gif" alt="" width="60" height="12" />,</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_5.gif" alt="" width="277" height="30" /></p>
<p>and observe that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_51.gif" alt="" width="196" height="14" />. The normalization factor for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_52.gif" alt="" width="11" height="12" /> functions is <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_53.gif" alt="" width="109" height="24" />. Similarly, the overlap between two <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_54.gif" alt="" width="11" height="12" /> functions (<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_55.gif" alt="" width="60" height="12" />) is</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_6.gif" alt="" width="422" height="32" /></p>
<p>where we have analogously used the recurrence relations to obtain <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_56.gif" alt="" width="37" height="12" /> and afterward, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_57.gif" alt="" width="37" height="12" />. Similar results are obtained for <span><img style="vertical-align: middle;" title="S_(p_y)" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Image_1.gif" alt="S_(p_y)" width="17" height="17" /></span> and <span><img style="vertical-align: middle;" title="S_(p_z)" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Image_3.gif" alt="S_(p_z)" width="17" height="16" /></span>. The normalization factor for the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_58.gif" alt="" width="6" height="12" /> function is then <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_59.gif" alt="" width="76" height="19" />. These results are special cases of the more general formula of the normalization factor</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_7.gif" alt="" width="291" height="45" /></p>
<p>and we note that this value depends only on the orbital exponent and the Cartesian angular values.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_8.gif" alt="" width="556" height="38" /></p>
<p>In the second example, we will calculate the overlap matrix of the water molecule (<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_60.gif" alt="" width="96" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_61.gif" alt="" width="92" height="12" />, geometry optimized at the HF/STO-3G level). The molecule lies in the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_62.gif" alt="" width="6" height="12" />-<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_63.gif" alt="" width="5" height="12" /> plane with Cartesian coordinates in atomic units.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_9.gif" alt="" width="245" height="43" /></p>
<p>In the STO-3G basis set, each atomic orbital is described by a sum of three Gaussians; here are their primitive contraction coefficients and orbital exponents (taken from [<a href="#Ref:8">8</a>]).</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_10.gif" alt="" width="392" height="223" /></p>
<p>Here are the centers and Cartesian angular values of the orbitals, in the following order: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_64.gif" alt="" width="19" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_65.gif" alt="" width="19" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_66.gif" alt="" width="19" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_67.gif" alt="" width="19" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_68.gif" alt="" width="25" height="15" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_69.gif" alt="" width="25" height="17" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_70.gif" alt="" width="25" height="16" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_11.gif" alt="" width="449" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_12.gif" alt="" width="344" height="28" /></p>
<p>For instance, the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_71.gif" alt="" width="5" height="12" /> atomic orbital of the hydrogen atom 1 is described as</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_13.gif" alt="" width="544" height="65" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.13">(13)</a></td>
</tr>
</tbody>
</table>
<p>Similarly, the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_72.gif" alt="" width="11" height="12" /> orbital of the oxygen atom is</p>
<table class="DisplayFormulaNumbered">
<tbody>
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormulaNumbered_14.gif" alt="" width="380" height="66" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="Eq.14">(14)</a></td>
</tr>
</tbody>
</table>
<p>The overlap integral between the two orbitals expands to nine integrals involving the primitives. Using the function <tt>Ov</tt>, for example, the overlap integral between the first two primitives of equations (<a href="#Eq.13">13</a>) and (<a href="#Eq.14">14</a>) is</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_13.gif" alt="" width="531" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_14.gif" alt="" width="388" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Output_1.gif" alt="" width="94" height="12" /></p>
<p>And the overlap between <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_73.gif" alt="" width="26" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_74.gif" alt="" width="26" height="12" /> is</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_15.gif" alt="" width="132" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_16.gif" alt="" width="313" height="163" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Output_2.gif" alt="" width="66" height="12" /></p>
<p>The overlap matrix <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_75.gif" alt="" width="7" height="12" /> for the entire water molecule in the STO-3G basis set can be calculated in a similar manner.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_17.gif" alt="" width="390" height="163" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Output_3.gif" alt="" width="469" height="110" /></p>
<p>Since the overlap matrix is symmetrical, we need to calculate only the elements above the main diagonal. The basis functions are normalized, as indicated by the unit diagonal elements. We note that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_76.gif" alt="" width="16" height="12" /> equals zero. This is the overlap between the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_77.gif" alt="" width="11" height="12" /> orbital of the first hydrogen atom and the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_78.gif" alt="" width="17" height="12" /> of the oxygen atom. The molecule lies in the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_79.gif" alt="" width="6" height="12" />-<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_80.gif" alt="" width="5" height="12" /> plane, so this overlap vanishes due to symmetry. Similar cases occur in the overlaps between the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_81.gif" alt="" width="5" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_82.gif" alt="" width="6" height="12" /> orbitals of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_83.gif" alt="" width="9" height="12" /> (<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_84.gif" alt="" width="81" height="12" />) and among the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_85.gif" alt="" width="12" height="12" /> orbitals (<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_86.gif" alt="" width="62" height="12" />), etc. The strongest overlaps are those between the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_87.gif" alt="" width="11" height="12" /> hydrogen orbitals and the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_88.gif" alt="" width="11" height="12" /> oxygen orbital.</p>
<p>We next plot the atomic-orbital overlap between <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_89.gif" alt="" width="11" height="12" /> of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_90.gif" alt="" width="15" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_91.gif" alt="" width="17" height="12" /> of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_92.gif" alt="" width="9" height="12" />,</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_18.gif" alt="" width="276" height="40" /></p>
<p>in the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_93.gif" alt="" width="6" height="12" />-<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_94.gif" alt="" width="5" height="12" /> plane, superimposing the molecule structure.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_19.gif" alt="" width="305" height="118" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_20.gif" alt="" width="580" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_21.gif" alt="" width="410" height="58" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Input_22.gif" alt="" width="155" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Output_4.gif" alt="" width="360" height="360" /></p>
<p>We observe a strong distortion of the positive (lower) lobe of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_95.gif" alt="" width="17" height="12" /> function toward the hydrogen atom. The negative lobe shows less deformation, whereas the node remains precisely at the atomic position. Note that this is simply the orbital overlap between noninteracting atoms, such as in the case of the &#8220;promolecule.&#8221;</p>
<h3>Conclusion</h3>
<p>We have provided an introduction to the evaluation of molecular integrals involving Gaussian-type basis functions both analytically and by use of recurrence relations. The results are general and relatively straightforward; the simple algorithm makes it suitable for implementation in a number of programming languages. Together with the kinetic, nuclear-electron attraction, and electron-electron repulsion energies, this is the first step toward the calculation of molecular energies and electronic properties.</p>
<h3>Appendix<span><img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Image_5.gif" alt="" width="5" height="16" /></span></h3>
<p>Here we provide the derivation of equations (<a href="#Eq.7">7</a>) and (<a href="#Eq.8">8</a>). Differentiate <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_96.gif" alt="" width="47" height="12" /> using equation (<a href="#Eq.5">5</a>) with respect to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_97.gif" alt="" width="13" height="12" />:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_23.gif" alt="" width="371" height="51" /></p>
<p>Consider the derivative term</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_24.gif" alt="" width="565" height="35" /></p>
<p>the first term is simply</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_25.gif" alt="" width="165" height="19" /></p>
<p>and, using the chain rule, the derivative of the second term is</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_26.gif" alt="" width="509" height="36" /></p>
<p>recalling that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_98.gif" alt="" width="131" height="12" />. Substitute the results into the expression for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_99.gif" alt="" width="82" height="12" />,</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_27.gif" alt="" width="578" height="51" /></p>
<p>Writing <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_100.gif" alt="" width="140" height="12" /> in the second term inside the bracket, after expanding we have</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_28.gif" alt="" width="580" height="111" /></p>
<p>Comparing the three integrals with the definitions of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_101.gif" alt="" width="47" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_102.gif" alt="" width="48" height="12" /> we have the desired equation (<a href="#Eq.7">7</a>).</p>
<p>To derive equation (<a href="#Eq.8">8</a>), we differentiate <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_103.gif" alt="" width="47" height="12" /> with respect to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_Math_104.gif" alt="" width="13" height="12" /> using equation (<a href="#Eq.6">6</a>) instead,</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_29.gif" alt="" width="580" height="282" /></p>
<p>We make use of the relationship</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_30.gif" alt="" width="343" height="34" /></p>
<p>to reduce the derivative above to</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2012/01/Ho_DisplayFormula_31.gif" alt="" width="465" height="178" /></p>
<p>This is equation (<a href="#Eq.8">8</a>), which is what was needed to prove.</p>
<h3>References</h3>
<table class="ReferenceTable">
<tbody>
<tr>
<td class="Reference"><a name="Szabo">[1] </a></td>
<td>A. Szabo and N. Ostlund, <em>Modern Quantum Chemistry: Introduction to Advanced Electronic Structure Theory</em>, New York: Dover, 1996.</td>
</tr>
<tr>
<td class="Reference"><a name="Helgaker">[2] </a></td>
<td>T. Helgaker, P. Jørgensen, and J. Olsen, <em>Molecular Electronic-Structure Theory</em>, New York: Wiley, 2000.</td>
</tr>
<tr>
<td class="Reference"><a name="Jensen">[3] </a></td>
<td>F. Jensen, <em>Introduction to Computational Chemistry</em>, New York: Wiley, 1999.</td>
</tr>
<tr>
<td class="Reference"><a name="Shavitt">[4] </a></td>
<td>I. Shavitt, &#8220;The Gaussian Function in Calculations of Quantum Mechanics,&#8221; in <em>Methods in Computational Physics</em>, Vol. 2, B. Alder, S. Fernbach, and M. Rotenberg, eds., New York: Academic Press, 1963 pp. 1-45.</td>
</tr>
<tr>
<td class="Reference"><a name="SFBoys">[5] </a></td>
<td>S. F. Boys, &#8220;A General Method of Calculation for Stationary States of Any Molecular System,&#8221; <em>Proceedings of the Royal Society A</em>, <strong>200</strong>, 1950 pp. 542-554. <a href="http://rspa.royalsocietypublishing.org/content/200/1063/542.abstract" target="blank">rspa.royalsocietypublishing.org/content/200/1063/542.abstract</a>.</td>
</tr>
<tr>
<td class="Reference"><a name="Taketa">[6] </a></td>
<td>H. Taketa, S. Huzinaga, and K. O-ohata, &#8220;Gaussian-Expansion Methods for Molecular Integrals,&#8221; <em>Journal of the Physical Society of Japan</em>, <strong>21</strong>, 1966 pp. 2313-2324.</td>
</tr>
<tr>
<td class="Reference"><a name="Abramowitz">[7] </a></td>
<td>M. Abramowitz and I. A. Stegun, <em>Handbook of Mathematical Functions with Formulas</em>, <em>Graphs</em>, <em>and Mathematical Tables</em>, New York: Dover, 1965, Eq. (7.4.4) p. 302.</td>
</tr>
<tr>
<td class="Reference"><a name="Ref:8">[8] </a></td>
<td>&#8220;Basis Set Exchange.&#8221; (Sep 13, 2011) <a href="https://bse.pnl.gov/bse/portal" target="blank">bse.pnl.gov/bse/portal</a>.</td>
</tr>
<tr>
<td class="DOIReference" colspan="2">M. Hô and J. M. Hernández-Pérez, &#8220;Evaluation of Gaussian Molecular Integrals,&#8221; <em>The Mathematica Journal</em>, 2012. dx.doi.org/doi:10.3888/tmj.14-3.</td>
</tr>
</tbody>
</table>
<h3 class="SectionAboutAuthor">About the Authors</h3>
<p class="TextAboutAuthor">Minhhuy Hô received his Ph.D. in theoretical chemistry at Queen&#8217;s University, Kingston, Ontario, Canada in 1998. He is currently a professor at the Centro de Investigaciones Químicas at the Universidad Autónoma del Estado de Morelos in Cuernavaca, Morelos, México.</p>
<p class="TextAboutAuthor">Julio-Manuel Hernández-Pérez obtained his Ph.D. at the Universidad Autónoma del Estado de Morelos in 2008. He has been a professor of chemistry at the Facultad de Ciencias Químicas at the Benemérita Universidad Autónoma de Puebla since 2010.</p>
<p class="TextAboutAuthor"><strong>Minhhuy Hô</strong><br />
<em>Universidad Autónoma del Estado de Morelos<br />
Centro de Investigaciones Químicas<br />
Ave</em>. <em>Universidad</em>, <em>No</em>. <em>1001</em>, <em>Col</em>. <em>Chamilpa<br />
Cuernavaca</em>, <em>Morelos</em>, <em>Mexico CP 92010<br />
</em><em><a href="mailto:homh@uaem.mx">homh@uaem.mx</a></em></p>
<p class="TextAboutAuthor"><strong>Julio-Manuel Hernández-Pérez<br />
</strong><em>Benemérita Universidad Autónoma de Puebla<br />
Facultad de Ciencias Químicas<br />
Ciudad Universitaria</em>, <em>Col</em>. <em>San Manuel<br />
Puebla</em>, <em>Puebla</em>, <em>Mexico CP 72570<br />
</em><em><a href="mailto:jumahernandez@gmail.com">jumahernandez@gmail.com</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mathematica-journal.com/2012/02/evaluation-of-gaussian-molecular-integrals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Return of the Riemann Surface</title>
		<link>http://www.mathematica-journal.com/2012/02/the-return-of-the-riemann-surface/</link>
		<comments>http://www.mathematica-journal.com/2012/02/the-return-of-the-riemann-surface/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 00:16:34 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 10, Issue 4]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=17814</guid>
		<description><![CDATA[My favorite subject&#8212;Riemann surfaces&#8212;is revisited through the eyes (functions) of Mathematica 6. Old friends will appear in a new light(ing) and new acquaintances with untreated surfaces will be made.
Introduction
This is my first column since Version 6 came out a few months ago. Version 6, after being in the works for many years, provides a wealth [...]]]></description>
			<content:encoded><![CDATA[<p>My favorite subject&#8212;Riemann surfaces&#8212;is revisited through the eyes (functions) of <em>Mathematica</em> 6. Old friends will appear in a new light(ing) and new acquaintances with untreated surfaces will be made.<span id="more-17814"></span></p>
<h3>Introduction</h3>
<p>This is my first column since Version 6 came out a few months ago. Version 6, after being in the works for many years, provides a wealth of new features that are useful in many numeric and symbolic calculations, advanced programs, visualizations, and elsewhere. With so many exciting new possibilities, it was not easy to decide what to discuss in this column. So instead of delving into a new subject, I think the best way to see some of the new features and resulting capabilities of Version 6 is to compare with some corresponding Version 5 results. That is why today I will come back to a subject discussed in earlier columns and demonstrate how to go quite a bit further with Version 6.</p>
<p>In earlier columns, I started discussing the visualization of Riemann surfaces. In this column, I want to come back to this mathematically (and aesthetically) beautiful subject. So far, we discussed the construction of Riemann surfaces of compositions of elementary functions with finitely many branch points (articles IIa to IId [<a href="#IIa">1</a>-<a href="#IId">4</a>]). With the new 3D graphics system and interactive features of Version 6, new possibilities open up for visualizing Riemann surfaces; the main ones are:</p>
<ul>
<li>Specifying vertex normals to obtain smooth-looking surfaces</li>
<li>Using opacity to get a better look &#8220;inside&#8221; a Riemann surface (instead of cutting holes in the polygons of the surface)</li>
<li>Using adaptive refinement of the built-in graphics functions to better resolve branch points</li>
<li>Using <tt>Manipulate</tt> to easily view a function <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_1.gif" alt="" width="55" height="12" /> as a 3D projection of the hypersurface <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_2.gif" alt="" width="85" height="15" /> (where showing <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_3.gif" alt="" width="42" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_4.gif" alt="" width="42" height="12" /> over the complex plane become special cases)</li>
<li>Using the <tt>Exclusions</tt> option to avoid branch cuts in <tt>Plot3D</tt>, <tt>ParametricPlot3D</tt>, and others</li>
</ul>
<p>These features offer a fresh look at the topic of Riemann surfaces (so, if numbered, this would be article IIdII, not yet IIe, which will appear later and deal with special functions).</p>
<p>Starting with this column, I will also introduce a formatting change: <em>Mathematica</em>&#8217;s default <tt>StandardForm</tt> is an ideal editing and reading environment for mathematical expressions and very small program snippets (one to two lines). Convenient automatic line breaking, two-dimensional fraction and radical formatting, and other features make writing and reading easy. But for multiline program-like code pieces, the formatted results are frequently less readable than manually formatting the code to avoid unexpected line breaks, alignments, and nonuniform line spacings. A new style in Version 6 (available as <img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/AltKey.gif" alt="" width="14" height="18" />-8) is the <tt>"Code"</tt> style. It is the default style for packages (the canonical place to store a program) and can, of course, also be used in notebooks. It does not automatically break lines, but allows for special characters and inherits the new syntax coloring feature to maximize code readability. So, function definitions are given in <tt>"Code"</tt> style (easily identifiable in this notebook by its light blue background) and example uses of the defined functions will be in style <tt>"Input"</tt>. In addition to an easier visual discrimination between actual definitions and example uses, this also allows for a quick selection of all definitions (using RIGHT<img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/KeyBar.gif" alt="" width="1" height="18" /><img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/AltKey.gif" alt="" width="14" height="18" />) for immediate use without having to run all (sometimes time-consuming) examples to experiment with further Riemann surfaces.</p>
<h3>The Updated Riemann Surface Package</h3>
<p>We start with an updated version of the construction discussed in my earlier columns. Let us quickly recap the construction idea: Starting with a given function <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_5.gif" alt="" width="55" height="12" />, a set of coupled nonlinear meromorphic differential equations is derived from <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_6.gif" alt="" width="20" height="12" /> and this set is solved on patches of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_7.gif" alt="" width="5" height="12" /> plane. These patches arise from a tensor product decomposition of the complex plane in a cylindrical coordinate system and are separated by branch points. The system of differential equations is solved numerically and the resulting function values are displayed. If at the same time we calculate <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_8.gif" alt="" width="24" height="14" />, it is possible to construct the normals to obtain a smooth-looking surface. (Calculating the normals of the 3D embedding of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_9.gif" alt="" width="40" height="12" /> can be done easily using the Cauchy-Riemann conditions.) An updated version of the package, based on the code from <em>The Mathematica GuideBook for Numerics</em> [<a href="#GuideBook">5</a>], is available on the <em>Mathematica</em> <em>GuideBooks</em> website (<a href="http://www.mathematicaguidebooks.org/V6/downloads/RiemannSurfacePlot3D.m">www.mathematicaguidebooks.org/V6/downloads/RiemannSurfacePlot3D.m</a>).</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_1.gif" alt="" width="580" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_2.gif" alt="" width="540" height="58" /></p>
<p>This is the main function defined in the package.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_3.gif" alt="" width="153" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Print_4.gif" alt="" width="580" height="73" /></p>
<p>The first example is <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_10.gif" alt="" width="54" height="15" /> displayed as <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_11.gif" alt="" width="29" height="12" /> over the complex <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_12.gif" alt="" width="5" height="12" /> plane.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_5.gif" alt="" width="519" height="18" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_1.gif" alt="" width="244" height="244" /></p>
<p>The function has a few new options in addition to the built-in 3D graphics options.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_6.gif" alt="" width="444" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_2.gif" alt="" width="580" height="89" /></p>
<p>Because 3D graphics in Version 6 are typically no longer made from individual polygons, but rather from <tt>GraphicsComplex</tt> objects that contain polygons and their adjacency information, it is possible to avoid the small gaps that were used in Version 5 between the patches. The function <tt>StitchPatches</tt> &#8220;stitches&#8221; the <tt>GraphicsComplex</tt> objects together along their boundaries. While in static images, the small (typically 1ppm or smaller) gaps are usually not visible; when interactively rotating graphics, such gaps can potentially become more pronounced.</p>
<p>Note that we do not have to specify a domain of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_13.gif" alt="" width="5" height="12" /> plane over which to plot the function. The domain is chosen automatically in such a way to include all finite branch points of the function.</p>
<p>Here is another example of a Riemann surface plotted with <tt>RiemannSurfacePlot3D</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_7.gif" alt="" width="525" height="38" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_3.gif" alt="" width="244" height="244" /></p>
<p>In the next example, we display the imaginary part of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_14.gif" alt="" width="52" height="18" /> over the complex plane and color the surface according to the real part.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_8.gif" alt="" width="519" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_4.gif" alt="" width="244" height="244" /></p>
<p>Using a transparent surface makes it easier to see the inner parts of more complicated Riemann surfaces.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_9.gif" alt="" width="499" height="39" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_5.gif" alt="" width="244" height="244" /></p>
<p>Here are some more examples.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_10.gif" alt="" width="510" height="38" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_6.gif" alt="" width="244" height="244" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_11.gif" alt="" width="524" height="38" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_7.gif" alt="" width="244" height="244" /></p>
<p><tt>RiemannSurfacePlot3D</tt> stores some intermediate time-consuming results for the function it has just plotted. As a result, once a function is plotted, calls to <tt>RiemannSurfacePlot3D</tt> with an identical first argument and mostly identical option settings, but a potentially different second argument, are generally quite fast.</p>
<p>Here is a demonstration that allows moving smoothly from the real to the imaginary part.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_12.gif" alt="" width="538" height="58" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_8.gif" alt="" width="245" height="122" /></p>
<p>By parametrizing the three Cartesian coordinates as a linear form of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_15.gif" alt="" width="26" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_16.gif" alt="" width="26" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_17.gif" alt="" width="29" height="12" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_18.gif" alt="" width="29" height="12" />, we can implement an even more general view of the surface. We do this through a <tt>Manipulate</tt> object that makes it easy to change any of the 12 parameters that define the actual function value that is plotted.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_13.gif" alt="" width="580" height="193" /></p>
<p>We give two examples: the first algebraic and the second inverse trigonometric.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_14.gif" alt="" width="207" height="18" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_9.gif" alt="" width="375" height="359" /></p>
<p>Here is the inverse trigonometric function.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_15.gif" alt="" width="500" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_10.gif" alt="" width="244" height="244" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_16.gif" alt="" width="198" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_11.gif" alt="" width="375" height="359" /></p>
<p>For details of the construction, see the package. Within the new package editor of Version 6, it is quite convenient and straightforward to experiment. Input cells are kept in the package and are easily evaluated; comments and examples can be easily embedded, but are invisible when the package is loaded. When developing or investigating package code with the package editor, we do not have to worry about contexts or saving outputs.</p>
<p><tt>RiemannSurfacePlot3D</tt> fails for functions with infinitely many branch points. Here is an example.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_17.gif" alt="" width="351" height="24" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_12.gif" alt="" width="344" height="20" /></p>
<p>The construction given next deals with functions having infinitely many branch points.</p>
<h3>The New ContourPlot3D</h3>
<p><tt>ContourPlot3D</tt>, a package function in Version 5, is now a much faster built-in kernel function. This function comes in handy sometimes for polynomials dealing with Riemann surfaces. Here is a simple example that uses <tt>ContourPlot3D</tt> to display the real part of the functions <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_19.gif" alt="" width="108" height="19" /> defined implicitly through a bivariate polynomial.</p>
<p>Eliminating <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_20.gif" alt="" width="29" height="12" /> yields a (more complicated) trivariate polynomial that can be plotted.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_18.gif" alt="" width="473" height="18" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_13.gif" alt="" width="255" height="18" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_19.gif" alt="" width="231" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_14.gif" alt="" width="501" height="34" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_20.gif" alt="" width="577" height="43" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_15.gif" alt="" width="244" height="244" /></p>
<h3>Using the Exclusions Options</h3>
<p>Now we will use new features of <tt>Plot3D</tt> and <tt>ParametricPlot3D</tt> to construct a Riemann surface. Using these 3D plotting functions on a function that has branch cuts, we now automatically get cuts along the branch cuts.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_21.gif" alt="" width="580" height="19" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_16.gif" alt="" width="244" height="244" /></p>
<p>Modulo the mesh, in Version 5, we would have gotten a graphic similar to the following, much less appealing (but still correct) image.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_22.gif" alt="" width="580" height="39" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_17.gif" alt="" width="244" height="244" /></p>
<p>We can also obtain descriptions of the excluded cuts using the function <tt>VisualizationDiscontinuities</tt> from the context <tt>Visualization`</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_23.gif" alt="" width="468" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_18.gif" alt="" width="262" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_24.gif" alt="" width="461" height="18" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_19.gif" alt="" width="262" height="12" /></p>
<p>This neat feature of plotting functions that recognize branch cuts can be used to construct Riemann surfaces by finding parametrizations of all sheets and then plotting them. Instead of the steep vertical walls in earlier versions of <em>Mathematica</em>, we now get small cuts between the sheet patches. The analytic continuation of the logarithm and the power function are straightforward to define through adding multiples of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_21.gif" alt="" width="19" height="12" /> and multiplication by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_22.gif" alt="" width="58" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_25.gif" alt="" width="550" height="159" /></p>
<p>For the logarithm, with its infinitely many sheets, we restrict ourselves to (typically) three sheets.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_26.gif" alt="" width="533" height="37" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_27.gif" alt="" width="486" height="181" /></p>
<p>This list can be easily extended to elliptic integrals, Bessel functions, polylogarithms, and others.</p>
<p>The function <tt>makeSheetList</tt> generates a list of the analytically continued sheets of a multivalued function by forming the outer product of all sheets of all the multivalued functions that occur.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_28.gif" alt="" width="505" height="146" /></p>
<p>Here is an example. The function <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_23.gif" alt="" width="137" height="20" /> results in six parametrized sheets: three from the outer logarithm combined with two from the inner square root.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_29.gif" alt="" width="496" height="24" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_20.gif" alt="" width="551" height="42" /></p>
<p>Here is a quick view on the branch cut structure of these six functions.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_30.gif" alt="" width="580" height="43" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_21.gif" alt="" width="469" height="320" /></p>
<p>And here are the real and imaginary parts of the six sheets shown as 3D plots.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_31.gif" alt="" width="580" height="37" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_22.gif" alt="" width="244" height="244" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_32.gif" alt="" width="580" height="37" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_23.gif" alt="" width="244" height="244" /></p>
<p>Emphasizing the cuts and making the surface transparent gives an even better looking graphic.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_33.gif" alt="" width="580" height="49" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_24.gif" alt="" width="244" height="244" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_34.gif" alt="" width="580" height="49" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_25.gif" alt="" width="244" height="244" /></p>
<p>As a second example, we use <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_24.gif" alt="" width="129" height="24" />. This time we have five sheets, all arising from the topmost fifth root.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_35.gif" alt="" width="476" height="19" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_26.gif" alt="" width="473" height="63" /></p>
<p>Here is a plot of the real part over the complex plane.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_36.gif" alt="" width="580" height="37" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_27.gif" alt="" width="244" height="244" /></p>
<p>The function <tt>RiemannSheetParametricPlot3D</tt> combines the sheet generation and plotting results. All sheets are included in the first argument of <tt>ParametricPlot3D</tt>, instead of making multiple calls to it. This has the advantage of getting better visual results because the optimal plot range will be calculated by taking all sheets into account.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_37.gif" alt="" width="526" height="134" /></p>
<p>Here are two examples of this function in action: one algebraic and the other a transcendental function containing <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_25.gif" alt="" width="10" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_38.gif" alt="" width="566" height="45" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_28.gif" alt="" width="244" height="244" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_39.gif" alt="" width="522" height="64" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_29.gif" alt="" width="244" height="244" /></p>
<p>Using <tt>RiemannSheetParametricPlot3D</tt>, we can now plot the Riemann surface of the function <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_26.gif" alt="" width="70" height="15" /> that failed earlier. We emphasize the vertical connection along the branch cuts through red polygons.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_40.gif" alt="" width="497" height="57" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_30.gif" alt="" width="244" height="244" /></p>
<p>And here are four considerably more complicated examples. Because of the infinite number of branch points, the function <tt>RiemannSurfacePlot3D</tt> cannot handle them.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_41.gif" alt="" width="580" height="74" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_31.gif" alt="" width="244" height="244" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_42.gif" alt="" width="580" height="69" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_32.gif" alt="" width="244" height="244" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_43.gif" alt="" width="556" height="83" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_33.gif" alt="" width="244" height="244" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_44.gif" alt="" width="580" height="67" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_34.gif" alt="" width="244" height="244" /></p>
<p>We generalize <tt>RiemannSheetParametricPlot3D</tt> to allow for a matrix as its second argument.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_45.gif" alt="" width="526" height="145" /></p>
<p>In effect, the next graphic shows <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_27.gif" alt="" width="67" height="12" /> over the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_28.gif" alt="" width="64" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_29.gif" alt="" width="70" height="12" /> plane.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_46.gif" alt="" width="580" height="73" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_35.gif" alt="" width="244" height="244" /></p>
<p>To summarize: Using analytically continued sheets and <tt>Plot3D</tt>/<tt>ParametricPlot3D</tt> with the <tt>Exclusions</tt> option lets you plot Riemann surfaces of compositions of elementary functions. This includes cases where the branch points cannot be calculated in closed form or the case of countably many branch points. Two potential disadvantages of this approach are that calculating the exclusions can be potentially very time consuming and the gaps arising from cutting out thin strips along potential branch cuts cannot be made arbitrarily small with reasonable computational effort.</p>
<h3>Higher-Order Polynomial Roots</h3>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_47.gif" alt="" width="100" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_36.gif" alt="" width="549" height="28" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_48.gif" alt="" width="580" height="18" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_37.gif" alt="" width="438" height="38" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_49.gif" alt="" width="87" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_38.gif" alt="" width="206" height="18" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_50.gif" alt="" width="428" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_39.gif" alt="" width="244" height="244" /></p>
<p>This is not unexpected. The internal function <tt>VisualizationDiscontinuities</tt> yields no exclusions for the <tt>Root</tt> object.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_51.gif" alt="" width="502" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_40.gif" alt="" width="14" height="12" /></p>
<p>The reason is the quite large computational effort needed to determine the cuts for parametrized roots. While the branch points are relatively easy to determine as the common root of the function and its derivative through one resultant calculation, calculating the cuts is more expensive.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_52.gif" alt="" width="580" height="28" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_53.gif" alt="" width="195" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_41.gif" alt="" width="580" height="107" /></p>
<p>Expanding the first of the roots, we see the existence of three second-order branch points.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_54.gif" alt="" width="395" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_42.gif" alt="" width="580" height="172" /></p>
<p>A discontinuity of a <tt>Root</tt> object occurs due to a switch in the root numbering. This in turn results from coinciding real parts of different roots. At the point where two roots have a common real part, the real part must have a double root. So carrying out the two resultant calculations gives a sufficient set of algebraic curves along which the roots will exhibit discontinuities.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_55.gif" alt="" width="525" height="148" /></p>
<p>Even for just a fourth root, evaluating <tt>rootExclusions</tt> is quite time consuming and the resulting polynomial is large and complex.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_56.gif" alt="" width="351" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_43.gif" alt="" width="104" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_57.gif" alt="" width="100" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_44.gif" alt="" width="580" height="92" /></p>
<p>Here is a quick overview of the last polynomial.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_58.gif" alt="" width="576" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_45.gif" alt="" width="217" height="12" /></p>
<p>Once the set of exclusions has been calculated, we can use it as the setting for the <tt>Exclusions</tt> option.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_59.gif" alt="" width="580" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_46.gif" alt="" width="244" height="244" /></p>
<h3>Life in Four Dimensions</h3>
<p>Up to now, we have seen <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_30.gif" alt="" width="42" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_31.gif" alt="" width="42" height="12" /> over the complex <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_32.gif" alt="" width="5" height="12" /> plane (we plotted a more general graphic earlier). The most complete information of a function <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_33.gif" alt="" width="43" height="12" /> is the set of pairs of complex numbers <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_34.gif" alt="" width="28" height="12" /> or the quadruple of real numbers <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_35.gif" alt="" width="139" height="12" />. Imagining the hypersurface <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_36.gif" alt="" width="42" height="12" /> in <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_37.gif" alt="" width="14" height="15" /> (isomorphic to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_38.gif" alt="" width="15" height="15" />), we are naturally led to Banchoff&#8217;s tetraview [<a href="#Banchoff">6</a>] of a complex-valued function. In this section, we implement such a construction. Starting with a plot of the real and imaginary parts of a complex-valued function over the complex plane, we extract the underlying graphic complex, and re-evaluate the function at the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_39.gif" alt="" width="6" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_40.gif" alt="" width="6" height="12" /> values. (Using the already discretized surface in the form of a <tt>GraphicsComplex</tt> gives us the advantage of adaptive subdivision.) Because we will use the values of the derivatives of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_41.gif" alt="" width="21" height="12" /> with respect to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_42.gif" alt="" width="5" height="12" /> for calculating the normals, we store the values of the derivatives at the (complex) vertex points in the option setting for <tt>VertexNormals</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_60.gif" alt="" width="472" height="133" /></p>
<p>We define a function <tt>RiemannSheetParametricPlots4D</tt> that generates a <tt>GraphicsComplex</tt> with vertex values in <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_43.gif" alt="" width="15" height="15" />. (Here we use the suboption <tt>TimeConstraint</tt> in the <tt>Exclusions</tt> option to allow the calculation of more complicated exclusion sets for more complicated functions.) The sheets are generated using the function <tt>analyticallyContinue</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_61.gif" alt="" width="530" height="241" /></p>
<p>Here is an example. The abbreviated output shows that the actual 4D <tt>GraphicsComplex</tt> is quite large.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_62.gif" alt="" width="314" height="23" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_47.gif" alt="" width="473" height="209" /></p>
<p>In the absence of a direct 4D viewer in Version 6 (or any support for <tt>Graphics4D</tt>, for that matter&#8212;hopefully Version 7 will have at least some), we must project into a 3D subspace to view the surface as a <tt>Graphics3D</tt> object. For a given <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_44.gif" alt="" width="24" height="12" /> projection matrix, this is straightforward for the vertices and not too difficult for the normals. Fortunately, having the information of the embedded hypersurface in 4D allows a projection with normals in 3D using the Cauchy-Riemann conditions, which will result in a smooth-looking surface. Because we will use the final function interactively, we use <tt>Compile</tt> to carry out the calculation of the 3D vertex coordinates and the 3D normals as efficiently as possible.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_63.gif" alt="" width="519" height="148" /></p>
<p>The function <tt>to3DGraphicsComplex</tt> carries out the projection from 4D to 3D.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_64.gif" alt="" width="491" height="103" /></p>
<p>Putting the last two functions together, we have <tt>TetraviewPlot</tt>. We give it the option <tt>ColorFunction4D</tt>, which operates on the unscaled 4D coordinates to easily color a surface.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_65.gif" alt="" width="410" height="28" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_66.gif" alt="" width="483" height="88" /></p>
<p>Here are a few examples of the function <tt>TetraviewPlot</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_67.gif" alt="" width="471" height="23" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_48.gif" alt="" width="244" height="244" /></p>
<p>The next example colors the surface of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_45.gif" alt="" width="35" height="12" />)) according to values of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_46.gif" alt="" width="43" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_68.gif" alt="" width="567" height="43" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_49.gif" alt="" width="244" height="244" /></p>
<p>A convenient and natural way to parametrize the projection matrix is to use the six rotation angles between the four coordinate planes in <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_47.gif" alt="" width="15" height="15" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_69.gif" alt="" width="580" height="118" /></p>
<p>In the definition for <tt>RiemannSheetParametricPlots4D</tt>, we used the line</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_70.gif" alt="" width="446" height="28" /></p>
<p>to cache the result of the most time-consuming step inside <tt>TetraviewPlot</tt>. This allows us to implement a demonstration that interactively changes the 3D subspace selected.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_71.gif" alt="" width="580" height="19" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_50.gif" alt="" width="104" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Input_72.gif" alt="" width="580" height="99" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Output_51.gif" alt="" width="261" height="262" /></p>
<p>This demonstration gives us a feeling for the 2D hypersurface. Branch point neighborhoods in <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_48.gif" alt="" width="43" height="12" /> correspond to flat regions in <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_49.gif" alt="" width="40" height="12" />, and vice versa. As a result, the initial small gaps along the cuts of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/Corner10-4_Math_50.gif" alt="" width="43" height="12" /> widen as we look at the 4D surface from different directions.</p>
<p>This ends our short visit into the world of Riemann surface visualizations with <em>Mathematica</em> 6. We will be coming back to this subject in future columns.</p>
<h3>References</h3>
<table class="ReferenceTable">
<tbody>
<tr>
<td class="Reference"><a name="IIa">[1] </a></td>
<td>M. Trott, &#8220;Trott&#8217;s Corner: Visualization of Riemann Surfaces IIa: Compositions of Elementary Transcendental Functions,&#8221; <em>The Mathematica Journal</em>, <strong>7</strong>(4), 2000 pp. 465-496.</td>
</tr>
<tr>
<td class="Reference"><a name="IIb">[2] </a></td>
<td>M. Trott, &#8220;Trott&#8217;s Corner: Visualization of Riemann Surfaces IIb: Compositions of Elementary Transcendental Functions,&#8221; <em>The Mathematica Journal</em>, <strong>8</strong>(1), 2001 pp. 50-62.</td>
</tr>
<tr>
<td class="Reference"><a name="IIc">[3] </a></td>
<td>M. Trott, &#8220;Trott&#8217;s Corner: Visualization of Riemann Surfaces IIc: Compositions of Elementary Transcendental Functions,&#8221; <em>The Mathematica Journal</em>, <strong>8</strong>(3), 2002 pp. 409-432.</td>
</tr>
<tr>
<td class="Reference"><a name="IId">[4] </a></td>
<td>M. Trott, &#8220;Trott&#8217;s Corner: Visualization of Riemann Surfaces IId: Compositions of Elementary Transcendental Functions,&#8221; <em>The Mathematica Journal</em>, <strong>8</strong>(4), 2002 pp. 532-562.</td>
</tr>
<tr>
<td class="Reference"><a name="GuideBook">[5] </a></td>
<td>M. Trott, <em>The Mathematica GuideBook for Numerics</em>, New York: Springer-Verlag, 2006.</td>
</tr>
<tr>
<td class="Reference"><a name="Banchoff">[6] </a></td>
<td>T. F. Banchoff, &#8220;Computer Graphics in Geometric Research,&#8221; in <em>Recent Trends in Mathematics: Conference in Reinhardsbrunn (Reinhardsbrunn 1982)</em>, (H. Kurke, J. Mecke, H. Triebel, and R. Thiele, eds.), Teubner-Texte Math., <strong>50</strong>, Leipzig: Teubner, 1983 pp. 316-327.</td>
</tr>
<tr>
<td class="DOIReference" colspan="2">M. Trott, &#8220;The Return of the Riemann Surface,&#8221; <em>The Mathematica Journal</em>, 2011. dx.doi.org/10.3888/tmj.10.4-1.</td>
</tr>
</tbody>
</table>
<p class="TextAboutAuthor"><strong>Michael Trott</strong><br />
Senior Member, Technical Staff<br />
<em>Wolfram Research, Inc.<br />
</em><em><a href="mailto:mtrott@wolfram.com">mtrott@wolfram.com</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mathematica-journal.com/2012/02/the-return-of-the-riemann-surface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Classic Puzzles in Wolfram Demonstrations</title>
		<link>http://www.mathematica-journal.com/2012/02/classic-puzzles-in-wolfram-demonstrations/</link>
		<comments>http://www.mathematica-journal.com/2012/02/classic-puzzles-in-wolfram-demonstrations/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 00:10:32 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 10, Issue 4]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=17816</guid>
		<description><![CDATA[Sudoku, today&#8217;s most well-known grid-solving puzzle, was originally invented by Howard Garns in 1978. Almost 30 years later, its popularity suddenly exploded. This column presents logic puzzles of various sorts and challenges readers to solve the puzzles in two ways: by hand and with Mathematica. Solvers are invited to send their code to edp@wolfram.com.
Fifteen Classic [...]]]></description>
			<content:encoded><![CDATA[<p>Sudoku, today&#8217;s most well-known grid-solving puzzle, was originally invented by Howard Garns in 1978. Almost 30 years later, its popularity suddenly exploded. This column presents logic puzzles of various sorts and challenges readers to solve the puzzles in two ways: by hand and with <em>Mathematica. </em>Solvers are invited to send their code to <a href="mailto:edp@wolfram.com">edp@wolfram.com</a>.<span id="more-17816"></span></p>
<h3>Fifteen Classic Puzzles</h3>
<ol class="ItemNumbered">
<li>If a package has one of 50 random baseball cards, how many packages do you need to buy to get a complete set?<br />
(<a href="http://demonstrations.wolfram.com/CouponCollectorProblem/">demonstrations.wolfram.com/CouponCollectorProblem</a>)</li>
<li>A chess king is at one corner of a chessboard. It makes a series of moves that always take it closer to the opposite corner. How many possible paths are there? (<a href="http://demonstrations.wolfram.com/DelannoyPathExhaustionDiagonal/">demonstrations.wolfram.com/DelannoyPathExhaustionDiagonal</a>)</li>
<li>A hallway has 100 lockers, all closed. 100 students are sent down the hall as follows: student 1 opens all the lockers; student 2 closes every other locker, beginning with the second; student 3 changes the state of every third locker, beginning with the third; and so on. After all the students have marched, which lockers remain open?<br />
(<a href="http://demonstrations.wolfram.com/TheLockerProblem/">demonstrations.wolfram.com/TheLockerProblem</a>)</li>
<li>In the Florida Lotto<img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/Trademark.gif" alt="" width="12" height="19" />, a player picks 6 numbers out of 1 to 53. Matching 3, 4, 5, or 6 numbers from the drawing wins a prize. What are the odds of winning? (<a href="http://demonstrations.wolfram.com/UrnProblem/">demonstrations.wolfram.com/UrnProblem</a>)</li>
<li>On a <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Math_1.gif" alt="" width="24" height="12" /> grid, plant 10 trees to make 5 lines each containing 4 trees. (<a href="http://demonstrations.wolfram.com/OrchardPlantingProblem/">demonstrations.wolfram.com/OrchardPlantingProblem</a>)</li>
<li>A <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Math_2.gif" alt="" width="24" height="12" /> grid has 25 &#8220;on&#8221; buttons. Pressing a button changes the state of that button and the surrounding buttons. Can all the buttons be turned &#8220;off&#8221;? (<a href="http://demonstrations.wolfram.com/LightsOutPuzzle/">demonstrations.wolfram.com/LightsOutPuzzle</a>)</li>
<li>Can books similar in size be stacked on the edge of a desk so that the top book is not over the desk at all? (<a href="http://demonstrations.wolfram.com/StackingDominoesToTheLimit/">demonstrations.wolfram.com/StackingDominoesToTheLimit</a>)</li>
<li>McDonald&#8217;s <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Math_3.gif" alt="" width="7" height="14" /> once sold Chicken McNuggets<img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/Trademark.gif" alt="" width="12" height="19" /> in packs of 6, 9, or 20. What is the highest number of McNuggets you cannot buy?<br />
(<a href="http://demonstrations.wolfram.com/McNuggetProblemAndFrobeniusNumbers/">demonstrations.wolfram.com/McNuggetProblemAndFrobeniusNum-bers</a>)</li>
<li>Thirty people are in a room. What is the probability that two of them share a birthday? (<a href="http://demonstrations.wolfram.com/TheBirthdayProblem/">demonstrations.wolfram.com/TheBirthdayProblem</a>)</li>
<li>Wheels of radius 30cm and 40cm are 10cm apart. What is the length of a snug belt that goes around both? (<a href="http://demonstrations.wolfram.com/TwoWheelBelt/">demonstrations.wolfram.com/TwoWheelBelt</a>)</li>
<li>You have a 3 gallon jug and a 5 gallon jug at a well. How can exactly 1 gallon be measured? (<a href="http://demonstrations.wolfram.com/JugProblem/">demonstrations.wolfram.com/JugProblem</a>)</li>
<li>A 30-foot statue is on a 10-foot pedestal. Where should you stand so that the statue itself looks as big as possible from your point of view? (<a href="http://demonstrations.wolfram.com/TheStatueOfRegiomontanus/">demonstrations.wolfram.com/TheStatueOfRegiomontanus</a>)</li>
<li>As a contestant on Monty Hall&#8217;s game show, you are presented with three doors. Behind one door is a new car and behind each of the other two is a goat. You select a door behind which, you hope, is the new car. Monty Hall then opens another door to reveal a goat and asks if you would like to change your selection. Should you switch?<br />
(<a href="http://demonstrations.wolfram.com/MontyHallProblem/">demonstrations.wolfram.com/MontyHallProblem</a>)</li>
<li>You have 12 coins, labeled with letters M, I, T, F, O, L, K, D, A, N, C, and E. One of the coins is fake, and is heavier or lighter than the others. Can you find the fake coin in 3 weighings?<br />
(<a href="http://demonstrations.wolfram.com/The12CoinWeighingProblem/">demonstrations.wolfram.com/The12CoinWeighingProblem</a>)</li>
<li>A 55-gallon barrel, with a diameter of 22.5 inches, is lying on its side. 10 inches of grain is in the barrel. How much grain does the barrel contain? (<a href="http://demonstrations.wolfram.com/SagittaApothemAndChord/">demonstrations.wolfram.com/SagittaApothemAndChord</a>)</li>
</ol>
<h3>Discussion&#8212;Coupon Collecting</h3>
<p>For each of the problems listed above, a link is given to a Wolfram Demonstration that allows for further exploration [<a href="#Nikoli #73">1</a>]. For example, the Coupon Collector Demonstration comes with the following explanatory caption:</p>
<div class="Quote">If a package has one of 50 random baseball cards, how many packages do you need to buy to get a complete set? The expected answer is <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Math_4.gif" alt="" width="65" height="12" /> packages, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Math_5.gif" alt="" width="19" height="12" /> is the harmonic number <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Math_6.gif" alt="" width="146" height="12" />. Switching from baseball cards to coupons gives the coupon collector problem: if there are <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Math_7.gif" alt="" width="6" height="12" /> different kinds of coupons and many of them are distributed randomly, the expected number of purchases necessary for a complete set is <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Math_8.gif" alt="" width="23" height="12" />.</div>
<p>Code containing a <tt>Manipulate</tt> is provided for each Demonstration, allowing for exploration of the problem with either <em>Mathematica</em> or <em>Mathematica</em> <span class="TI">Player</span>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Input_1.gif" alt="" width="580" height="208" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/ClassicPuzzles_Output_1.gif" alt="" width="352" height="156" /></p>
<p>All the classic puzzles listed here have been given a similar Demonstration treatment. Is there another classic problem that should be added to The Wolfram Demonstrations Project? If so, please consider adding it at <a href="http://demonstrations.wolfram.com/participate.html">demonstrations.wolfram.com/participate.html</a>. Drop me a line once your new Demonstration has been published.</p>
<h3>References</h3>
<table class="ReferenceTable">
<tbody>
<tr>
<td class="Reference"><a name="Nikoli #73">[1] </a></td>
<td>The Wolfram Demonstrations Project, <a href="http://demonstrations.wolfram.com/">demonstrations.wolfram.com</a>.</td>
</tr>
<tr>
<td class="DOIReference" colspan="2">E. Pegg Jr., &#8220;Classic Puzzles in Wolfram Demonstrations,&#8221; <em>The Mathematica Journal</em>, 2011. dx.doi.org/10.3888/tmj.10.4-2.</td>
</tr>
</tbody>
</table>
<p class="TextAboutAuthor"><strong>Ed Pegg Jr</strong><br />
Scientific Information Editor<br />
<em>Wolfram Research, Inc.<br />
</em><em><a href="mailto:edp@wolfram.com">edp@wolfram.com</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mathematica-journal.com/2012/02/classic-puzzles-in-wolfram-demonstrations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Integration of Interpolating Functions and Some Concrete Optimal Stopping Problems</title>
		<link>http://www.mathematica-journal.com/2012/02/dynamic-integration-of-interpolating-functions-and-some-concrete-optimal-stopping-problems/</link>
		<comments>http://www.mathematica-journal.com/2012/02/dynamic-integration-of-interpolating-functions-and-some-concrete-optimal-stopping-problems/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 00:09:04 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 10, Issue 4]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=17818</guid>
		<description><![CDATA[This article describes a streamlined method for simultaneous integration of an entire family of interpolating functions that uses one and the same interpolation grid in one or more dimensions. A method for creating customized quadrature/cubature rules that takes advantage of certain special features of Mathematica&#8217;s InterpolatingFunction objects is presented. The use of such rules leads [...]]]></description>
			<content:encoded><![CDATA[<p>This article describes a streamlined method for simultaneous integration of an entire family of interpolating functions that uses one and the same interpolation grid in one or more dimensions. A method for creating customized quadrature/cubature rules that takes advantage of certain special features of <em>Mathematica</em>&#8217;s <tt>InterpolatingFunction</tt> objects is presented. The use of such rules leads to a new and more efficient implementation of the method for optimal stopping of stochastic systems that was developed in [<a href="#lyas04">1</a>]. In particular, this new implementation allows one to extend the scope of the method to free boundary optimal stopping problems in higher dimensions. Concrete applications to finance&#8212;mainly to American-style financial derivatives&#8212;are presented. In particular, the price of an American put option that can be exercised with any one of two uncorrelated underlying assets is calculated as a function of the observed prices. This method is similar in nature to the well-known Longstaff-Schwartz algorithm, but does not involve Monte-Carlo simulation of any kind.<span id="more-17818"></span></p>
<h3>Preliminaries</h3>
<p>The most common encounter with the concept of dynamic programming&#8212;and here we will be concerned only with continuous time dynamic programming (CTDP)&#8212;occurs in the context of the optimal stopping of an observable stochastic process with given termination payoff. This is a special case of stochastic optimal control where the control consists of a simple on-off switch, which, once turned off, cannot be turned on again. A generic example of an optimal stopping problem can be described as this: the investor observes a continuous Markov process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_1.gif" width="39" style="vertical-align:middle" height="12" alt="" /> with state-space <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_2.gif" width="39" style="vertical-align:middle" height="15" alt="" /> and with known stochastic dynamics and, having observed at time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_3.gif" width="24" style="vertical-align:middle" height="12" alt="" /> the value <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_4.gif" width="34" style="vertical-align:middle" height="16" alt="" />, where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_5.gif" width="12" style="vertical-align:middle" height="16" alt="" /> denotes the observed realization of the random variable <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_6.gif" width="12" style="vertical-align:middle" height="12" alt="" />, the investor must decide whether to terminate the process immediately and collect the payoff <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_7.gif" width="29" style="vertical-align:middle" height="16" alt="" />, where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_8.gif" width="49" style="vertical-align:middle" height="12" alt="" /> is some a priori determined (deterministic) payoff function, or to wait until time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_9.gif" width="24" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_10.gif" width="8" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_11.gif" width="4" style="vertical-align:middle" height="12" alt="" /> for the next opportunity to terminate the process (in the context of CTDP, the time step <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_12.gif" width="8" style="vertical-align:middle" height="12" alt="" /> should be understood as an infinitesimally small quantity). In addition, having observed the quantity <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_13.gif" width="34" style="vertical-align:middle" height="16" alt="" />, the investor must determine the value of the observable system (associated with that state) at time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_14.gif" width="24" style="vertical-align:middle" height="12" alt="" />. In many situations, the stopping of the process cannot occur after some finite deterministic time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_15.gif" width="28" style="vertical-align:middle" height="12" alt="" />.</p>
<p>There is a vast literature that deals with both the theoretical and the computational aspects of modeling, analysis, and optimal control of stochastic systems. The works of Bensoussan and Lions [<a href="#Ref:BenLio82">2</a>] and Davis [<a href="#Ref:Dav93">3</a>] are good examples of classical treatments of this subject. </p>
<p>Many important problems from the realm of finance can be formulated&#8212;and solved&#8212;as optimal stopping problems. For example, a particular business can be established at the fixed cost of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_16.gif" width="8" style="vertical-align:middle" height="12" alt="" /> dollars, but the actual market price of that business follows some continuous stochastic process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_17.gif" width="39" style="vertical-align:middle" height="12" alt="" />, which is observable. Having observed at time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_18.gif" width="4" style="vertical-align:middle" height="12" alt="" /> the value <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_19.gif" width="12" style="vertical-align:middle" height="16" alt="" />, an investor who owns the right to incorporate such a business must decide whether to exercise that right immediately, in which case the investor would collect the payoff of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_20.gif" width="32" style="vertical-align:middle" height="16" alt="" /> dollars, or to postpone the investment decision until time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_21.gif" width="24" style="vertical-align:middle" height="12" alt="" />, with the hope that the business will become more valuable. In addition, having observed the value <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_22.gif" width="12" style="vertical-align:middle" height="16" alt="" />, the investor may need to determine the market price of the guaranteed right (say, a patent) to eventually incorporate this business&#8212;now, or at any time in the future.</p>
<p>Another classical problem from the realm of finance is the optimal exercise of a stock option of American type, that is, an option that can be exercised at any time prior to the expiration date. A stock option is a contract that guarantees the right to buy (a <em>call option</em>) or to sell (a <em>put option</em>) a particular stock (the <em>underlying</em>) at some fixed price (the <em>strike price</em>, stated explicitly in the option contract) at any time prior to the expiration date <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_23.gif" width="8" style="vertical-align:middle" height="12" alt="" /> (the <em>maturity date</em>, also stated in the option contract). Consider, for example, an American style put option which expires at some future date <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_24.gif" width="28" style="vertical-align:middle" height="12" alt="" />. On date <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_25.gif" width="26" style="vertical-align:middle" height="12" alt="" />, the holder of the option observes the underlying price <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_26.gif" width="12" style="vertical-align:middle" height="16" alt="" /> and decides whether to exercise the option, that is, to sell the stock at the guaranteed price <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_27.gif" width="9" style="vertical-align:middle" height="12" alt="" /> (and, consequently, collect an immediate payoff of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_28.gif" width="33" style="vertical-align:middle" height="16" alt="" /> dollars) or to wait, hoping that at some future moment, but no later than the expiration date <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_29.gif" width="8" style="vertical-align:middle" height="12" alt="" />, the price will fall below the current level <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_30.gif" width="12" style="vertical-align:middle" height="16" alt="" />. If the option is not exercised before the expiration date, the option is lost if <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_31.gif" width="38" style="vertical-align:middle" height="12" alt="" /> or, ignoring any transaction costs, is always exercised if <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_32.gif" width="38" style="vertical-align:middle" height="12" alt="" />; that is, if the option is not exercised prior to the expiration date <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_33.gif" width="8" style="vertical-align:middle" height="12" alt="" />, on that date the owner of the option collects the amount <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_34.gif" width="79" style="vertical-align:middle" height="12" alt="" />. In general, stock options are traded in the same way that stocks are traded and, therefore, have a market value determined by the laws of supply and demand. At the same time, the price of such contracts, treated as a function of the observed stock price <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_35.gif" width="12" style="vertical-align:middle" height="16" alt="" />, can be calculated from general economic principles in conjunction with the principles of dynamic programming.</p>
<p>In general, the solution to any optimal stopping problem has two components: (1) for every moment in time, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_36.gif" width="4" style="vertical-align:middle" height="12" alt="" />, one must obtain a termination rule in the form of a termination set <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_37.gif" width="33" style="vertical-align:middle" height="12" alt="" /> so that the process is terminated at the first moment <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_38.gif" width="4" style="vertical-align:middle" height="12" alt="" /> at which the event <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_39.gif" width="45" style="vertical-align:middle" height="12" alt="" /> occurs; and (2) for every moment <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_40.gif" width="4" style="vertical-align:middle" height="12" alt="" />, one must determine the value function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_41.gif" width="49" style="vertical-align:middle" height="12" alt="" />, which measures how &#8220;valuable&#8221; different possible observations are at time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_42.gif" width="4" style="vertical-align:middle" height="12" alt="" />. Clearly, if <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_43.gif" width="24" style="vertical-align:middle" height="12" alt="" /> denotes the termination payoff function (e.g., in the case of an American style put option that would be <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_44.gif" width="23" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_45.gif" width="8" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_46.gif" width="70" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_47.gif" width="36" style="vertical-align:middle" height="12" alt="" />), then for every <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_48.gif" width="31" style="vertical-align:middle" height="12" alt="" /> one must have <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_49.gif" width="60" style="vertical-align:middle" height="12" alt="" />, while for every <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_50.gif" width="31" style="vertical-align:middle" height="12" alt="" /> one must have <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_51.gif" width="60" style="vertical-align:middle" height="12" alt="" />; that is, one would choose to continue only when continuation is more valuable than immediate termination. Consequently, the termination sets <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_52.gif" width="11" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_53.gif" width="24" style="vertical-align:middle" height="12" alt="" />, can be identified with the sets <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_54.gif" width="111" style="vertical-align:middle" height="18" alt="" />, and the associated optimal stopping time can be described as</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_1.gif" width="174" height="12" alt="" /></p>
<p>Thus, the entire solution to the optimal stopping problem can be expressed in terms of the family of value functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_55.gif" width="87" style="vertical-align:middle" height="12" alt="" />, which may be treated as a single function of the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_2.gif" width="152" height="12" alt="" /></p>
<p>It is important to recognize that the solution to the optimal stopping problem, that is, the value function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_56.gif" width="25" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_57.gif" width="17" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_58.gif" width="23" style="vertical-align:middle" height="12" alt="" />, must be computed before the observation process has begun. </p>
<p>In most practical situations, an approximate solution to the optimal stopping problem associated with some observable process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_59.gif" width="39" style="vertical-align:middle" height="12" alt="" /> and some fixed termination payoff <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_60.gif" width="49" style="vertical-align:middle" height="12" alt="" /> may be obtained as a finite sequence of approximate value functions</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_3.gif" width="243" height="32" alt="" /></p>
<p>where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_61.gif" width="28" style="vertical-align:middle" height="12" alt="" /> is the termination date and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_62.gif" width="6" style="vertical-align:middle" height="12" alt="" /> is some sufficiently large integer number. This approximate solution can be calculated from the following recursive rule, which is nothing but a special discrete version of what is known as the <em>dynamic programming equation</em>: for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_63.gif" width="26" style="vertical-align:middle" height="12" alt="" />, we set <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_64.gif" width="75" style="vertical-align:middle" height="14" alt="" />, that is, at time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_65.gif" width="26" style="vertical-align:middle" height="12" alt="" />, the value function coincides with the termination payoff on the entire state-space <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_66.gif" width="8" style="vertical-align:middle" height="12" alt="" />, and for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_67.gif" width="50" style="vertical-align:middle" height="23" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_68.gif" width="40" style="vertical-align:middle" height="23" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_69.gif" width="12" style="vertical-align:middle" height="12" alt="" /> , we set</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_1.gif" width="356" height="18" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="discountedAverage">(1)</a></td>
</tr>
</table>
<p>where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_70.gif" width="9" style="vertical-align:middle" height="12" alt="" /> is the instantaneous discount rate at time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_71.gif" width="4" style="vertical-align:middle" height="12" alt="" />, and the probability measure <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_72.gif" width="9" style="vertical-align:middle" height="12" alt="" />, with respect to which the conditional expectation is calculated, is the so-called <em>pricing measure</em> (in general, the instantaneous discount rate <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_73.gif" width="46" style="vertical-align:middle" height="12" alt="" /> may depend on the observed position <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_74.gif" width="32" style="vertical-align:middle" height="16" alt="" /> and the pricing measure may be different from the probability measure that governs the actual stochastic dynamics of the process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_75.gif" width="39" style="vertical-align:middle" height="12" alt="" />). Furthermore, for every <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_76.gif" width="48" style="vertical-align:middle" height="23" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_77.gif" width="38" style="vertical-align:middle" height="23" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_78.gif" width="12" style="vertical-align:middle" height="12" alt="" /> , the approximate termination set is given by</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_4.gif" width="160" height="18" alt="" /></p>
<p>If, given any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_79.gif" width="47" style="vertical-align:middle" height="12" alt="" />, the limit </p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_2.gif" width="159" height="17" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="limitToBelman">(2)</a></td>
</tr>
</table>
<p>(<img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_80.gif" width="15" style="vertical-align:middle" height="12" alt="" /> denotes the integer part of a real number) exists, in some appropriate topology on the space of functions defined on the state-space <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_81.gif" width="8" style="vertical-align:middle" height="12" alt="" />, then one can declare that the function</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_5.gif" width="158" height="15" alt="" /></p>
<p>gives <em>the</em> solution to the optimal stopping problem with observable process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_82.gif" width="39" style="vertical-align:middle" height="12" alt="" />, payoff function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_83.gif" width="24" style="vertical-align:middle" height="12" alt="" />, <em>and</em> pricing measure <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_84.gif" width="9" style="vertical-align:middle" height="12" alt="" />.</p>
<p>It is common to assume that under the pricing probability measure <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_85.gif" width="9" style="vertical-align:middle" height="12" alt="" /> the stochastic dynamics of the observable process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_86.gif" width="39" style="vertical-align:middle" height="12" alt="" /> are given by some diffusion equation of the form</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_3.gif" width="138" height="12" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="diffEq">(3)</a></td>
</tr>
</table>
<p>for some (sufficiently nice) matrix-valued function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_87.gif" width="123" style="vertical-align:middle" height="15" alt="" />, vector field <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_88.gif" width="107" style="vertical-align:middle" height="15" alt="" />, and some <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_89.gif" width="17" style="vertical-align:middle" height="15" alt="" />-valued Brownian motion process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_90.gif" width="41" style="vertical-align:middle" height="12" alt="" />, which is independent from the starting position <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_91.gif" width="14" style="vertical-align:middle" height="12" alt="" /> (note that <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_92.gif" width="41" style="vertical-align:middle" height="12" alt="" /> is Brownian motion with respect to the law <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_93.gif" width="9" style="vertical-align:middle" height="12" alt="" />). Given any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_94.gif" width="37" style="vertical-align:middle" height="15" alt="" />, let <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_95.gif" width="22" style="vertical-align:middle" height="12" alt="" /> denote the matrix <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_96.gif" width="53" style="vertical-align:middle" height="15" alt="" /> and let <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_97.gif" width="9" style="vertical-align:middle" height="12" alt="" /> denote the second-order field in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_98.gif" width="17" style="vertical-align:middle" height="15" alt="" /> given by</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_6.gif" width="264" height="43" alt="" /></p>
<p>As is well known, under certain fairly general conditions for the coefficients <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_99.gif" width="23" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_100.gif" width="21" style="vertical-align:middle" height="12" alt="" />, and the termination payoff <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_101.gif" width="49" style="vertical-align:middle" height="12" alt="" />, the value function</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_7.gif" width="152" height="12" alt="" /></p>
<p>that is, the solution to the associated optimal stopping problem, is known to satisfy the following equation, which is nothing but a special case of the Hamilton-Jacobi-Bellman (HJB) equation </p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_4.gif" width="303" height="18" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="BellmanEqG">(4)</a></td>
</tr>
</table>
<p>with boundary condition <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_102.gif" width="63" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_103.gif" width="37" style="vertical-align:middle" height="15" alt="" />. This equation is a more or less trivial consequence of equations (<a href="#discountedAverage">1</a>) and (<a href="#limitToBelman">2</a>), in conjunction with the It&#244; formula. It is important to recognize that the dynamic programming equation (<a href="#discountedAverage">1</a>) is primary for the optimal stopping problem, while the HJB equation (<a href="#BellmanEqG">4</a>) is only secondary, in that it is nothing more than a computational tool. Unfortunately, equation (<a href="#BellmanEqG">4</a>) admits a closed form solution only for some rather special choices of the payoff function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_104.gif" width="24" style="vertical-align:middle" height="12" alt="" />, the diffusion coefficients <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_105.gif" width="21" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_106.gif" width="23" style="vertical-align:middle" height="12" alt="" />, and the discount rate <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_107.gif" width="24" style="vertical-align:middle" height="12" alt="" />. Thus, in most practical situations, the HJB equation (<a href="#BellmanEqG">4</a>) happens to be useful only to the extent to which this equation gives rise to some numerical procedure that may allow one to compute the solution <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_108.gif" width="67" style="vertical-align:middle" height="12" alt="" /> approximately. Essentially, all known numerical methods for solving equation (<a href="#BellmanEqG">4</a>) are some variations of the finite difference method. The most common approach is to reformulate equation (<a href="#BellmanEqG">4</a>) as a <em>free boundary value problem</em>: one must find a closed domain <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_109.gif" width="69" style="vertical-align:middle" height="12" alt="" /> with a piecewise differentiable boundary <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_110.gif" width="17" style="vertical-align:middle" height="12" alt="" /> and nonempty interior <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_111.gif" width="14" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_112.gif" width="8" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_113.gif" width="31" style="vertical-align:middle" height="12" alt="" />, plus a continuous function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_114.gif" width="48" style="vertical-align:middle" height="12" alt="" />, which is continuously differentiable with respect to the variable <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_115.gif" width="49" style="vertical-align:middle" height="12" alt="" /> and is twice continuously differentiable with respect to the variables <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_116.gif" width="34" style="vertical-align:middle" height="12" alt="" />, that satisfies the following two relations everywhere inside <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_117.gif" width="14" style="vertical-align:middle" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_8.gif" width="359" height="12" alt="" /></p>
<p>and, finally, satisfies the following boundary conditions along the free (i.e., unknown) boundary <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_118.gif" width="17" style="vertical-align:middle" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_9.gif" width="371" height="39" alt="" /></p>
<p>The last two conditions are known, respectively, as the <em>value matching</em> and the <em>smooth pasting</em> conditions. It can be shown that under some rather general&#8212;but still quite technical&#8212;assumptions, the free boundary value problem that was just described has a unique solution (consisting of a function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_119.gif" width="32" style="vertical-align:middle" height="12" alt="" /> and domain <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_120.gif" width="10" style="vertical-align:middle" height="12" alt="" />) which then allows one to write the solution to the optimal stopping problem as follows: for any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_121.gif" width="47" style="vertical-align:middle" height="12" alt="" /> and any<img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_122.gif" width="30" style="vertical-align:middle" height="12" alt="" />, one has</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_10.gif" width="235" height="31" alt="" /></p>
<p>The drawbacks from formulating an optimal stopping problem as a free boundary value problem for some parabolic partial differential equation (PDE), which&#8212;in principle, at least&#8212;can be solved numerically by way of finite differencing, are well known. First, the value matching and the smooth pasting conditions are difficult to justify and this makes the very formulation of the problem rather problematic in many situations. Second, just in general, for purely technical reasons, it is virtually impossible to use finite differencing when the dimension of the state-space is higher than 3 and, in fact, in the case of free boundary value problems, even a state-space of dimension 2 is rather challenging. Third, with the exception of the explicit finite difference scheme, which is guaranteed to be stable only under some rather restrictive conditions, the implementation of most finite differencing procedures on parallel processors is anything but trivial.</p>
<p>At the time of this writing, it is safe to say that finite differencing is no longer at the center of attention in computational finance, where most problems are inherently complex and multidimensional. Indeed, most of the research in computational finance in the last five years or so appears to be focused on developing new simulation-based tools&#8212;see [<a href="#Ref:bp03">4</a>-<a href="#Ref:LS01">8</a>], for example (even a cursory review of the existing literature from the last few years is certain to be very long and is beyond the scope of this article). Among these methods, the Longstaff-Schwartz algorithm [<a href="#Ref:LS01">8</a>] seems to be the most popular among practitioners.</p>
<p>The attempts to avoid the use of finite differencing are nothing new (there is a section in the book <em>Numerical Recipes in</em> C entitled There Is More to Life than Finite Differencing&#8212;see [<a href="#Ref:nr">9</a>], p. 833). Indeed, Monte Carlo, finite element, and several other computational tools have been in use for solving fixed boundary value problems for PDEs for quite some time. Apparently, from the point of view of stochastic optimal control, it is more efficient&#8212;and in some ways more natural&#8212;to develop numerical procedures directly from the dynamic programming equation (<a href="#discountedAverage">1</a>) and skip the formulation of the HJB equation altogether&#8212;at least this is the approach that most recent studies in computational finance are taking.</p>
<p>Since the solution to an optimal stopping problem is given by an infinite family of functions of the form <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_123.gif" width="49" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_124.gif" width="47" style="vertical-align:middle" height="12" alt="" />, for some set <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_125.gif" width="39" style="vertical-align:middle" height="15" alt="" />, from a computational point of view the actual representation of any such object involves two levels of discretization. First, one must discretize the state-space <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_126.gif" width="8" style="vertical-align:middle" height="12" alt="" />, that is, functions defined on <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_127.gif" width="8" style="vertical-align:middle" height="12" alt="" /> must be replaced with finite lists of values attached to some fixed set of distinct abscissas</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_11.gif" width="98" height="12" alt="" /></p>
<p>for some fixed <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_128.gif" width="36" style="vertical-align:middle" height="12" alt="" />. This means that for every list of values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_129.gif" width="34" style="vertical-align:middle" height="12" alt="" />, one must be able to construct a function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_130.gif" width="50" style="vertical-align:middle" height="15" alt="" /> which &#8220;extends&#8221; the discrete assignment <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_131.gif" width="40" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_132.gif" width="46" style="vertical-align:middle" height="12" alt="" />, to some function defined on the entire state-space <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_133.gif" width="8" style="vertical-align:middle" height="12" alt="" />. Second, one must discretize time, that is, replace the infinite family of functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_134.gif" width="85" style="vertical-align:middle" height="12" alt="" /> not just with the finite sequence of functions</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_12.gif" width="250" height="32" alt="" /></p>
<p>determined by the discrete dynamic programming equation (<a href="#discountedAverage">1</a>), but, rather, with a finite sequence of lists</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_13.gif" width="219" height="32" alt="" /></p>
<p>computed from the following space-discretized analog of the time-discrete dynamic programming equation (<a href="#discountedAverage">1</a>): for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_135.gif" width="48" style="vertical-align:middle" height="23" alt="" />, we set</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_14.gif" width="328" height="18" alt="" /></p>
<p>and then define </p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_5.gif" width="371" height="18" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="QDPE">(5)</a></td>
</tr>
</table>
<p>consecutively, for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_136.gif" width="56" style="vertical-align:middle" height="23" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_137.gif" width="56" style="vertical-align:middle" height="23" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_138.gif" width="14" style="vertical-align:middle" height="12" alt="" />, where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_139.gif" width="78" style="vertical-align:middle" height="15" alt="" /> is simply the map that &#8220;extends&#8221; the discrete assignment <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_140.gif" width="74" style="vertical-align:middle" height="24" alt="" />.</p>
<p>Of course, in order to compute the conditional expectation in equation (<a href="#QDPE">5</a>), the distribution law of the random variable <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_141.gif" width="31" style="vertical-align:middle" height="14" alt="" /> relative to the pricing measure <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_142.gif" width="9" style="vertical-align:middle" height="12" alt="" /> and conditioned to the event <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_143.gif" width="45" style="vertical-align:middle" height="12" alt="" /> must be expressed in computable form. Unfortunately, this is possible only for some rather special choices for the diffusion coefficients <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_144.gif" width="23" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_145.gif" width="21" style="vertical-align:middle" height="12" alt="" /> that govern the stochastic dynamics of the process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_146.gif" width="39" style="vertical-align:middle" height="12" alt="" /> under the pricing measure <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_147.gif" width="9" style="vertical-align:middle" height="12" alt="" />. This imposes yet another&#8212;third&#8212;level of approximation. The simplest such approximation is to replace <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_148.gif" width="31" style="vertical-align:middle" height="14" alt="" /> in equation (<a href="#QDPE">5</a>) with the random quantity</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_6.gif" width="222" height="38" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="ladiff">(6)</a></td>
</tr>
</table>
<p>where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_149.gif" width="7" style="vertical-align:middle" height="12" alt="" /> is some standard normal <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_150.gif" width="17" style="vertical-align:middle" height="15" alt="" />-valued random variable. As a result of this approximation, expression (<a href="#QDPE">5</a>) becomes</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_7.gif" width="383" height="30" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="compInt">(7)</a></td>
</tr>
</table>
<p>where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_151.gif" width="30" style="vertical-align:middle" height="12" alt="" /> stands for the standard normal probability density function in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_152.gif" width="17" style="vertical-align:middle" height="15" alt="" />. Another (somewhat more sophisticated) approximation scheme for the diffusion process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_153.gif" width="39" style="vertical-align:middle" height="12" alt="" /> in the case <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_154.gif" width="29" style="vertical-align:middle" height="12" alt="" /> is discussed in [<a href="#Ref:lyas04">1</a>]. Fortunately, in the most widely used diffusion models in finance, the conditional law of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_155.gif" width="31" style="vertical-align:middle" height="14" alt="" />, given the event <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_156.gif" width="45" style="vertical-align:middle" height="12" alt="" />, coincides with the law of a random variable of the form <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_157.gif" width="40" style="vertical-align:middle" height="12" alt="" /> for some function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_158.gif" width="37" style="vertical-align:middle" height="12" alt="" /> that can be expressed in computable form, so that in this special case the right side of equation (<a href="#compInt">7</a>) is exactly identical to the right side of equation (<a href="#QDPE">5</a>) and the approximation given in equation (<a href="#ladiff">6</a>) is no longer necessary.</p>
<p>One must be aware that, in general, the distribution law of the random variable <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_159.gif" width="40" style="vertical-align:middle" height="12" alt="" /> is spread on both sides of the point <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_160.gif" width="11" style="vertical-align:middle" height="12" alt="" /> and, even though for a reasonably small time step <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_161.gif" width="10" style="vertical-align:middle" height="23" alt="" /> this law is concentrated almost exclusively in some small neighborhood of the node <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_162.gif" width="11" style="vertical-align:middle" height="12" alt="" />, when this node happens to be very close to the border of the interpolation region, the computation of the integral on the right side of equation (<a href="#compInt">7</a>) inevitably requires information about the function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_163.gif" width="53" style="vertical-align:middle" height="15" alt="" /> outside the interpolation domain. However, strictly speaking, interpolating functions are well defined only inside the convex hull of the abscissas used in the interpolation. One way around this problem is to choose the interpolation domain in such a way that along its border the solution to the optimal stopping problem takes values that are very close to certain asymptotic values. For example, if one is pricing an American put option with strike price of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_164.gif" width="12" style="vertical-align:middle" height="12" alt="" />, one may assume that the value of the option is practically <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_165.gif" width="6" style="vertical-align:middle" height="12" alt="" /> when the price of the underlying asset is above <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_166.gif" width="24" style="vertical-align:middle" height="12" alt="" />, regardless of the time left to maturity (of course, this assumption may be grossly inaccurate when the volatility is very large and/or when the option matures in the very distant future). Consequently, for those nodes <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_167.gif" width="11" style="vertical-align:middle" height="12" alt="" /> that are close to the border of the interpolation domain, the respective values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_168.gif" width="22" style="vertical-align:middle" height="12" alt="" /> will not depend on <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_169.gif" width="4" style="vertical-align:middle" height="12" alt="" /> and will not be updated according to the prescription (<a href="#compInt">7</a>). At the same time, the remaining nodes <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_170.gif" width="11" style="vertical-align:middle" height="12" alt="" /> must be chosen in such a way that the probability mass of the random variable <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_171.gif" width="40" style="vertical-align:middle" height="12" alt="" /> is concentrated almost exclusively inside the interpolation region. </p>
<p>In general, numerical procedures for optimal stopping of stochastic systems differ in the following: First, they differ in the space-discretization method, that is, the method for choosing the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_172.gif" width="11" style="vertical-align:middle" height="12" alt="" /> and for &#8220;reconstructing&#8221; the function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_173.gif" width="25" style="vertical-align:middle" height="15" alt="" /> from the (finite) list of values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_174.gif" width="5" style="vertical-align:middle" height="12" alt="" /> assigned to the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_175.gif" width="11" style="vertical-align:middle" height="12" alt="" />. Second, numerical procedures differ in the concrete quadrature rule used in the calculation of the conditional expectation in the discretized dynamic programming equation (<a href="#QDPE">5</a>). The method described in this article is essentially a refinement of the method of dynamic interpolation and integration described in [<a href="#Ref:lyas04">1</a>]. The essence of this method is that the functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_176.gif" width="25" style="vertical-align:middle" height="15" alt="" /> are defined by way of spline interpolation&#8212;or some other type of interpolation&#8212;from the list of values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_177.gif" width="5" style="vertical-align:middle" height="12" alt="" />, while the integral in equation (<a href="#compInt">7</a>) is calculated by using some standard procedures for numerical integration. As was illustrated in [<a href="#Ref:lyas04">1</a>], the implementation of this procedure in <em>Mathematica</em> is particularly straightforward, since <tt>NIntegrate</tt> accepts as an input <tt>InterpolatingFunction</tt> objects, and, in fact, can handle such objects rather efficiently.</p>
<p>The key point in the method developed in this article is a procedure for computing a universal list of vectors <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_178.gif" width="43" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_179.gif" width="46" style="vertical-align:middle" height="12" alt="" />, associated with the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_180.gif" width="11" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_181.gif" width="46" style="vertical-align:middle" height="12" alt="" />, that depend only on the stochastic dynamics of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_182.gif" width="39" style="vertical-align:middle" height="12" alt="" /> and the time step <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_183.gif" width="10" style="vertical-align:middle" height="23" alt="" />, so that one can write</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_8.gif" width="226" height="33" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="quadratureRule">(8)</a></td>
</tr>
</table>
<p>for any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_184.gif" width="4" style="vertical-align:middle" height="12" alt="" /> and for any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_185.gif" width="6" style="vertical-align:middle" height="12" alt="" />, where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_186.gif" width="8" style="vertical-align:middle" height="12" alt="" /> stands for the convex hull of the interpolation grid <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_187.gif" width="11" style="vertical-align:middle" height="12" alt="" />, that is, the domain of interpolation. Essentially, equation (<a href="#quadratureRule">8</a>) is a variation of the well-known cubic spline quadrature rule&#8212;see &#167;4.0 in [<a href="#nr">9</a>] and &#167;5.2, p. 89, in [<a href="#fmm77">10</a>]. It is also analogous to the &#8220;cubature formula,&#8221; which was developed in [<a href="#Ref:LyoVic04">11</a>] with the help of a completely different computational tool (following [<a href="#Ref:Str71">14</a>] and [<a href="#Ref:LyoVic04">11</a>], we use the term <em>cubature</em> as a reference to some integration rule for multiple integrals and the term <em>quadrature</em> as a reference to some integration rule for single integrals). One of the principal differences between the interpolation quadrature/cubature rules and the cubature formula obtained in [<a href="#Ref:LyoVic04">11</a>] is that the former allows for a greater freedom in the choice of the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_188.gif" width="11" style="vertical-align:middle" height="12" alt="" />. This means that, in practice, one can choose the evaluation points in a way that takes into account the geometry of the payoff function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_189.gif" width="24" style="vertical-align:middle" height="12" alt="" /> and not just the geometry of the diffusion <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_190.gif" width="39" style="vertical-align:middle" height="12" alt="" />. From a practical point of view, this feature is quite important and, in fact, was the main reason for developing what is now known as <em>Gaussian quadratures</em>.</p>
<p>An entirely different method for computing conditional averages of the form </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_15.gif" width="117" height="18" alt="" /></p>
<p>was developed in [<a href="#Ref:Kus01">12</a>] and [<a href="#Ref:Kus04">13</a>]. In this method, quantities of this form are approximated by finite products of linear operators acting on the function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_191.gif" width="22" style="vertical-align:middle" height="12" alt="" />. One must be aware that the methods described in [<a href="#Ref:LyoVic04">11</a>-<a href="#Ref:Kus04">13</a>] require a certain degree of smoothness for the integrand <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_192.gif" width="22" style="vertical-align:middle" height="12" alt="" />, which, generally, <tt>InterpolatingFunction</tt> objects may not have.</p>
<p>The method developed in this article bears some similarity also with the quantization algorithm described in [<a href="#Ref:bp03">4</a>, <a href="#Ref:bp03a">5</a>]. The key feature in both methods is that one can separate and compute&#8212;once and for all&#8212;certain quantities that depend on the observable process but not on the payoff function. Essentially, this means that one must solve, simultaneously, an entire family of boundary value problems corresponding to different payoff functions. The two methods differ in the way in which functions on the state-space are encoded in terms of finite lists of values. The advantages in using interpolating functions in general, or splines in particular, are well known: one can &#8220;restore&#8221; the pricing maps from fewer evaluation points and this feature is crucial for optimal stopping problems in higher dimensions.</p>
<h3>Making Quadrature/Cubature Rules with <em>Mathematica</em></h3>
<p>In this section we show how to make quadrature/cubature rules for a fixed set of abscissas that are similar to the well-known Gaussian rules or the cubic spline rules. We consider quadrature rules on the real line <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_193.gif" width="9" style="vertical-align:middle" height="12" alt="" /> first. To begin with, suppose that one must integrate some cubic spline <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_194.gif" width="45" style="vertical-align:middle" height="12" alt="" /> that is defined from the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_195.gif" width="89" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_196.gif" width="64" style="vertical-align:middle" height="12" alt="" />, and from some list of tabulated values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_197.gif" width="87" style="vertical-align:middle" height="12" alt="" />. Since <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_198.gif" width="21" style="vertical-align:middle" height="12" alt="" /> is a cubic spline, for any fixed <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_199.gif" width="72" style="vertical-align:middle" height="12" alt="" /> there are real numbers <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_200.gif" width="18" style="vertical-align:middle" height="15" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_201.gif" width="18" style="vertical-align:middle" height="15" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_202.gif" width="18" style="vertical-align:middle" height="15" alt="" />, and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_203.gif" width="18" style="vertical-align:middle" height="15" alt="" /> for which we have</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_16.gif" width="300" height="18" alt="" /></p>
<p>As a result, one can write</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_9.gif" width="200" height="40" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq0">(9)</a></td>
</tr>
</table>
<p>which reduces the computation of the integral <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_204.gif" width="54" style="vertical-align:middle" height="21" alt="" /> to the computation of the coefficients <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_205.gif" width="16" style="vertical-align:middle" height="15" alt="" /> from the tabulated values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_206.gif" width="10" style="vertical-align:middle" height="12" alt="" /> and the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_207.gif" width="11" style="vertical-align:middle" height="12" alt="" />.</p>
<p>Though not immediately obvious, it so happens that when all abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_208.gif" width="64" style="vertical-align:middle" height="12" alt="" /> are fixed, each coefficient <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_209.gif" width="16" style="vertical-align:middle" height="15" alt="" />&#8212;and, consequently, the entire expression on the right side of equation (<a href="#eq0">9</a>)&#8212;can be treated as a linear function of the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_210.gif" width="58" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_211.gif" width="8" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_212.gif" width="15" style="vertical-align:middle" height="12" alt="" /> (in fact, each <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_213.gif" width="16" style="vertical-align:middle" height="15" alt="" /> is a linear function only of a small portion of that list). This property is instrumental for the quadrature/cubature rules that will be developed in the next section. To see why one can make this claim, recall that (e.g., see &#167;3.3 in [<a href="#nr">9</a>]) the cubic spline created from the discrete assignment <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_214.gif" width="40" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_215.gif" width="46" style="vertical-align:middle" height="12" alt="" />, is given by</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_10.gif" width="336" height="55" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq1">(10)</a></td>
</tr>
</table>
<p>where</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_17.gif" width="180" height="127" alt="" /></p>
<p>and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_216.gif" width="59" style="vertical-align:middle" height="16" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_217.gif" width="46" style="vertical-align:middle" height="12" alt="" /> (note that the definition implies <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_218.gif" width="49" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_219.gif" width="46" style="vertical-align:middle" height="12" alt="" />). Usually, one sets <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_220.gif" width="56" style="vertical-align:middle" height="16" alt="" /> (which gives the so-called <em>natural splines</em>) and determines the remaining values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_221.gif" width="69" style="vertical-align:middle" height="18" alt="" /> from the requirement that the cubic spline has a continuous first derivative on the entire interval <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_222.gif" width="41" style="vertical-align:middle" height="12" alt="" /> (it is a trivial matter to check that with this choice the second derivative of the function defined in equation (<a href="#eq1">10</a>) is automatically continuous on <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_223.gif" width="43" style="vertical-align:middle" height="12" alt="" />). Finally, recall that this last requirement leads to a system of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_224.gif" width="24" style="vertical-align:middle" height="12" alt="" /> linear equations with unknowns <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_225.gif" width="58" style="vertical-align:middle" height="18" alt="" /> and that the right side of each of these equations is a linear function of the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_226.gif" width="58" style="vertical-align:middle" height="12" alt="" />, while the coefficients in the left side are linear functions of the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_227.gif" width="60" style="vertical-align:middle" height="12" alt="" />&#8212;see equation (3.3.7) in [<a href="#nr">9</a>], for example. This means that each quantity in the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_228.gif" width="69" style="vertical-align:middle" height="18" alt="" /> can be treated as a linear function of the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_229.gif" width="58" style="vertical-align:middle" height="12" alt="" />. Since the quantities <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_230.gif" width="27" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_231.gif" width="26" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_232.gif" width="27" style="vertical-align:middle" height="12" alt="" />, and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_233.gif" width="28" style="vertical-align:middle" height="12" alt="" /> are all independent from the choice of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_234.gif" width="58" style="vertical-align:middle" height="12" alt="" />, one can claim that the entire expression in the right side of equation (<a href="#eq1">10</a>) is a linear function of the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_235.gif" width="58" style="vertical-align:middle" height="12" alt="" />. Thus, for every <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_236.gif" width="72" style="vertical-align:middle" height="12" alt="" /> and every <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_237.gif" width="66" style="vertical-align:middle" height="12" alt="" />, we can write</p>
<p><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_238.gif" width="256" style="vertical-align:middle" height="38" alt="" />
<p>where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_239.gif" width="31" style="vertical-align:middle" height="15" alt="" /> are polynomials of degree at most <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_240.gif" width="6" style="vertical-align:middle" height="12" alt="" />. More importantly, these polynomials are universal in the sense that they depend on the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_241.gif" width="11" style="vertical-align:middle" height="12" alt="" /> but not on the tabulated values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_242.gif" width="10" style="vertical-align:middle" height="12" alt="" />. In fact, this representation holds for spline interpolation objects of any degree, except that in the general case the degree of the polynomials <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_243.gif" width="31" style="vertical-align:middle" height="15" alt="" /> may be bigger than 3. An analogous representation is valid for other (non-spline) interpolating functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_244.gif" width="21" style="vertical-align:middle" height="12" alt="" />; in particular, such a representation is valid for interpolating functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_245.gif" width="21" style="vertical-align:middle" height="12" alt="" /> defined by way of divided differences interpolation, which is the method used by <tt>ListInterpolation</tt>. For example, if <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_246.gif" width="21" style="vertical-align:middle" height="12" alt="" /> denotes the usual interpolating polynomial of degree <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_247.gif" width="24" style="vertical-align:middle" height="12" alt="" /> defined from the discrete assignment <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_248.gif" width="40" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_249.gif" width="46" style="vertical-align:middle" height="12" alt="" />, then <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_250.gif" width="71" style="vertical-align:middle" height="15" alt="" /> is simply the <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_251.gif" width="14" style="vertical-align:middle" height="15" alt="" /> Lagrange polynomial for the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_252.gif" width="11" style="vertical-align:middle" height="12" alt="" />. Thus, when <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_253.gif" width="21" style="vertical-align:middle" height="12" alt="" /> is an interpolating function object (defined by, say, splines, divided differences, or standard polynomial interpolation) from the assignment <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_254.gif" width="40" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_255.gif" width="46" style="vertical-align:middle" height="12" alt="" />, assuming that <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_256.gif" width="22" style="vertical-align:middle" height="12" alt="" /> is some (fixed) continuous and strictly monotone function and that <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_257.gif" width="22" style="vertical-align:middle" height="12" alt="" /> is some (fixed) integrable function and, finally, assuming that the values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_258.gif" width="33" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_259.gif" width="46" style="vertical-align:middle" height="12" alt="" />, are chosen so that <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_260.gif" width="50" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_261.gif" width="46" style="vertical-align:middle" height="12" alt="" />, one can write</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_11.gif" width="294" height="38" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="eq2">(11)</a></td>
</tr>
</table>
<p>where</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_18.gif" width="243" height="40" alt="" /></p>
<p>Since the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_262.gif" width="93" style="vertical-align:middle" height="12" alt="" /> can be computed once and for all and independently from the interpolated values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_263.gif" width="10" style="vertical-align:middle" height="12" alt="" />, and since the calculation of the integral <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_264.gif" width="92" style="vertical-align:middle" height="21" alt="" /> comes down to the calculation of the dot product <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_265.gif" width="124" style="vertical-align:middle" height="12" alt="" />, the computation of the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_266.gif" width="93" style="vertical-align:middle" height="12" alt="" /> may be viewed as a simultaneous integration of all functions of the form <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_267.gif" width="83" style="vertical-align:middle" height="12" alt="" /> for all interpolating functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_268.gif" width="21" style="vertical-align:middle" height="12" alt="" /> (of the same interpolation type) that are based on one and the same (forever fixed) set of abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_269.gif" width="60" style="vertical-align:middle" height="12" alt="" />. Indeed, the knowledge of the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_270.gif" width="64" style="vertical-align:middle" height="12" alt="" /> turns the expression <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_271.gif" width="92" style="vertical-align:middle" height="21" alt="" /> into a trivial function of the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_272.gif" width="58" style="vertical-align:middle" height="12" alt="" />, that is, a trivial function defined on the entire class of interpolating functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_273.gif" width="21" style="vertical-align:middle" height="12" alt="" />. <em>In other words, in equation (</em><a href="#eq2">11</a><em>) we have made a quadrature (or cubature) rule</em>.</p>
<p>The preceding remarks contain nothing new or surprising in any way and we only need to find a practical way for computing the weights <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_274.gif" width="13" style="vertical-align:middle" height="12" alt="" />. Fortunately, <em>Mathematica</em> allows one to define <tt>InterpolatingFunction</tt> objects even if the interpolated values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_275.gif" width="10" style="vertical-align:middle" height="12" alt="" /> are defined as symbols that do not have actual numerical values assigned to them. Furthermore, such objects can be treated as ordinary functions that can be evaluated, differentiated, and so on&#8212;all in symbolic form. We illustrate this powerful feature next (recall that <tt>ListInterpolation</tt> has default setting <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_276.gif" width="154" style="vertical-align:middle" height="12" alt="" />).</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_19.gif" width="118" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_1.gif" width="185" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_20.gif" width="125" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_2.gif" width="173" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_21.gif" width="158" height="15" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_3.gif" width="148" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_22.gif" width="157" height="32" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_4.gif" width="100" height="32" alt="" /></p>
<p>Since, just by definition, the restriction of the function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_277.gif" width="42" style="vertical-align:middle" height="12" alt="" /> to the interval <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_278.gif" width="27" style="vertical-align:middle" height="12" alt="" /> coincides with a polynomial of degree 3, by Taylor&#8217;s theorem, for any fixed <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_279.gif" width="54" style="vertical-align:middle" height="12" alt="" />, we have</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_23.gif" width="244" height="40" alt="" /></p>
<p>Thus, if we define</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_24.gif" width="269" height="34" alt="" /></p>
<p>then on the interval <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_280.gif" width="27" style="vertical-align:middle" height="12" alt="" /> we can identify the function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_281.gif" width="6" style="vertical-align:middle" height="12" alt="" /> with the dot product</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_25.gif" width="369" height="23" alt="" /></p>
<p><em>Mathematica</em> can rearrange this expression as an ordinary polynomial:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_26.gif" width="148" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_5.gif" width="419" height="32" alt="" /></p>
<p>We can also do</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_27.gif" width="370" height="32" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_28.gif" width="154" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_6.gif" width="580" height="32" alt="" /></p>
<p>Essentially, this is some sort of reverse engineering of the <tt>InterpolatingFunction</tt> object <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_282.gif" width="6" style="vertical-align:middle" height="12" alt="" />, that is, a way to &#8220;coerce&#8221; <em>Mathematica</em> to reveal what polynomials the object <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_283.gif" width="6" style="vertical-align:middle" height="12" alt="" /> is constructed of between the interpolation points.</p>
<p>There are other tricks that we can use in order to reverse engineer an <tt>InterpolatingFunction</tt> object. For example, with the definition of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_284.gif" width="6" style="vertical-align:middle" height="12" alt="" />, the entire function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_285.gif" width="109" style="vertical-align:middle" height="12" alt="" /> can be restored from the values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_286.gif" width="125" style="vertical-align:middle" height="24" alt="" />. To do the restoration we must solve a linear system for the four unknown coefficients in some (unknown) cubic polynomial. This can be implemented in <em>Mathematica</em> rather elegantly without the use of <tt>CoefficientList</tt>. The only drawback in this approach is that one must resort to <tt>Solve</tt> or <tt>LinearSolve</tt>, which, generally, are more time-consuming, especially in dimensions 2 or higher. Of course, instead of using reverse engineering, one can simply implement the interpolation algorithm step-by-step, which, of course, involves a certain amount of work.</p>
<p>For example, we have</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_29.gif" width="144" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_7.gif" width="334" height="16" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_30.gif" width="54" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_8.gif" width="6" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_31.gif" width="156" height="33" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_9.gif" width="6" height="12" alt="" /></p>
<p>In the definition of <tt>S34</tt>, the variable <tt>x0</tt> can be given any numerical value inside the interval <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_287.gif" width="29" style="vertical-align:middle" height="12" alt="" />; the choice <span class="MCode"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_288.gif" width="51" style="vertical-align:middle" height="12" alt="" /></span><span class="MCode"> </span>was completely arbitrary.</p>
<p>In the same way, one can reverse engineer <tt>InterpolatingFunction</tt> objects that depend on any number of independent abscissas. Here is an example.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_32.gif" width="163" height="19" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_33.gif" width="137" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_10.gif" width="175" height="29" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_34.gif" width="218" height="15" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_11.gif" width="580" height="33" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_35.gif" width="462" height="36" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_36.gif" width="580" height="40" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_37.gif" width="205" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_12.gif" width="563" height="58" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_38.gif" width="54" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_13.gif" width="6" height="12" alt="" /></p>
<p>In this example, for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_289.gif" width="46" style="vertical-align:middle" height="12" alt="" /> and for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_290.gif" width="46" style="vertical-align:middle" height="12" alt="" />, the quantity <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_291.gif" width="46" style="vertical-align:middle" height="12" alt="" /> can be expressed as the following polynomial of the independent variables <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_292.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_293.gif" width="6" style="vertical-align:middle" height="12" alt="" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_39.gif" width="64" height="12" alt="" /></p>
<p>If one does not suppress the output from the last line (which would produce a fairly long output), it  becomes clear that all coefficients in the polynomial of the variables <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_294.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_295.gif" width="6" style="vertical-align:middle" height="12" alt="" /> are linear functions of the array <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_296.gif" width="65" style="vertical-align:middle" height="15" alt="" />. Consequently, if the variables <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_297.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_298.gif" width="6" style="vertical-align:middle" height="12" alt="" /> are integrated out, the expression will turn into a linear combination of all the entries (symbols) in the array <tt>VV</tt>.</p>
<p>Now we turn to concrete applications of the features described in this section. For the purpose of illustration, all time-consuming operations in this notebook are executed within the <tt>Timing</tt> function. The CPU times reflect the speed on Dual 2 GHz PowerPC processors with 2.5&#160;GB of RAM. The <em>Mathematica</em> version is 6.0.1. For all inputs with computing time longer than 1 minute, a file that contains the returned value is made available. </p>
<h3>Some Examples of Cubic Cubature Rules for Gaussian Integrals in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_299.gif" width="19" style="vertical-align:middle" height="19" alt="" /></h3>
<p>First, define the standard normal density in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_300.gif" width="15" style="vertical-align:middle" height="15" alt="" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_40.gif" width="130" height="39" alt="" /></p>
<p>In this section, we develop cubature rules for computing integrals of the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_41.gif" width="146" height="30" alt="" /></p>
<p>for certain functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_301.gif" width="22" style="vertical-align:middle" height="12" alt="" />. We suppose that the last integral can be replaced by</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_12.gif" width="142" height="30" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="finiteIntegral">(12)</a></td>
</tr>
</table>
<p>without a significant loss of precision, where the value</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_42.gif" width="42" height="33" alt="" /></p>
<p>is chosen so that</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_43.gif" width="207" height="42" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_14.gif" width="23" height="12" alt="" /></p>
<p>The next step is to choose the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_302.gif" width="28" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_303.gif" width="49" style="vertical-align:middle" height="12" alt="" />, and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_304.gif" width="28" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_305.gif" width="43" style="vertical-align:middle" height="12" alt="" />:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_44.gif" width="191" height="33" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_45.gif" width="162" height="12" alt="" /></p>
<p>For the sake of simplicity, in display equations we will write <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_306.gif" width="11" style="vertical-align:middle" height="12" alt="" /> instead of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_307.gif" width="28" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_308.gif" width="10" style="vertical-align:middle" height="12" alt="" /> instead of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_309.gif" width="28" style="vertical-align:middle" height="12" alt="" />. Then we define the associated array of symbolic values attached to the interpolation nodes <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_310.gif" width="36" style="vertical-align:middle" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_46.gif" width="146" height="19" alt="" /></p>
<p>and produce the actual <tt>InterpolatingFunction</tt> object:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_47.gif" width="369" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_15.gif" width="192" height="49" alt="" /></p>
<p>Note that <tt>V</tt> was defined simply as an array of symbols and that, therefore, <tt>f</tt> can be treated as a symbolic object, too. In any case, we have</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_48.gif" width="241" height="73" alt="" /></p>
<p>Note that each double integral on the right side of the last identity is actually an integral of some polynomial of the variables <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_311.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_312.gif" width="6" style="vertical-align:middle" height="12" alt="" /> multiplied by the standard normal density in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_313.gif" width="15" style="vertical-align:middle" height="15" alt="" />. Furthermore, as was pointed out earlier, every coefficient in this polynomial is actually a linear function of the array <tt>V</tt>. Since the <tt>InterpolatingFunction</tt> object <tt>f</tt> was defined with <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_314.gif" width="154" style="vertical-align:middle" height="12" alt="" /> (the default setting for <tt>ListInterpolation</tt>), we can write</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_49.gif" width="270" height="79" alt="" /></p>
<p>where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_315.gif" width="101" style="vertical-align:middle" height="18" alt="" /> are linear functions of the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_316.gif" width="8" style="vertical-align:middle" height="12" alt="" /> that can be computed once and for all by using the method described in the previous section. In addition, the integrals in the right side of the last identity also can be computed once and for all by using straightforward integration&#8212;this is the only place in the procedure where actual integration takes place. Once these calculations are completed, the integral (<a href="#finiteIntegral">12</a>) can be expressed as an explicit linear function of the array <tt>V</tt> of the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_50.gif" width="153" height="12" alt="" /></p>
<p>for some fixed tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_317.gif" width="51" style="vertical-align:middle" height="15" alt="" />. Of course, the actual calculation of the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_318.gif" width="9" style="vertical-align:middle" height="12" alt="" /> is much more involved and time consuming than the direct numerical evaluation of the integral (<a href="#finiteIntegral">12</a>) in those instances where <tt>f</tt> is not a symbolic object, that is, when the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_319.gif" width="15" style="vertical-align:middle" height="15" alt="" /> are given concrete numeric values. The point is that the calculation of the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_320.gif" width="9" style="vertical-align:middle" height="12" alt="" /> is tantamount to writing the integral (<a href="#finiteIntegral">12</a>) as an explicit function of the symbols that define <tt>f</tt>; that is, we evaluate simultaneously the entire family of integrals for all possible choices of actual numeric values that can be assigned to the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_321.gif" width="15" style="vertical-align:middle" height="15" alt="" />. Once the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_322.gif" width="9" style="vertical-align:middle" height="12" alt="" /> is calculated, we can write</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_13.gif" width="268" height="30" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="approximation">(13)</a></td>
</tr>
</table>
<p>Note that this identity is exact if the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_323.gif" width="36" style="vertical-align:middle" height="12" alt="" /> are exact numbers. In other words, by computing the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_324.gif" width="9" style="vertical-align:middle" height="12" alt="" /> we have made a <em>cubature rule</em>. Furthermore, when <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_325.gif" width="23" style="vertical-align:middle" height="12" alt="" /> is some function defined in the rectangle <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_326.gif" width="88" style="vertical-align:middle" height="12" alt="" /> that has already been defined and the symbol <tt>V</tt> is given the numeric values</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_51.gif" width="277" height="12" alt="" /></p>
<p>then the right side of equation (<a href="#approximation">13</a>) differs from the left side by no more than <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_327.gif" width="22" style="vertical-align:middle" height="15" alt="" /> times the uniform distance between the function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_328.gif" width="23" style="vertical-align:middle" height="12" alt="" /> and the interpolating function created from the assignment</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_52.gif" width="102" height="12" alt="" /></p>
<p>First, we compute&#8212;once and for all&#8212;all integrals of the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_53.gif" width="156" height="33" alt="" /></p>
<p>for all choices of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_329.gif" width="49" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_330.gif" width="43" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_331.gif" width="43" style="vertical-align:middle" height="12" alt="" />, and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_332.gif" width="45" style="vertical-align:middle" height="12" alt="" />, and we represent these integrals as explicit functions of the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_333.gif" width="93" style="vertical-align:middle" height="12" alt="" />.</p>
<p>Instead of calculating the tensor <tt>integrals</tt>, one may load its (precomputed) value from the files <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_334.gif" width="59" style="vertical-align:middle" height="12" alt="" /> or integrals.txt (if available): </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_54.gif" width="205" height="12" alt="" /></p>
<p>or</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_55.gif" width="203" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_56.gif" width="508" height="23" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_16.gif" width="75" height="12" alt="" /></p>
<p>Next, we replace the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_335.gif" width="93" style="vertical-align:middle" height="12" alt="" /> with the actual numeric values of the associated abscissas on the grid: </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_57.gif" width="580" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_17.gif" width="75" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_58.gif" width="90" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_18.gif" width="65" height="12" alt="" /></p>
<p>The next step is to create the lists of the midpoints between all neighboring abscissas </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_59.gif" width="509" height="33" alt="" /></p>
<p>which would allow us to produce the Taylor expansion of the <tt>InterpolatingFunction</tt> object <tt>f</tt> at the points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_336.gif" width="126" style="vertical-align:middle" height="15" alt="" />, for any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_337.gif" width="67" style="vertical-align:middle" height="12" alt="" /> and any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_338.gif" width="61" style="vertical-align:middle" height="12" alt="" />. As noted earlier, for every choice of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_339.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_340.gif" width="3" style="vertical-align:middle" height="12" alt="" />, the associated expansion coincides with the object <tt>f</tt> everywhere in the rectangle</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_60.gif" width="196" height="15" alt="" /></p>
<p>Now we will compute the entire expression</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_61.gif" width="269" height="42" alt="" /></p>
<p>as a linear function of the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_341.gif" width="15" style="vertical-align:middle" height="15" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_342.gif" width="49" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_343.gif" width="43" style="vertical-align:middle" height="12" alt="" />. Note that for every fixed <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_344.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_345.gif" width="3" style="vertical-align:middle" height="12" alt="" />, the third summation simply gives the dot product between the vectors.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_62.gif" width="514" height="12" alt="" /></p>
<p>It is important to recognize that the sum of these dot products gives the <em>exact</em> value of the integral</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_63.gif" width="145" height="30" alt="" /></p>
<p>Unfortunately, the exact values of the integrals stored in the list <tt>WM</tt> are quite cumbersome:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_64.gif" width="80" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_19.gif" width="572" height="39" alt="" /></p>
<p>Since working with such expressions becomes prohibitively slow, we convert them to floating-point numbers (we can, of course, use any precision that we wish). </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_65.gif" width="154" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_20.gif" width="75" height="12" alt="" /></p>
<p>Note that this last operation is the <em>only</em> source of errors in the integration of the interpolating function <tt>f</tt>. </p>
<p>Finally, we compute the integral</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_66.gif" width="142" height="30" alt="" /></p>
<p>in symbolic form, that is, as an <em>explicit</em> function of the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_346.gif" width="15" style="vertical-align:middle" height="15" alt="" /> that define the interpolating function <tt>f</tt>&#8212;what we are going to compute are the actual numerical values of the coefficients for the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_347.gif" width="15" style="vertical-align:middle" height="15" alt="" /> in the linear combination that represents this integral. Consequently, an expression in symbolic form for the integral is nothing but a tensor of actual numeric values. We again stress that the only loss of precision comes from the conversion of the exact numeric values for the integrals stored in <tt>WM</tt> into the floating-point numbers stored in <tt>WMN</tt>.</p>
<p>Instead of calculating the object <tt>CoeffList</tt> in the following, which contains the actual linear combination of the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_348.gif" width="15" style="vertical-align:middle" height="15" alt="" />, one may load its (precomputed) value from the files CoeffList.mx or CoeffList.txt (if available): </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_67.gif" width="208" height="12" alt="" /></p>
<p>or</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_68.gif" width="206" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_69.gif" width="580" height="28" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_21.gif" width="75" height="12" alt="" /></p>
<p>Now we extract the numeric values for the coefficients <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_349.gif" width="15" style="vertical-align:middle" height="15" alt="" /> and store those values in the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_350.gif" width="9" style="vertical-align:middle" height="12" alt="" />:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_70.gif" width="314" height="19" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_22.gif" width="81" height="12" alt="" /></p>
<p>Note that in order to produce the actual cubature rule we need to compute the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_351.gif" width="9" style="vertical-align:middle" height="12" alt="" /> only once, so that several minutes of computing time is quite acceptable. Note also that <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_352.gif" width="9" style="vertical-align:middle" height="12" alt="" /> (i.e., the cubature rule) can be calculated with any precision. This means that if we approximate an integral of the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_71.gif" width="141" height="30" alt="" /></p>
<p>with the dot product</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_72.gif" width="387" height="12" alt="" /></p>
<p>then the upper estimate for the error in this approximation can be made arbitrarily close to the uniform distance between the function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_353.gif" width="32" style="vertical-align:middle" height="12" alt="" /> and the interpolating function created from the values that <tt>u</tt> takes on the grid multiplied by the area of the integration domain, which is <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_354.gif" width="22" style="vertical-align:middle" height="15" alt="" />.</p>
<p>Now we turn to some examples. First, consider the function</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_73.gif" width="102" height="17" alt="" /></p>
<p>and set</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_74.gif" width="206" height="12" alt="" /></p>
<p>Now we have</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_75.gif" width="110" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_23.gif" width="105" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_76.gif" width="304" height="39" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_24.gif" width="9" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_77.gif" width="75" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_25.gif" width="75" height="15" alt="" /></p>
<p>For the function</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_78.gif" width="358" height="23" alt="" /></p>
<p>we get</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_79.gif" width="458" height="39" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_26.gif" width="51" height="12" alt="" /></p>
<p>One must be aware that <tt>NIntegrate</tt> is a very efficient procedure and, generally, replacing <tt>NIntegrate</tt> with a &#8220;hand made&#8221; cubature rule of the type described in this section may be justified only when the integrand cannot be defined in terms of standard functions, or when a better control of the error is needed, provided that one can control the uniform distance between the integrand and the respective interpolating function created from the grid. In general, such &#8220;hand made&#8221; cubatures become useful mostly in situations where one has to compute a very large number of integrals for similarly defined integrands&#8212;assuming, of course, that the uniform error from the interpolation in all integrands can be controlled (this would be the case, for example, if one has a global bound on a sufficient number of derivatives).</p>
<p>Interpolation quadrature rules for single integrals can be obtained in much the same way. It must be noted that, in general, spline quadrature rules are considerably more straightforward than spline cubature rules. This is because smooth interpolation in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_355.gif" width="15" style="vertical-align:middle" height="15" alt="" /> can be obtained without any additional information about the derivatives at the grid points and this is not the case in spaces of dimension greater than or equal to<img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_356.gif" width="8" style="vertical-align:middle" height="12" alt="" />. For example, cubic splines in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_357.gif" width="15" style="vertical-align:middle" height="15" alt="" /> are uniquely determined by the values at the grid points and by the requirement that the second derivative vanishes at the first and last grid points (the so-called natural splines). In contrast, smooth interpolation in two or more dimensions depends on the choice of the gradient and at least one mixed derivative at every grid point, and when information about these quantities is not available&#8212;as is often the case&#8212;one usually resorts to divided difference interpolation. In general, the efficiency of the interpolation may be increased by using a nonuniform grid and by placing more grid points in the regions where the functions that are being interpolated are more &#8220;warped.&#8221; We will illustrate this approach in the last section. The use of nonsmooth interpolation, such as bilinear interpolation in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_358.gif" width="15" style="vertical-align:middle" height="15" alt="" />, for example, reduces considerably the computational complexity of the problem. In any case, the error from the interpolation must be controlled in one way or another. While the discussion of this issue is beyond the scope of this article, it should be noted that rather powerful estimates for the interpolation error are well known.</p>
<h3>Some Examples of Bilinear Cubature Rules for Gaussian Integrals in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_359.gif" width="19" style="vertical-align:middle" height="19" alt="" /></h3>
<p>In general, just as one would expect, cubature rules based on bilinear interpolation are less accurate than the cubature rules described in the previous section (assuming, of course, one uses the same grid and keeping in mind that this claim is made just in general). However, from a computational point of view, bilinear cubature rules are considerably simpler and easier to implement, especially for more general integrals of the form </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_80.gif" width="87" height="29" alt="" /></p>
<p>for some fixed functions <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_360.gif" width="46" style="vertical-align:middle" height="12" alt="" />Math and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_361.gif" width="46" style="vertical-align:middle" height="12" alt="" />. The greater computational simplicity actually allows one to use a denser grid, which, in many cases, is a reasonable compensation for the loss in smoothness. The dynamic integration procedures in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_362.gif" width="15" style="vertical-align:middle" height="15" alt="" /> that are discussed in the next section are all based on bilinear cubature rules (in the case of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_363.gif" width="15" style="vertical-align:middle" height="15" alt="" /> we will use cubic quadratures).</p>
<p>Here we will be working with the same interpolation grid <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_364.gif" width="28" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_365.gif" width="51" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_366.gif" width="28" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_367.gif" width="51" style="vertical-align:middle" height="12" alt="" />, that was defined in the previous section and, just as before, in all display formulas will write <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_368.gif" width="11" style="vertical-align:middle" height="12" alt="" /> instead of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_369.gif" width="28" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_370.gif" width="10" style="vertical-align:middle" height="12" alt="" /> instead of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_371.gif" width="28" style="vertical-align:middle" height="12" alt="" />. Instead of working with the interpolation objects of the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_81.gif" width="468" height="19" alt="" /></p>
<p>we will work with bilinear interpolation objects that we are going to &#8220;construct from scratch&#8221; as explicit functions of the variables <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_372.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_373.gif" width="6" style="vertical-align:middle" height="12" alt="" /> that depend on the respective grid points and tabulated values. This means that for</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_82.gif" width="200" height="12" alt="" /></p>
<p>the bilinear interpolation object can be expressed as</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_83.gif" width="428" height="18" alt="" /></p>
<p>where the function <tt>bi</tt> is defined as</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_84.gif" width="554" height="95" alt="" /></p>
<p>It is clear from this definition that, treated as a function of the variables <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_374.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_375.gif" width="6" style="vertical-align:middle" height="12" alt="" />, the expression <tt>bi</tt> can be written as</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_85.gif" width="103" height="12" alt="" /></p>
<p>where the entire list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_376.gif" width="61" style="vertical-align:middle" height="12" alt="" /> can be treated as a function of the grid points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_377.gif" width="93" style="vertical-align:middle" height="12" alt="" /> and the tabulated values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_378.gif" width="133" style="vertical-align:middle" height="19" alt="" />. In fact, this function (that is, a list of functions) can be written as</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_86.gif" width="580" height="12" alt="" /></p>
<p>For every fixed <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_379.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_380.gif" width="3" style="vertical-align:middle" height="12" alt="" />, the integral of the bilinear interpolating object taken over the rectangle</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_87.gif" width="197" height="12" alt="" /></p>
<p>is simply the dot product between the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_381.gif" width="61" style="vertical-align:middle" height="12" alt="" /> and the list of integrals (over the same rectangle) of the functions</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_88.gif" width="223" height="28" alt="" /></p>
<p>Now we will calculate&#8212;and store&#8212;all these lists (of integrals) of dimension 4 for all choices for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_382.gif" width="49" style="vertical-align:middle" height="12" alt="" /> and for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_383.gif" width="43" style="vertical-align:middle" height="12" alt="" />:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_89.gif" width="580" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_27.gif" width="252" height="37" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_90.gif" width="580" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_28.gif" width="240" height="56" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_91.gif" width="580" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_29.gif" width="238" height="56" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_92.gif" width="470" height="28" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_30.gif" width="270" height="47" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_93.gif" width="572" height="43" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_31.gif" width="81" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_94.gif" width="166" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_32.gif" width="75" height="12" alt="" /></p>
<p>and then compute the sum of all dot products:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_95.gif" width="580" height="58" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_33.gif" width="81" height="12" alt="" /></p>
<p>This sum is a linear function of the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_384.gif" width="15" style="vertical-align:middle" height="15" alt="" />, and this linear function represents the integral of the interpolating function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_385.gif" width="22" style="vertical-align:middle" height="12" alt="" /> over the entire interpolation region (each dot product gives the integral of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_386.gif" width="22" style="vertical-align:middle" height="12" alt="" /> in the respective sub-region). Finally, we must extract the coefficients for the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_387.gif" width="16" style="vertical-align:middle" height="15" alt="" /> from the linear combination (of those symbols) that we just obtained.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_96.gif" width="230" height="25" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_34.gif" width="81" height="12" alt="" /></p>
<p>Now we can use the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_388.gif" width="8" style="vertical-align:middle" height="12" alt="" /> in the same way in which we used the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_389.gif" width="9" style="vertical-align:middle" height="12" alt="" /> in the previous section: although <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_390.gif" width="8" style="vertical-align:middle" height="12" alt="" /> contains concrete numeric values, this tensor still has the meaning of &#8220;integral of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_391.gif" width="22" style="vertical-align:middle" height="12" alt="" /> in symbolic form.&#8221; For example, if we set</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_97.gif" width="97" height="17" alt="" /></p>
<p>then we obtain</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_98.gif" width="364" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_35.gif" width="93" height="12" alt="" /></p>
<p>which is reasonably close to</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_99.gif" width="356" height="39" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_36.gif" width="99" height="12" alt="" /></p>
<p>In this case, the cubic cubature rule is considerably more accurate:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_100.gif" width="366" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_37.gif" width="99" height="12" alt="" /></p>
<p>Here is another example.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_101.gif" width="222" height="12" alt="" /></p>
<p>This function is nonsmooth, as the following 3D plot shows.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_102.gif" width="460" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_38.gif" width="360" height="286" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_103.gif" width="364" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_39.gif" width="177" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_104.gif" width="364" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_40.gif" width="165" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_105.gif" width="356" height="39" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_41.gif" width="105" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_106.gif" width="220" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_42.gif" width="123" height="12" alt="" /></p>
<p>Finally, we remark that&#8212;at least in principle&#8212;the procedures developed in this and in the previous sections may be used with any other, that is, nonGaussian, probability density <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_392.gif" width="22" style="vertical-align:middle" height="12" alt="" />, including nonsmooth densities. </p>
<h3>Application to Optimal Stopping Problems and Option Pricing</h3>
<p>The main objective in this section is to revisit the method of dynamic interpolation and integration described in [<a href="#Ref:lyas04">1</a>] and to present a considerably more efficient implementation of this method, which takes advantage of the fact that the objects that are being integrated sequentially are actually interpolating functions. More importantly, we will show how to implement this method in the case of optimal stopping problems for certain diffusions in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_393.gif" width="15" style="vertical-align:middle" height="15" alt="" />. Essentially, this section is about the actual implementation in <em>Mathematica</em> of the method encoded in the <em>space-discretized version</em> of the <em>time-discrete dynamic programming equation</em> (<a href="#compInt">7</a>). The solution is approximated in terms of dynamic cubature rules of the form (<a href="#quadratureRule">8</a>) by using the procedure encoded in equation (<a href="#eq2">11</a>). For the sake of simplicity, the examples presented in this section will be confined to the case where the (diffusion type) observation process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_394.gif" width="39" style="vertical-align:middle" height="12" alt="" /> is computable, in the sense that for any fixed <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_395.gif" width="19" style="vertical-align:middle" height="12" alt="" /> <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_396.gif" width="24" style="vertical-align:middle" height="12" alt="" />, the random variable <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_397.gif" width="15" style="vertical-align:middle" height="12" alt="" /> can be expressed as </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_107.gif" width="122" height="12" alt="" /></p>
<p>for some computable function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_398.gif" width="105" style="vertical-align:middle" height="15" alt="" /> (note that the position of the diffusion process at time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_399.gif" width="4" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_400.gif" width="12" style="vertical-align:middle" height="12" alt="" />, is independent of the future increment of the Brownian motion <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_401.gif" width="43" style="vertical-align:middle" height="12" alt="" />). In other words, the conditional law of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_402.gif" width="15" style="vertical-align:middle" height="12" alt="" /> given the event <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_403.gif" width="43" style="vertical-align:middle" height="12" alt="" /> can be identified with the distribution law of a random variable of the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_108.gif" width="95" height="20" alt="" /></p>
<p>where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_404.gif" width="7" style="vertical-align:middle" height="12" alt="" /> is a standard normal <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_405.gif" width="17" style="vertical-align:middle" height="15" alt="" />-valued random variable. Of course, when <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_406.gif" width="39" style="vertical-align:middle" height="12" alt="" /> is computable in the sense that we just described, then there is no need to use the approximation (<a href="#ladiff">6</a>), and, if <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_407.gif" width="39" style="vertical-align:middle" height="12" alt="" /> is not computable, in principle at least, the procedures described in this section can still be used, provided that the definition of the function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_408.gif" width="49" style="vertical-align:middle" height="12" alt="" /> is changed according to the recipe (<a href="#ladiff">6</a>). </p>
<p>We note that the <em>Mathematica</em> code included in this section is independent of the code included in all previous sections.</p>
<p>To begin with, define the following symmetric probability density function in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_409.gif" width="15" style="vertical-align:middle" height="15" alt="" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_109.gif" width="138" height="39" alt="" /></p>
<p>This is nothing but the probability density of a bivariate normal random variable <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_410.gif" width="60" style="vertical-align:middle" height="15" alt="" />, with standard normal marginals <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_411.gif" width="8" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_412.gif" width="8" style="vertical-align:middle" height="12" alt="" /> and with correlation <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_413.gif" width="54" style="vertical-align:middle" height="12" alt="" />. Let <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_414.gif" width="14" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_415.gif" width="24" style="vertical-align:middle" height="12" alt="" />, and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_416.gif" width="11" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_417.gif" width="24" style="vertical-align:middle" height="12" alt="" />, be two standard Brownian motions that are independent, so that <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_418.gif" width="63" style="vertical-align:middle" height="12" alt="" /> for any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_419.gif" width="24" style="vertical-align:middle" height="12" alt="" />. Next, for some fixed <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_420.gif" width="28" style="vertical-align:middle" height="12" alt="" /> and for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_421.gif" width="56" style="vertical-align:middle" height="15" alt="" />, set <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_422.gif" width="59" style="vertical-align:middle" height="15" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_423.gif" width="58" style="vertical-align:middle" height="15" alt="" /> for any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_424.gif" width="24" style="vertical-align:middle" height="12" alt="" />. Note that the joint distribution law of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_425.gif" width="37" style="vertical-align:middle" height="18" alt="" /> is the same as the joint distribution law of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_426.gif" width="105" style="vertical-align:middle" height="24" alt="" />. It is a trivial matter to check that</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_110.gif" width="119" height="18" alt="" /></p>
<p>is a diffusion process in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_427.gif" width="15" style="vertical-align:middle" height="15" alt="" /> with generator</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_111.gif" width="126" height="35" alt="" /></p>
<p>In what follows, we study the problem for optimal stopping of the process <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_428.gif" width="56" style="vertical-align:middle" height="19" alt="" /> no later than time <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_429.gif" width="28" style="vertical-align:middle" height="12" alt="" />, for a given termination payoff <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_430.gif" width="57" style="vertical-align:middle" height="17" alt="" />. For the sake of simplicity, we will suppose that the discount rate for future payoffs is <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_431.gif" width="6" style="vertical-align:middle" height="12" alt="" />. In the special case where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_432.gif" width="70" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_433.gif" width="26" style="vertical-align:middle" height="12" alt="" />, for some &#8220;sufficiently nice&#8221; function of one variable <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_434.gif" width="54" style="vertical-align:middle" height="12" alt="" />, the problem comes down to the optimal stopping of the one-dimensional diffusion <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_435.gif" width="38" style="vertical-align:middle" height="12" alt="" />. We analyze this one-dimensional case first.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_112.gif" width="116" height="43" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_113.gif" width="41" height="12" alt="" /></p>
<p>The time step in the procedure, that is, the quantity <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_436.gif" width="10" style="vertical-align:middle" height="23" alt="" />, is set to</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_114.gif" width="37" height="12" alt="" /></p>
<p>Next, define the interpolation grid:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_115.gif" width="529" height="64" alt="" /></p>
<p>Just as before, in all display equations we will write <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_437.gif" width="11" style="vertical-align:middle" height="12" alt="" /> instead of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_438.gif" width="28" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_439.gif" width="12" style="vertical-align:middle" height="12" alt="" /> instead of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_440.gif" width="35" style="vertical-align:middle" height="12" alt="" />. The boundary conditions will be prescribed at the first and last two abscissas on the extended grid . This means that the recursive procedure will update only the values on the grid . Thus, the procedure that we develop can be applied only to situations where the value function does not change at those points in the state-space that are either close to <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_441.gif" width="6" style="vertical-align:middle" height="12" alt="" /> or happen to be very large.</p>
<p>The next step is to define the list of tabulated symbols</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_116.gif" width="155" height="12" alt="" /></p>
<p>and the associated (symbolic) <tt>InterpolatingFunction</tt> object</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_117.gif" width="328" height="12" alt="" /></p>
<p>Next, extract the coefficients of the cubic polynomials that represent the object <tt>g</tt> between the interpolation nodes.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_118.gif" width="277" height="35" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_119.gif" width="373" height="12" alt="" /></p>
<p>For example,</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_120.gif" width="105" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_43.gif" width="472" height="28" alt="" /></p>
<p>which means that for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_442.gif" width="60" style="vertical-align:middle" height="12" alt="" />, the expression <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_443.gif" width="28" style="vertical-align:middle" height="12" alt="" /> can be identified with the polynomial</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_121.gif" width="134" height="23" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_44.gif" width="580" height="34" alt="" /></p>
<p>Next, note that the relation</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_122.gif" width="113" height="22" alt="" /></p>
<p>is equivalent to</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_123.gif" width="315" height="38" alt="" /></p>
<p>For every fixed <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_444.gif" width="63" style="vertical-align:middle" height="12" alt="" />, we will compute all integrals</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_124.gif" width="318" height="32" alt="" /></p>
<p>and will store these integrals in the matrix (initially filled with zeros):</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_125.gif" width="232" height="12" alt="" /></p>
<p>In particular, the integral</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_126.gif" width="150" height="32" alt="" /></p>
<p>can be replaced by</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_127.gif" width="249" height="19" alt="" /></p>
<p>and we remark that</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_128.gif" width="281" height="41" alt="" /></p>
<p>The entire expression</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_129.gif" width="440" height="41" alt="" /></p>
<p>then becomes a linear function of the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_445.gif" width="72" style="vertical-align:middle" height="12" alt="" />. This linear function (i.e., vector of dimension <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_446.gif" width="27" style="vertical-align:middle" height="12" alt="" />) depends on the index <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_447.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and represents a quadrature rule for the integral in the left side in the last identity. We stress that we need one such rule for every abscissa <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_448.gif" width="11" style="vertical-align:middle" height="15" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_449.gif" width="49" style="vertical-align:middle" height="12" alt="" />. We will store all these rules (i.e., vectors of dimension <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_450.gif" width="27" style="vertical-align:middle" height="12" alt="" />) in the tensor (initially filled with zeroes):</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_130.gif" width="227" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_131.gif" width="473" height="26" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_132.gif" width="580" height="110" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_45.gif" width="81" height="12" alt="" /></p>
<p>As a first application, we compute the price of an American put option with payoff: </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_133.gif" width="121" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_46.gif" width="72" height="12" alt="" /></p>
<p>The first iteration will be done by using a direct numerical integration&#8212;not interpolation quadrature rules.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_134.gif" width="580" height="135" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_47.gif" width="75" height="12" alt="" /></p>
<p>Now we will do <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_451.gif" width="12" style="vertical-align:middle" height="12" alt="" /> additional iterations by using interpolation quadratures. The time step is <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_452.gif" width="39" style="vertical-align:middle" height="12" alt="" /> years. This will give us the approximate price of an American put with strike price <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_453.gif" width="12" style="vertical-align:middle" height="12" alt="" /> and one year left to expiry. Note that the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_454.gif" width="28" style="vertical-align:middle" height="12" alt="" /> gives the integral associated with the abscissa <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_455.gif" width="11" style="vertical-align:middle" height="12" alt="" />, which is the same as <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_456.gif" width="23" style="vertical-align:middle" height="12" alt="" /> for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_457.gif" width="49" style="vertical-align:middle" height="12" alt="" />. The idea is to keep the value at the abscissa <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_458.gif" width="13" style="vertical-align:middle" height="12" alt="" /> fixed, throughout the iterations, equal to the initial value <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_459.gif" width="12" style="vertical-align:middle" height="12" alt="" /> and, similarly, keep the values at the abscissas <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_460.gif" width="25" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_461.gif" width="25" style="vertical-align:middle" height="12" alt="" /> forever fixed at the initial values <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_462.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_463.gif" width="6" style="vertical-align:middle" height="12" alt="" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_135.gif" width="89" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_136.gif" width="487" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_48.gif" width="75" height="12" alt="" /></p>
<p>Note that the first iteration took about 20 times longer to compute than the remaining 19. Now we define the value function after <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_464.gif" width="12" style="vertical-align:middle" height="12" alt="" /> iterations&#8212;this is nothing but the option price with one year left to maturity treated as a function defined on the entire range of observable stock prices.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_137.gif" width="308" height="12" alt="" /></p>
<p>We plot the value function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_465.gif" width="22" style="vertical-align:middle" height="12" alt="" /> together with the termination payoff <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_466.gif" width="24" style="vertical-align:middle" height="12" alt="" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_138.gif" width="499" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_49.gif" width="360" height="226" alt="" /></p>
<p>The range of abscissas at which the value function and the termination payoff coincide is precisely the price range where immediate exercise of the option is optimal, assuming that there is exactly one year left to expiry.</p>
<p>Now we consider the same optimal stopping problem but with a smooth payoff function, namely</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_139.gif" width="201" height="33" alt="" /></p>
<p>Essentially, this is an example of some sort of a generalized obstacle problem.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_140.gif" width="196" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_50.gif" width="360" height="226" alt="" /></p>
<p>In this case, there is no need to perform the first iteration by using direct numerical integration.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_141.gif" width="247" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_51.gif" width="81" height="12" alt="" /></p>
<p>Now we perform <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_467.gif" width="12" style="vertical-align:middle" height="12" alt="" /> iterations, which will give us the value function with three years left to the termination deadline.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_142.gif" width="89" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_143.gif" width="500" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_52.gif" width="81" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_144.gif" width="308" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_145.gif" width="405" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_53.gif" width="360" height="226" alt="" /></p>
<p>The most common approach to dynamic programing problems of this type is to use the free boundary problem formulation, followed by some suitable variation of the finite difference scheme. However, such an approach is much less straightforward and usually takes much longer to compute&#8212;a fraction of a second would be very hard to imagine even on a very powerful computer. In fact, we could double the number of the interpolation nodes and cut the time step in half, which would roughly quadruple the timing, and still complete the calculation within one second. </p>
<p>It is important to point out that, in terms of the previous procedure, there is nothing special about the standard normal density. In fact, we could have used just about any other reasonably behaved probability density function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_468.gif" width="26" style="vertical-align:middle" height="12" alt="" />&#8212;as long as the associated integrals are computable. In particular, one can use this procedure in the context of financial models in which the pricing process is driven by some nonwhite noise process, including a noise process with jumps. The only requirement is that we must be able to compute the integrals</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_146.gif" width="318" height="32" alt="" /></p>
<p>for any <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_469.gif" width="63" style="vertical-align:middle" height="12" alt="" />.</p>
<p>Finally, we consider the two-dimensional case. We use the same grid-points on both axes:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_147.gif" width="175" height="12" alt="" /></p>
<p>Although the lists <tt>Y</tt> and <tt>YY</tt> are the same as  and , in the notation that we use we will pretend that these lists might be different and will write <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_470.gif" width="11" style="vertical-align:middle" height="12" alt="" /> instead of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_471.gif" width="28" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_472.gif" width="11" style="vertical-align:middle" height="14" alt="" /> instead of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_473.gif" width="35" style="vertical-align:middle" height="12" alt="" />, and set</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_148.gif" width="129" height="38" alt="" /></p>
<p>We will use bilinear cubature rules, which are easier to implement&#8212;we need one such rule for every point <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_474.gif" width="37" style="vertical-align:middle" height="19" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_475.gif" width="49" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_476.gif" width="46" style="vertical-align:middle" height="12" alt="" />. Thus, we must compute all integrals</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_14.gif" width="318" height="32" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="genericIntegral">(14)</a></td>
</tr>
</table>
<p>for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_477.gif" width="48" style="vertical-align:middle" height="12" alt="" />, for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_478.gif" width="67" style="vertical-align:middle" height="12" alt="" />, for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_479.gif" width="61" style="vertical-align:middle" height="12" alt="" />, for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_480.gif" width="49" style="vertical-align:middle" height="12" alt="" />, and for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_481.gif" width="46" style="vertical-align:middle" height="12" alt="" /> (because of the symmetry in the density <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_482.gif" width="22" style="vertical-align:middle" height="12" alt="" />, it is enough to consider only the case <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_483.gif" width="26" style="vertical-align:middle" height="12" alt="" />). Now we need to operate with substantially larger lists of data and for that reason we need to organize the <em>Mathematica</em> code differently.</p>
<p>Assuming that <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_484.gif" width="80" style="vertical-align:middle" height="12" alt="" /> is an interpolating function constructed by way of bilinear interpolation from the tabulated symbols</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_149.gif" width="263" height="19" alt="" /></p>
<p>we can express each of the integrals</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_150.gif" width="284" height="32" alt="" /></p>
<p>in the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_151.gif" width="103" height="19" alt="" /></p>
<p>where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_485.gif" width="30" style="vertical-align:middle" height="15" alt="" /> is the <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_486.gif" width="24" style="vertical-align:middle" height="12" alt="" /> matrix given by (here we use the definition of <tt>cfbi</tt> from the previous section)</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_152.gif" width="482" height="19" alt="" /></p>
<p>and <tt>locIntegrals</tt> is the <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_487.gif" width="24" style="vertical-align:middle" height="12" alt="" /> matrix of the integrals (<a href="#genericIntegral">14</a>) indexed by <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_488.gif" width="55" style="vertical-align:middle" height="12" alt="" /> (<img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_489.gif" width="3" style="vertical-align:middle" height="12" alt="" /> is the first index and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_490.gif" width="5" style="vertical-align:middle" height="12" alt="" /> is the second). In fact, with our special choice for the bi-variate density <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_491.gif" width="34" style="vertical-align:middle" height="12" alt="" />, all these integrals can be calculated in closed form:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_153.gif" width="156" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_154.gif" width="405" height="26" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_54.gif" width="252" height="37" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_155.gif" width="405" height="26" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_55.gif" width="343" height="40" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_156.gif" width="405" height="26" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_56.gif" width="343" height="40" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_157.gif" width="405" height="26" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_57.gif" width="402" height="39" alt="" /></p>
<p>After fixing the values for the volatility <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_492.gif" width="8" style="vertical-align:middle" height="12" alt="" /> and the time step <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_493.gif" width="4" style="vertical-align:middle" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_158.gif" width="141" height="12" alt="" /></p>
<p>the integrals can be written as explicit functions of the integration limits:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_159.gif" width="580" height="38" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_160.gif" width="427" height="71" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_161.gif" width="427" height="71" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_162.gif" width="493" height="71" alt="" /></p>
<p>Our method depends in a crucial way on the fact that <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_494.gif" width="126" style="vertical-align:middle" height="15" alt="" /> is actually a linear function of the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_495.gif" width="137" style="vertical-align:middle" height="19" alt="" />. In fact, we can compute the coefficients in this linear combination explicitly, as we now demonstrate.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_163.gif" width="308" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_164.gif" width="204" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_58.gif" width="62" height="29" alt="" /></p>
<p>The next two definitions are taken from the previous section, so that the code included in this section can be self-contained.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_165.gif" width="554" height="95" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_166.gif" width="580" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_167.gif" width="442" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_59.gif" width="232" height="35" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_168.gif" width="442" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_60.gif" width="241" height="35" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_169.gif" width="441" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_61.gif" width="241" height="35" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_170.gif" width="443" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_62.gif" width="232" height="35" alt="" /></p>
<p>Thus, the entire integral</p>
<table class="DisplayFormulaNumbered">
<tr>
<td><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormulaNumbered_15.gif" width="352" height="32" alt="" /></td>
<td class="DisplayFormulaNumberedLabel"><a name="globalIntegral">(15)</a></td>
</tr>
</table>
<p>can be treated as a linear function of the symbols</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_DisplayFormula_171.gif" width="170" height="19" alt="" /></p>
<p>This linear function is nothing but a tensor of dimension <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_496.gif" width="75" style="vertical-align:middle" height="12" alt="" /> and this tensor is nothing but a cubature rule for the integral (<a href="#globalIntegral">15</a>), which, obviously, depends on the indices <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_497.gif" width="6" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_498.gif" width="6" style="vertical-align:middle" height="12" alt="" />. Consequently, we have one such cubature rule (tensor) for every choice of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_499.gif" width="63" style="vertical-align:middle" height="12" alt="" /> and every choice of <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_500.gif" width="63" style="vertical-align:middle" height="12" alt="" /> and all such rules will be stored in the tensor (initially filled with zeros):</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_172.gif" width="300" height="12" alt="" /></p>
<p>The calculation of the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_501.gif" width="7" style="vertical-align:middle" height="12" alt="" /> is the key step in the implementation of the method of dynamic interpolation and integration in dimension 2. This is the place where we must face the &#8220;curse of the dimension.&#8221; It is no longer efficient to express the global integral (<a href="#globalIntegral">15</a>) as the sum of local integrals of the form (<a href="#genericIntegral">14</a>) and then extract from the sum the coefficients for the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_502.gif" width="16" style="vertical-align:middle" height="15" alt="" />. It turns out to be much faster if one updates the coefficients for the symbols <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_503.gif" width="16" style="vertical-align:middle" height="15" alt="" /> sequentially, as the local integrals in (<a href="#genericIntegral">14</a>) are calculated one by one. Just as one would expect, dealing with the boundary conditions in higher dimensions is also trickier. Throughout the dynamic integration procedure we will be updating the values of the value function at the grid points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_504.gif" width="37" style="vertical-align:middle" height="19" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_505.gif" width="49" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_506.gif" width="46" style="vertical-align:middle" height="12" alt="" />, or, which amounts to the same, the grid points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_507.gif" width="37" style="vertical-align:middle" height="18" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_508.gif" width="67" style="vertical-align:middle" height="12" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_509.gif" width="64" style="vertical-align:middle" height="12" alt="" />. The values on the remaining grid points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_510.gif" width="37" style="vertical-align:middle" height="18" alt="" /> with <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_511.gif" width="26" style="vertical-align:middle" height="12" alt="" />, or <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_512.gif" width="47" style="vertical-align:middle" height="12" alt="" />, or <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_513.gif" width="47" style="vertical-align:middle" height="12" alt="" />, or <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_514.gif" width="23" style="vertical-align:middle" height="12" alt="" />, or <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_515.gif" width="41" style="vertical-align:middle" height="12" alt="" />, or <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_516.gif" width="41" style="vertical-align:middle" height="12" alt="" />, will be kept unchanged, or, depending on the nature of the problem, will be updated according to some other&#8212;one-dimensional, perhaps&#8212;dynamic quadrature rule.</p>
<p>Now we turn to the actual computation of the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_517.gif" width="7" style="vertical-align:middle" height="12" alt="" />. On a single &#8220;generic&#8221; processor, this task takes about <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_518.gif" width="9" style="vertical-align:middle" height="23" alt="" /> hour (if more processors are available the task can be distributed between several different <em>Mathematica</em> kernels in a trivial way). <em>The key point is that this is a calculation that we do once and for all.</em> As will be illustrated shortly, once the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_519.gif" width="7" style="vertical-align:middle" height="12" alt="" /> is calculated, a whole slew of optimal stopping problems can be solved within seconds. Of course, the &#8220;slew of optimal stopping problems&#8221; is limited to the ones where the termination payoff and the value functions obtained throughout the dynamic integration can be approximated reasonably well by way of bilinear interpolation from the same interpolation grid. In general, the set of these &#8220;quickly solvable&#8221; problems can be increased by choosing a denser grid and/or a grid that covers a larger area. However, doing so may become quite expensive. For example, if we were to double the number of grid points in each coordinate, the computing time for the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_520.gif" width="7" style="vertical-align:middle" height="12" alt="" /> would increase roughly 16 times&#8212;again, everything is easily parallelizable.</p>
<p>It is important to recognize that the calculation of the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_521.gif" width="7" style="vertical-align:middle" height="12" alt="" /> involves only the evaluation of standard functions and accessing the elements of a fairly large tensor (<img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_522.gif" width="7" style="vertical-align:middle" height="12" alt="" /> has a total of 1,245,621 entries). This is close to the &#8220;minimal number of calculations&#8221; that one may expect to get away with: if the number of discrete data from which the value function can be restored with a reasonable degree of accuracy is fairly large, then there is a fairly large number of values that will have to be calculated no matter what. Furthermore, the evaluation of standard functions is very close to the fastest numerical procedure that one may hope to get away with in this situation. From an algorithmic point of view, the efficiency of this procedure can be improved in two ways: (1) find a new way to encrypt the value function with fewer discrete values; and/or (2) represent all functions involved in the procedure (i.e., all combinations of standard functions) in a form that allows for an even faster evaluation. In terms of hardware, the speed of the procedure depends not only on the speed of the processor but also on the speed and the organization of the memory. </p>
<p>Instead of calculating the list <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_523.gif" width="7" style="vertical-align:middle" height="12" alt="" />, one may load its (precomputed) value from the files Xi.mx or Xi.txt (if available): </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_173.gif" width="172" height="12" alt="" /></p>
<p>or</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_174.gif" width="170" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_175.gif" width="580" height="381" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_63.gif" width="75" height="12" alt="" /></p>
<p>Now we can turn to some concrete applications. First, we will consider an American put option with strike price <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_524.gif" width="12" style="vertical-align:middle" height="12" alt="" /> on the choice of one of two uncorrelated assets (the owner of the option can choose one of the two assets when the option is exercised). The termination payoff from this option is </p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_176.gif" width="184" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_177.gif" width="460" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_64.gif" width="360" height="287" alt="" /></p>
<p>The two underlying assets are uncorrelated and follow the processes <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_525.gif" width="40" style="vertical-align:middle" height="15" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_526.gif" width="24" style="vertical-align:middle" height="12" alt="" />, and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_527.gif" width="39" style="vertical-align:middle" height="15" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_528.gif" width="24" style="vertical-align:middle" height="12" alt="" />, where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_529.gif" width="7" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_530.gif" width="11" style="vertical-align:middle" height="12" alt="" /> are the Brownian motions described earlier in this section. Let <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_531.gif" width="82" style="vertical-align:middle" height="12" alt="" /> be the value function with <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_532.gif" width="4" style="vertical-align:middle" height="12" alt="" /> years left to expiry, that is, if the time left to expiry is <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_533.gif" width="4" style="vertical-align:middle" height="12" alt="" /> years and the prices of the two assets are, respectively, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_534.gif" width="36" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_535.gif" width="36" style="vertical-align:middle" height="12" alt="" />, then the value of the option is <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_536.gif" width="66" style="vertical-align:middle" height="12" alt="" /> and we remark that what is meant here as &#8220;the value of the option&#8221; is actually a function defined on the entire range of prices, that is, the range of prices covered by the interpolation grid. Clearly, we must have <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_537.gif" width="112" style="vertical-align:middle" height="12" alt="" />. Furthermore, when one of the assets has a very large value, then the option is very close to a canonical American put option on the other asset. Consequently, the values at the grid points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_538.gif" width="37" style="vertical-align:middle" height="18" alt="" />, where <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_539.gif" width="26" style="vertical-align:middle" height="12" alt="" /> or <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_540.gif" width="23" style="vertical-align:middle" height="12" alt="" />, will never be updated and will remain forever fixed at the initial value <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_541.gif" width="12" style="vertical-align:middle" height="12" alt="" />. When <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_542.gif" width="6" style="vertical-align:middle" height="12" alt="" /> is fixed to either <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_543.gif" width="47" style="vertical-align:middle" height="12" alt="" /> or <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_544.gif" width="47" style="vertical-align:middle" height="12" alt="" />, the values at the grid points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_545.gif" width="37" style="vertical-align:middle" height="18" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_546.gif" width="61" style="vertical-align:middle" height="12" alt="" />, will be updated exactly as we did earlier in the case of an American put on a single asset with a strike price <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_547.gif" width="12" style="vertical-align:middle" height="12" alt="" />. Similarly, when <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_548.gif" width="3" style="vertical-align:middle" height="12" alt="" /> is fixed to either <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_549.gif" width="41" style="vertical-align:middle" height="12" alt="" /> or <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_550.gif" width="44" style="vertical-align:middle" height="12" alt="" />, the values at the grid points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_551.gif" width="37" style="vertical-align:middle" height="18" alt="" />, <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_552.gif" width="67" style="vertical-align:middle" height="12" alt="" />, will be updated in the same way, that is, as if we were dealing with an American put on a single asset with strike price 40. Of course, since the payoff is symmetric and so is the density <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_553.gif" width="33" style="vertical-align:middle" height="12" alt="" />, we only need to update the values at the grid points <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_554.gif" width="37" style="vertical-align:middle" height="18" alt="" /> for <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_555.gif" width="81" style="vertical-align:middle" height="12" alt="" /> and <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_556.gif" width="75" style="vertical-align:middle" height="12" alt="" />.</p>
<p>Now we turn to the actual calculation. By using the method of dynamic integration of interpolating functions, we will compute&#8212;approximately&#8212;the pricing function <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_557.gif" width="84" style="vertical-align:middle" height="12" alt="" /> that maps the prices of the underlying assets in the range covered by the grid into the price of the option with one year left to maturity. With time step <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_558.gif" width="39" style="vertical-align:middle" height="12" alt="" />, we need to perform 20 iterations in the dynamic integration procedure. Note that if we choose to perform the first iteration by direct numerical integration of the termination payoff, then that would require the calculation of 741 integrals.</p>
<p>We initialize the procedure by tabulating the termination payoff over the interpolation grid.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_178.gif" width="347" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_65.gif" width="81" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_179.gif" width="89" height="12" alt="" /></p>
<p>Then we do 20 iterations&#8212;at each iteration we update the tensor <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_559.gif" width="24" style="vertical-align:middle" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_180.gif" width="580" height="73" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_66.gif" width="69" height="12" alt="" /></p>
<p>Now we can produce the pricing map with one year left to maturity.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_181.gif" width="462" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_182.gif" width="470" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_67.gif" width="360" height="287" alt="" /></p>
<p>Calculating the derivatives of the value function (treated as functions, too) is straightforward as we now illustrate, and before we do we remark that in the realm of finance, information about these derivatives (known as the deltas of the option) is often more important than the pricing function itself.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_183.gif" width="479" height="23" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_68.gif" width="360" height="290" alt="" /></p>
<p>In our second example, we consider an American put option on the <em>more expensive</em> of two given underlying assets (in contrast, in our previous example we dealt with an American put option on the less expensive of the two underlying assets). In this case, the termination payoff function is given by</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_184.gif" width="186" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_185.gif" width="460" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_69.gif" width="360" height="286" alt="" /></p>
<p>This time the boundary conditions must take into account the fact that when one of the assets becomes too expensive, the option becomes worthless, and when one of the assets is worthless, the option may be treated as a standard American option on the other asset. For the purpose of illustration we will compute the value of the option with three years left to maturity.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_186.gif" width="347" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_70.gif" width="81" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_187.gif" width="89" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_188.gif" width="580" height="58" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_71.gif" width="75" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_189.gif" width="460" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_190.gif" width="470" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_72.gif" width="360" height="287" alt="" /></p>
<p>This is a good example of a value function which is neither everywhere smooth nor convex.</p>
<p>In our final example we consider optimal stopping of the same diffusion in <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_560.gif" width="15" style="vertical-align:middle" height="15" alt="" /> but with smooth termination payoff of the form</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_191.gif" width="335" height="33" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_192.gif" width="460" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_73.gif" width="360" height="287" alt="" /></p>
<p>With this termination payoff the boundary conditions must reflect the fact that the option is worthless when one of the assets is worthless or when one of the assets is too expensive. We will execute <img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Math_561.gif" width="12" style="vertical-align:middle" height="12" alt="" /> iterations, which will give us the value function with three years left to expiry.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_193.gif" width="347" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_74.gif" width="81" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_194.gif" width="89" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_195.gif" width="580" height="58" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_75.gif" width="63" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_196.gif" width="458" height="12" alt="" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Input_197.gif" width="470" height="12" alt="" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/InterpolationQuadratures_Output_76.gif" width="360" height="287" alt="" /></p>
<h3>Conclusions</h3>
<p>Most man-made computing devices can operate only with finite lists of numbers or symbols. Any use of such devices for modeling, analysis, and optimal control of stochastic systems inevitably involves the encoding of inherently complex phenomena in terms of finite lists of numbers or symbols. Thus one can think of two general directions that may lead to expanding the realm of computable models. First, one may try to construct computing devices that can handle larger and larger lists, faster and faster. Second, one may try to develop &#8220;smarter&#8221; procedures with which more complex objects can be encrypted with shorter lists of numbers. With this general direction in mind, the methodology developed in this article is entirely logical and natural. Indeed, the approximation of functions with splines or other types of interpolating functions is a familiar, well developed, and entirely natural computing tool. The use of special quadrature/cubature rules&#8212;as opposed to general methods for numerical integration&#8212;in the context of dynamic integration of interpolating functions is just as logical and natural. However, only recently have computer languages in which one can implement such procedures in a relatively simple and straightforward fashion become widely available. This article is an attempt to demonstrate how the advent of more sophisticated computing technologies may lead to the development of new and more efficient algorithms. Curiously, new and more efficient algorithms often lead to the design of new and more efficient computing devices. In particular, all procedures described in this article can be implemented on parallel processors or on computing grids in essentially a trivial way. There is a strong incentive to build computing devices that can perform simultaneously several numerical integrations, or can compute dot products between very large lists of numbers very fast.</p>
<p>Finally, we must point out that most of the examples presented in the article are only prototypes. They were meant to be executed on a generic (and slightly out of date) laptop computer with the smallest possible number of complications. Many further improvements in terms of higher accuracy and overall efficiency can certainly be made. </p>
<h3>Acknowledgments</h3>
<p>The author thanks all three anonymous referees for their extremely helpful comments and suggestions.</p>
<h3>References</h3>
<table class="ReferenceTable">
<tr>
<td class="Reference"><a name="{lyas04, Ref:lyas04}">[1] </a></td>
<td>A. Lyasoff, &#8220;Path Integral Methods for Parabolic Partial Differential Equations with Examples from Computational Finance,&#8221; <em>The Mathematica Journal</em>, <strong>9</strong>(2), 2004 pp. 399-422.</td>
</tr>
<tr>
<td class="Reference"><a name="Ref:BenLio82">[2] </a></td>
<td>A. Bensoussan and J. L. Lions, <em>Applications of the Variational Inequalities in Stochastic Control</em>, Amsterdam: North Holland, 1982.</td>
</tr>
<tr>
<td class="Reference"><a name="Ref:Dav93">[3] </a></td>
<td>M. H. A. Davis, &#8220;Markov Models and Optimization,&#8221; Monographs on Statistics and Applied Probability, Vol. 49, Chapman &amp; Hall/CRC Press,1993.</td>
</tr>
<tr>
<td class="Reference"><a name="{bp03, Ref:bp03}">[4] </a></td>
<td>V. Bally and G. Pag&#232;s, &#8220;A Quantization Algorithm for Solving Discrete Time Multidimensional Optimal Stopping Problems,&#8221; <em>Bernoulli</em>, <strong>9</strong>(6), 2003 pp. 1003-1049. </td>
</tr>
<tr>
<td class="Reference"><a name="{bp03a, Ref:bp03a}">[5] </a></td>
<td>V. Bally and G. Pag&#232;s, &#8220;Error Analysis of the Quantization Algorithm for Obstacle Problems,&#8221; <em>Stochastic Processes and their Applications</em>, <strong>106</strong>(1), 2003 pp. 1-40.</td>
</tr>
<tr>
<td class="Reference"><a name="{bet04, Ref:bet04}">[6] </a></td>
<td>B. Bouchard, I. Ekeland, and N. Touzi, &#8220;On the Malliavin Approach to Monte Carlo Approximation of Conditional Expectations,&#8221; <em>Finance and Stochastics</em>, <strong>8</strong>(1), 2004 pp. 45-71.</td>
</tr>
<tr>
<td class="Reference"><a name="{bj97, Ref:bj97}">[7] </a></td>
<td>M. Broadie and P. Glasserman, &#8220;Pricing American-Style Securities Using Simulation,&#8221; <em>Journal of Economic Dynamics and Control</em>, <strong>21</strong>(8-9),1997 pp.1323-1352.</td>
</tr>
<tr>
<td class="Reference"><a name="Ref:LS01">[8] </a></td>
<td>F. A. Longstaff and R. S. Schwartz, &#8220;Valuing American Options By Simulation: A Simple Least-Square Approach,&#8221; <em>Review of Financial Studies</em>, <strong>14</strong>(1), 2001 pp. 113-147.</td>
</tr>
<tr>
<td class="Reference"><a name="{nr, Ref:nr}">[9] </a></td>
<td>W. H. Press, B. Flannery, S. A. Teukolsky, and W. T. Vetterling, <em>Numerical Recipes in C: The Art of Scientific Computing</em>, 2nd ed., Cambridge: Cambridge University Press, 1992.</td>
</tr>
<tr>
<td class="Reference"><a name="{fmm77, Ref:fmm77}">[10] </a></td>
<td>G. E. Forsythe, M. A. Malcolm, and C. B. Moler, <em>Computer Methods for Mathematical Computations</em>, Englewood Cliffs, NJ: Prentice Hall, 1977.</td>
</tr>
<tr>
<td class="Reference"><a name="Ref:LyoVic04">[11] </a></td>
<td>T. Lyons and N. Victoir, &#8220;Cubature on Wiener Space,&#8221; in <em>Proceedings of the Royal Society of London</em>, Series A (<em>Mathematica</em>l and Physical Sciences), <strong>460</strong>(2041), 2004 pp. 169-198.</td>
</tr>
<tr>
<td class="Reference"><a name="Ref:Kus01">[12] </a></td>
<td>S. Kusuoka, &#8220;Approximation of Expectation of Diffusion Process and Mathematical Finance,&#8221; in <em>Advanced Studies in Pure Mathematics</em>, <em>Proceedings of the Taniguchi Conference on Mathematics</em> <em>(Nara 1998)</em>, (M. Maruyama and T. Sunada, eds.), <strong>31</strong>, 2001 pp. 147-165.</td>
</tr>
<tr>
<td class="Reference"><a name="Ref:Kus04">[13] </a></td>
<td>S. Kusuoka, &#8220;Approximation of Expectation of Diffusion Processes Based on Lie Algebra and Malliavin Calculus,&#8221; <em>Advances in Mathematical Economics</em>, <strong>6</strong>, 2004 pp. 69-83.</td>
</tr>
<tr>
<td class="Reference"><a name="Ref:Str71">[14] </a></td>
<td>A. H. Stroud, <em>Approximate Calculation of Multiple Integrals</em>, Englewood Cliffs, NJ: Prentice-Hall, 1971.</td>
</tr>
<tr>
<td class="DOIReference" colspan="2">A. Lyasoff, &#8220;Dynamic Integration of Interpolating Functions and Some Concrete Optimal Stopping Problems,&#8221; <em>The Mathematica Journal</em>, 2011. dx.doi.org/10.3888/tmj.10.4-3. </td>
</tr>
</table>
<h3>Additional Material</h3>
<p>Lyasoff.zip contains:<br/>integrals.mx<br/>integrals.txt<br/>CoeffList.mx<br/>CoeffList.txt<br/>Xi.mx<br/>Xi.txt</p>
<p class="SmallText" name="SmallText">Available at <a href="http://www.mathematica-journal.com/data/uploads/2011/12/Lyasoff.zip">www.mathematica-journal.com/data/uploads/2011/12/Lyasoff.zip</a>.</p>
<h3 class="SectionAboutAuthor">About the Author</h3>
<p class="TextAboutAuthor">Andrew Lyasoff is director of the Graduate Program in Mathematical Finance at Boston University. His research interests are mainly in the areas of Stochastic Analysis, Optimization Theory, and Mathematical Finance and Economics. </p>
<p class="TextAboutAuthor"><strong>Andrew Lyasoff</strong><br/><em>Boston University<br/>Mathematical Finance Program<br/>Boston, MA<br/></em><em><a href="mailto:ALyasoff@msn.com">alyasoff@bu.edu</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mathematica-journal.com/2012/02/dynamic-integration-of-interpolating-functions-and-some-concrete-optimal-stopping-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making Holes and Windows in Surfaces</title>
		<link>http://www.mathematica-journal.com/2012/02/making-holes-and-windows-in-surfaces/</link>
		<comments>http://www.mathematica-journal.com/2012/02/making-holes-and-windows-in-surfaces/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 00:08:50 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 10, Issue 4]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=17820</guid>
		<description><![CDATA[In this article, we demonstrate , a program which removes points from any  or  picture whose coordinates satisfy a stated condition. We also demonstrate  and , programs which make an entire or a specific portion of an opaque surface into a transparent mesh. We use these programs to view the region of [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, we demonstrate <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_1.gif" alt="" width="63" height="13" />, a program which removes points from any <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_2.gif" alt="" width="62" height="13" /> or <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_3.gif" alt="" width="78" height="13" /> picture whose coordinates satisfy a stated condition. We also demonstrate <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_4.gif" alt="" width="86" height="13" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_5.gif" alt="" width="78" height="13" />, programs which make an entire or a specific portion of an opaque surface into a transparent mesh. We use these programs to view the region of integration for a triple integral. This article uses <em>Mathematica</em> 5.2, but with minor modifications all three programs work in <em>Mathematica</em> 6, as well as earlier versions. The <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_6.gif" alt="" width="63" height="13" /> program duplicates some of the functionality of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_7.gif" alt="" width="94" height="13" /> command and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_8.gif" alt="" width="109" height="13" /> option of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_9.gif" alt="" width="125" height="13" /> command in Version 6, while the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_10.gif" alt="" width="86" height="13" /> program behaves like the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_11.gif" alt="" width="118" height="13" /> option of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_12.gif" alt="" width="47" height="13" /> command (see <a href="#Editor's Note">Editor&#8217;s Note</a> for a demonstration of the <em>Mathematica</em> 6 code).<span id="more-17820"></span></p>
<h3>Introduction</h3>
<p>Given a 2D or 3D object, it is useful to view its cutaway image along an intersecting curve or surface. For example, we could cut an object along planes where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_13.gif" alt="" width="6" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_14.gif" alt="" width="6" height="12" />, or <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_15.gif" alt="" width="5" height="12" /> are constant by adjusting the <tt>PlotRange</tt> parameters. For cutting along a general curve or outside a surface, we introduce <tt>makehole</tt>, a program that removes vertices in <tt>Polygon</tt>, <tt>Line</tt>, and <tt>Point</tt> graphics primitives whose coordinates satisfy a stated condition (e.g., an equation, inequality, or compound inequality). We also introduce <tt>transparent</tt> and <tt>makewindow</tt>, which make an opaque surface into a transparent mesh by replacing each <tt>Polygon</tt> primitive with line segments around the boundary of the polygon. Viewing a region of integration for a triple integral in rectangular coordinates is one application of these programs.</p>
<h3>Making Surfaces Transparent</h3>
<p>We first introduce <tt>transparent</tt>. To outline the boundary of a polygon, it repeats the first vertex at the end of each list of vertices and replaces all <tt>Polygon</tt> heads with <tt>Line</tt>. Unlike <tt>WireFrame</tt> in the <em>Shapes</em> package, <tt>transparent</tt> has adjustable <tt>PlotStyle</tt> options and works on graphics with either <tt>Graphics</tt> or <tt>Graphics3D</tt> heads.</p>
<div id="DefinitionBox" class="MathTable"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_MathTable1.gif" alt="" width="620" height="87" /></div>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_1.gif" alt="" width="580" height="88" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_2.gif" alt="" width="575" height="43" /></p>
<p>We use <tt>Plot3D</tt> to draw the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_16.gif" alt="" width="75" height="12" /> plane and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_17.gif" alt="" width="94" height="12" /> to convert the <tt>SurfaceGraphics</tt> head to <tt>Graphics3D</tt>. Then we apply <tt>transparent</tt> and show the result.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_3.gif" alt="" width="571" height="43" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/1_g.gif" alt="" width="288" height="234" /></div>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/2_g.gif" alt="" width="288" height="234" /></div>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_1.gif" alt="" width="92" height="12" /></p>
<h3>Making Holes</h3>
<p>Before introducing <tt>makehole</tt>, we demonstrate its main ideas by removing portions of the following <tt>Graphics3D</tt> object that lie on or above the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_18.gif" alt="" width="75" height="12" /> plane. The object consists of two polygons.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_4.gif" alt="" width="490" height="43" /></p>
<p>The condition <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_19.gif" alt="" width="75" height="12" /> is expressed as <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_20.gif" alt="" width="64" height="12" /> in the following. Also, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_21.gif" alt="" width="130" height="12" /> is used to represent the vertices with either two or three coordinates that satisfy condition <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_22.gif" alt="" width="93" height="12" /> removes vertices from the polygons that satisfy the condition, as well as polygons with no remaining vertices.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_5.gif" alt="" width="580" height="58" /></p>
<p>We view the output in <tt>InputForm</tt> and observe that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_23.gif" alt="" width="57" height="12" /> and all vertices of the second polygon were removed from the original object because they satisfied <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_24.gif" alt="" width="75" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_6.gif" alt="" width="122" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_2.gif" alt="" width="580" height="25" /></p>
<p>We note that <tt>Polygon</tt> primitives with fewer than three remaining vertices will not be displayed by <tt>Show</tt>.</p>
<p>To demonstrate that this strategy works for more complicated objects, we generate a torus (suppressing the plot using <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_25.gif" alt="" width="183" height="12" />), then use the same commands to remove the portions on and above <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_26.gif" alt="" width="75" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_7.gif" alt="" width="580" height="88" /></p>
<p>We show the results with the transparent <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_27.gif" alt="" width="75" height="12" /> plane.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_8.gif" alt="" width="406" height="12" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/3_g.gif" alt="" width="288" height="234" /></div>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_3.gif" alt="" width="92" height="12" /></p>
<p>The complete <tt>makehole</tt> program, which follows, removes vertices with two and three coordinates that satisfy a stated condition from within <tt>Polygon</tt>, <tt>Line</tt>, and <tt>Point</tt> primitives.</p>
<div id="DefinitionBox" class="MathTable"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_MathTable2.gif" alt="" width="613" height="101" /></div>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_9.gif" alt="" width="580" height="343" /></p>
<p>To demonstrate <tt>makehole</tt> on graphics with <tt>Graphics</tt> heads, we use it to remove regions inside three ellipses from the graph of a spiral and three points. In order to remove a union of regions, we use <tt>||</tt> to represent &#8220;or,&#8221; so that points that satisfy any of the three inequalities are removed.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_10.gif" alt="" width="567" height="73" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/4_g.gif" alt="" width="288" height="287" /></div>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_4.gif" alt="" width="78" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_11.gif" alt="" width="580" height="28" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/5_g.gif" alt="" width="288" height="287" /></div>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_5.gif" alt="" width="78" height="12" /></p>
<h3>Making Windows in Surfaces</h3>
<p>The <tt>makewindow</tt> program replaces <tt>Polygon</tt> with <tt>Line</tt> primitives if one of the vertices satisfies a stated condition.</p>
<div id="DefinitionBox" class="MathTable"><img style="vertical-align: middle;" title="makewindow[object_,{x_,y_,condition__},options___] object must have a Graphics head; makewindow[object_,{x_,y_,z_,condition__},options___] object must have a Graphics3D head" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Image_3.gif" alt="makewindow[object_,{x_,y_,condition__},options___] object must have a Graphics head; makewindow[object_,{x_,y_,z_,condition__},options___] object must have a Graphics3D head" width="476" height="31" /><br />
<img style="vertical-align: middle;" title="options are the same as PlotStyle options, including RGBColor, Thickness, and Dashing" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Image_4.gif" alt="options are the same as PlotStyle options, including RGBColor, Thickness, and Dashing" width="425" height="12" /></div>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_12.gif" alt="" width="580" height="253" /></p>
<p>To show how it works, we use the torus and the condition <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_28.gif" alt="" width="75" height="12" /> to create a mesh window on portions lying above the corresponding plane. We use <tt>PlotStyle</tt> options to control the color and thickness of the mesh and to introduce dashing.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_13.gif" alt="" width="567" height="73" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/6_g.gif" alt="" width="288" height="205" /></div>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_6.gif" alt="" width="92" height="12" /></p>
<h3>Showing the Region of Integration for a Triple Integral</h3>
<p>We use <tt>makehole</tt> and <tt>makewindow</tt> to draw a region corresponding to the triple integral, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_29.gif" alt="" width="143" height="34" />. To visualize the region, we draw many of the bounding surfaces and then impose the three conditions <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_30.gif" alt="" width="45" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_31.gif" alt="" width="70" height="15" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_32.gif" alt="" width="55" height="12" /> from the limits of integration.</p>
<p>We first draw bounding surfaces for the region of integration. The surfaces drawn with <tt>Plot3D</tt> are converted to a <tt>Graphics3D</tt> head using <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_33.gif" alt="" width="94" height="12" />. The blue bounding surface <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_34.gif" alt="" width="25" height="12" /> and the purple surface <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_35.gif" alt="" width="25" height="12" /> are suppressed.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_14.gif" alt="" width="580" height="103" /></p>
<p>To sketch the surface <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_36.gif" alt="" width="50" height="15" />, we view it as a parametrized surface between cross-sectional curves on planes <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_37.gif" alt="" width="34" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_38.gif" alt="" width="25" height="12" />. We color the surface red and suppress the plot.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_15.gif" alt="" width="538" height="88" /></p>
<p>The region of integration is enclosed by the surfaces. We omit the surfaces <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_39.gif" alt="" width="26" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_40.gif" alt="" width="35" height="12" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_41.gif" alt="" width="26" height="12" /> from the image because the present region does not cross these planes.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_16.gif" alt="" width="554" height="28" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/7_g.gif" alt="" width="277" height="288" /></div>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_7.gif" alt="" width="92" height="12" /></p>
<p>To isolate the region of integration, we use <tt>makehole</tt> to remove all polygons that lie outside the region.</p>
<p>The condition <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_42.gif" alt="" width="75" height="12" /> removes all points <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_43.gif" alt="" width="39" height="12" /> whose coordinates do not satisfy each of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_44.gif" alt="" width="45" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_45.gif" alt="" width="70" height="15" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_46.gif" alt="" width="55" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_17.gif" alt="" width="422" height="43" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/8_g.gif" alt="" width="271" height="288" /></div>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_8.gif" alt="" width="92" height="12" /></p>
<p>To display the region inside the original image, we use <tt>makewindow</tt> to make everything outside the region transparent.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_18.gif" alt="" width="380" height="43" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/9_g.gif" alt="" width="277" height="288" /></div>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Output_9.gif" alt="" width="92" height="12" /></p>
<h3>Conclusion</h3>
<p>We have demonstrated programs for making holes and windows in portions of two- and three-dimensional objects which satisfy one or more specified inequalities. Applications include slicing an opaque surface along an intersecting surface and viewing the region of integration for a triple integral.</p>
<h3><a name="Editor's Note">Editor&#8217;s Note</a></h3>
<p>This article describes techniques for working with legacy graphics in <em>Mathematica</em>. The code would need to be modified to work with <tt>GraphicsComplex</tt> and other extensions to the <em>Mathematica</em> graphics language in Version 6. <em>Mathematica</em> 6 also contains new features that overlap with the functionality described in this article.</p>
<p><img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Math_47.gif" alt="" width="136" height="12" /> to generate wireframe surfaces.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_19.gif" alt="" width="368" height="12" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/10_g.gif" alt="" width="298" height="243" /></div>
<p>Use <tt>Opacity</tt> to create partially transparent surfaces.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_20.gif" alt="" width="577" height="28" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/11_g.gif" alt="" width="300" height="215" /></div>
<p>Use <tt>RegionFunction</tt> to remove portions of a surface.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_21.gif" alt="" width="577" height="28" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/12_g.gif" alt="" width="300" height="215" /></div>
<p>Use <tt>RegionPlot3D</tt> to show a region defined by inequalities.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/makeholepaperlite_Input_22.gif" alt="" width="580" height="38" /></p>
<div class="Graphics"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/13_g.gif" alt="" width="301" height="340" /></div>
<h3>References</h3>
<table class="ReferenceTable">
<tbody>
<tr>
<td class="DOIReference" colspan="2">A. Horwitz, &#8220;Making Holes and Windows in Surfaces,&#8221; <em>The Mathematica Journal</em>, 2011. dx.doi.org/doi:10.3888/tmj.10.4-4.</td>
</tr>
</tbody>
</table>
<h3 class="SectionAboutAuthor">About the Author</h3>
<p class="TextAboutAuthor">Alan Horwitz received a B.S. from Ohio State University in 1980 and an M.A. in 1985 and a Ph.D in 1988 from SUNY at Stonybrook. He is currently a faculty member in the Mathematics Department at Marshall University.</p>
<p class="TextAboutAuthor"><strong>Alan Horwitz<br />
</strong><em>Dept. of Math, Marshall University<br />
Huntington, West Virginia 25755<br />
</em><em><a href="mailto:horwitz@marshall.edu">horwitz@marshall.edu</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mathematica-journal.com/2012/02/making-holes-and-windows-in-surfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Graphing on the Riemann Sphere</title>
		<link>http://www.mathematica-journal.com/2012/02/graphing-on-the-riemann-sphere/</link>
		<comments>http://www.mathematica-journal.com/2012/02/graphing-on-the-riemann-sphere/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 00:08:25 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 10, Issue 4]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=17822</guid>
		<description><![CDATA[We give a procedure to plot parametric curves on the sphere whose advantages over classical graphs in the Cartesian plane are obvious whenever the graph involves infinite domains or infinite branches.
Introduction
Graphing a curve in the Cartesian plane can be done only in a restricted &#8220;window&#8221; . If the function to be plotted has a large [...]]]></description>
			<content:encoded><![CDATA[<p>We give a procedure to plot parametric curves on the sphere whose advantages over classical graphs in the Cartesian plane are obvious whenever the graph involves infinite domains or infinite branches.<span id="more-17822"></span></p>
<h3>Introduction</h3>
<p>Graphing a curve in the Cartesian plane can be done only in a restricted &#8220;window&#8221; <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_1.gif" alt="" width="65" height="12" />. If the function to be plotted has a large domain or range, it is practically impossible to get a global view of the curve. This makes it difficult to understand the asymptotic behavior of complicated curves with various kinds of infinities. Furthermore, for most functions (e.g., polynomials of degree greater than four), graphing in a large window loses important details, while graphing in a small window loses the global features.</p>
<p>The remedy is to compactify the plane and represent graphs on the Riemann sphere. The usual method is to map the plane graph to the sphere using the inverse stereographic projection. We prefer a slightly modified version: we smoothly wrap the plane <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_2.gif" alt="" width="26" height="12" /> on the sphere <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_3.gif" alt="" width="79" height="15" /> using the inverse stereographic projection from the pole <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_4.gif" alt="" width="49" height="12" />. The origin <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_5.gif" alt="" width="27" height="12" /> maps to the blue point <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_6.gif" alt="" width="40" height="12" /> on the sphere, and the point at infinity maps to the red point <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_7.gif" alt="" width="73" height="12" />.</p>
<h3>Benefits of the Method</h3>
<h4>Asymptotic Behavior</h4>
<p>Although the point at infinity cannot be reached, the mapping gives points so close to <img style="vertical-align: middle;" src="http://reference.wolfram.com/chars/Omega.gif" alt="" width="8" height="19" /> that it is as if we had reached it. As an illustration, here are the graphs of a polar curve first in the plane (with asymptotes) and then on the sphere.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_1.gif" alt="" width="172" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_2.gif" alt="" width="579" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_1.gif" alt="" width="360" height="360" /></p>
<p>The default view point shows <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_8.gif" alt="" width="8" height="12" />, the image of the point at infinity.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_3.gif" alt="" width="466" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_2.gif" alt="" width="360" height="360" /></p>
<p>Here is an animation where the view point goes once around the equator. The blue point zero is the image of the origin in the plane.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_4.gif" alt="" width="336" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_3.gif" alt="" width="360" height="360" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_5.gif" alt="" width="580" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_4.gif" alt="" width="403" height="435" /></p>
<p>To see through the sphere we use the <tt>Opacity</tt> option.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_6.gif" alt="" width="436" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_5.gif" alt="" width="360" height="360" /></p>
<h4>Sensitivity and Faithfulness</h4>
<p>This modified inverse stereographic projection <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_9.gif" alt="" width="7" height="12" /> is very sensitive close to the origin: two points near the origin in the plane will appear far apart on the sphere. On the other hand, points near infinity will appear close together, thus showing the asymptotic behavior that is the qualitative property of the curve far away from the origin.</p>
<p>Faithfulness means that mapping the plane curve to the sphere does not alter the shape of the curve. In particular, at <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_10.gif" alt="" width="8" height="12" />, the slopes of the different branches are exactly what they should be. We illustrate this by plotting the function <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_11.gif" alt="" width="57" height="15" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_7.gif" alt="" width="150" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_8.gif" alt="" width="169" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_6.gif" alt="" width="360" height="223" /></p>
<p>Making the interval somewhat larger loses details close to the origin.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_9.gif" alt="" width="183" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_7.gif" alt="" width="360" height="233" /></p>
<p>Here is the same curve on the sphere, viewed to show zero and then <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_12.gif" alt="" width="8" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_10.gif" alt="" width="454" height="12" /></p>
<p>The derivatives at <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_13.gif" alt="" width="19" height="12" /> are <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_14.gif" alt="" width="10" height="12" />, and, indeed, we see that the curve is vertical near <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_15.gif" alt="" width="8" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_11.gif" alt="" width="462" height="12" /></p>
<h4>Unveiling the True Nature of a Curve</h4>
<p>Geometers know that plane geometry is incomplete. We have to look at a curve in the projective plane (i.e., the Riemann sphere) to get complete results.</p>
<p>For example, students in high school are told that hyperbolas have two branches.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_12.gif" alt="" width="136" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_13.gif" alt="" width="184" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_8.gif" alt="" width="360" height="223" /></p>
<p>In this animation, a hyperbola appears as a one-branched figure-eight curve on the sphere.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_14.gif" alt="" width="511" height="43" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_9.gif" alt="" width="403" height="435" /></p>
<h3>The Mapping</h3>
<p>We define the mapping <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_16.gif" alt="" width="57" height="15" /> using the following picture.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_15.gif" alt="" width="385" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_10.gif" alt="" width="124" height="32" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_16.gif" alt="" width="580" height="73" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_11.gif" alt="" width="360" height="394" /></p>
<p>The projection pole is <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_17.gif" alt="" width="71" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_18.gif" alt="" width="119" height="15" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_19.gif" alt="" width="60" height="12" /> is the point corresponding to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_20.gif" alt="" width="11" height="12" /> on <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_21.gif" alt="" width="12" height="15" />. Then <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_22.gif" alt="" width="54" height="18" /> and to find the mapping <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_23.gif" alt="" width="95" height="12" />, we solve for <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_24.gif" alt="" width="4" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_17.gif" alt="" width="465" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_12.gif" alt="" width="172" height="33" /></p>
<p>One solution gives the point <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_25.gif" alt="" width="8" height="12" /> and the other solution determines the mapping to the sphere.</p>
<h3>The Program: Parametric Curves on the Sphere</h3>
<p>The modified inverse stereographic function <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_26.gif" alt="" width="7" height="12" /> turns out to be fairly simple.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_18.gif" alt="" width="362" height="18" /></p>
<p>We define the sphere and the equator, two meridians, a blue zero, and a red <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_27.gif" alt="" width="8" height="12" /> for orientation.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_19.gif" alt="" width="580" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_20.gif" alt="" width="554" height="58" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_21.gif" alt="" width="554" height="58" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_22.gif" alt="" width="554" height="58" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_23.gif" alt="" width="513" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_24.gif" alt="" width="492" height="12" /></p>
<p>To avoid infinities, <tt>SphereParametricPlot</tt> is based on <tt>ScatterPlot3D</tt> rather than <tt>ParametricPlot3D</tt>. In analogy with <tt>Plot</tt> and <tt>PolarPlot</tt>, we define the functions <tt>SpherePlot</tt> and <tt>SpherePolarPlot</tt>, both in terms of <tt>SphereParametricPlot</tt>.</p>
<p>The first argument to <tt>SphereParametricPlot</tt> is a pair of functions. The first argument to <tt>SpherePlot</tt> or <tt>SpherePolarPlot</tt> is a single function. The second argument to all three functions is a range specification. A <tt>ViewPoint</tt> option may be given.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_25.gif" alt="" width="580" height="88" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_26.gif" alt="" width="458" height="48" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_27.gif" alt="" width="382" height="28" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_28.gif" alt="" width="382" height="28" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_29.gif" alt="" width="474" height="28" /></p>
<p>Here is one final example.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Input_30.gif" alt="" width="578" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Output_13.gif" alt="" width="360" height="360" /></p>
<h3>Additional Material</h3>
<p>ImplicitEquOnSphere.nb</p>
<p class="SmallText">Available at <a href="http://www.mathematica-journal.com/data/uploads/2011/12/ImplicitEquOnSphere.nb">www.mathematica-journal.com/data/uploads/2011/12/ImplicitEquOnSphere.nb</a>.</p>
<h3>References</h3>
<table class="ReferenceTable">
<tbody>
<tr>
<td class="DOIReference" colspan="2">D. Benayat, &#8220;Graphing on the Riemann Sphere,&#8221; <em>The Mathematica Journal</em>, 2011. dx.doi.org/doi:10.3888/tmj.10.4-5.</td>
</tr>
</tbody>
</table>
<h3 class="SectionAboutAuthor">About the Author</h3>
<p class="TextAboutAuthor">Djilali Benayat received a B.Sc. in mathematics (1968) and a Diploma of Advanced Studies (DEA) in functional analysis (1969) from Algiers University, a DEA in algebraic topology (1970) and a &#8220;Doctorat de 3e cycle&#8221; in <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/GraphingOnSphere_Math_28.gif" alt="" width="9" height="12" />-theory (1972) from Strasbourg University, and a D.Sc. in algebraic topology (Theory of Adams Algebras in Stable Homotopy) (1987) from Metz and Algiers universities. Presently he is a professor of mathematics at the École Normale Supérieure of Algiers, Algeria. He has supervised a master&#8217;s thesis and three D.Sc.&#8217;s in algebraic topology and number theory.</p>
<p class="TextAboutAuthor"><strong>Djilali Benayat</strong><br />
<em>École Normale Supérieure<br />
Vieux Kouba<br />
Algiers, Algeria<br />
</em><em><a href="mailto:dbenayat@yahoo.fr">dbenayat@yahoo.fr</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mathematica-journal.com/2012/02/graphing-on-the-riemann-sphere/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Airfoil Aerodynamics Using Panel Methods</title>
		<link>http://www.mathematica-journal.com/2012/02/airfoil-aerodynamics-using-panel-methods/</link>
		<comments>http://www.mathematica-journal.com/2012/02/airfoil-aerodynamics-using-panel-methods/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 00:08:10 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 10, Issue 4]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=17824</guid>
		<description><![CDATA[Potential flow over an airfoil plays an important historical role in the theory of flight. The governing equation for potential flow is Laplace&#8217;s equation, a widely studied linear partial differential equation. One of Green&#8217;s identities can be used to write a solution to Laplace&#8217;s equation as a boundary integral. Numerical models based on this approach [...]]]></description>
			<content:encoded><![CDATA[<p>Potential flow over an airfoil plays an important historical role in the theory of flight. The governing equation for potential flow is Laplace&#8217;s equation, a widely studied linear partial differential equation. One of Green&#8217;s identities can be used to write a solution to Laplace&#8217;s equation as a boundary integral. Numerical models based on this approach are known as panel methods in the aerodynamics community. This article introduces the availability of a collection of computational tools for constructing numerical models for potential flow over an airfoil based on panel methods. Use of the software is illustrated by implementing a specific model using vortex panels of linearly varying strength to compute the flow over a member of the NACA four-digit family of airfoils.<span id="more-17824"></span></p>
<h3>Introduction</h3>
<p>Fluid dynamics is a branch of mechanics concerned with the motion of a fluid continuum under the action of applied forces. The motion and general behavior of a fluid is governed by the fundamental laws of classical mechanics and thermodynamics and plays an important role in such diverse fields as biology, meteorology, chemical engineering, and aerospace engineering. An introductory text on fluid mechanics, such as [<a href="#Sabersky">1</a>], surveys the basic concepts of fluid dynamics and the various mathematical models used to describe fluid flow under different restrictive assumptions. Advances in computational power and in modeling algorithms during the past few decades have enabled industry to use increasingly realistic models to solve problems of practical geometric complexity. Alternatively, these advances make it feasible to adapt some of the older, simpler models to inexpensive desktop computers.</p>
<p>Aerodynamics is a branch of fluid dynamics concerned primarily with the design of vehicles moving through air. In the not-so-distant past, a collection of relatively simple numerical models, known as panel methods, was the primary computational tool for estimating some of the aerodynamic characteristics of airplanes and their components for cruise conditions. For example, Hess [<a href="#Hess">2</a>] commented in 1990 that at Douglas Aircraft Company, a major design calculation was performed using panel methods approximately 10 times per day.</p>
<p>Panel methods are numerical models based on simplifying assumptions about the physics and properties of the flow of air over an aircraft. The viscosity of air in the flow field is neglected, and the net effect of viscosity on a wing is summarized by requiring that the flow leaves the sharp trailing edge of the wing smoothly. The compressibility of air is neglected, and the curl of the velocity field is assumed to be zero (no vorticity in the flow field). Under these assumptions, the vector velocity describing the flow field can be represented as the gradient of a scalar velocity potential, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_1.gif" alt="" width="42" height="18" />, and the resulting flow is referred to as potential flow. A statement of conservation of mass in the flow field leads to Laplace&#8217;s equation as the governing equation for the velocity potential, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_2.gif" alt="" width="45" height="15" />. Laplace&#8217;s equation is a widely studied linear partial differential equation and is discussed in detail in classical books on applied mathematics such as [<a href="#Morse">3</a>]. It also plays an important role in the theoretical development of several fields, including electrostatics and elastic membranes as well as fluid dynamics.</p>
<p>To solve the problem of potential flow over a solid object, Laplace&#8217;s equation must be solved subject to the boundary condition that there be no flow across the surface of the object. This is usually referred to as the tangent-flow boundary condition. Additionally, the flow far from the object is required to be uniform. The results of solving Laplace&#8217;s equation subject to tangent-flow boundary conditions provide an approximation of cruise conditions for an airplane.</p>
<p>Using a vector identity, the solution to this linear partial differential equation can be written in terms of an integral over the surface of the object. This boundary integral contains expressions for surface distributions of basic singular solutions to Laplace&#8217;s equation. A linear combination of relatively simple singular solutions is also a solution to the differential equation. This superposition of simple solutions provides the complexity needed for satisfying boundary conditions for flow over objects of complex geometry. Panel methods are based on this approach and are described in detail in [<a href="#Katz">4</a>]. Commonly used singular solutions for panel methods are referred to as source, vortex, and doublet distributions. Analogies can be made to other fields of study. The velocity field induced by a point source is analogous to the electrostatic field induced by a point charge. A doublet would be positive and negative charges of equal strength in close proximity. The velocity induced by a line vortex is analogous to the magnetic field induced by a current-carrying wire.</p>
<p>The basic solution procedure for panel methods consists of discretizing the surface of the object with flat panels and selecting singularities to be distributed over the panels in a specified manner, but with unknown singularity-strength parameters. Since each singularity is a solution to Laplace&#8217;s equation, a linear combination of the singular solutions is also a solution. The tangent-flow boundary condition is required to be satisfied at a discrete number of points called collocation points. This process leads to a system of linear algebraic equations to be solved for the unknown singularity-strength parameters. Details of the procedure vary depending on the singularities used and other details of problem formulation, but the end result is always a system of linear algebraic equations to be solved for the unknown singularity-strength parameters.</p>
<p>Panel methods are applicable to two- and three-dimensional flows. For flow over a two-dimensional object, the flat panels become straight lines, but can be thought of as infinitely long rectangular panels in the three-dimensional interpretation. For two-dimensional potential flow, the powerful technique of conformal mapping can also be used as a solution procedure. Conformal mapping provides exact solutions for certain airfoil shapes and is useful for validating numerical models.</p>
<p>This article introduces a collection of three packages providing computational tools for the formulation and solution of steady potential flow over an airfoil. In addition to the packages and associated online help for functions defined, examples of model implementation and use are included. Each package is discussed briefly. This is followed by an example of step-by-step implementation of a particular model for a small discretization number with intermediate results displayed. Finally, the steps are assembled into a module representing a particular model, and the lift and pressure distribution on an airfoil are computed.  The current version of the software collection is available form the Wolfram Library Archive as <em>Aerodynamics</em> 1.2.</p>
<h3>Load Required Packages</h3>
<p>Set your working directory and then load the application package needed for this notebook.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_1.gif" alt="" width="172" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_2.gif" alt="" width="165" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_3.gif" alt="" width="208" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_4.gif" alt="" width="115" height="12" /></p>
<p>Test that packages are loaded.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_5.gif" alt="" width="124" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Print_6.gif" alt="" width="580" height="45" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_7.gif" alt="" width="117" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Print_8.gif" alt="" width="580" height="45" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_9.gif" alt="" width="160" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Print_10.gif" alt="" width="580" height="59" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_11.gif" alt="" width="67" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Print_12.gif" alt="" width="580" height="47" /></p>
<h3>Nomenclature and Basic Equations for Airfoil Aerodynamics</h3>
<p>Much of the nomenclature associated with the theory of lift on an airfoil has made its way into everyday vocabulary, but some terms may be unfamiliar or have more specific meanings than occur in common usage. Also, there are some basic equations used in the example problem that should be mentioned. An introductory book on aerodynamics such as [<a href="#Anderson">5</a>] or [<a href="#Bertin">6</a>] presents the basic nomenclature and concepts associated with the theory of flight.</p>
<p>The term <em>airfoil</em> is used to denote the cross section, or profile, of a three-dimensional wing (see inset in Figure 2). The <em>chord line</em> of an airfoil is the straight line from the leading edge of the airfoil to the sharp trailing edge; the length of this line is referred to as the chord of the airfoil and is denoted by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_3.gif" alt="" width="5" height="12" />. The <em>camber line</em> of an airfoil is the locus of points midway between the upper and lower surfaces of the airfoil, measured perpendicular to the camber line. When describing an airfoil in dimensionless variables and in a local coordinate system, the chord of the airfoil is the segment of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_4.gif" alt="" width="6" height="12" /> axis from 0 to 1. The <em>angle of attack</em> is the angle between the chord line of the airfoil and the uniform onset velocity, and is denoted by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_5.gif" alt="" width="7" height="12" />.</p>
<p>In incompressible potential flow, the pressure is related to the fluid speed by Bernoulli&#8217;s equation, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_6.gif" alt="" width="134" height="23" />, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_7.gif" alt="" width="7" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_8.gif" alt="" width="11" height="12" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_9.gif" alt="" width="7" height="12" /> are the density, speed, and pressure at a point in the flow field, and the subscript <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_10.gif" alt="" width="10" height="12" /> refers to conditions far from the airfoil. The dimensionless measure of pressure is the pressure coefficient, defined by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_11.gif" alt="" width="64" height="30" />. Combining Bernoulli&#8217;s equation and the definition for the pressure coefficient yields a simple equation for the pressure coefficient in terms of the local speed of the fluid, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_12.gif" alt="" width="65" height="31" />.</p>
<p>Using the aerodynamic sign convention, the <em>circulation</em> of the velocity field <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_13.gif" alt="" width="9" height="16" /> around a closed contour <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_14.gif" alt="" width="8" height="12" /> is defined by the line integral <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_15.gif" alt="" width="72" height="20" />. The lift force per unit length on an airfoil can be related to the circulation around the airfoil by the Kutta-Joukowski lift theorem <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_16.gif" alt="" width="55" height="12" />. The aerodynamic sign convention used in the definition of circulation is chosen so that positive circulation leads to positive lift. The dimensionless measure for lift on an airfoil is the two-dimensional lift coefficient, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_17.gif" alt="" width="65" height="31" />. If a dimensionless circulation is defined by <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_18.gif" alt="" width="46" height="26" />, then the lift coefficient is simply twice the dimensionless circulation. The <em>Kutta condition</em> summarizes the primary viscous effect of the flow on the airfoil and establishes the circulation around the airfoil by the simple statement that the flow leaves the sharp trailing edge of the airfoil smoothly.</p>
<h3>Input Parameters</h3>
<p>The example airfoil for illustrating the implementation of a panel method in this article is a member of the NACA four-digit family of airfoils. Specify the identification number for the airfoil and the angle of attack in degrees.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_13.gif" alt="" width="173" height="12" /></p>
<p>The first of the four digits in the identification number gives the maximum camber in percent chord, the second digit gives the location of maximum camber in tenths of chord, and the last two digits give the thickness in percent chord.</p>
<p>The discretization process is determined by a discretization number and one of three layout options (<tt>ConstantSpacing</tt>, <tt>CosineSpacing</tt>, or <tt>HalfCosineSpacing</tt>) providing two alternatives to constant spacing of discretization points. Specify small (to illustrate a step-by-step implementation of the example) and large (for computing results) discretization numbers, and a layout option.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_14.gif" alt="" width="303" height="12" /></p>
<p>Finally, specify a small number used to ensure that collocation points are computed to be outside of the discretization panels representing the airfoil.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_15.gif" alt="" width="62" height="15" /></p>
<p>If you have installed the software discussed in this article, you can change the input parameters and rerun the notebook for additional results.</p>
<h3>Packages</h3>
<h4>Data Type for Handling Collections of Vectors</h4>
<p>The package <em>CartesianVectors</em> defines a data type to simplify the manipulation of large collections of <em>n</em>-dimensional vectors while maintaining packed arrays for efficient computation using machine numbers. The data type <tt>CartesianVectors</tt> is represented in the format <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_19.gif" alt="" width="74" height="12" />, where <tt>vx</tt>, <tt>vy</tt>, and <tt>vz</tt> are simple or nested lists of the components of the collection of vectors. The data type is designed to enable the manipulation of collections of vectors with notation commonly used for a single vector or for lists. Using a data type also simplifies pattern matching for valid input arguments for exported functions developed in other packages. The properties of the data type are defined by overloading existing <em>Mathematica</em> functions whenever possible. The package contains some exported functions including several functions specific to two-dimensional vectors.</p>
<p>As an example of using the data type, specify two collections of two-dimensional vectors, and compute the collection of displacement vectors from each vector in one group to every vector in the other group. This is a computation common to many <em>n</em>-body problems. The constructor for the data type is <tt>MakeCartesianVectors</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_16.gif" alt="" width="377" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_1.gif" alt="" width="291" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_17.gif" alt="" width="376" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_2.gif" alt="" width="199" height="12" /></p>
<p>Compute the displacement vectors from each point in <tt>rB</tt> to all points in <tt>rA</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_18.gif" alt="" width="283" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_3.gif" alt="" width="397" height="46" /></p>
<p>Count the number of vectors in the collection of displacement vectors.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_19.gif" alt="" width="144" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_4.gif" alt="" width="7" height="12" /></p>
<h4>Airfoil Geometry</h4>
<p>The package <em>AirfoilGeometry</em> provides functions to compute the geometry and discretization of airfoils in support of the construction of numerical models for potential flow over an airfoil. A list of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_20.gif" alt="" width="6" height="12" /> values used for discretization can be specified directly by the user or generated by the function <tt>NDiscretizeUnitSegment</tt>, which accepts a discretization number, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_21.gif" alt="" width="6" height="12" />, as its input argument and divides the unit segment into <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_22.gif" alt="" width="6" height="12" /> pieces. A layout option for this function allows constant spacing (default), cosine spacing, or half-cosine spacing. Cosine spacing provides finer discretization near the leading and trailing edges of the airfoil compared to constant spacing, and half-cosine spacing provides even finer discretization near the leading edge, but coarser discretization near the trailing edge compared to constant spacing. The function <tt>NACA4DigitAirfoil</tt> computes a list of thickness and camber properties at the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_23.gif" alt="" width="6" height="12" /> values, and the function <tt>AirfoilSurfacePoints</tt> computes the collection of vectors locating points on the surface of the airfoil from the list of thickness and camber properties. These points on the surface of the airfoil serve as panel end points for the discretized airfoil. Note that the result is expressed as the data type <tt>CartesianVectors</tt> as indicated by the arrow separating the lists of components.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_20.gif" alt="" width="580" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_5.gif" alt="" width="530" height="28" /></p>
<p>Compute a list of panel lengths. Note the use of <em>Mathematica</em> functions that have been overloaded for use with the data type <tt>CartesianVectors</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_21.gif" alt="" width="251" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_6.gif" alt="" width="417" height="12" /></p>
<p>Count the number of panels describing the discretized airfoil.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_22.gif" alt="" width="208" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_7.gif" alt="" width="7" height="12" /></p>
<p>The functions <tt>PanelPoints</tt> and <tt>PanelNormals</tt> are used to locate collocation points at midpanel and outward facing unit normals to the panels. Note that collocation points are displaced a small distance, proportional to the panel length, in the direction of the outward unit normal to ensure that these points are outside the discretized airfoil. This is done in preparation for applying the tangent-flow boundary condition at collocation points.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_23.gif" alt="" width="223" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_8.gif" alt="" width="462" height="28" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_24.gif" alt="" width="537" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_9.gif" alt="" width="484" height="28" /></p>
<p>Figure 1 shows the geometry of the discretized airfoil and the numbering convention for panels. The panels are straight-line segments joining points on the airfoil contour, and panel normals are shown at panel midpoints with the panel number near the head of the arrow. For an airfoil with thickness, the number of panels describing the airfoil is twice the discretization number. This numbering of panels is referred to as the clockwise convention. For a reference airfoil with no thickness (camber line), the number of panels is equal to the discretization number, and the convention is to number panels from leading edge to trailing edge. The airfoil shape is plotted in a local coordinate system with the origin at the leading edge of the airfoil and the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_24.gif" alt="" width="6" height="12" /> axis coincident with the chord line. Lengths are nondimensionalized using the chord of the airfoil, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_25.gif" alt="" width="5" height="12" />.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_25.gif" alt="" width="578" height="73" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_10.gif" alt="" width="504" height="180" /></p>
<p class="NumberedFigureCaption"><strong>Figure 1. </strong>Panels and panel normals for NACA 4412 airfoil discretized to six panels.</p>
<p>The online help for this package also includes examples of importing data files for individual airfoils. These are defined by specifying points on the airfoil contour and rearranging the imported data for use with this software. The UIUC Airfoil Data Site, maintained by Michael Selig of the University of Illinois at Urbana-Champaign, contains specifications for over 1500 airfoils [<a href="#Selig">7</a>].</p>
<h4>Two-Dimensional Influence Coefficients</h4>
<p>When computing the velocity field induced at a point due to a singularity located elsewhere, the velocity can be written as the product of a geometric term (called an influence coefficient) and a measure of the strength of the singularity. For example, consider the velocity induced at an arbitrary field point <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_26.gif" alt="" width="11" height="14" /> due to a point source located at the origin of the coordinate system.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_26.gif" alt="" width="68" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_11.gif" alt="" width="39" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_27.gif" alt="" width="68" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_12.gif" alt="" width="39" height="12" /></p>
<p>Compute the velocity, velocity-potential, and stream-function influence coefficients at the field point due to the singularity using the package functions <tt>ICSourcePoint</tt>, <tt>ICPhiSourcePoint</tt><span class="Output">,</span> and <tt>ICPsiSourcePoint</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_28.gif" alt="" width="580" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_13.gif" alt="" width="382" height="42" /></p>
<p>The velocity, velocity-potential, or stream-function at <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_27.gif" alt="" width="11" height="14" /> would be obtained by multiplying the appropriate influence coefficient by the strength of the source. Influence coefficients can also be thought of as the velocity, velocity-potential, or stream-function induced by a singularity of unit strength.</p>
<p>The package <em>InfluenceCoefficients</em> contains over thirty functions for velocity, velocity-potential, and stream-function influence coefficients for source, vortex, and doublet singularities commonly used in two-dimensional panel methods. They serve as a tool box for constructing numerical models for two-dimensional potential flow.</p>
<h3>Potential-Flow Model Using Vortex Panels of Linearly Varying Strength</h3>
<h4>Step-by-Step Model Formulation Using Coarse Discretization</h4>
<p>The singularity element chosen for this model is the vortex panel of linearly varying strength, which provides a circulation density along the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_28.gif" alt="" width="11" height="15" /> panel of the form, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_29.gif" alt="" width="92" height="12" /> in local coordinates, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_30.gif" alt="" width="10" height="12" /> is the distance from the &#8220;beginning&#8221; of the panel. Each singularity panel involves two unknown constants, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_31.gif" alt="" width="17" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_32.gif" alt="" width="9" height="12" />.</p>
<p>The boundary condition that the velocity be everywhere tangent to the airfoil contour is discretized to require that the velocity component normal to each panel at the collocation point be zero. Since each vortex panel introduces two unknown strength parameters, application of the tangent-flow boundary condition provides <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_33.gif" alt="" width="30" height="15" /> equations and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_34.gif" alt="" width="38" height="15" /> unknowns, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_35.gif" alt="" width="30" height="15" /> is the number of panels describing the geometry of the discretized airfoil. Continuity of circulation density from one panel to the next and the Kutta condition provide <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_36.gif" alt="" width="30" height="15" /> additional equations to complete a system of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_37.gif" alt="" width="38" height="15" /> linear algebraic equations and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_38.gif" alt="" width="38" height="15" /> unknowns. The system of equations can be put into standard form. The terms involving unknowns are collected on the left-hand side of the system of equations and the known quantities are collected on the right-hand side. The result can be written in block-matrix form as</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_DisplayFormula_29.gif" alt="" width="137" height="31" /></p>
<p>The symbols <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_39.gif" alt="" width="12" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_40.gif" alt="" width="5" height="12" /> represent lists of the unknown constant and linear strength parameters for the vortex panels: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_41.gif" alt="" width="16" height="12" /> represents the projection of the panel influence coefficients associated with <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_42.gif" alt="" width="12" height="12" /> on the unit normal vectors, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_43.gif" alt="" width="16" height="12" /> represents the projection of the panel influence coefficients associated with <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_44.gif" alt="" width="5" height="12" /> on the unit normal vectors, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_45.gif" alt="" width="16" height="12" />Math and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_46.gif" alt="" width="16" height="12" /> represent terms in the equations imposing continuity of circulation density between panels and the Kutta condition, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_47.gif" alt="" width="15" height="12" /> is the projection of the free-stream velocity on unit normals at collocation points.</p>
<p>Use the block-matrix form to write the system of equations as <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_48.gif" alt="" width="103" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_49.gif" alt="" width="85" height="12" />. Solve the latter system for the list of slope strengths, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_50.gif" alt="" width="78" height="17" />. Substitute this into the former system of equations to eliminate the slope-strength parameters. The resulting system of equations can be written as <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_51.gif" alt="" width="144" height="18" />. This system of equations can be solved for the list of strength parameters <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_52.gif" alt="" width="12" height="12" />, and then the transformation is used to compute the list of slope parameters <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_53.gif" alt="" width="5" height="12" />. All variables in the following formulation and solution are dimensionless.</p>
<p>Compute the matrix of velocity influence coefficients and project them on the panel normals.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_30.gif" alt="" width="488" height="28" /></p>
<p>Write the equations expressing the continuity of circulation density between panels and the Kutta condition. The equation expressing the Kutta condition is written to accommodate the different numbering conventions for airfoils with thickness and reference airfoils without thickness.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_31.gif" alt="" width="534" height="58" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_32.gif" alt="" width="338" height="12" /></p>
<p>Form the coefficient matrix for the system of linear algebraic equations to be solved for unknown strength parameters.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_33.gif" alt="" width="292" height="12" /></p>
<p>Display the matrix in reduced precision to illustrate the coefficient matrix for the full system of equations.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_34.gif" alt="" width="580" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_14.gif" alt="" width="540" height="190" /></p>
<p>The upper half of the matrix represents normal-component influence coefficients. The first row of the lower half of the matrix represents terms in an equation implementing the Kutta condition and sums the circulation density at the beginning of the first panel and the circulation density at the end of the last panel. Setting this sum to zero imposes zero circulation at the trailing edge of the airfoil. The remaining rows in the lower half of the matrix are coefficients of terms in the equations requiring that the circulation density at the end of one panel be equal to the circulation density at the beginning of the next panel, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_54.gif" alt="" width="96" height="18" />, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_55.gif" alt="" width="11" height="14" /> denotes the length of the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_56.gif" alt="" width="13" height="15" /> panel.</p>
<p>Define the transformation matrix to compute the list of slope parameters (<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_57.gif" alt="" width="5" height="12" />) from the list of constant parameters (<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_58.gif" alt="" width="12" height="12" />).</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_35.gif" alt="" width="195" height="12" /></p>
<p>Compute the free-stream velocity at collocation points.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_36.gif" alt="" width="213" height="12" /></p>
<p>Compute the components of the uniform flow normal to panels at collocation points.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_37.gif" alt="" width="152" height="12" /></p>
<p>Solve the system of equations for the list of constant parameters.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_38.gif" alt="" width="291" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_15.gif" alt="" width="409" height="12" /></p>
<p>Use the transformation matrix to compute the list of slope parameters.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_39.gif" alt="" width="93" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_16.gif" alt="" width="394" height="12" /></p>
<p>The lift coefficient for the airfoil can be computed using the Kutta-Joukowski theorem. Recall that the distribution of circulation on a panel in local panel coordinates can be written as <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_59.gif" alt="" width="92" height="12" />, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_60.gif" alt="" width="10" height="12" /> denotes the distance from the leading edge of the panel. The contribution of each panel to the lift is computed and the results summed over all panels.</p>
<p>In terms of the dimensionless variables used in this example, the contribution by each panel to the lift coefficient is just twice the net circulation associated with the panel, which is obtained by integrating the linear circulation density function, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_61.gif" alt="" width="194" height="21" />. Compute contributions of each panel to the airfoil lift coefficient.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_40.gif" alt="" width="353" height="18" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_17.gif" alt="" width="469" height="28" /></p>
<p>Sum the two terms for each panel to obtain the list of contributions of each panel to the lift coefficient.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_41.gif" alt="" width="108" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_18.gif" alt="" width="416" height="12" /></p>
<p>Sum the panel contributions to obtain the airfoil lift coefficient.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_42.gif" alt="" width="101" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_19.gif" alt="" width="50" height="12" /></p>
<p>The computations in this section illustrate the process of model implementation using a coarse discretization so that intermediate results can be viewed; however, the discretization is too coarse to provide useful results.</p>
<p>Remove names from computer memory, except those with values needed in the subsequent section, which presents an example computation of the pressure distribution and lift coefficient for a specified airfoil using a larger discretization number.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_43.gif" alt="" width="542" height="12" /></p>
<h4>Numerical Model for Fine Discretization</h4>
<p>In the following expression, the individual steps for implementing the model in the previous section are collected into a module. Most names for variables have been shortened for conciseness, but should be recognizable.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_44.gif" alt="" width="580" height="205" /></p>
<p>The results of this computation are the singularity strength parameters for all panels.</p>
<p>This model implementation has been validated by computing the results for a van de Vooren airfoil for which an exact solution is known by the method of conformal mapping. Also, convergence and timing studies have been performed and are available as online help documents in the software collection.</p>
<h4>Pressure and Lift Coefficients</h4>
<p>Use previously computed influence coefficients to determine the pressure coefficient at collocation points using Bernoulli&#8217;s equation.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_45.gif" alt="" width="554" height="12" /></p>
<p>Lift and pitching moments can be computed from the pressure distribution. For example, the lift coefficient is computed by approximating the integral, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_62.gif" alt="" width="88" height="19" />, where the integral is over the airfoil contour, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_63.gif" alt="" width="14" height="14" /> is the pressure coefficient, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_64.gif" alt="" width="6" height="14" /> is the outward unit normal to the airfoil surface, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_65.gif" alt="" width="5" height="16" /> is a unit vector perpendicular to the free-stream velocity in the direction of positive lift. The integral is approximated by considering the pressure coefficient constant over each panel, computing the contribution to lift of each panel, and summing the results.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_46.gif" alt="" width="511" height="58" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_20.gif" alt="" width="50" height="12" /></p>
<p>The lift can also be computed from the circulation distribution as described in the section on step-by-step model formulation.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_47.gif" alt="" width="580" height="18" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_21.gif" alt="" width="50" height="12" /></p>
<p>Figure 2 shows the surface pressure distribution on the airfoil in the conventional manner for such plots.  Useful information from such plots include the locations of the stagnation point and the point of minimum pressure, and the severity of the positive pressure gradient on the upper surface.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Input_48.gif" alt="" width="580" height="43" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Output_22.gif" alt="" width="504" height="328" /></p>
<p class="NumberedFigureCaption"><strong>Figure 2.</strong> Pressure coefficient for a NACA 4412 airfoil, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/PanelMethods_Math_66.gif" alt="" width="43" height="12" /> discretization 200 panels with <tt>HalfCosineSpacing</tt>.</p>
<h3>Conclusions</h3>
<p>A brief summary of some features of a collection of packages that provide computational tools for formulating numerical models for two-dimensional potential flow over an airfoil using panel methods is presented. An example of solving the problem of steady flow over a specific airfoil is given using vortex panels of linearly varying strength and tangent-flow boundary conditions. This example includes the computation of surface pressure distribution and lift coefficient.</p>
<p>Session time for a typical PC indicates the practicality of such computations on low-cost computing systems and suggests the feasibility of going to the next level of modeling. This could include unsteady two-dimensional potential flow, steady three-dimensional potential flow, or including an integral boundary-layer method with the steady two-dimensional potential flow model presented in this article.</p>
<h3>References</h3>
<table class="ReferenceTable">
<tbody>
<tr>
<td class="Reference"><a name="Sabersky">[1] </a></td>
<td>R. H. Sabersky, A. J. Acosta, E. G. Hauptmann, and E. M. Gates, <em>Fluid Flow: A First Course in Fluid Mechanics</em>, 4th ed., Englewood Cliffs, NJ: Prentice Hall, 1998.</td>
</tr>
<tr>
<td class="Reference"><a name="Hess">[2] </a></td>
<td>J. L. Hess, &#8220;Panel Methods in Computational Fluid Dynamics,&#8221; <em>Annual Review of Fluid Mechanics</em>, <strong>22</strong>, 1990 pp. 255-274.</td>
</tr>
<tr>
<td class="Reference"><a name="Morse">[3] </a></td>
<td>P. M. Morse and H. Feshbach, <em>Methods of Theoretical Physics</em>, New York: McGraw-Hill, 1953.</td>
</tr>
<tr>
<td class="Reference"><a name="Katz">[4] </a></td>
<td>J. Katz and A. Plotkin, <em>Low-Speed Aerodynamics</em>, Cambridge Aerospace Series (No. 13), 2nd ed., New York: Cambridge University Press, 2001.</td>
</tr>
<tr>
<td class="Reference"><a name="Anderson">[5] </a></td>
<td>J. D. Anderson, <em>Introduction to Flight</em>, 3rd ed., New York: McGraw-Hill, 1989.</td>
</tr>
<tr>
<td class="Reference"><a name="Bertin">[6] </a></td>
<td>J. J. Bertin and M. L. Smith, <em>Aerodynamics for Engineers</em>, 3rd ed., Englewood Cliffs, NJ: Prentice Hall, 1998.</td>
</tr>
<tr>
<td class="Reference"><a name="Selig">[7] </a></td>
<td>M. S. Selig, &#8220;UIUC Airfoil Data Site, Department of Aerospace Engineering.&#8221; Urbana, Illinois: University of Illinois, (Jan 2007) <a href="http://www.ae.uiuc.edu/m-selig/ads.html" target="blank">www.ae.uiuc.edu/m-selig/ads.html</a>.</td>
</tr>
<tr>
<td class="DOIReference" colspan="2">R. L. Fearn, &#8220;Airfoil Aerodynamics Using Panel Methods,&#8221; <em>The Mathematica Journal</em>, 2011. dx.doi.org/10.3888/tmj.10.4-6.</td>
</tr>
</tbody>
</table>
<h3>Additional Material</h3>
<p>Fearn.zip</p>
<p class="SmallText">Available at <a href="http://library.wolfram.com/infocenter/MathSource/7785/">library.wolfram.com/infocenter/MathSource/7785/</a>.</p>
<h3 class="SectionAboutAuthor">About the Author</h3>
<p class="TextAboutAuthor">While teaching for thirty years at the University of Florida, I often wished for effective computational tools to help students learn aerodynamics. Since retirement, I have started developing software for that purpose. When not playing with <em>Mathematica</em>, I can usually be found summers hiking in the Canadian Rockies or, spring and fall, walking or canoeing in the northern part of Florida with family and friends.</p>
<p class="TextAboutAuthor"><strong>Richard L. Fearn</strong><br />
<em>Professor Emeritus</em><br />
<em>Department of Mechanical and Aerospace Engineering<br />
University of Florida<br />
Gainesville, FL 32611-6250<br />
</em><em><a href="mailto:rlf@ufl.edu">rlf@ufl.edu</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mathematica-journal.com/2012/02/airfoil-aerodynamics-using-panel-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MathCode: A System for C++ or Fortran Code Generation from Mathematica</title>
		<link>http://www.mathematica-journal.com/2012/02/mathcode-a-system-for-c-or-fortran-code-generation-from-mathematica/</link>
		<comments>http://www.mathematica-journal.com/2012/02/mathcode-a-system-for-c-or-fortran-code-generation-from-mathematica/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 00:07:47 +0000</pubDate>
		<dc:creator>Todd Akers</dc:creator>
				<category><![CDATA[Volume 10, Issue 4]]></category>

		<guid isPermaLink="false">http://www.internal.mathematica-journal.com/?p=17826</guid>
		<description><![CDATA[MathCode is a package that translates a subset of Mathematica into a compiled language like Fortran or C++. The chief goal of the design of MathCode is to add extra performance and portability to the symbolic prototyping capabilities offered by Mathematica. This article discusses several important features of MathCode, such as adding type declarations, examples [...]]]></description>
			<content:encoded><![CDATA[<p><em>MathCode</em> is a package that translates a subset of <em>Mathematica</em> into a compiled language like Fortran or C++. The chief goal of the design of <em>MathCode</em> is to add extra performance and portability to the symbolic prototyping capabilities offered by <em>Mathematica</em>. This article discusses several important features of <em>MathCode,</em> such as adding type declarations, examples of functions that can be translated, ways to extend the compilable subset, and generating a stand-alone executable, and presents a few application examples.<span id="more-17826"></span></p>
<h3>Introduction</h3>
<p><em>MathCode</em> is a <em>Mathematica</em> add-on that translates a <em>Mathematica</em> program into C++ or Fortran 90. The subset of <em>Mathematica</em> that <em>MathCode</em> is able to translate involves purely numerical operations, and no symbolic operations. In the following sections we provide a variety of examples that show precisely what we mean. The code that is generated can be called and run from within <em>Mathematica</em>, as if you were running a <em>Mathematica</em> function.</p>
<p>There are two important purposes that are served by <em>MathCode</em>. Firstly, the C++/Fortran 90 code runs faster, typically by a factor of about a few hundreds (or about 50 to 100) over interpreted (compiled) <em>Mathematica</em> code, resulting in considerable performance gains, while still requiring hardly any knowledge of C++/Fortran 90 on the part of the user. Secondly, the generated code can also be executed as a stand-alone program outside <em>Mathematica</em>, offering a portability otherwise not possible. You should note, however, that these advantages come at some loss of generality since integer and floating point overflow are not trapped and switched to arbitrary precision as in standard <em>Mathematica</em> code. Here the user is responsible for ensuring an appropriate choice of scaling and input data to avoid such problems. The measurements in this article were made using <em>Mathematica</em> 6.</p>
<p>There are situations in which having a system such as <em>MathCode</em> can be particularly helpful and effective, like when a certain calculation involves a symbolic phase followed by a numerical one. In such a hybrid situation, <em>Mathematica</em> can be employed for the symbolic part to give a  set of expressions involving only numerical operations that can be made part of a <em>Mathematica</em> function, which can then be translated into C++/Fortran 90 using <em>MathCode</em>.</p>
<p>In this article, we describe some of the more important features of <em>MathCode</em>. For a more detailed discussion the reader is referred to [<a href="#Fritz98">1</a>]. For brevity, we simply say C++ when we actually mean C++ or Fortran 90: <em>MathCode</em> can generate code in both C++ and Fortran, although we illustrate C++ code generation in this article.</p>
<p>In Section 2, we show how to quickly get started with <em>MathCode</em> using a simple example of a function to add integers.</p>
<p>Section 3 presents many useful features of <em>MathCode</em>. In Section 3.1, we discuss the way the system works, the various auxiliary files generated and what to make of them, and how to build C++ code and install the executable. We then compare the execution times of the interpreted <em>Mathematica</em> code and the compiled C++ code. This section also illustrates how <em>MathCode</em> works with packages.</p>
<p>Section 3.2 briefly makes a few points about types and type declarations in <em>MathCode</em>. There are two ways to declare argument types and return types of a function mentioned in this section.</p>
<p>In Section 3.3, we show how to generate a stand-alone C++ executable. This executable can be run outside of <em>Mathematica.</em> We illustrate how to design a suitable main program that the executable runs.</p>
<p>It should be emphasized that <em>MathCode</em> can generate C++ for only that subset of <em>Mathematica</em> functions referred to as the <em>compilable subset</em>. Section 3.4 gives a sample of this subset, while Section 3.5 presents three ways to extend it with the already-available features of <em>MathCode</em>: Sections 3.5.1 through 3.5.3 discuss, respectively, symbolic expansion of function bodies, callbacks to <em>Mathematica</em>, and handling external functions. Each of these extensions has its own strengths and limitations.</p>
<p>Section 3.6 discusses common subexpression elimination, a feature that is aimed at enhancing the efficiency of generated code.</p>
<p>Section 3.7 presents some shortcuts available in <em>MathCode</em> to extract and make assignments to elements of matrices and submatrices, while Section 3.8 is about array declarations.</p>
<p>In Section 4, we present several examples of effectively using <em>MathCode</em>. Section 4.1 provides a summary of the examples.</p>
<p>Section 4.2 discusses an essentially simple example, that of computing the function <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_1.gif" alt="" width="46" height="12" /> over a grid in the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_2.gif" alt="" width="6" height="12" />-<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_3.gif" alt="" width="6" height="12" /> plane, but done in a somewhat roundabout manner so as to illustrate various features of <em>MathCode</em>.</p>
<p>Section 4.3 discusses an implementation of the Gaussian elimination algorithm [<a href="#GerWhe94">2</a>] to solve matrix systems of the type <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_4.gif" alt="" width="40" height="12" />, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_5.gif" alt="" width="8" height="12" /> is a square matrix of size <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_6.gif" alt="" width="6" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_7.gif" alt="" width="8" height="12" /> (the solution vector) and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_8.gif" alt="" width="7" height="12" /> are vectors of size <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_9.gif" alt="" width="6" height="12" />. In this section, we make a detailed performance study by computing the solution of a matrix system by turning on a few compilation options available in <em>MathCode</em>, and also make comparisons with <tt>LinearSolve</tt>.</p>
<p>In Section 4.4, we show how to call external libraries and object files from a C++ program that is automatically generated by <em>MathCode</em>. We take the example of a well-known matrix library called SuperLU [<a href="#SuperLU">3</a>], and demonstrate how to solve, using one of its object modules, a sparse matrix system arising from a partial differential equation.</p>
<p>The <em>MathCode</em> User Guide that is available online discusses more advanced aspects, like a detailed account of types and declarations, the numerous options available in <em>MathCode</em> with the aid of which the user can control code generation and compilation, and other features. We refer interested readers to [<a href="#Fritz98">1</a>].</p>
<p>In Section 5, we summarize the salient aspects of <em>MathCode</em> and discuss the kinds of applications for which <em>MathCode</em> is particularly useful. We conclude the article with a brief summary of various points made. The first version of <em>MathCode</em>, released in 1998, was partly developed from the code generator in the ObjectMath environment [<a href="#Omath">4</a>, <a href="#Omath2">5</a>]. The current version is almost completely rewritten and very much improved.</p>
<h3>2. Getting Started with <em>MathCode</em></h3>
<h4>2.1. An Example Function</h4>
<p>In this section we take the reader on a quick tour of <em>MathCode</em> using the simple example of a function to add integers.</p>
<p>The following command loads <em>MathCode</em>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_1.gif" alt="" width="129" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_2.gif" alt="" width="580" height="30" /></p>
<p><em>MathCode</em> works by generating a set of files in the current directory (see Section 3.1). We can set the directory in the standard way as follows; here, <tt>$MCRoot</tt> is the <em>MathCode</em> root directory. The user can, however, use any other directory to store the files.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_3.gif" alt="" width="349" height="12" /></p>
<p>Let us now define a <em>Mathematica</em> function <tt>sumint</tt> to add the first <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_10.gif" alt="" width="6" height="12" /> natural numbers.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_4.gif" alt="" width="499" height="12" /></p>
<p>Note that the body of this function has purely numerical operations, like incrementing the loop index <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_11.gif" alt="" width="3" height="12" />, adding two numbers, and assigning the result to a variable.</p>
<h4>2.2. Declaration of Types</h4>
<p>We must now declare the data types of the parameter <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_12.gif" alt="" width="6" height="12" /> and the local variables <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_13.gif" alt="" width="15" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_14.gif" alt="" width="3" height="12" />; we must also specify the return type of the function. We do this using the function <tt>Declare</tt> that <em>MathCode</em> provides.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_5.gif" alt="" width="399" height="12" /></p>
<p>Note that <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_15.gif" alt="" width="68" height="12" /> <em>does not</em> mean <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_16.gif" alt="" width="68" height="12" />; the function <tt>Declare</tt> creates an environment in which this is interpreted as a type declaration, that is, an integer variable <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_17.gif" alt="" width="6" height="12" /> is being declared in the example. The type <tt>Integer</tt> is translated to a native C int type, and the type <tt>Real</tt> to a native C double type.</p>
<h4>2.3. C++ Code</h4>
<p>To generate and compile the C++ code, we execute the following command.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_6.gif" alt="" width="150" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_7.gif" alt="" width="580" height="30" /></p>
<p>Since we have not specified the context of <tt>sumint</tt>, its default context is <tt>Global</tt>. We could, therefore, have simply executed the following command instead.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_8.gif" alt="" width="86" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_9.gif" alt="" width="580" height="30" /></p>
<p>With the following command, we seamlessly integrate an external program with <em>Mathematica</em>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_10.gif" alt="" width="100" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_11.gif" alt="" width="580" height="30" /></p>
<p>We can now run the external program in the same way that we would execute a <em>Mathematica</em> command.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_12.gif" alt="" width="86" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_1.gif" alt="" width="45" height="12" /></p>
<p>If we want to run the <em>Mathematica</em> code (and not the generated C++ code) for <tt>sumint</tt>, we must first uninstall the C++ executable.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_13.gif" alt="" width="115" height="12" /></p>
<p>Now the <em>Mathematica</em> code for <tt>sumint</tt> will run.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_14.gif" alt="" width="86" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_2.gif" alt="" width="45" height="12" /></p>
<h3>3. A Tour of <em>MathCode</em></h3>
<h4>3.1. How the <em>MathCode</em> System Works</h4>
<p><em>MathCode</em> works by generating a set of files in the home directory. In the example of <tt>sumint</tt>, the default context is <tt>Global</tt> and the files generated by <em>MathCode</em> are: Global.cc (the C++ source file), Global.h and Global.mh (the header files), Globaltm.c, Global.tm and Globalif.cc (the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_18.gif" alt="" width="55" height="15" />-related files that enable transparently calling C++ versions of the function <tt>sumint</tt> from <em>Mathematica</em>), and Globalmain.cc, which contains the function main( ) needed when building a stand-alone executable.</p>
<p>We can also create a package (let us call it <tt>foo</tt>) that defines its own context <tt>foo</tt> instead of the default context <tt>Global</tt>. See Figure 1 for a block diagram of the way the overall system works. The <em>MathCode</em> code generator translates the <em>Mathematica</em> package to a corresponding C++ source file foo.cc. Additional files are automatically generated: the header file foo.h, the <em>MathCode</em> header file foo.mh, the <em>MathLink-</em>related files footm.c, foo.tm, foo.icc, and fooif.cc, which enable calling the C++ versions from <em>Mathematica</em>, and foomain.cc, which contains the function main that is needed when building a stand-alone executable for <tt>foo</tt> (see Section 3.3). The generated file foo.cc created from the package <tt>foo</tt>, the header file foo.h, and additional files are compiled and linked into two executables. In the case of <em>MathCode</em> F90, Fortran 90 is generated and a file foo.f90 is created. No header file is generated in that case since Fortran 90 provides directives for the use of module. External numerical libraries may be included in the linking process by specifying their inclusion (Sections 3.5.3 and 4.5). The executable produced, foo.exe, can be used for stand-alone execution, whereas fooml.exe is used when calling on the compiled C++ functions from <em>Mathematica</em> via <em>MathLink.</em></p>
<p><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Fig1.gif" alt="" width="404" height="131" /></p>
<p class="NumberedFigureCaption"><strong>Figure 1. </strong>Generating C++ code with <em>MathCode</em> for a package called <tt>foo</tt>.</p>
<p>Let us see how to work with a package again using the same <tt>sumint</tt> example.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_15.gif" alt="" width="136" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_16.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_17.gif" alt="" width="349" height="12" /></p>
<p>If we are compiling the package <tt>foo</tt> using <em>MathCode</em>, we also need to mention <tt>MathCodeContexts</tt> within the path of the package.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_18.gif" alt="" width="290" height="12" /></p>
<p>We define the function <tt>sumint</tt></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_19.gif" alt="" width="506" height="12" /></p>
<p>and close the context <tt>foo</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_20.gif" alt="" width="93" height="12" /></p>
<p>We next declare the types, and then build and install as before.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_21.gif" alt="" width="399" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_22.gif" alt="" width="129" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_23.gif" alt="" width="580" height="30" /></p>
<p>Again, since the package <tt>foo</tt> has been defined, it is the default context, and so we could simply have executed the following command.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_24.gif" alt="" width="86" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_25.gif" alt="" width="580" height="30" /></p>
<p>To run the executable from the notebook, we must install it.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_26.gif" alt="" width="100" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_27.gif" alt="" width="580" height="30" /></p>
<p>Now the following command runs the C++ executable fooml.exe. The call to <tt>sumint</tt> via <em>MathLink</em> is executed 1000 times. The timing measurement includes <em>MathLink</em> overhead, which typically for small functions is much more than the execution time for the compiled function. This can be avoided if the loop is executed within the external function itself, as in the example in Section 4.2.5.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_28.gif" alt="" width="295" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_3.gif" alt="" width="106" height="12" /></p>
<p>Here is the C++ code that was generated.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_29.gif" alt="" width="136" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_30.gif" alt="" width="580" height="281" /></p>
<p>Note that the function <tt>sumint</tt> appears as foo_Tsumint in the generated code. This is because the full name of the function is in fact <tt>foo`sumint</tt>, and <em>MathCode</em> replaces the backquote &#8220;<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_19.gif" alt="" width="4" height="12" />&#8221; by &#8220;<tt>_T</tt>&#8221; in the C++ code.</p>
<p>To run the <em>Mathematica</em> function (and not its C++ equivalent) <tt>sumint</tt>, we must use the following command to uninstall the C++ code.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_31.gif" alt="" width="115" height="12" /></p>
<p>Now it is the <em>Mathematica</em> code that runs when you execute <tt>sumint</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_32.gif" alt="" width="295" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_4.gif" alt="" width="113" height="12" /></p>
<p>You can see that the C++ executable together with the <em>MathLink</em> overhead runs about 15 times faster than the <em>Mathematica</em> code. The factor by which the performance is enhanced is problem dependent, however. The performance of the <em>Mathematica</em> code could also have been improved by using the built-in <tt>Compile</tt> function. In Section 4 we will see many more examples, some quite involved, where we get a range of performance enhancements, also including usage of the <tt>Compile</tt> function.</p>
<p>We clean up the current directory by removing the files automatically generated by <em>MathCode</em>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_33.gif" alt="" width="376" height="12" /></p>
<h4>3.2. Types and Declarations</h4>
<p>To be able to generate efficient code, the types of function arguments and return values must be specified, as we have seen in the preceding examples. The basic types used by <em>MathCode</em> are</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_34.gif" alt="" width="144" height="12" /></p>
<p>Arrays (vectors and matrices) of these types can also be declared.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_35.gif" alt="" width="436" height="12" /></p>
<p>Type declarations can be given in two different ways:</p>
<li>Directly in the function definition</li>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_36.gif" alt="" width="149" height="15" /></p>
<li>In a separate command</li>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_37.gif" alt="" width="104" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_38.gif" alt="" width="178" height="12" /></p>
<p>The latter construction can be useful if you want to separate already existing <em>Mathematica</em> code with the type information needed to be able to generate C++ code using <em>MathCode</em>.</p>
<h4>3.3. Generating a Stand-Alone Program</h4>
<p>So far we have only seen examples in which the installed C++ code can be run within <em>Mathematica</em>. However, we can also produce a stand-alone executable. This offers a degree of portability that can be useful in practice.</p>
<p>To illustrate, we take the same example function <tt>sumint</tt> that we discussed in the previous sections. The sequence of commands is very much as in the previous section, except for the option <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_20.gif" alt="" width="190" height="12" /> for the <em>MathCode</em> function <tt>MakeBinary</tt>, and an appropriate option <tt>MainFileAndFunction</tt> for the function <tt>SetCompilationOptions</tt> immediately after <tt>BeginPackage</tt>. Figure 2 illustrates the process of building the two kinds of executable, namely fooml.exe and foo.exe (on some systems foomain.exe) from a package called <tt>foo</tt>.</p>
<p><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Fig2.gif" alt="" width="438" height="187" /></p>
<p class="NumberedFigureCaption"><strong>Figure 2. </strong>Building two executables from the package <tt>foo</tt>, possibly including numerical libraries.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_39.gif" alt="" width="136" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_40.gif" alt="" width="349" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_41.gif" alt="" width="290" height="12" /></p>
<p>The option <tt>MainFileAndFunction</tt> is used to specify the main file. The functions defined in <em>Mathematica</em> must have the prefix Global_T (<em>packagename</em>_T in general) to be recognized in the main file.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_42.gif" alt="" width="540" height="58" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_43.gif" alt="" width="506" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_44.gif" alt="" width="93" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_45.gif" alt="" width="399" height="12" /></p>
<p>Now we are ready to generate and compile the C++ code for the package <tt>foo</tt>. We can do this in two ways: we can either employ the <em>MathCode</em> function <tt>BuildCode</tt>, as in the previous examples, or first execute <tt>CompilePackage</tt> (which generates the C++ source and header files) and then the function <tt>MakeBinary</tt> (which creates the executable).</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_46.gif" alt="" width="165" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_47.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_48.gif" alt="" width="337" height="12" /></p>
<p>The last command generates the stand-alone executable foo.exe that can be executed from a command line, or, alternatively, by using the <em>Mathematica</em> function <tt>Run</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_49.gif" alt="" width="101" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_5.gif" alt="" width="7" height="12" /></p>
<p>If you desire, you can, in addition to the stand-alone executable foo.exe, also generate fooml.exe that can be run from within <em>Mathematica</em>, just like before.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_50.gif" alt="" width="136" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_51.gif" alt="" width="100" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_52.gif" alt="" width="580" height="30" /></p>
<p>Now the following command runs the C++ program foo.cc.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_53.gif" alt="" width="86" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_6.gif" alt="" width="45" height="12" /></p>
<h5>3.3.1. Generating a DLL</h5>
<p>Here we briefly mention the possibility of generating a DLL, without giving a full example. To generate a DLL from a package, you have to write a file containing one simple wrapper function in order to make a generated function visible outside the DLL. You write a wrapper function for each generated function. The flags used are as follows:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_54.gif" alt="" width="441" height="28" /></p>
<p>Here &#8220;ext.cpp&#8221; is a C++ file with wrapper functions, and &#8220;<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_21.gif" alt="" width="33" height="12" />&#8221; is a flag for the Visual C++ linker. For other C++ compilers this procedure is not automatic and requires several operating system commands, but the wrapper functions are not needed.</p>
<h4>3.4. The Compilable Subset</h4>
<p><em>MathCode</em> generates C++ code for a subset of <em>Mathematica</em> functions, called the <em>compilable subset</em>. The following items give a sample of the compilable subset. For a complete list of <em>Mathematica</em> functions in the compilable subset, see [<a href="#Fritz98">1</a>].</p>
<ul>
<li>Statically typed functions, where the types of function arguments and return values are given by the types discussed in Section 3.2</li>
<li>Scoping constructs: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_22.gif" alt="" width="100" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_23.gif" alt="" width="86" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_24.gif" alt="" width="57" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_25.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_26.gif" alt="" width="43" height="12" /></li>
<li>Procedural constructs: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_27.gif" alt="" width="36" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_28.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_29.gif" alt="" width="28" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_30.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_31.gif" alt="" width="28" height="12" /></li>
<li>Lists and tables: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_32.gif" alt="" width="43" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_33.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_34.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_35.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_36.gif" alt="" width="115" height="12" /></li>
<li>Size functions: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_37.gif" alt="" width="86" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_38.gif" alt="" width="57" height="12" /></li>
<li>Arithmetic and logical expressions, for example: <tt>+</tt>, <tt>-</tt>, <tt>*</tt>, <tt>/</tt>, <tt>==</tt>, <tt>!=</tt>, <tt>&gt;</tt>, <tt>!</tt>, <tt>&amp;&amp;</tt>, <tt>||</tt>, and so forth</li>
<li>Elementary functions and some others, for example: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_39.gif" alt="" width="36" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_40.gif" alt="" width="36" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_41.gif" alt="" width="57" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_42.gif" alt="" width="43" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_43.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_44.gif" alt="" width="36" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_45.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_46.gif" alt="" width="79" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_47.gif" alt="" width="36" height="12" /></li>
<li>Constants: <tt>True</tt>, <tt>False</tt>, <tt>E</tt>, <tt>Pi</tt></li>
<li>Assignments: <tt>:=</tt>, <tt>=</tt></li>
<li>Functional commands: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_48.gif" alt="" width="36" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_49.gif" alt="" width="50" height="12" /></li>
<li>Some special commands: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_50.gif" alt="" width="36" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_51.gif" alt="" width="64" height="12" /></li>
</ul>
<p>Functions not in the compilable subset can be used in external code by callbacks to <em>Mathematica</em> (see Section 3.5.2 for an example).</p>
<p>Examples of functions that are not a part of the compilable subset include: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_52.gif" alt="" width="79" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_53.gif" alt="" width="50" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_54.gif" alt="" width="72" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_55.gif" alt="" width="93" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_56.gif" alt="" width="57" height="12" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_57.gif" alt="" width="57" height="12" />.</p>
<p>These functions can be used if <em>Mathematica</em> can evaluate them at compile time to expressions that belong to the compilable subset. In general, <em>Mathematica</em> functions that perform symbolic operations are not in the compilable subset. Also, many functions in the subset are implemented with limitations, that is, more difficult cases are not always supported. However, <em>MathCode</em> currently provides several ways to extend the compilable subset, as we discuss in the next section.</p>
<h4>3.5 Ways to Extend the Compilable Subset</h4>
<h5>3.5.1. Symbolic Expansion of Function Bodies</h5>
<p>Functions not entirely written using <em>Mathematica</em> code in the compilable subset, but whose definitions can be evaluated symbolically to expressions that belong to the compilable subset, can be handled by <em>MathCode</em>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_55.gif" alt="" width="136" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_56.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_57.gif" alt="" width="349" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_58.gif" alt="" width="397" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_59.gif" alt="" width="60" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_7.gif" alt="" width="50" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_60.gif" alt="" width="16" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_61.gif" alt="" width="580" height="31" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_62.gif" alt="" width="580" height="35" /></p>
<p>Generate C++ code and compile it to an executable file.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_63.gif" alt="" width="239" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_64.gif" alt="" width="580" height="30" /></p>
<p>The option <tt>EvaluateFunctions</tt> tells <em>MathCode</em> to let <em>Mathematica</em> expand the function body as much as possible. Everything works fine because the result belongs to the compilable subset.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_65.gif" alt="" width="200" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_8.gif" alt="" width="231" height="12" /></p>
<p>The generated executable is connected to <em>Mathematica</em>:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_66.gif" alt="" width="100" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_67.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_68.gif" alt="" width="60" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_9.gif" alt="" width="50" height="12" /></p>
<h5>3.5.2. Callbacks to <em>Mathematica</em></h5>
<p>Consider the following function whose definition includes the <tt>Zeta</tt> function, which does not belong to the compilable subset.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_69.gif" alt="" width="129" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_70.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_71.gif" alt="" width="342" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_10.gif" alt="" width="237" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_72.gif" alt="" width="232" height="35" /></p>
<p>Let us plot the function:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_73.gif" alt="" width="254" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_11.gif" alt="" width="360" height="204" /></p>
<p>We now make the declarations:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_74.gif" alt="" width="178" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_75.gif" alt="" width="200" height="12" /></p>
<p>These declare statements do not change the way <em>Mathematica</em> computes the function.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_76.gif" alt="" width="101" height="31" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_12.gif" alt="" width="266" height="53" /></p>
<p>Let us now generate C++ code and compile it to an executable file. The option <tt>CallBackFunctions</tt> tells <em>MathCode </em>which functions have to be evaluated by <em>Mathematica</em>. As a result, although the function <tt>Zeta</tt> is not in the compilable subset, an executable is still generated and communicates with the kernel to evaluate <tt>Zeta</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_77.gif" alt="" width="261" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_78.gif" alt="" width="580" height="30" /></p>
<p>The generated executable is connected to <em>Mathematica</em>:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_79.gif" alt="" width="100" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_80.gif" alt="" width="580" height="30" /></p>
<p>Now it is the external code that is used to compute the function:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_81.gif" alt="" width="101" height="31" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_13.gif" alt="" width="145" height="31" /></p>
<p>In this case the external code calls <em>Mathematica</em> when the <tt>Zeta</tt> function has to be evaluated. After the evaluation the computation proceeds in the external code.</p>
<p>Note that it is the installed code for the function <tt>f</tt> that is executed above, and not the original <em>Mathematica</em> function. In the installed code, the argument of <tt>f</tt> must be real, according to our declaration. As a result, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_58.gif" alt="" width="46" height="12" />, in which we pass a rational number as an argument, is left unevaluated.</p>
<p>We again plot the function, but this time using the external code to evaluate it:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_82.gif" alt="" width="254" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_14.gif" alt="" width="360" height="204" /></p>
<h5>3.5.3. External Functions</h5>
<p>We can have references to external objects in C++ code generated by <em>MathCode</em>. Let us consider three very simple external functions that compute <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_59.gif" alt="" width="12" height="15" />, <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_60.gif" alt="" width="10" height="12" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_61.gif" alt="" width="28" height="12" /> to illustrate the idea. These must be defined as follows in an external source file that must be in the working directory.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_83.gif" alt="" width="129" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_84.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_85.gif" alt="" width="299" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_86.gif" alt="" width="179" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_87.gif" alt="" width="580" height="185" /></p>
<p>Observe here that each function definition, which is in C language syntax, is followed by a &#8220;wrapper&#8221; that enables <em>MathCode</em> to recognize the object as external. We can then create an object file corresponding to these functions and link the object as follows.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_88.gif" alt="" width="366" height="43" /></p>
<p>We define a function to create a list of numbers using the external functions.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_89.gif" alt="" width="561" height="43" /></p>
<p>We now compile the package. Since this is a very small example, we do not bother to create a special package for the code.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_90.gif" alt="" width="115" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_91.gif" alt="" width="580" height="30" /></p>
<p>Let us now create the <em>MathLink</em> binary; to do this when there are external functions, we must specify the option <tt>NeedsExternalObjectModule</tt> as follows.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_92.gif" alt="" width="376" height="12" /></p>
<p>Here, as we noted above, <tt>external1</tt> and <tt>external2</tt> represent the external object modules external1.o and external2.o. Install the <em>MathCode</em>-compiled code so it is called using <em>MathLink</em>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_93.gif" alt="" width="100" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_94.gif" alt="" width="580" height="30" /></p>
<p>When we make the following plot, it is the external code for <tt>extsqr</tt>, <tt>extexp</tt>, and <tt>extoscillation</tt> that is used.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_95.gif" alt="" width="266" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_15.gif" alt="" width="360" height="227" /></p>
<h4>3.6. Common Subexpression Elimination</h4>
<p>Consider the following function whose definition contains a number of common subexpressions (e.g., <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_62.gif" alt="" width="30" height="15" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_63.gif" alt="" width="46" height="24" />).</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_96.gif" alt="" width="136" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_97.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_98.gif" alt="" width="580" height="115" /></p>
<p>There are very efficient algorithms to evaluate functions containing common subexpressions. The basic idea is to evaluate common subexpressions only once and put the results in temporary variables.</p>
<p>Now we generate C++ code using <em>MathCode</em> and run it.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_99.gif" alt="" width="79" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_100.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_101.gif" alt="" width="100" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_102.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_103.gif" alt="" width="175" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_16.gif" alt="" width="83" height="12" /></p>
<p><em>MathCode</em> does common subexpression elimination (CSE) when the option <tt>EvaluateFunctions</tt> is given to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_64.gif" alt="" width="93" height="12" /> or <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_65.gif" alt="" width="79" height="12" />. This basic strategy could be further improved for special cases in future versions of <em>MathCode</em>. Moreover, since mathematical expressions are intrinsically free of side effects and do not have a specific evaluation order, the CSE optimization may change the order of computing subexpressions if this improves performance. Changing the order can sometimes have a small influence on the result when floating-point arithmetic is used.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_104.gif" alt="" width="115" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_105.gif" alt="" width="376" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_106.gif" alt="" width="239" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_107.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_108.gif" alt="" width="100" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_109.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_110.gif" alt="" width="175" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_17.gif" alt="" width="83" height="12" /></p>
<p>We take a look at the generated C++ file.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_111.gif" alt="" width="157" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_112.gif" alt="" width="580" height="965" /></p>
<p>Note how the computation of the function has been divided into small subexpressions that are evaluated only once and then stored in temporary variables for future use. This gives very efficient code for large functions. The speed enhancement of roughly 150% brought about by CSE in this example is not appreciable because the example itself is rather small.</p>
<h4>3.7. Extended Matrix Operations</h4>
<p>When dealing with matrices, it is very convenient to have a short notation for part extraction. <em>MathCode</em> extends the functionality of <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_66.gif" alt="" width="43" height="12" /> or <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_67.gif" alt="" width="30" height="12" /> to achieve this.</p>
<p>Consider the following <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_68.gif" alt="" width="24" height="12" /> matrix:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_113.gif" alt="" width="330" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_18.gif" alt="" width="282" height="62" /></p>
<p>We can extract rows 2 to 4 as follows, with the shorthand available in <em>MathCode</em>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_114.gif" alt="" width="148" height="14" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_19.gif" alt="" width="282" height="46" /></p>
<p>We can extract the elements in all rows that belong to column 3 and higher:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_115.gif" alt="" width="160" height="14" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_20.gif" alt="" width="174" height="62" /></p>
<p>We can assign values to a submatrix of <tt>A</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_116.gif" alt="" width="217" height="14" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_117.gif" alt="" width="107" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_21.gif" alt="" width="282" height="62" /></p>
<p>All these operations belong to the compilable subset and can result in compact code. Note: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_69.gif" alt="" width="69" height="14" /> denotes the same <em>Mathematica</em> computation as <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_70.gif" alt="" width="133" height="12" />, and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_71.gif" alt="" width="83" height="14" /> is equivalent to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_72.gif" alt="" width="141" height="12" />.</p>
<h4>3.8. Array Declaration and Dimension</h4>
<p>In this subsection, we give a few examples of array declarations. There are two main cases to consider.</p>
<ul>
<li>Arrays that are passed as function parameters or returned as function values, where the actual array size has been previously allocated</li>
<li>Declaration of array variables, usually specifying both the type and the allocation of the declared array</li>
</ul>
<p>There are five allowed ways to specify array dimension sizes in array types for function arguments and results.</p>
<ul>
<li>Integer constant dimension sizes, for example: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_73.gif" alt="" width="68" height="12" /></li>
<li>Symbolic-constant dimension sizes, for example: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_74.gif" alt="" width="119" height="12" /></li>
<li>Unknown dimension sizes with unnamed placeholders, for example: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_75.gif" alt="" width="68" height="12" /></li>
<li>Unknown dimension sizes with named placeholders, for example: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_76.gif" alt="" width="84" height="12" /></li>
<li>Unknown dimension sizes with variables as dimension sizes, for example: <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_77.gif" alt="" width="68" height="12" /></li>
</ul>
<p>The dimension sizes can be constant, in which case the size information is part of the type. Alternatively, the sizes are unknown and thus fixed later at runtime when the array is allocated. Such unknown dimension sizes are specified through named (e.g., <tt>n_</tt>) or unnamed (<tt>_</tt>) placeholders.</p>
<p>All arrays that are passed as arguments to functions have already been allocated at runtime. Thus, their sizes are already determined. These sizes might, however, be different for different calls. Therefore it is not allowed to specify conflicting dimension sizes through integer variables (e.g., <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_78.gif" alt="" width="68" height="12" />) in array types of function parameters or results, as can be done for ordinary declared variables. Only constants and named, or unnamed, placeholders are allowed.</p>
<p>We now give examples of the five different ways of specifying array dimension information in variable declarations. The examples show a global variable declaration using <tt>Declare</tt>, but the same kinds of declarations can also be used for local declarations in functions.</p>
<p>The fifth case is where sizes are specified through integer variables. This is needed to handle declaration and allocation of arrays for which the sizes are not determined until runtime.</p>
<li>Integer constant dimension sizes using the array <tt>arr</tt>:</li>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_118.gif" alt="" width="164" height="12" /></p>
<li>Symbolic constant dimension sizes:</li>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_119.gif" alt="" width="215" height="12" /></p>
<li>Unknown dimension sizes with unnamed placeholders:</li>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_120.gif" alt="" width="164" height="12" /></p>
<li>Unknown dimension sizes with named placeholders:</li>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_121.gif" alt="" width="180" height="12" /></p>
<li>Unknown dimension sizes that are specified and fixed to the values of integer variables, for example, <tt>n</tt>, <tt>m</tt> (e.g., function parameters, local or global variables that are visible from the declaration):</li>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_122.gif" alt="" width="164" height="12" /></p>
<p>Integer variables, such as <tt>n</tt> and <tt>m</tt>, are assumed to be assigned once; that is, their values are not changed after the initial assignment, so that the declared sizes of allocated arrays are kept consistent with the values of those variables. This single-assignment property is not checked by the current version of the system, however. Thus, the user is responsible for maintaining such consistency.</p>
<h3>4. Application Examples</h3>
<h4>4.1. Summary of Examples</h4>
<p>In the following we present a few complete application examples using <em>MathCode</em>. The first example application is a small <em>Mathematica</em> program called <tt>SinSurface</tt> (Section 4.2), which has been designed to illustrate two basic modes of the code generator: compiling without symbolic evaluation (the default mode, in which the function body is translated into C++ as it is), and compilation preceded by symbolic expansion, which is indicated by setting the option <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_79.gif" alt="" width="168" height="12" /> (the function body is expanded using symbolic operations, simplified, and then translated).</p>
<p>The second example, presented in Section 4.3, is an implementation of the Gaussian elimination procedure to solve a linear algebraic system of equations (see any standard text on numerical techniques for a discussion of the procedure, e.g., [<a href="#GerWhe94">2</a>]). Here we compile generated C++ code with various options and do a detailed performance analysis.</p>
<p>In Section 4.4, we discuss the example of SuperLU, an external library [<a href="#SuperLU">3</a>] that performs efficient sparse matrix operations. We give an example of a program useful in solving partial differential equations that calls the SuperLU library and some of its object modules to solve a matrix equation of the type <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_80.gif" alt="" width="40" height="12" />, where <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_81.gif" alt="" width="8" height="12" /> is a very sparse square matrix.</p>
<h4>4.2. The SinSurface Application Example</h4>
<p>Here we describe the <tt>SinSurface</tt> program example. The actual computation is performed by the functions <tt>calcPlot</tt>, <tt>sinFun2</tt>, and their helper functions. The two functions <tt>calcPlot</tt> and <tt>sinFun2</tt> in the <tt>SinSurface</tt> package will be translated into C++ and are declared together with a global array <tt>xyMatrix</tt>.</p>
<p>The array <tt>xyMatrix</tt> represents a <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_82.gif" alt="" width="36" height="12" /> grid on which the numerical function <tt>sinFun2</tt> will be computed. The function <tt>calcPlot</tt> accepts five arguments: four of these are coordinates describing a square in the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_83.gif" alt="" width="6" height="12" />-<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_84.gif" alt="" width="6" height="12" /> plane and one is a counter (<tt>iter</tt>) to make the function repeat the computation as many times as necessary in order to measure execution time. For each point on a <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_85.gif" alt="" width="36" height="12" /> grid, the numeric function <tt>sinFun2</tt> is called to compute a value that is stored as an element in the matrix representing the grid.</p>
<h5>4.2.1. Introduction</h5>
<p>The <tt>SinSurface</tt> example application computes a function (here <tt>sinFun2</tt>) over a two-dimensional grid. The function values are stored in the matrix <tt>xyMatrix</tt>. The execution of compiled C++ code for the function <tt>sinFun2</tt> is over 500 times faster than evaluating the same function interpretively within <em>Mathematica</em>.</p>
<p>The function <tt>sinFun2</tt> computes essentially the same values as <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_86.gif" alt="" width="46" height="12" />, but in a more complicated way, using a rather large expression obtained through converting the arguments into polar coordinates (through <tt>ArcTan</tt>) and then using a series expansion of both <tt>Sin</tt> and <tt>Cos</tt>, up to 10 terms. The resulting large symbolic expression (more than a page) becomes the body of <tt>sinFun2</tt>, and is then used as input to <tt>CompileEvaluateFunction</tt> to generate efficient C++ code. The symbolic expression and the call to <tt>CompileEvaluateFunction</tt> is initiated by using the <tt>EvaluateFunctions</tt> option.</p>
<h5>4.2.2. Initialization</h5>
<p>We first set the directory in which <em>MathCode</em> will store the auxiliary files, the C++ code, and executable, and then load <em>MathCode</em>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_123.gif" alt="" width="129" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_124.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_125.gif" alt="" width="227" height="12" /></p>
<p>The <tt>SinSurface</tt> package starts in the usual way with a <tt>BeginPackage</tt> declaration that references other packages. <tt>MathCodeContexts</tt> is needed in order to call the code generation related functions.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_126.gif" alt="" width="340" height="28" /></p>
<p>Next we define possibly exported symbols. Even though it is not necessary here, we enclose these names within <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_87.gif" alt="" width="143" height="12" />&#8230;  <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_88.gif" alt="" width="36" height="12" /> as a kind of context bracket, since this can be put into a cell, which can be conveniently re-evaluated by itself if new names are added to the list.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_127.gif" alt="" width="143" height="178" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_22.gif" alt="" width="79" height="12" /></p>
<p>Now we set compilation options as follows. This defines how the functions and variables in the package should be compiled to C++. By default, all typed variables and functions are compiled. However, the compilation process can be controlled in a more detailed way by giving compilation options to <tt>CompilePackage</tt> or via <tt>SetCompilationOptions</tt>. For example, in this package the function <tt>sinFun2</tt> should be symbolically evaluated before being translated to code, because it contains symbolic operations; the functions <tt>sin</tt>, <tt>cos</tt>, and <tt>arcTan</tt> should not be compiled at all, because they are expanded within the body of <tt>sinFun2</tt>. The remaining typed function, <tt>calcPlot</tt>, will be compiled in the normal way.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_128.gif" alt="" width="580" height="28" /></p>
<h5>4.2.3. The Body of the SinSurface Package</h5>
<p>We begin the implementation section of the <tt>SinSurface</tt> package, where functions are defined. This is usually private, to avoid accidental name shadowing due to identical local variables in several packages.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_129.gif" alt="" width="208" height="12" /></p>
<p>Declare public global variables and private package-global variables:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_130.gif" alt="" width="214" height="12" /></p>
<p>Taylor-expanded <tt>sin</tt> and <tt>cos</tt> functions called by <tt>sinFun2</tt> are now defined, just for the sake of the example, even though such a series gives lower relative accuracy close to zero. A substitution of the symbol <tt>z</tt> for the actual parameter <tt>x</tt> is necessary to force the series expansion before replacing with the actual parameter.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_131.gif" alt="" width="450" height="28" /></p>
<p>Define <tt>arcTan</tt>, which converts a grid point to an angle, called by <tt>sinFun2</tt>:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_132.gif" alt="" width="580" height="31" /></p>
<p><tt>sinFun2</tt> is the function to be computed and plotted, called by <tt>calcPlot</tt>. It provides a computationally heavy (series expansion) and complicated way of calculating an approximation to <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_89.gif" alt="" width="46" height="12" />. This gives an example of a combination of symbolic and numeric operations as well as a rather standard mix of arithmetic operations. The expanded symbolic expression, which comprises the body of <tt>sinFun2</tt>, is about two pages long when printed.</p>
<p>Note that the types of local variables to <tt>sinFun2</tt> need not be declared, since setting the <tt>EvaluateFunctions</tt> option will make the whole function body be symbolically expanded before translation.</p>
<p>Note also that a function should be without side effects in order to be symbolically expanded before final code generation. For example, there should be no assignments to global variables or input/output, since the relative order of these actions when executing the code often changes when the symbolic expression is created and later rearranged and optimized by the code generator.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_133.gif" alt="" width="237" height="136" /></p>
<p>The function <tt>calcPlot</tt> calculates data for a plot of <tt>sinFun2</tt> over a <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_90.gif" alt="" width="36" height="12" /> grid, which is returned as a <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_91.gif" alt="" width="36" height="12" /> array.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_134.gif" alt="" width="580" height="260" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_135.gif" alt="" width="93" height="28" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_23.gif" alt="" width="137" height="12" /></p>
<h5>4.2.4. Execution</h5>
<p>We first execute the application interpretively within <em>Mathematica</em>, and then use <tt>Compile</tt> on the key function and execute the application again. Then we compile the application to C++, build an executable, and call the same functions from <em>Mathematica</em> via <em>MathLink</em>.</p>
<p>Let us first do the <em>Mathematica</em> evaluation and plot.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_136.gif" alt="" width="403" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_24.gif" alt="" width="43" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_137.gif" alt="" width="115" height="12" /></p>
<div class="OutputScaled">
<p>Next, we redefine <tt>sinFun2</tt> to become a compiled version, using <em>Mathematica</em>&#8217;s standard <tt>Compile</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_138.gif" alt="" width="347" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_139.gif" alt="" width="391" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_25.gif" alt="" width="90" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_140.gif" alt="" width="185" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_26.gif" alt="" width="50" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_141.gif" alt="" width="67" height="12" /></p>
<h5>4.2.5. Using the MathCode Code Generator</h5>
<p>Compile the <tt>SinSurface</tt> package.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_142.gif" alt="" width="201" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Message_143.gif" alt="" width="580" height="56" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_144.gif" alt="" width="580" height="30" /></p>
<p>The warnings concern local variables in <tt>sinFun2</tt> that have no type information. This is not important because those variables disappear upon symbolic expansion.</p>
<p>The command <tt>MakeBinary</tt> compiles the generated code using a compiler (g++ in the present case). The object code is by default linked into the executable SinSurfaceml.exe for calling the compiled code via <em>MathLink</em>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_145.gif" alt="" width="93" height="12" /></p>
<p>If any problems are encountered during code compilation, then warning and error messages are shown. Otherwise no messages are shown. When <tt>MakeBinary</tt> is called without arguments, the call applies to the current package.</p>
<p>The command <tt>InstallCode</tt> installs and connects the external process containing the compiled and linked <tt>SinSurface</tt> code.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_146.gif" alt="" width="179" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_147.gif" alt="" width="580" height="30" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_27.gif" alt="" width="272" height="12" /></p>
<p>Execute the generated C++ code for <tt>calcPlot</tt>.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_148.gif" alt="" width="415" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_28.gif" alt="" width="119" height="12" /></p>
<p>Since the external computation was performed 3000 times, the time needed for one external computation is</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_149.gif" alt="" width="135" height="32" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_29.gif" alt="" width="86" height="12" /></p>
<p>Check that the result appears graphically the same.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_150.gif" alt="" width="115" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_30.gif" alt="" width="288" height="229" /></p>
<h5>4.2.6. Performance Comparison</h5>
<p>Let us now compare the running times for the three cases, the standard <em>Mathematica</em>, compiled <em>Mathematica</em>, and the generated C++ code.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_151.gif" alt="" width="313" height="12" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_31.gif" alt="" width="118" height="12" /></p>
<p>The performance between the three forms of execution are compared in Table 1. The generated C++ code for this example is roughly 100 times faster than standard interpreted <em>Mathematica</em> code, and 50 times faster than code compiled by the internal <em>Mathematica</em> <tt>Compile</tt> command. This is on a Toshiba Satellite-2100, 400 Mhz AMD-K6, running Windows XP Pro SP2 and <em>Mathematica</em> 6, without inline and norange optimization. If the inline is specified, the inline directive is passed to the C++ compiler for all functions to be compiled. If norange is specified, array element index range checking is turned off in the code generated by the C++ compiler, resulting in faster but less safe code.</p>
<p>We should emphasize that the comparisons in Table 1 are rather crude for several reasons. From a separate measurement, the loop part of <tt>calcPlot</tt> excluding the call to <tt>sinFun2</tt> comprises 25% of the total <tt>calcPlot</tt> time executed in interpreted <em>Mathematica</em>. The <tt>calcPlot</tt> function itself cannot be compiled using <tt>Compile</tt>, since it contains an assignment to a global matrix variable that cannot currently be handled by <tt>Compile</tt>. This might be regarded as unfair to <tt>Compile</tt>. On the other hand, a <em>MathLink</em> overhead (divided by 500) in returning the <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_92.gif" alt="" width="36" height="12" /> matrix is embedded in the figure for <em>MathCode</em>, which can be regarded as unfair to <em>MathCode</em>. A better comparison for another small application example is available in Section 4.3.6.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_152.gif" alt="" width="386" height="73" /></p>
<p class="output"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Output_32.gif" alt="" width="348" height="62" /></p>
<div class="NumberedTableCaption"><a name="timing table"></a><strong>Table 1. </strong>Approximate performance comparison for the <tt>calcPlot</tt> example.</div>
<h4>4.3. Gauss Application Example</h4>
<h5>4.3.1. Introduction</h5>
<p>In this section, we present a textbook algorithm, Gaussian elimination (e.g., [<a href="#GerWhe94">2</a>]), to solve a linear equation system. The given linear system, represented by a matrix equation of the type <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_93.gif" alt="" width="40" height="12" />, is subjected to a sequence of transformations involving a pivot, resulting in the solution to the system, contained in the matrix <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_94.gif" alt="" width="8" height="12" />.</p>
<p>The following subsections illustrate the various aspects of the application.</p>
<h5>4.3.2. Initialization</h5>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_153.gif" alt="" width="129" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Print_154.gif" alt="" width="580" height="30" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_155.gif" alt="" width="580" height="12" /></p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_156.gif" alt="" width="304" height="12" /></p>
<p>Define exported symbols:</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_157.gif" alt="" width="151" height="43" /></p>
<h5>4.3.3. Body of the Package</h5>
<p>We now define the function <tt>GaussSolveArraySlice</tt>, based on the Gaussian elimination algorithm.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_158.gif" alt="" width="580" height="748" /></p>
<p>This function accepts three arguments in an attempt to solve a matrix equation of the form <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_95.gif" alt="" width="40" height="12" />. The first two arguments are essentially the matrices <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_96.gif" alt="" width="8" height="12" /> and <img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_97.gif" alt="" width="7" height="12" />. The third argument specifies the number of times the body of the function must run; this is useful for an accurate measurement of the running time. The function output (<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_98.gif" alt="" width="8" height="12" />) has the same shape as the second argument (<img style="vertical-align: middle;" src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Math_99.gif" alt="" width="7" height="12" />).</p>
<h5>4.3.4. Mathematica Execution</h5>
<p>Let us create two random matrices.</p>
<p class="input"><img src="http://www.mathematica-journal.com/data/uploads/2011/12/MathCode_Input_159.gif" alt="" width="218" height="2
