------------------------------------------------ lines 644-900 of file: introduction/exp_eps.xrst ------------------------------------------------ {xrst_begin exp_eps_for2} exp_eps: Second Order Forward Mode ################################## Second Order Expansion ********************** We define :math:`x(t)` and :math:`\varepsilon(t) ]` near :math:`t = 0` by the second order expansions .. math:: :nowrap: \begin{eqnarray} x(t) & = & x^{(0)} + x^{(1)} * t + x^{(2)} * t^2 / 2 \\ \varepsilon(t) & = & \varepsilon^{(0)} + \varepsilon^{(1)} * t + \varepsilon^{(2)} * t^2 / 2 \end{eqnarray} It follows that for :math:`k = 0 , 1 , 2`, .. math:: :nowrap: \begin{eqnarray} x^{(k)} & = & \dpow{k}{t} x (0) \\ \varepsilon^{(k)} & = & \dpow{k}{t} \varepsilon (0) \end{eqnarray} Purpose ******* In general, a second order forward sweep is given the :ref:`exp_2_for1@First Order Expansion` for all of the variables in an operation sequence, and the second order derivatives for the independent variables. It uses these to compute the second order derivative, and thereby obtain the second order expansion, for all the variables in the operation sequence. Mathematical Form ***************** Suppose that we use the algorithm :ref:`exp_eps.hpp-name` to compute ``exp_eps`` ( *x* , *epsilon* ) with *x* is equal to .5 and *epsilon* is equal to .2. For this case, the mathematical function for the operation sequence corresponding to ``exp_eps`` is .. math:: f ( x , \varepsilon ) = 1 + x + x^2 / 2 The corresponding second partial derivative with respect to :math:`x`, and the value of the derivative, are .. math:: \Dpow{2}{x} f ( x , \varepsilon ) = 1. Operation Sequence ****************** Index ===== The Index column contains the index in the operation sequence of the corresponding atomic operation. A Forward sweep starts with the first operation and ends with the last. Zero ==== The Zero column contains the zero order sweep results for the corresponding variable in the operation sequence (see :ref:`zero order sweep` ). Operation ========= The Operation column contains the first order sweep operation for this variable. First ===== The First column contains the first order sweep results for the corresponding variable in the operation sequence (see :ref:`first order sweep` ). Derivative ========== The Derivative column contains the mathematical function corresponding to the second derivative with respect to :math:`t`, at :math:`t = 0`, for each variable in the sequence. Second ====== The Second column contains the second order derivatives for the corresponding variable in the operation sequence; i.e., the second order expansion for the *i*-th variable is given by .. math:: v_i (t) = v_i^{(0)} + v_i^{(1)} * t + v_i^{(2)} * t^2 / 2 We use :math:`x^{(1)} = 1`, :math:`x^{(2)} = 0`, use :math:`\varepsilon^{(1)} = 1`, and :math:`\varepsilon^{(2)} = 0` so that second order differentiation with respect to :math:`t`, at :math:`t = 0`, is the same as the second partial differentiation with respect to :math:`x` at :math:`x = x^{(0)}`. Sweep ===== .. list-table:: :widths: auto * - **Index** - - **Zero** - - **Operation** - - **First** - - **Derivative** - - **Second** * - 1 - - 0.5 - - :math:`v_1^{(1)} = x^{(1)}` - - 1 - - :math:`v_2^{(2)} = x^{(2)}` - - 0 * - 2 - - 0.5 - - :math:`v_2^{(1)} = 1 * v_1^{(1)}` - - 1 - - :math:`v_2^{(2)} = 1 * v_1^{(2)}` - - 0 * - 3 - - 0.5 - - :math:`v_3^{(1)} = v_2^{(1)} / 1` - - 1 - - :math:`v_3^{(2)} = v_2^{(2)} / 1` - - 0 * - 4 - - 1.5 - - :math:`v_4^{(1)} = v_3^{(1)}` - - 1 - - :math:`v_4^{(2)} = v_3^{(2)}` - - 0 * - 5 - - 0.25 - - :math:`v_5^{(1)} = v_3^{(1)} * v_1^{(0)} + v_3^{(0)} * v_1^{(1)}` - - 1 - - :math:`v_5^{(2)} = v_3^{(2)} * v_1^{(0)} + 2 * v_3^{(1)} * v_1^{(1)}` :math:`+ v_3^{(0)} * v_1^{(2)}` - - 2 * - 6 - - 0.125 - - :math:`v_6^{(1)} = v_5^{(1)} / 2` - - 0.5 - - :math:`v_6^{(2)} = v_5^{(2)} / 2` - - 1 * - 7 - - 1.625 - - :math:`v_7^{(1)} = v_4^{(1)} + v_6^{(1)}` - - 1.5 - - :math:`v_7^{(2)} = v_4^{(2)} + v_6^{(2)}` - - 1 Return Value ************ The second derivative of the return value for this case is .. math:: :nowrap: \begin{eqnarray} 1 & = & v_7^{(2)} = \left[ \Dpow{2}{t} v_7 \right]_{t=0} = \left[ \Dpow{2}{t} f( x^{(0)} + x^{(1)} * t , \varepsilon^{(0)} ) \right]_{t=0} \\ & = & x^{(1)} * \Dpow{2}{x} f ( x^{(0)} , \varepsilon^{(0)} ) * x^{(1)} = \Dpow{2}{x} f ( x^{(0)} , \varepsilon^{(0)} ) \end{eqnarray} (We have used the fact that :math:`x^{(1)} = 1`, :math:`x^{(2)} = 0`, :math:`\varepsilon^{(1)} = 1`, and :math:`\varepsilon^{(2)} = 0`.) {xrst_toc_hidden introduction/exp_eps_for2.cpp } Verification ************ The file :ref:`exp_eps_for2.cpp-name` contains a routine which verifies the values computed above. Exercises ********* #. Which statement in the routine defined by :ref:`exp_eps_for2.cpp-name` uses the values that are calculated by the routine defined by :ref:`exp_eps_for1.cpp-name` ? #. Suppose that :math:`x = .1`, what are the results of a zero, first, and second order forward sweep for the operation sequence above; i.e., what are the corresponding values for :math:`v_i^{(k)}` for :math:`i = 1, \ldots , 7` and :math:`k = 0, 1, 2`. #. Create a modified version of :ref:`exp_eps_for2.cpp-name` that verifies the derivative values from the previous exercise. Also create and run a main program that reports the result of calling the modified version of :ref:`exp_eps_for2.cpp-name` . {xrst_end exp_eps_for2}