.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_functional_modeling/univariate_functions/plot_createUnivariateFunction.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_auto_functional_modeling_univariate_functions_plot_createUnivariateFunction.py: =========================== Create univariate functions =========================== This example presents different ways to create univariate functions which can be used to create a functional basis. This is the type of functions involved, for example, in the :class:`~openturns.FunctionalChaosAlgorithm` class. The univariate functions considered in this example can be, in some cases, orthogonal to a distribution, but this is not a necessary condition to create univariate functions. For example, Legendre polynomials are orthogonal with respect to the uniform distribution, but the monomials of the canonical polynomial basis are not necessarily orthogonal. .. GENERATED FROM PYTHON SOURCE LINES 16-37 Description =========== We want to build univariate functions :math:`f : \mathbb{R} \mapsto \mathbb{R}`. We can do that: - Case 1: using orthogonal polynomials, - Case 2: using univariate polynomials, - Case 3: using orthogonal functions. Case 1: Orthogonal polynomials ============================== In that case, the polynomials are orthogonal with respect to a measure. For example: we consider the Legendre polynomials family, orthogonal with respect to the uniform distribution on :math:`[0,1]`. We use the :class:`~openturns.LegendreFactory` class. Its method :meth:`~openturns.LegendreFactory.build` applied to :math:`k` returns the polynomial number :math:`k` of the family. .. GENERATED FROM PYTHON SOURCE LINES 37-55 .. code-block:: Python import openturns as ot import openturns.viewer as otv f1 = ot.LegendreFactory().build(1) f2 = ot.LegendreFactory().build(2) f3 = ot.LegendreFactory().build(3) print(type(f1)) g = f1.draw(-1.0, 1.0, 256) g.add(f2.draw(-1.0, 1.0, 256)) g.add(f3.draw(-1.0, 1.0, 256)) g.setLegends([r"$\phi_1(x)$", r"$\phi_2(x)$", r"$\phi_3(x)$"]) g.setLegendPosition("lower right") g.setColors(ot.Drawable.BuildDefaultPalette(3)) g.setTitle("Legendre Polynomials") view = otv.View(g) .. image-sg:: /auto_functional_modeling/univariate_functions/images/sphx_glr_plot_createUnivariateFunction_001.png :alt: Legendre Polynomials :srcset: /auto_functional_modeling/univariate_functions/images/sphx_glr_plot_createUnivariateFunction_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 56-57 We get the measure associated to the polynomial family: .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: Python measure_Legendre = ot.LegendreFactory().getMeasure() print("Measure orthogonal to Legendre polynomials = ", measure_Legendre) .. rst-class:: sphx-glr-script-out .. code-block:: none Measure orthogonal to Legendre polynomials = Uniform(a = -1, b = 1) .. GENERATED FROM PYTHON SOURCE LINES 61-80 Case 2: Univariate polynomials ============================== Univariate polynomials are not necessarily orthogonal with respect to a measure. We can use: - the :class:`~openturns.UniVariatePolynomial` class, - the :class:`~openturns.MonomialFunctionFactory` class, - the :class:`~openturns.MonomialFunction` class. For example, we consider : .. math:: \begin{array}{lcl} f(x) & = & 1+2x+3x^2+4x^3\\ g(x) & = & x^3 \end{array} .. GENERATED FROM PYTHON SOURCE LINES 80-88 .. code-block:: Python f = ot.UniVariatePolynomial([1.0, 2.0, 3.0, 4.0]) g1 = ot.MonomialFunctionFactory().build(3) g2 = ot.MonomialFunction(3) print("f = ", f) print("g1 = ", g1) print("g2 = ", g2) .. rst-class:: sphx-glr-script-out .. code-block:: none f = 1 + 2 * X + 3 * X^2 + 4 * X^3 g1 = [x] --> x^3 g2 = [x] --> x^3 .. GENERATED FROM PYTHON SOURCE LINES 89-90 There is no associated measure: if it is uncommented, the following command will fail, as expected. .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: Python # print(ot.MonomialFunctionFactory().getMeasure()) .. GENERATED FROM PYTHON SOURCE LINES 94-107 Case 3: Orthogonal functions ============================ In that case, the functions are orthogonal with respect to a measure :math:`\mu`. We can use: - the :class:`~openturns.HaarWaveletFactory` class, - the :class:`~openturns.FourierSeriesFactory` class. The method :meth:`~openturns.HaarWaveletFactory.build` returns the function number :math:`k` of the family. For example, we consider a Haar Wawelet. .. GENERATED FROM PYTHON SOURCE LINES 107-120 .. code-block:: Python f1 = ot.HaarWaveletFactory().build(1) f2 = ot.HaarWaveletFactory().build(2) f3 = ot.HaarWaveletFactory().build(3) g = f1.draw(0.0, 1.0, 256) g.add(f2.draw(0.0, 1.0, 256)) g.add(f3.draw(0.0, 1.0, 256)) g.setLegends([r"$\phi_1(x)$", r"$\phi_21(x)$", r"$\phi_3(x)$"]) g.setLegendPosition("upper right") g.setColors(ot.Drawable.BuildDefaultPalette(3)) g.setTitle("Haar Wavelets") view = otv.View(g) .. image-sg:: /auto_functional_modeling/univariate_functions/images/sphx_glr_plot_createUnivariateFunction_002.png :alt: Haar Wavelets :srcset: /auto_functional_modeling/univariate_functions/images/sphx_glr_plot_createUnivariateFunction_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 121-122 We get the measure: for the Haar Wavelet family, the :math:`\mathcal{U}(0,1)` distribution. .. GENERATED FROM PYTHON SOURCE LINES 122-125 .. code-block:: Python measure_Haar = ot.HaarWaveletFactory().getMeasure() print("Measure orthogonal to Haar wavelets = ", measure_Haar) .. rst-class:: sphx-glr-script-out .. code-block:: none Measure orthogonal to Haar wavelets = Uniform(a = 0, b = 1) .. GENERATED FROM PYTHON SOURCE LINES 126-127 For example, we consider a Fourier Series. .. GENERATED FROM PYTHON SOURCE LINES 127-140 .. code-block:: Python f1 = ot.FourierSeriesFactory().build(1) f2 = ot.FourierSeriesFactory().build(2) f3 = ot.FourierSeriesFactory().build(3) g = f1.draw(-3.0, 3.0, 256) g.add(f2.draw(-3.0, 3.0, 256)) g.add(f3.draw(-3.0, 3.0, 256)) g.setLegends([r"$\phi_1(x)$", r"$\phi_21(x)$", r"$\phi_3(x)$"]) g.setLegendPosition("upper right") g.setColors(ot.Drawable.BuildDefaultPalette(3)) g.setTitle("Fourier Series") view = otv.View(g) .. image-sg:: /auto_functional_modeling/univariate_functions/images/sphx_glr_plot_createUnivariateFunction_003.png :alt: Fourier Series :srcset: /auto_functional_modeling/univariate_functions/images/sphx_glr_plot_createUnivariateFunction_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 141-142 We get the measure: for the Fourier Series, the :math:`\mathcal{U}(-\pi, \pi)` distribution. .. GENERATED FROM PYTHON SOURCE LINES 142-146 .. code-block:: Python measure_Fourier = ot.FourierSeriesFactory().getMeasure() print("Measure orthogonal to Fourier series = ", measure_Fourier) .. rst-class:: sphx-glr-script-out .. code-block:: none Measure orthogonal to Fourier series = Uniform(a = -3.14159, b = 3.14159) .. GENERATED FROM PYTHON SOURCE LINES 147-148 .. code-block:: Python otv.View.ShowAll() .. _sphx_glr_download_auto_functional_modeling_univariate_functions_plot_createUnivariateFunction.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_createUnivariateFunction.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_createUnivariateFunction.py `