{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Function manipulation\n", "\n", "In this example we are going to exhibit some of the generic function services such as:\n", "\n", "- to ask the dimension of its input and output vectors\n", "- to evaluate itself, its gradient and hessian\n", "- to set a finite difference method for the evaluation of the gradient or the hessian\n", "- to evaluate the number of times the function or its gradient or its hessian have been evaluated\n", "- to disable or enable (enabled by default) the history mechanism\n", "- to disable or enable the cache mechanism\n", "- to get all the values evaluated by the function and the associated inputs with the methods\n", "- to clear the history\n", "- to ask the description of its input and output vectors\n", "- to extract output components\n", "- to get a graphical representation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import print_function\n", "import openturns as ot\n", "import math as m" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create a vectorial function R ^n --> R^p\n", "# for example R^2 --> R^2\n", "f = ot.SymbolicFunction(['x1', 'x2'], ['1+2*x1+x2', '2+x1+2*x2'])\n", "\n", "# Create a scalar function R --> R\n", "func1 = ot.SymbolicFunction(['x'], ['x^2'])\n", "\n", "# Create another function R^2 --> R\n", "func2 = ot.SymbolicFunction(['x', 'y'], ['x*y'])\n", "\n", "# Create a vectorial function R ^3 --> R^2\n", "func3 = ot.SymbolicFunction(['x1', 'x2', 'x3'], ['1+2*x1+x2+x3^3', '2+sin(x1+2*x2)-sin(x3) * x3^4'])\n", "\n", "# Create a second vectorial function R ^n --> R^p\n", "# for example R^2 --> R^2\n", "g = ot.SymbolicFunction(['x1', 'x2'], ['x1+x2', 'x1^2+2*x2^2'])\n", "\n", "\n", "def python_eval(X):\n", " a, b = X\n", " y = a+b\n", " return [y]\n", "func4 = ot.PythonFunction(2, 1, python_eval)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "2\n" ] } ], "source": [ "# Ask for the dimension of the input and output vectors\n", "print(f.getInputDimension())\n", "print(f.getOutputDimension())" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Enable the history mechanism\n", "f = ot.MemoizeFunction(f)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x= [1.0, 1.0] y= [4,5]\n" ] } ], "source": [ "# Evaluate the function at a particular point\n", "x = [1.0] * f.getInputDimension()\n", "y = f(x)\n", "print('x=', x, 'y=', y)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "evaluation history = 0 : [ 1 1 ] 0 : [ 4 5 ]\n" ] } ], "source": [ "# Get the history\n", "samplex = f.getInputHistory()\n", "sampley = f.getOutputHistory()\n", "print('evaluation history = ', samplex, sampley)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Clear the history mechanism\n", "f.clearHistory()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Disable the history mechanism\n", "f.disableHistory()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Enable the cache mecanism\n", "f4 = ot.MemoizeFunction(func4)\n", "f4.enableCache()\n", "for i in range(10):\n", " f4(x)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the number of times cached values are reused\n", "f4.getCacheHits()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
[[ 2 1 ]
\n",
" [ 1 2 ]]
\n",
"
sheet #0
\n",
"[[ 0 0 ]
\n",
" [ 0 0 ]]
\n",
"sheet #1
\n",
"[[ 0 0 ]
\n",
" [ 0 0 ]]
\n",
"
NonCenteredFiniteDifferenceGradient epsilon : [1e-07,1e-07]
\n",
"
CenteredFiniteDifferenceHessian epsilon : [1e-07,1e-07]
\n",
"
[x1,x2]->[2+x1+2*x2]
" ], "text/plain": [ "class=Function name=Unnamed implementation=class=FunctionImplementation name=Unnamed description=[x1,x2,y1] evaluationImplementation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y1] formulas=[2+x1+2*x2] gradientImplementation=class=ComposedGradient name=Unnamed leftGradient=class=ConstantGradient name=Unnamed constant=class=Matrix implementation=class=MatrixImplementation name=Unnamed rows=2 columns=1 values=[0,1] rightFunction=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x0,x1] outputVariablesNames=[y0,y1] formulas=[0.0,0.0] rightGradient=class=NonCenteredFiniteDifferenceGradient name=Unnamed epsilon=class=Point name=Unnamed dimension=2 values=[1e-07,1e-07] evaluation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y0,y1] formulas=[1+2*x1+x2,2+x1+2*x2] hessianImplementation=class=ComposedHessian name=Unnamed leftGradient=class=ConstantGradient name=Unnamed constant=class=Matrix implementation=class=MatrixImplementation name=Unnamed rows=2 columns=1 values=[0,1] leftHessian=class=ConstantHessian name=Unnamed constant=class=SymmetricTensor implementation=class=TensorImplementation name=Unnamed rows=2 columns=2 sheets=1 values=[0,0,0,0] rightFunction=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x0,x1] outputVariablesNames=[y0,y1] formulas=[0.0,0.0] rightGradient=class=ConstantGradient name=Unnamed constant=class=Matrix implementation=class=MatrixImplementation name=Unnamed rows=2 columns=2 values=[0,0,0,0] rightHessian=class=CenteredFiniteDifferenceHessian name=Unnamed epsilon=class=Point name=Unnamed dimension=2 values=[1e-07,1e-07] evaluation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y0,y1] formulas=[1+2*x1+x2,2+x1+2*x2]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the component i\n", "f.getMarginal(1)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "([x1,x2]->[1+2*x1+x2,2+x1+2*x2])o([x1,x2]->[x1+x2,x1^2+2*x2^2])
" ], "text/plain": [ "class=ComposedFunction name=Unnamed description=[x1,x2,y0,y1] left function=class=FunctionImplementation name=Unnamed description=[x1,x2,y0,y1] evaluationImplementation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y0,y1] formulas=[1+2*x1+x2,2+x1+2*x2] gradientImplementation=class=NonCenteredFiniteDifferenceGradient name=Unnamed epsilon=class=Point name=Unnamed dimension=2 values=[1e-07,1e-07] evaluation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y0,y1] formulas=[1+2*x1+x2,2+x1+2*x2] hessianImplementation=class=CenteredFiniteDifferenceHessian name=Unnamed epsilon=class=Point name=Unnamed dimension=2 values=[1e-07,1e-07] evaluation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y0,y1] formulas=[1+2*x1+x2,2+x1+2*x2] right function=class=FunctionImplementation name=Unnamed description=[x1,x2,y0,y1] evaluationImplementation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y0,y1] formulas=[x1+x2,x1^2+2*x2^2] gradientImplementation=class=SymbolicGradient name=Unnamed evaluation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y0,y1] formulas=[x1+x2,x1^2+2*x2^2] hessianImplementation=class=SymbolicHessian name=Unnamed evaluation=class=SymbolicEvaluation name=Unnamed inputVariablesNames=[x1,x2] outputVariablesNames=[y0,y1] formulas=[x1+x2,x1^2+2*x2^2]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Compose two functions : h = f o g\n", "ot.ComposedFunction(f, g)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "[e_ -> Euler's constant (2.71828...),pi_ -> Pi constant (3.14159...)]
\n",
"