/usr/include/openturns/swig/RandomVector.i is in libopenturns-dev 1.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | // SWIG file RandomVector.i
%{
#include "RandomVector.hxx"
#include "PythonRandomVectorImplementation.hxx"
%}
%include RandomVector_doc.i
%pythoncode %{
class PythonRandomVector(object):
"""
Allow to overload RandomVector from Python.
Parameters
----------
dim : positive int
Vector dimension.
Default is 0.
See also
--------
RandomVector
Examples
--------
>>> import openturns as ot
>>> ot.RandomGenerator.SetSeed(0)
Overload RandomVector from Python:
>>> class RVEC(ot.PythonRandomVector):
... def __init__(self):
... super(RVEC, self).__init__(2)
... self.setDescription(['R', 'S'])
...
... def getRealization(self):
... X = [ot.RandomGenerator.Generate(), 2 + ot.RandomGenerator.Generate()]
... return X
...
... def getSample(self, size):
... X = []
... for i in range(size):
... X.append([ot.RandomGenerator.Generate(), 2 + ot.RandomGenerator.Generate()])
... return X
...
... def getMean(self):
... return [0.5, 2.5]
...
... def getCovariance(self):
... return [[0.0833333, 0.], [0., 0.0833333]]
Use the overloaded class:
>>> R = RVEC()
>>> # Instance creation
>>> myRV = ot.RandomVector(R)
>>> # Realization
>>> print(myRV.getRealization())
[0.629877,2.88281]
>>> # Sample
>>> print(myRV.getSample(5))
0 : [ 0.135276 2.0325 ]
1 : [ 0.347057 2.96942 ]
2 : [ 0.92068 2.50304 ]
3 : [ 0.0632061 2.29276 ]
4 : [ 0.714382 2.38336 ]
>>> # Mean
>>> print(myRV.getMean())
[0.5,2.5]
>>> # Covariance
>>> print(myRV.getCovariance())
[[ 0.0833333 0 ]
[ 0 0.0833333 ]]
"""
def __init__(self, dim=0):
# Warning: these names are used in PythonRandomVectorImplementation class. Synchronize the files if changed
self.__dim = dim
self.__desc = list(map(lambda i: 'x' + str(i), range(dim)))
def __str__(self):
return 'PythonRandomVector -> %s #%d' % (self.__desc, self.__dim)
def __repr__(self):
return self.__str__()
def getDimension(self):
"""
Get the dimension.
Returns
-------
dim : positive int
Dimension of the RandomVector.
"""
return self.__dim
def setDescription(self, desc):
"""
Set the description.
Parameters
----------
desc : sequence of str
*desc* describes the components of the RandomVector.
Its size must be equal to the dimension of the RandomVector.
"""
if (len(desc) != self.__dim):
raise ValueError('Description size does NOT match dimension')
self.__desc = desc
def getDescription(self):
"""
Get the description.
Returns
-------
desc : :class:`~openturns.Description`
*desc* describes the components of the RandomVector.
"""
return self.__desc
def getRealization(self):
"""
Get a realization of the random vector.
Returns
-------
realization : :class:`~openturns.NumericalPoint`
Sequence of values randomly determined from the RandomVector definition.
"""
raise RuntimeError('You must define a method getRealization() -> X, where X is a NumericalPoint')
def getMean(self):
"""
Get the mean.
Returns
-------
mean : :class:`~openturns.NumericalPoint`
Mean of the RandomVector.
"""
raise RuntimeError('You must define a method mean -> X, where X is a NumericalPoint')
def getCovariance(self):
"""
Get the covariance.
Returns
-------
covariance : :class:`~openturns.CovarianceMatrix`
Covariance of the RandomVector.
"""
raise RuntimeError('You must define a method var -> M, where M is a CovarianceMatrix')
class SciPyRandomVector(PythonRandomVector):
"""
Build a PythonRandomVector from a scipy distribution.
Parameters
----------
dist : a scipy.stats distribution
the distribution to wrap
"""
def __init__(self, dist):
super(SciPyRandomVector, self).__init__(1)
if dist.__class__.__name__ != 'rv_frozen':
raise TypeError('Argument is not a scipy distribution')
self._dist = dist
def getRealization(self):
rvs = self._dist.rvs()
return [rvs]
def getSample(self, size):
rvs = self._dist.rvs(size)
return rvs.reshape(size, 1)
def getMean(self):
mean = float(self._dist.stats('m'))
return [mean]
def getCovariance(self):
var = float(self._dist.stats('v'))
return [[var]]
%}
OTTypedInterfaceObjectHelper(RandomVector)
%include RandomVector.hxx
namespace OT { %extend RandomVector {
RandomVector(const RandomVector & other)
{
return new OT::RandomVector(other);
}
RandomVector(PyObject * pyObj)
{
return new OT::RandomVector( new OT::PythonRandomVectorImplementation(pyObj) );
}
} // class RandomVector
} // namespace OT
|