1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
<a name="Module:Scientific.Functions.FirstDerivatives"><h1>Module Scientific.Functions.FirstDerivatives</h1></a>
<p>This module provides automatic differentiation for functions with
any number of variables. Instances of the class DerivVar represent the
values of a function and its partial derivatives with respect to a
list of variables. All common mathematical operations and functions
are available for these numbers. There is no restriction on the type
of the numbers fed into the code; it works for real and complex
numbers as well as for any Python type that implements the necessary
operations.</p>
<p>This module is as far as possible compatible with the n-th order
derivatives module Derivatives. If only first-order derivatives
are required, this module is faster than the general one.</p>
Example:
<pre>
print sin(DerivVar(2))
</pre>
<p> produces the output</p>
<pre>
(0.909297426826, [-0.416146836547])
</pre>
<p>The first number is the value of sin(2); the number in the following
list is the value of the derivative of sin(x) at x=2, i.e. cos(2).</p>
<p>When there is more than one variable, DerivVar must be called with
an integer second argument that specifies the number of the variable.</p>
Example:
<pre>
x = DerivVar(7., 0)
y = DerivVar(42., 1)
z = DerivVar(pi, 2)
print (sqrt(pow(x,2)+pow(y,2)+pow(z,2)))
</pre>
<p> produces the output</p>
<pre>
(42.6950770511, [0.163953328662, 0.98371997197, 0.0735820818365])
</pre>
<p>The numbers in the list are the partial derivatives with respect
to x, y, and z, respectively.</p>
Note: It doesn't make sense to use DerivVar with different values
for the same variable index in one calculation, but there is
no check for this. I.e.
<pre>
print DerivVar(3, 0)+DerivVar(5, 0)
</pre>
<p> produces</p>
<pre>
(8, [2])
</pre>
<p>but this result is meaningless.
</p>
<hr width=70%>
<h2>Functions</h2>
<ul>
<li> <p>
<a name="Function:Scientific.Functions.FirstDerivatives.isDerivVar"><b><i>isDerivVar</i></b>(<i>x</i>)</a><br>
</p>
<p>Returns 1 if <i>x</i> is a DerivVar object.</p><li> <p>
<a name="Function:Scientific.Functions.FirstDerivatives.DerivVector"><b><i>DerivVector</i></b>(<i>x</i>, <i>y</i>, <i>z</i>, <i>index</i>=<tt>0</tt>)</a><br>
</p>
<p>Returns a vector whose components are DerivVar objects.</p>
<p><dl>
<dt><i>x</i>, <i>y</i>, <i>z</i></dt>
<dd><p>
vector components (numbers)</p></dd>
<dt><i>index</i></dt>
<dd><p>
the DerivVar index for the x component. The y and z
components receive consecutive indices.
</p></dd>
</dl>
</p></ul>
<hr width=70%>
<a name="Class:Scientific.Functions.FirstDerivatives.DerivVar"><h2>Class DerivVar: Variable with derivatives</h2></a>
<p>Constructor: DerivVar(<i>value</i>, <i>index</i> = 0)</p>
<p><dl>
<dt><i>value</i></dt>
<dd><p>
the numerical value of the variable</p></dd>
<dt><i>index</i></dt>
<dd><p>
the variable index (an integer), which serves to
distinguish between variables and as an index for
the derivative lists. Each explicitly created
instance of DerivVar must have a unique index.</p></dd>
</dl>
</p>
<p>Indexing with an integer yields the derivatives of the corresponding
order.
</p>
|