.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/ex02_numeric_variation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_ex02_numeric_variation.py: Numeric calculus of variation ----------------------------- Each Tensor has four attributes: the (real) tensor array and the (hyper-dual) variational arrays. To obtain the :math:`12` - component of the gradient and the :math:`1223` - component of the hessian, a tensor has to be created with the appropriate small-changes of the tensor components (dual arrays). .. GENERATED FROM PYTHON SOURCE LINES 9-37 .. code-block:: Python import numpy as np import tensortrax as tr from tensortrax import Tensor, Δ, Δδ, f, δ from tensortrax.math import trace δF_12 = np.array( [ [0, 1, 0], [0, 0, 0], [0, 0, 0], ], dtype=float, ) ΔF_23 = np.array( [ [0, 0, 0], [0, 0, 1], [0, 0, 0], ], dtype=float, ) x = np.eye(3) + np.arange(9).reshape(3, 3) / 10 F = Tensor(x=x, δx=δF_12, Δx=ΔF_23, Δδx=None) I1_C = trace(F.T @ F) .. GENERATED FROM PYTHON SOURCE LINES 38-40 The function as well as the gradient and hessian components are accessible with helpers. .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python ψ = f(I1_C) P_12 = δ(I1_C) A_1223 = Δδ(I1_C) .. GENERATED FROM PYTHON SOURCE LINES 45-47 To obtain full gradients and hessians of scalar-valued functions in one function call, ``tensortrax`` provides helpers (decorators) which handle the multiple function calls. .. GENERATED FROM PYTHON SOURCE LINES 47-53 .. code-block:: Python fun = lambda F: trace(F.T @ F) func = tr.function(fun)(x) grad = tr.gradient(fun)(x) hess = tr.hessian(fun)(x) .. GENERATED FROM PYTHON SOURCE LINES 54-55 For tensor-valued functions, use ``jacobian()`` instead of ``gradient()``. .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: Python fun = lambda F: F.T @ F jac = tr.jacobian(fun)(x) .. GENERATED FROM PYTHON SOURCE LINES 59-60 Evaluate the gradient- as well as the hessian-vector(s)-product. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python gvp = tr.gradient_vector_product(fun)(x, δx=x) hvp = tr.hessian_vector_product(fun)(x, δx=x) hvsp = tr.hessian_vectors_product(fun)(x, δx=x, Δx=x) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.003 seconds) .. _sphx_glr_download_examples_ex02_numeric_variation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ex02_numeric_variation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ex02_numeric_variation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ex02_numeric_variation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_