.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/disciplines/basics/plot_compute_jacobian.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_disciplines_basics_plot_compute_jacobian.py: Compute the Jacobian of a discipline analytically ================================================= In this example, we will compute the Jacobians of some outputs of an :class:`.Discipline` with respect to some inputs, based on its analytical derivatives. .. GENERATED FROM PYTHON SOURCE LINES 29-36 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo.disciplines.analytic import AnalyticDiscipline .. GENERATED FROM PYTHON SOURCE LINES 37-39 First, we create a discipline, e.g. an :class:`.AnalyticDiscipline`: .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python discipline = AnalyticDiscipline({"y": "a**2+b", "z": "a**3+b**2"}) .. GENERATED FROM PYTHON SOURCE LINES 42-43 We can execute it with its default input values: .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python discipline.execute() discipline.local_data .. rst-class:: sphx-glr-script-out .. code-block:: none {'a': array([0.]), 'b': array([0.]), 'y': array([0.]), 'z': array([0.])} .. GENERATED FROM PYTHON SOURCE LINES 47-48 or with custom ones: .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: Python discipline.execute({"a": array([1.0])}) discipline.local_data .. rst-class:: sphx-glr-script-out .. code-block:: none {'a': array([1.]), 'b': array([0.]), 'y': array([1.]), 'z': array([1.])} .. GENERATED FROM PYTHON SOURCE LINES 52-54 Then, we use the method :meth:`.Discipline.linearize` to compute the derivatives: .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python jacobian_data = discipline.linearize() jacobian_data .. rst-class:: sphx-glr-script-out .. code-block:: none {} .. GENERATED FROM PYTHON SOURCE LINES 58-67 There is no Jacobian data because we need to set the input variables with respect to which to compute the Jacobian of the output ones. For that, we use the method :meth:`~.Discipline.add_differentiated_inputs`. We also need to set these output variables: with the method :meth:`~.Discipline.add_differentiated_outputs`. For instance, we may want to only compute the derivative of ``"z"`` with respect to ``"a"``: .. GENERATED FROM PYTHON SOURCE LINES 67-72 .. code-block:: Python discipline.add_differentiated_inputs(["a"]) discipline.add_differentiated_outputs(["z"]) jacobian_data = discipline.linearize() jacobian_data .. rst-class:: sphx-glr-script-out .. code-block:: none defaultdict(, {'z': defaultdict(None, {'a': array([[0.]])})}) .. GENERATED FROM PYTHON SOURCE LINES 73-77 By default, |g| uses :attr:`.Discipline.default_input_data` as input data for which to compute the Jacobian on. We can change them with ``input_data``: .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python jacobian_data = discipline.linearize({"a": array([1.0])}) jacobian_data .. rst-class:: sphx-glr-script-out .. code-block:: none defaultdict(, {'z': defaultdict(None, {'a': array([[3.]])})}) .. GENERATED FROM PYTHON SOURCE LINES 81-83 We can also force the discipline to compute the derivatives of all the outputs with respect to all the inputs: .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python jacobian_data = discipline.linearize(compute_all_jacobians=True) jacobian_data .. rst-class:: sphx-glr-script-out .. code-block:: none defaultdict(, {'y': defaultdict(None, {'a': array([[0.]]), 'b': array([[1.]])}), 'z': defaultdict(None, {'a': array([[0.]]), 'b': array([[0.]])})}) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.013 seconds) .. _sphx_glr_download_examples_disciplines_basics_plot_compute_jacobian.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_compute_jacobian.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_compute_jacobian.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_compute_jacobian.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_