matrixd UnscentedExpectedImprovement::convertMatrixNoise(const matrixd& matrix, const double scale , const int dim ) { matrixd matrix_output = matrix; // Scale Matrix matrix_output *= (dim + scale); // Square root Matrix if (!isDiag(matrix_output)) { matrixd L; utils::cholesky_decompose(matrix_output, L); matrix_output = L; } else { for (size_t idx = 0; idx < matrix_output.size1(); ++idx) { matrix_output(idx, idx) = std::sqrt(matrix_output(idx, idx)); } } return matrix_output; }
void rswapcount(double *m, int *nr, int *nc, int *mfill) { int row[2], col[2], i, k, ij[4], n, change, cfill, pm[4] = {1, -1, -1, 1} ; double sm[4], ev; /* Get the current fill 'cfill' */ n = (*nr) * (*nc); for (i = 0, cfill=0; i < n; i++) { if (m[i] > 0) cfill++; } GetRNGstate(); /* Loop while fills differ */ while (cfill != *mfill) { /* Select a random 2x2 matrix*/ i2rand(row, *nr - 1); i2rand(col, *nc - 1); ij[0] = INDX(row[0], col[0], *nr); ij[1] = INDX(row[1], col[0], *nr); ij[2] = INDX(row[0], col[1], *nr); ij[3] = INDX(row[1], col[1], *nr); for (k = 0; k < 4; k ++) sm[k] = m[ij[k]]; /* The largest value that can be swapped */ ev = isDiag(sm); if (ev != 0) { /* Check the change in fills */ for (k = 0, change=0; k < 4; k++) { if(sm[k] > 0) change--; if (sm[k] + pm[k]*ev > 0) change++; } /* Fill does not change, but swap to bail out from * non-swappable configurations */ if (change == 0) { for (k = 0; k < 4; k++) m[ij[k]] += pm[k]*ev; } else if ((change < 0 && *mfill < cfill) || (change > 0 && *mfill > cfill)) { for (k = 0; k < 4; k++) m[ij[k]] += pm[k]*ev; cfill += change; } } } PutRNGstate(); }
static void rswapcount(int *m, int *nr, int *nc, int *mfill) { int i, k, ij[4], n, change, cfill, pm[4] = {1, -1, -1, 1} ; int sm[4], ev; size_t intcheck; /* Get the current fill 'cfill' */ n = (*nr) * (*nc); for (i = 0, cfill=0; i < n; i++) { if (m[i] > 0) cfill++; } /* GetRNGstate in calling C code */ /* GetRNGstate(); */ /* Loop while fills differ */ intcheck = 0; while (cfill != *mfill) { /* Select a random 2x2 matrix*/ get2x2(n - 1, (*nr), ij); for (k = 0; k < 4; k ++) sm[k] = m[ij[k]]; /* The largest value that can be swapped */ ev = isDiag(sm, &change); if (ev != 0) { /* Fill does not change, but swap to bail out from * non-swappable configurations */ if (change == 0) { for (k = 0; k < 4; k++) m[ij[k]] += pm[k]*ev; } else if ((change < 0 && *mfill < cfill) || (change > 0 && *mfill > cfill)) { for (k = 0; k < 4; k++) m[ij[k]] += pm[k]*ev; cfill += change; } } if (intcheck % 10000 == 9999) R_CheckUserInterrupt(); intcheck++; } /* PutRNGstate(); */ }
void swapcount(double *m, int *nr, int *nc, int *thin) { int row[2], col[2], k, ij[4], changed, oldn, newn, pm[4] = {1, -1, -1, 1} ; double sm[4], ev; GetRNGstate(); changed = 0; while (changed < *thin) { /* Select a random 2x2 matrix*/ i2rand(row, *nr - 1); i2rand(col, *nc - 1); ij[0] = INDX(row[0], col[0], *nr); ij[1] = INDX(row[1], col[0], *nr); ij[2] = INDX(row[0], col[1], *nr); ij[3] = INDX(row[1], col[1], *nr); for (k = 0; k < 4; k ++) sm[k] = m[ij[k]]; /* The largest value that can be swapped */ ev = isDiag(sm); if (ev != 0) { /* Check that the fill doesn't change*/ for (k = 0, oldn = 0, newn = 0; k < 4; k++) { if(sm[k] > 0) oldn++; if (sm[k] + pm[k]*ev > 0) newn++; } /* Swap */ if (oldn == newn) { for (k = 0; k < 4; k++) m[ij[k]] += pm[k]*ev; changed++; } } } PutRNGstate(); }