25 #include <queso/VectorGaussianRandomField.h>
30 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
38 m_env (indexSet.env()),
39 m_prefix ((std::string)(prefix)+
"grf_"),
40 m_indexSet (indexSet),
41 m_imageSetPerIndex (imageSetPerIndex),
42 m_meanFunction (meanFunction),
43 m_covarianceFunction (covarianceFunction),
44 m_savedRvImageSpace (NULL),
45 m_savedRvLawExpVector(NULL),
46 m_savedRvLawCovMatrix(NULL),
52 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
57 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
64 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
68 return m_meanFunction;
71 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
75 return m_covarianceFunction;
78 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
82 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
83 *m_env.subDisplayFile() <<
"Entering VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
87 queso_require_equal_to_msg(( sampleValues.sizeLocal() % fieldPositions.size() ), 0,
"input data is not multiple of each other");
89 unsigned int numberOfImageValuesPerIndex = sampleValues.sizeLocal()/fieldPositions.size();
91 queso_require_equal_to_msg(numberOfImageValuesPerIndex, m_imageSetPerIndex.vectorSpace().dimLocal(),
"invalid input data dimension");
93 if ((m_savedPositions.size() == 0 ) &&
94 (m_savedRvImageSpace == NULL) &&
95 (m_savedRvLawExpVector == NULL) &&
96 (m_savedRvLawCovMatrix == NULL) &&
97 (m_savedRv == NULL)) {
100 else if ((m_savedPositions.size() != 0 ) &&
101 (m_savedRvImageSpace != NULL) &&
102 (m_savedRvLawExpVector != NULL) &&
103 (m_savedRvLawCovMatrix != NULL) &&
104 (m_savedRv != NULL)) {
111 unsigned int numberOfPositions = fieldPositions.size();
112 bool instantiate =
true;
113 if (m_savedPositions.size() == numberOfPositions) {
114 bool allPositionsAreEqual =
true;
115 for (
unsigned int i = 0; i < numberOfPositions; ++i) {
116 queso_require_msg(m_savedPositions[i],
"m_savedPositions[i] should not be NULL");
117 if ((m_savedPositions[i]->sizeLocal() == fieldPositions[i]->sizeLocal()) &&
118 (*(m_savedPositions[i]) == *(fieldPositions[i]) )) {
122 allPositionsAreEqual =
false;
126 instantiate = !allPositionsAreEqual;
129 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
130 *m_env.subDisplayFile() <<
"In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
131 <<
": numberOfPositions = " << numberOfPositions
132 <<
", instantiate = " << instantiate
138 delete m_savedRvLawCovMatrix;
139 delete m_savedRvLawExpVector;
140 delete m_savedRvImageSpace;
141 for (
unsigned int i = 0; i < m_savedPositions.size(); ++i) {
142 delete m_savedPositions[i];
144 m_savedPositions.clear();
147 m_savedPositions.resize(numberOfPositions,NULL);
148 for (
unsigned int i = 0; i < m_savedPositions.size(); ++i) {
149 m_savedPositions[i] =
new P_V(*(fieldPositions[i]));
153 m_savedRvImageSpace =
new VectorSpace<Q_V,Q_M>(m_env,
"grf_", numberOfPositions*numberOfImageValuesPerIndex, NULL);
156 Q_V tmpVec(m_imageSetPerIndex.vectorSpace().zeroVector());
157 m_savedRvLawExpVector =
new Q_V(m_savedRvImageSpace->zeroVector());
158 for (
unsigned int i = 0; i < numberOfPositions; ++i) {
159 m_meanFunction.compute(*(fieldPositions[i]),NULL,tmpVec,NULL,NULL,NULL);
160 for (
unsigned int j = 0; j < numberOfImageValuesPerIndex; ++j) {
161 (*m_savedRvLawExpVector)[i*numberOfImageValuesPerIndex + j] = tmpVec[j];
166 Q_M tmpMat(m_imageSetPerIndex.vectorSpace().zeroVector());
167 m_savedRvLawCovMatrix =
new Q_M(m_savedRvImageSpace->zeroVector());
168 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
169 *m_env.subDisplayFile() <<
"In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
170 <<
": m_savedRvLawCovMatrix order = " << m_savedRvLawCovMatrix->numCols()
171 <<
", numberOfPositions = " << numberOfPositions
172 <<
", tmpMat order = " << tmpMat.numCols()
173 <<
", numberOfImageValuesPerIndex = " << numberOfImageValuesPerIndex
176 for (
unsigned int i = 0; i < numberOfPositions; ++i) {
177 for (
unsigned int j = 0; j < numberOfPositions; ++j) {
178 m_covarianceFunction.covMatrix(*(fieldPositions[i]),*(fieldPositions[j]),tmpMat);
181 if (testMat.chol() != 0) {
182 *m_env.subDisplayFile() <<
"In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
185 <<
", *(fieldPositions[i]) = " << *(fieldPositions[i])
186 <<
", *(fieldPositions[j]) = " << *(fieldPositions[j])
187 <<
", tmpMat = " << tmpMat
188 <<
", testMat = " << testMat
189 <<
", tmpMat is not positive definite"
194 for (
unsigned int k1 = 0; k1 < numberOfImageValuesPerIndex; ++k1) {
195 for (
unsigned int k2 = 0; k2 < numberOfImageValuesPerIndex; ++k2) {
196 unsigned int tmpI = i*numberOfImageValuesPerIndex + k1;
197 unsigned int tmpJ = j*numberOfImageValuesPerIndex + k2;
198 (*m_savedRvLawCovMatrix)(tmpI,tmpJ) = tmpMat(k1,k2);
199 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
200 *m_env.subDisplayFile() <<
"In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
205 <<
", tmpI = " << tmpI
206 <<
", tmpJ = " << tmpJ
207 <<
", *(fieldPositions[i]) = " << *(fieldPositions[i])
208 <<
", *(fieldPositions[j]) = " << *(fieldPositions[j])
209 <<
", (*m_savedRvLawCovMatrix)(tmpI,tmpJ) = " << (*m_savedRvLawCovMatrix)(tmpI,tmpJ)
219 *m_savedRvImageSpace,
220 *m_savedRvLawExpVector,
221 *m_savedRvLawCovMatrix);
223 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
224 *m_env.subDisplayFile() <<
"In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
225 <<
": just instantiated Gaussian RV"
226 <<
"\n *m_savedRvLawExpVector = " << *m_savedRvLawExpVector
227 <<
"\n *m_savedRvLawCovMatrix = " << *m_savedRvLawCovMatrix
229 for (
unsigned int i = 0; i < numberOfPositions; ++i) {
230 *m_env.subDisplayFile() <<
" *(m_savedPositions[" << i
231 <<
"]) = " << *(m_savedPositions[i])
238 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
239 *m_env.subDisplayFile() <<
"In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
240 <<
": about to realize sample values"
243 m_savedRv->realizer().realization(sampleValues);
244 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
245 *m_env.subDisplayFile() <<
"In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
246 <<
": just realized sample values"
250 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
251 *m_env.subDisplayFile() <<
"Leaving VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
VectorGaussianRandomField(const char *prefix, const VectorSet< P_V, P_M > &indexSet, const VectorSet< Q_V, Q_M > &imageSetPerIndex, const BaseVectorFunction< P_V, P_M, Q_V, Q_M > &meanFunction, const BaseMatrixCovarianceFunction< P_V, P_M, Q_V, Q_M > &covarianceFunction)
Constructor.
std::vector< P_V * > m_savedPositions
Saved positions.
A templated (base) class for handling vector functions.
void sampleFunction(const std::vector< P_V * > &fieldPositions, Q_V &sampleValues)
Function that samples from a Gaussian PDF.
const VectorSet< P_V, P_M > & indexSet() const
Index set; access to protected attribute m_indexSet.
const BaseMatrixCovarianceFunction< P_V, P_M, Q_V, Q_M > & covarianceFunction() const
Covariance function; access to protected attribute m_covarianceFunction.
#define queso_error_msg(msg)
#define queso_require_equal_to_msg(expr1, expr2, msg)
const BaseVectorFunction< P_V, P_M, Q_V, Q_M > & meanFunction() const
Mean function; access to protected attribute m_meanFunction.
#define queso_require_msg(asserted, msg)
~VectorGaussianRandomField()
Destructor.
A templated (base) class to accommodate covariance matrix of (random) vector functions.