1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#! /usr/bin/env python
from __future__ import print_function
from openturns import *
TESTPREAMBLE()
class FUNC(OpenTURNSPythonFunction):
def __init__(self):
super(FUNC, self).__init__(2, 1)
self.setInputDescription(['R', 'S'])
self.setOutputDescription(['T'])
def _exec(self, X):
Y = [X[0] + X[1]]
return Y
F = FUNC()
print(('in_dim=' + str(F.getInputDimension())
+ ' out_dim=' + str(F.getOutputDimension())))
print((F((10, 5))))
print((F(((10, 5), (6, 7)))))
# Instance creation
myFunc = NumericalMathFunction(F)
# Copy constructor
newFunc = NumericalMathFunction(myFunc)
print(('myFunc input dimension= ' + str(myFunc.getInputDimension())))
print(('myFunc output dimension= ' + str(myFunc.getOutputDimension())))
inPt = NumericalPoint(2, 2.)
print((repr(inPt)))
outPt = myFunc(inPt)
print((repr(outPt)))
outPt = myFunc((10., 11.))
print((repr(outPt)))
inSample = NumericalSample(10, 2)
for i in range(10):
inSample[i] = NumericalPoint((i, i))
print((repr(inSample)))
outSample = myFunc(inSample)
print((repr(outSample)))
outSample = myFunc(((100., 100.), (101., 101.), (102., 102.)))
print((repr(outSample)))
# Test cache behavior
myFunc.enableCache()
print(('calls = ' + str(myFunc.getEvaluationCallsNumber())
+ ' hits = ' + str(myFunc.getCacheHits())))
outPt = myFunc(inPt)
print(('T = ' + repr(outPt)))
print(('calls = ' + str(myFunc.getEvaluationCallsNumber())
+ ' hits = ' + str(myFunc.getCacheHits())))
outPt = myFunc(inPt)
print(('T = ' + repr(outPt)))
print(('calls = ' + str(myFunc.getEvaluationCallsNumber())
+ ' hits = ' + str(myFunc.getCacheHits())))
# duplicate one value
inSample[4] = inSample[5]
outSample = myFunc(inSample)
print(('T = ' + repr(outSample)))
print(('calls = ' + str(myFunc.getEvaluationCallsNumber())
+ ' hits = ' + str(myFunc.getCacheHits())))
outSample = myFunc(inSample)
print(('T = ' + repr(outSample)))
print(('calls = ' + str(myFunc.getEvaluationCallsNumber())
+ ' hits = ' + str(myFunc.getCacheHits())))
myFunc.clearCache()
outSample = myFunc(inSample)
print(('T = ' + repr(outSample)))
print(('calls = ' + str(myFunc.getEvaluationCallsNumber())
+ ' hits = ' + str(myFunc.getCacheHits())))
# test PythonFunction
def a_exec(X):
Y = [0]
Y[0] = X[0] + X[1]
return Y
def a_exec_sample(Xs):
Ys = []
for X in Xs:
Ys.append([X[0] + X[1]])
return Ys
a_sample = ((100., 100.), (101., 101.), (102., 102.))
print('exec')
myFunc = PythonFunction(2, 1, a_exec)
outSample = myFunc(a_sample)
print(outSample)
print('exec + exec_sample')
myFunc = PythonFunction(2, 1, a_exec, a_exec_sample)
outSample = myFunc(a_sample)
print(outSample)
print('exec_sample only on a point')
myFunc = PythonFunction(2, 1, func_sample=a_exec_sample)
outSample = myFunc([100., 100.])
print(outSample)
print('exec_sample only on a sample')
myFunc = PythonFunction(2, 1, func_sample=a_exec_sample)
outSample = myFunc(a_sample)
print(outSample)
def a_grad(X):
# wrong but allows to verify
dY = [[1.], [-1.]]
return dY
print('gradient')
myFunc = PythonFunction(2, 1, a_exec, gradient=a_grad)
grad = myFunc.gradient([100., 100.])
print(grad)
def a_hess(X):
# wrong but allows to verify
d2Y = [[[0.1], [0.3]], [[0.3], [0.1]]]
return d2Y
print('hessian')
myFunc = PythonFunction(2, 1, a_exec, hessian=a_hess)
hess = myFunc.hessian([100., 100.])
print(hess)
print('no func')
try:
myFunc = PythonFunction(2, 1)
outSample = myFunc(a_sample)
except:
# must raise exception
print('no function detected : ok.')
else:
raise Exception('no function not detected!')
def a_exec(X):
Y = [0]
if X[0] == 0.0:
raise RuntimeError('Oups')
elif X[0] == 1.0:
'2' + 2
return Y
for n in range(2):
myFunc = PythonFunction(1, 1, a_exec)
try:
X = NumericalPoint(1, n)
myFunc(X)
except Exception as exc:
# print exc
print('exception handling: ok')
|