From: Steinar H. Gunderson Date: Tue, 20 Mar 2012 09:50:37 +0000 (+0100) Subject: Use the matrix class from Eigen directly instead of copying from an array. Seems... X-Git-Url: https://git.sesse.net/?p=wloh;a=commitdiff_plain;h=35021b4f76156feb48fd9420a9116da50acee3e1 Use the matrix class from Eigen directly instead of copying from an array. Seems to be a bit (100ms!?) faster. --- diff --git a/bayeswf.cpp b/bayeswf.cpp index ca205ef..a691b8f 100644 --- a/bayeswf.cpp +++ b/bayeswf.cpp @@ -199,13 +199,14 @@ float compute_total_logl(float *mu, int num_players) * * Note that this does not depend on mu or the margin at all. */ -double hessian[MAX_PLAYERS * MAX_PLAYERS]; +Matrix hessian; void construct_hessian(const float *mu, int num_players) { - memset(hessian, 0, sizeof(hessian)); + hessian = Matrix(num_players, num_players); + hessian.fill(0.0f); for (int i = 0; i < num_players; ++i) { - hessian[i * num_players + i] += 1.0f / (prior_sigma * prior_sigma); + hessian(i, i) += 1.0f / (prior_sigma * prior_sigma); } for (unsigned i = 0; i < all_matches.size(); ++i) { const match &m = all_matches[i]; @@ -216,11 +217,11 @@ void construct_hessian(const float *mu, int num_players) double sigma_sq = global_sigma * global_sigma; float w = m.weight; - hessian[p1 * num_players + p2] -= w / sigma_sq; - hessian[p2 * num_players + p1] -= w / sigma_sq; + hessian(p1, p2) -= w / sigma_sq; + hessian(p2, p1) -= w / sigma_sq; - hessian[p1 * num_players + p1] += w / sigma_sq; - hessian[p2 * num_players + p2] += w / sigma_sq; + hessian(p1, p1) += w / sigma_sq; + hessian(p2, p2) += w / sigma_sq; } } @@ -228,15 +229,8 @@ void construct_hessian(const float *mu, int num_players) // where H is the Hessian (see construct_hessian()). void compute_mu_uncertainty(const float *mu, int num_players) { - Matrix h(num_players, num_players); - for (int i = 0; i < num_players; ++i) { - for (int j = 0; j < num_players; ++j) { - h(i, j) = hessian[i * num_players + j]; - } - } - // FIXME: Use pseudoinverse if applicable. - Matrix ih = h.inverse(); + Matrix ih = hessian.inverse(); for (int i = 0; i < num_players; ++i) { mu_stddev[i] = sqrt(ih(i, i)); }