{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Create a multivariate basis of functions from scalar multivariable functions\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Description\n\nWe want to build a finite basis of functions $\\mathcal{B}=\\{f_1,...,f_n\\}$ where each\n$f_i: \\mathbb{R}^d \\mapsto \\mathbb{R}^q$ with $d>1, q>1$, from given scalar multivariable\nfunctions $g_i: \\mathbb{R}^d \\mapsto \\mathbb{R}$.\n\nIn other words, we want to create a basis of functions having both a multivariate input *and* output from\nfunctions having a multivariate input and a scalar output.\n\nIn this example, we do that by the aggregation of the given functions $g_i$,\nusing the :class:`~openturns.AggregatedFunction` class. This class allows one to create a multivariate output\nfunction from a list of functions having a univariate output dimension. Note that we can only aggregate functions\nthat share the same input space.\n\nWe illustrate two cases that differ from the way the functions $g_i$ have been created:\n\n- Case 1: we suppose that the functions $g_i$ have no particular origin,\n- Case 2: we suppose that the functions $g_i$ have been built from particular bases.\n\nIn both cases, we use the :class:`~openturns.Basis` class to create the basis and the\n:class:`~openturns.AggregatedFunction` class to create a multivariate output function from a\nlist of scalar multivariable functions.\n\n## Case 1: Aggregation of given functions\n\nIn that case, we have some functions $g_i: \\mathbb{R}^d \\mapsto \\mathbb{R}$ and we aggregate them\nin order to build the basis $\\mathcal{B}$.\n\nFor example, we set $d=2$, $q=3$ and we use the functions $(g_1, g_2, g_3)$ defined by:\n\n\\begin{align}\\begin{array}{lcl}\n g_1(x_1, x_2) & = & 1\\\\\n g_2(x_1, x_2) & = & x_1 + x_2\\\\\n g_3(x_1, x_2) & = & x_1^2 + x_2^2\n \\end{array}\\end{align}\n\nand we want to build the finite basis $\\mathcal{B}=\\{f_1,f_2\\}$ defined by:\n\n\\begin{align}\\begin{array}{lcl}\n f_1(x_1, x_2) & = & (1, 1, 1)\\\\\n f_2(x_1, x_2) & = & (1, x_1 + x_2, x_1^2 + x_2^2)\n \\end{array}\\end{align}\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import openturns as ot\nd = 2\nq = 3\n\n# Create the functions g_i\ng_1 = ot.SymbolicFunction(['x1', 'x2'], ['1'])\ng_2 = ot.SymbolicFunction(['x1', 'x2'], ['x1+x2'])\ng_3 = ot.SymbolicFunction(['x1', 'x2'], ['x1^2+x2^2'])\n\n# Create the multivariate basis\nf_1 = ot.AggregatedFunction([g_1] * q)\nf_2 = ot.AggregatedFunction([g_1, g_2, g_3])\nbasis = ot.Basis([f_1, f_2])\nprint('Fonction f_1 : ', basis.build(0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Case 2: Aggregation of functions built from given bases\n\nIn that case, we use some functions $g_i: \\mathbb{R}^d \\mapsto \\mathbb{R}$ that have been\nbuilt from particular bases. For example, they are members of polynomial families which are orthonormal\nto given distributions.\n\nIn this example, we use both polynomial families:\n\n- $(g_1^i)_{i\\geq 0}$ which is a orthonormal multivariate polynomial basis with respect to the uniform\n distributipn on $[-1,1]^d$ (it is obtained through a tensor product of univariate Legendre polynomials\n orthonormal with respect to the uniform distribution on $[-1,1]$),\n- $(g_2^i)_{i\\geq 0}$ which is a orthonormal multivariate polynomial basis with respect to the multivariate\n standard normal distribution (it is obtained through a tensor product of univariate Hermite polynomials\n orthonormal with respect to the standard normal univariate distribution).\n\nWe consider the linear\nenumeration of the polynomial family. The functions $f_i$ are built as the aggregation of\nthe $i$-th polynomials of the families $(g_1^k)_k$ and $(g_2^k)_k$.\nWe only consider $g_1^i$ and $g_2^i$ of total degree less than 2:\nthen we have 6 polynomials $f_i$.\n\nWe still set $d=2$, $q=3$.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"d = 2\n\nlegendre_builder = ot.OrthogonalProductPolynomialFactory([ot.LegendreFactory()] * d)\nhermite_builder = ot.OrthogonalProductPolynomialFactory([ot.HermiteFactory()] * d)\n\n# Generate the multivariate functions f_i (quadratic)\ntotal_deg = 2\nbasis_size = ot.LinearEnumerateFunction(d).getStrataCumulatedCardinal(total_deg)\nbasis = []\nfor i in range(basis_size):\n f_i = ot.AggregatedFunction([legendre_builder.build(i), hermite_builder.build(i)])\n basis.append(f_i)\n\nbasis = ot.Basis(basis)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}