STColor4ub STColor4ub::operator+(const STColor4ub& rhs) const { STColor4ub rval; rval.r = minc(255, this->r + rhs.r); rval.g = minc(255, this->g + rhs.g); rval.b = minc(255, this->b + rhs.b); rval.a = this->a; return rval; }
void drawdots(struct fb *fb, struct dots *d) { int i; for(i=0; i<256; i++) { putpixel(fb, d->x[d->i], d->y[d->j], palette[gi]); d->i = minc(d->i); d->j = minc(d->j); gi = minc(gi); } }
template<typename MatrixType> void matrixRedux(const MatrixType& m) { typedef typename MatrixType::Index Index; typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::RealScalar RealScalar; Index rows = m.rows(); Index cols = m.cols(); MatrixType m1 = MatrixType::Random(rows, cols); // The entries of m1 are uniformly distributed in [0,1], so m1.prod() is very small. This may lead to test // failures if we underflow into denormals. Thus, we scale so that entires are close to 1. MatrixType m1_for_prod = MatrixType::Ones(rows, cols) + Scalar(0.2) * m1; VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1)); VERIFY_IS_APPROX(MatrixType::Ones(rows, cols).sum(), Scalar(float(rows*cols))); // the float() here to shut up excessive MSVC warning about int->complex conversion being lossy Scalar s(0), p(1), minc(internal::real(m1.coeff(0))), maxc(internal::real(m1.coeff(0))); for(int j = 0; j < cols; j++) for(int i = 0; i < rows; i++) { s += m1(i,j); p *= m1_for_prod(i,j); minc = (std::min)(internal::real(minc), internal::real(m1(i,j))); maxc = (std::max)(internal::real(maxc), internal::real(m1(i,j))); } const Scalar mean = s/Scalar(RealScalar(rows*cols)); VERIFY_IS_APPROX(m1.sum(), s); VERIFY_IS_APPROX(m1.mean(), mean); VERIFY_IS_APPROX(m1_for_prod.prod(), p); VERIFY_IS_APPROX(m1.real().minCoeff(), internal::real(minc)); VERIFY_IS_APPROX(m1.real().maxCoeff(), internal::real(maxc)); // test slice vectorization assuming assign is ok Index r0 = internal::random<Index>(0,rows-1); Index c0 = internal::random<Index>(0,cols-1); Index r1 = internal::random<Index>(r0+1,rows)-r0; Index c1 = internal::random<Index>(c0+1,cols)-c0; VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).sum(), m1.block(r0,c0,r1,c1).eval().sum()); VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).mean(), m1.block(r0,c0,r1,c1).eval().mean()); VERIFY_IS_APPROX(m1_for_prod.block(r0,c0,r1,c1).prod(), m1_for_prod.block(r0,c0,r1,c1).eval().prod()); VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).real().minCoeff(), m1.block(r0,c0,r1,c1).real().eval().minCoeff()); VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).real().maxCoeff(), m1.block(r0,c0,r1,c1).real().eval().maxCoeff()); // test empty objects VERIFY_IS_APPROX(m1.block(r0,c0,0,0).sum(), Scalar(0)); VERIFY_IS_APPROX(m1.block(r0,c0,0,0).prod(), Scalar(1)); }
double c_ctr::doc_inference(const c_document* doc, const gsl_vector* theta_v, const gsl_matrix* log_beta, gsl_matrix* phi, gsl_vector* gamma, gsl_matrix* word_ss, bool update_word_ss) { double pseudo_count = 1.0; double likelihood = 0; gsl_vector* log_theta_v = gsl_vector_alloc(theta_v->size); gsl_vector_memcpy(log_theta_v, theta_v); vct_log(log_theta_v); int n, k, w; double x; for (n = 0; n < doc->m_length; n ++) { w = doc->m_words[n]; for (k = 0; k < m_num_factors; k ++) mset(phi, n, k, vget(theta_v, k) * mget(m_beta, k, w)); gsl_vector_view row = gsl_matrix_row(phi, n); vnormalize(&row.vector); for (k = 0; k < m_num_factors; k ++) { x = mget(phi, n, k); if (x > 0) likelihood += x*(vget(log_theta_v, k) + mget(log_beta, k, w) - log(x)); } } if (pseudo_count > 0) { likelihood += pseudo_count * vsum(log_theta_v); } gsl_vector_set_all(gamma, pseudo_count); // smoothing with small pseudo counts for (n = 0; n < doc->m_length; n ++) { for (k = 0; k < m_num_factors; k ++) { x = doc->m_counts[n] * mget(phi, n, k); vinc(gamma, k, x); if (update_word_ss) minc(word_ss, k, doc->m_words[n], x); } } gsl_vector_free(log_theta_v); return likelihood; }
gsl_matrix* compute_total_counts(const corpus_seq_t* c) { int t, d, n; gsl_matrix* ret = gsl_matrix_alloc(c->nterms, c->len); for (t = 0; t < c->len; t++) { corpus_t* corpus = c->corpus[t]; for (d = 0; d < corpus->ndocs; d++) { doc_t* doc = corpus->doc[d]; for (n = 0; n < doc->nterms; n++) { minc(ret, doc->word[n], t, (double) doc->count[n]); } } } return(ret); }
double lda_e_step(lda* model, corpus_t* data, lda_suff_stats* ss) { int d, k, n; if (ss != NULL) reset_lda_suff_stats(ss); lda_post* p = new_lda_post(model->ntopics, data->max_unique); p->model = model; double lhood = 0; // for each document for (d = 0; d < data->ndocs; d++) { if (((d % 1000) == 0) && (d > 0)) { outlog( "e-step: processing doc %d", d); } // fit posterior p->doc = data->doc[d]; lhood += fit_lda_post(d, 0, p, NULL, NULL, NULL, NULL, NULL); // update expected sufficient statistics if (ss != NULL) for (k = 0; k < model->ntopics; k++) for (n = 0; n < p->doc->nterms; n++) minc(ss->topics_ss, p->doc->word[n], k, mget(p->phi, n, k) * p->doc->count[n]); } // !!! FREE POSTERIOR return(lhood); }
static cairo_surface_t* _cairocks_create_embossed_surface( cairo_surface_t* surface, double azimuth, double elevation, double height, double ambient, double diffuse ) { unsigned char* src = cairo_image_surface_get_data(surface); int w = cairo_image_surface_get_width(surface); int h = cairo_image_surface_get_height(surface); int stride = cairo_image_surface_get_stride(surface); cairo_surface_t* dst_surface = cairo_image_surface_create(CAIRO_FORMAT_A8, w, h); unsigned char* dst = cairo_image_surface_get_data(dst_surface); double scale = height / (4.0f * 255.0f); double Lx = cos(azimuth) * cos(elevation); double Ly = sin(azimuth) * cos(elevation); double Lz = sin(elevation); int y; int x; memset(dst, minc(ambient + diffuse * Lz), stride * h); /* I'm going to set a minimum requirement size for embossing here for now, since the matrix "d" below assumes 3x3. Later, if it's necessary for such small surfaces, we could rectify this. */ if(w < 4 || h < 4) goto dirty_ret; for(y = 0; y < h; y++) { for(x = 0; x < w; x++) { double Nx; double Ny; double Nz; double NdotL; double d[3][3]; int i; int j; for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { d[i][j] = (unsigned char)(src[OFFSET(x + i - 1, y + j - 1, w, h, stride)]); } } /* Compute the normal from the source. */ Nx = d[2][0] + (2.0f * d[2][1]) + d[2][2]; Nx -= d[0][0] + (2.0f * d[0][1]) + d[0][2]; Nx /= 9; Nx *= scale; Ny = d[0][2] + (2.0f * d[1][2]) + d[2][2]; Ny -= d[0][0] + (2.0f * d[1][0]) + d[2][0]; Ny /= 9; Ny *= scale; Nz = 0.5; NdotL = (Nx * Lx) + (Ny * Ly) + (Nz * Lz); NdotL = (NdotL > 0.0) ? NdotL : 0.0; dst[(y * stride) + x] = minc(ambient + (diffuse * NdotL)); } } dirty_ret: cairo_surface_mark_dirty(dst_surface); return dst_surface; }
template<typename VectorType> void vectorRedux(const VectorType& w) { typedef typename VectorType::Index Index; typedef typename VectorType::Scalar Scalar; typedef typename NumTraits<Scalar>::Real RealScalar; Index size = w.size(); VectorType v = VectorType::Random(size); VectorType v_for_prod = VectorType::Ones(size) + Scalar(0.2) * v; // see comment above declaration of m1_for_prod for(int i = 1; i < size; i++) { Scalar s(0), p(1); RealScalar minc(internal::real(v.coeff(0))), maxc(internal::real(v.coeff(0))); for(int j = 0; j < i; j++) { s += v[j]; p *= v_for_prod[j]; minc = (std::min)(minc, internal::real(v[j])); maxc = (std::max)(maxc, internal::real(v[j])); } VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(s - v.head(i).sum()), Scalar(1)); VERIFY_IS_APPROX(p, v_for_prod.head(i).prod()); VERIFY_IS_APPROX(minc, v.real().head(i).minCoeff()); VERIFY_IS_APPROX(maxc, v.real().head(i).maxCoeff()); } for(int i = 0; i < size-1; i++) { Scalar s(0), p(1); RealScalar minc(internal::real(v.coeff(i))), maxc(internal::real(v.coeff(i))); for(int j = i; j < size; j++) { s += v[j]; p *= v_for_prod[j]; minc = (std::min)(minc, internal::real(v[j])); maxc = (std::max)(maxc, internal::real(v[j])); } VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(s - v.tail(size-i).sum()), Scalar(1)); VERIFY_IS_APPROX(p, v_for_prod.tail(size-i).prod()); VERIFY_IS_APPROX(minc, v.real().tail(size-i).minCoeff()); VERIFY_IS_APPROX(maxc, v.real().tail(size-i).maxCoeff()); } for(int i = 0; i < size/2; i++) { Scalar s(0), p(1); RealScalar minc(internal::real(v.coeff(i))), maxc(internal::real(v.coeff(i))); for(int j = i; j < size-i; j++) { s += v[j]; p *= v_for_prod[j]; minc = (std::min)(minc, internal::real(v[j])); maxc = (std::max)(maxc, internal::real(v[j])); } VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(s - v.segment(i, size-2*i).sum()), Scalar(1)); VERIFY_IS_APPROX(p, v_for_prod.segment(i, size-2*i).prod()); VERIFY_IS_APPROX(minc, v.real().segment(i, size-2*i).minCoeff()); VERIFY_IS_APPROX(maxc, v.real().segment(i, size-2*i).maxCoeff()); } // test empty objects VERIFY_IS_APPROX(v.head(0).sum(), Scalar(0)); VERIFY_IS_APPROX(v.tail(0).prod(), Scalar(1)); VERIFY_RAISES_ASSERT(v.head(0).mean()); VERIFY_RAISES_ASSERT(v.head(0).minCoeff()); VERIFY_RAISES_ASSERT(v.head(0).maxCoeff()); }