double Phong::pdf(const vec& incoming, const vec& direction, const vec& normal) { //vec newDirection = direction; //newDirection.normalize(); if (normal.dot(direction) < 0) return 0; vec reflect = normal * (2 * normal.dot(incoming * -1)) + incoming; double ans = (exp) * pow(reflect.dot(direction * -1), exp); if (ans < 0) return 0; else return ans * BOUNCE_MULT; }
vec Phong::sample(const vec& incoming, const vec& normal) { //No meaning to the following vector. Only purpose is that we usually won't be parallel // to it. vec reflect = normal * (2 * normal.dot(incoming * -1)) + (incoming); vec u = reflect.cross(vec(0.973483, 0.228749, 0.00225264,0)); u.normalize(); vec v = reflect.cross(u); v.normalize(); vec r = Random::randomCosVec(exp); vec ans = u * r[0] + v * r[1] + reflect * r[2]; if (normal.dot(ans) < 0) ans = normal * (2 * normal.dot(ans)) - ans; return ans; }
vec MonolingualModel::hierarchicalUpdate(const HuffmanNode& node, const vec& hidden, float alpha, bool update) { int dimension = config.dimension; vec temp(dimension, 0); for (int j = 0; j < node.code.size(); ++j) { int parent_index = node.parents[j]; float x = hidden.dot(output_weights_hs[parent_index]); if (x <= -MAX_EXP || x >= MAX_EXP) { continue; } float pred = sigmoid(x); float error = -alpha * (pred - node.code[j]); temp += error * output_weights_hs[parent_index]; if (update && !config.freeze) { output_weights_hs[parent_index] += error * hidden; } } return temp; }
grasswedge(int i) : dir(2*M_PI*(i+0.5f)/float(NUMGRASSWEDGES), 0), across(2*M_PI*((i+0.5f)/float(NUMGRASSWEDGES) + 0.25f), 0), edge1(vec(2*M_PI*i/float(NUMGRASSWEDGES), 0).div(cos(M_PI/NUMGRASSWEDGES))), edge2(vec(2*M_PI*(i+1)/float(NUMGRASSWEDGES), 0).div(cos(M_PI/NUMGRASSWEDGES))), bound1(vec(2*M_PI*(i/float(NUMGRASSWEDGES) - 0.25f), 0), 0), bound2(vec(2*M_PI*((i+1)/float(NUMGRASSWEDGES) + 0.25f), 0), 0) { across.div(-across.dot(bound1)); }
bool raysphereintersect(vec c, float radius, const vec &o, const vec &ray, float &dist) { c.sub(o); float v = c.dot(ray), inside = radius*radius - c.squaredlen(), d = inside + v*v; if(inside<0 && d<0) return false; dist += v - sqrt(d); return true; }
void results(const mat& Q, const vec& c, const mat& A, const vec& b, const vec& x, vec reference = vec()) { // std::cout << "solution: " << x.transpose() << std::endl; using namespace std; const math::natural m = c.rows(); const math::natural n = b.rows(); cout << "violation: " << (A * x - b).cwiseMin( vec::Zero(n) ).squaredNorm() / m << endl; cout << "obj: " << x.dot( 0.5 * (Q * x) + c ) << endl; if(!reference.empty()) { core::log("error:", (x - reference).norm() ); } cout << endl; }
vec MonolingualModel::negSamplingUpdate(const HuffmanNode& node, const vec& hidden, float alpha, bool update) { int dimension = config.dimension; vec temp(dimension, 0); for (int d = 0; d < config.negative + 1; ++d) { int label; const HuffmanNode* target; if (d == 0) { // 1 positive example target = &node; label = 1; } else { // n negative examples target = getRandomHuffmanNode(); if (*target == node) continue; label = 0; } float x = hidden.dot(output_weights[target->index]); float pred; if (x >= MAX_EXP) { pred = 1; } else if (x <= -MAX_EXP) { pred = 0; } else { pred = sigmoid(x); } float error = alpha * (label - pred); temp += error * output_weights[target->index]; if (update && !config.freeze) { output_weights[target->index] += error * hidden; } } return temp; }
inline double dot(const vec&a, const vec& b){ return a.dot(b); }