int LDA::sampleFullConditional(int m,int n) { double u,topic=z(m,n); VectorXd p(K); nw((*documents)(m,n),topic)--; nd(m,topic)--; nwsum(topic)--; ndsum(m)--; for(int k=0; k<K; k++) p(k)=((nw((*documents)(m,n),k)+beta)/(nwsum(k)+V*beta)) * ((nd(m,k)+alpha)/(ndsum(m)+K*alpha)); for(int i=1; i<p.size(); i++) p(i)+=p(i-1); u = ((double) rand() / (RAND_MAX))*p(K-1); for(topic=0; topic<p.size(); topic++) if(u<p(topic)) break; nw((*documents)(m,n),topic)++; nd(m,topic)++; nwsum(topic)++; ndsum(m)++; return topic; }
int main () { int i; for (i=0;i<10;i++) { if (nd ()) a[i] =0; else a[i] =5; } for (i=0;i<10;i++) { if (nd ()) b[i] =20; else b[i] =25; } int x = a[i-1] + b[i-1]; if (x >= 0 && x<=30) return 42; else return 0; }
Trainee::Trainee(int n_hid1, int n_hid2, float init_sigma) { n_inputvec = MNISTreader::pixelSize; n_hid1vec = n_hid1; n_hid2vec = n_hid2; n_outputvec = 10; gsq_w1 = Eigen::ArrayXXf::Zero(n_hid1vec, n_inputvec); gsq_b1 = Eigen::ArrayXf::Zero(n_hid1vec); gsq_w2 = Eigen::ArrayXXf::Zero(n_hid2vec, n_hid1vec); gsq_b2 = Eigen::ArrayXf::Zero(n_hid2vec); gsq_w3 = Eigen::ArrayXXf::Zero(n_outputvec, n_hid2vec); gsq_b3 = Eigen::ArrayXf::Zero(n_outputvec); std::random_device rd; std::mt19937 mt(rd()); std::normal_distribution<float> nd(0.0, init_sigma); weight1 = Eigen::MatrixXf(n_hid1vec, n_inputvec); for(int i=0;i<n_hid1vec;i++) for(int j=0;j<n_inputvec;j++) weight1(i, j) = nd(mt); bias1 = Eigen::VectorXf::Zero(n_hid1vec); weight2 = Eigen::MatrixXf(n_hid2vec, n_hid1vec); for(int i=0;i<n_hid2vec;i++) for(int j=0;j<n_hid1vec;j++) weight2(i, j) = nd(mt); bias2 = Eigen::VectorXf::Zero(n_hid2vec); weight3 = Eigen::MatrixXf(n_outputvec, n_hid2vec); for(int i=0;i<n_outputvec;i++) for(int j=0;j<n_hid2vec;j++) weight3(i, j) = nd(mt); bias3 = Eigen::VectorXf::Zero(n_outputvec); }
void convolution_layer::randomize_data( layer_data& data, layer_data_custom& data_custom, random_generator& generator) const { unsigned int input_neuron_count = input_feature_map_count; std::for_each(window_sizes.begin(), window_sizes.end(), input_neuron_count *= boost::lambda::_1); float standard_deviation = 1.0F / sqrtf(static_cast<float>(input_neuron_count)); float max_abs_value = 3.0F * standard_deviation; nnforge_normal_distribution<float> nd(0.0F, standard_deviation); //nnforge_uniform_real_distribution<float> nd(-2.0F * standard_deviation, 2.0F * standard_deviation); for(unsigned int i = 0; i < data[0].size(); ++i) { float val = nd(generator); while (fabs(val) > max_abs_value) val = nd(generator); data[0][i] = val; } std::fill(data[1].begin(), data[1].end(), 0.0F); }
ParticleEmitter::ParticleEmitter(int numParticles) : numParticles(numParticles) { particles = std::vector<Particle>(numParticles, Particle()); for (auto &particle : particles) { particle.position = Vector3{ nd(el), 2 * nd(el), nd(el) }.normalize() * 0.2 * spreadScale; particle.velocity = Vector3{ nd(el) / 2, 10, nd(el) } * pow(10.0, -2.0) * velocityScale; particle.acceleration = Vector3{ 0.0, -2.0, 0.0 } * pow(10.0, -5.0); particle.lifespan = lifetime; } }
T from_newick_node(std::string x) { const size_t i = x.rfind(":"); if (i != std::string::npos) { // We do have a branch length here. const double length = util::string_to_double(x.substr(i + 1, x.size())); x = x.substr(0, i); T nd(x, length); return nd; } else { T nd(x); return nd; } }
void main(void) { /** ** The idea is to non-deterministically store a state during ** execution and then check if it was repeated or not. CBMC takes ** care of resolving the non-determinism. **/ // -- place to store 'saved' version of x and dir int sx; int sdir; // -- flag to indicate that we've stored a previous state int f; // -- x and dir of the original program int x; int dir = 1; // -- initially, the flag is unset, everything else is // -- non-deterministic, but saved values differ from real values f = 0; x = nd (); sx = nd (); sdir = nd (); __VERIFIER_assume (x != sx); __VERIFIER_assume (sdir != dir); while (x > 0) { // -- non-deterministically either save current state, or check // -- if current state is different from saved state. The f flag // -- ensures that the state is saved at least once before the // -- assertion is made if (nd ()) { f = 1; sx = x; sdir = dir; } else assert (!f || (x != sx || dir != sdir)); x = x + dir; if (x > 10) dir = -1 * dir; if (x < 5) dir = -1 * dir; } }
void layer_data::random_fill( float min, float max, random_generator& gen) { for(std::vector<std::vector<float> >::iterator it = begin(); it != end(); ++it) { nnforge_uniform_real_distribution<float> nd(min, max); for(std::vector<float>::iterator it2 = it->begin(); it2 != it->end(); ++it2) *it2 = nd(gen); } }
/** * Check if the sender and receiver threads are working correctly. * To do this, we start the neighbour discovery, and put in test mode. * With this we are going to have a neighbour, ourselves. */ TEST(NeighbourDiscoveryTest, NeighbourSendAndReceiveTest) { g_stop = false; std::ofstream ss; ss.open("adtn1.ini"); ss << "[Node]" << std::endl << "nodeId : node1" << std::endl << "nodeAddress : 127.0.0.1" << std::endl << "nodePort : 40000" << std::endl << "[NeighbourDiscovery]" << std::endl << "discoveryAddress : 239.100.100.100" << std::endl << "discoveryPort : 40001" << std::endl << "discoveryPeriod : 2" << std::endl << "neighbourExpirationTime : 4" << std::endl << "neighbourCleanerTime : 2" << std::endl << "testMode : true" << std::endl << "[Logger]" << std::endl << "filename : /tmp/adtn.log" << std::endl << "level : 100" << std::endl << "[Constants]" << std::endl << "timeout : 3" << std::endl << "[BundleProcess]" << std::endl << "dataPath : /tmp/.adtn/" << std::endl; ss.close(); Config cf = Config("adtn1.ini"); // clear the Nighbour table to ensure test values. sleep(1); std::shared_ptr<NeighbourTable> nt = std::shared_ptr<NeighbourTable>( new NeighbourTable()); NeighbourDiscovery nd(cf, nt); sleep(3); auto neighbours = nt->getValues(); ASSERT_EQ(1, neighbours.size()); ASSERT_EQ("node1", nt->getValue(*neighbours.begin())->getId()); ASSERT_EQ("127.0.0.1", nt->getValue(*neighbours.begin())->getNodeAddress()); ASSERT_EQ(40000, nt->getValue(*neighbours.begin())->getNodePort()); g_stop = true; sleep(5); }
int main( int ac, char * av[] ) { std::vector< double > smooth, derivative; noise noise( 0.0, 0.05 ); adportable::SGFilter smoother( M, adportable::SGFilter::Smoothing ); adportable::SGFilter d1cubic( M, adportable::SGFilter::Derivative1 ); adportable::SGFilter d1quadratic( M, adportable::SGFilter::Derivative1, adportable::SGFilter::Quadratic ); std::vector<double> waveform; double mean = double(N) / 2; for ( int i = 0; i < N; ++i ) { boost::math::normal_distribution< double > nd( mean, double(N)/16 /* sd */); double y = boost::math::pdf( nd, i ) / boost::math::pdf( nd, mean ) + noise(); waveform.push_back( y ); } double a = waveform[0]; for ( int i = M / 2; i < N - M; ++i ) { double s = smoother( waveform.data() + i ); double dc = d1cubic( waveform.data() + i ); double dq = d1quadratic( waveform.data() + i ); std::cout << i << "\t" << std::fixed << std::setprecision(7) << waveform[i] << "\t" << s << "\t" << dc << "\t" << dq << std::endl; } }
int main () { int x = nd(); while (x >= 0) x = -2 * x + 10; return 0; }
void ParseTreeLablerForm::showNbrButtonClicked() { //get the text of the selected item const QModelIndex index = widget.treeView->selectionModel()->currentIndex(); QString selectedText = index.data(Qt::DisplayRole).toString(); string name = getCppString(selectedText); segNumToColor.clear(); Node nd(name); colorMapTableModel->clearAll(); if (nd.type == "Terminal") { set<int> & nbrs =nbrMap[nd.id]; for(set<int>::iterator it= nbrs.begin(); it!=nbrs.end(); it++) { float colorS=randColor(); segNumToColor[*it] =colorS; ColorRGB colorRGB(colorS); colorMapTableModel->addItem(string("Terminal__")+lexical_cast<string>(*it),QColor(colorRGB.r*255,colorRGB.g*255,colorRGB.b*255)); } colorSegs(segNumToColor,true); updatePCDVis(); } else { QMessageBox::warning(this, tr("Parse Tree Labaler"), tr("Neighbors can be shown only for a Terminal" "Please select a Terminal in the tree explorer"), QMessageBox::Ok,QMessageBox::Ok); return; } }
void AddQueries::addDataToNodePath( osg::NodePath& np, unsigned int numVertices, const osg::BoundingBox& bb ) { // For every Node in the NodePath that has a QueryCullCallback and a // QueryComputation object, add the number of vertices and the transformed // bounding box to that Node's QueryComputation. osg::NodePath localNP; osg::NodePath::reverse_iterator rit; for( rit=np.rbegin(); rit!=np.rend(); rit++ ) { QueryCullCallback* qcc = dynamic_cast< QueryCullCallback* >( (*rit)->getCullCallback() ); osgwQuery::QueryComputation* nd( NULL ); if( qcc != NULL ) nd = qcc->getQueryComputation(); if( nd != NULL ) { nd->setNumVertices( nd->getNumVertices() + numVertices ); osg::BoundingBox xformBB = osgwTools::transform( osg::computeLocalToWorld( localNP ), bb ); osg::BoundingBox ndBB = nd->getBoundingBox(); ndBB.expandBy( xformBB ); nd->setBoundingBox( ndBB ); } // Yuck. std::vector doesn't support push_back(). //localNP.push_front( *rit ); localNP.resize( localNP.size() + 1 ); unsigned int idx; for( idx=localNP.size()-1; idx>0; idx-- ) localNP[ idx ] = localNP[ idx-1 ]; localNP[ 0 ] = *rit; } }
void MainWindow::draw_spectrum() { x_.clear(); y0_.clear(); boost::math::normal_distribution<double> nd( to_time( 500 ), to_time( 5 ) ); double max = boost::math::pdf( nd, to_time( 500 ) ); const int b18fs = 0x3ffff / 16; qDebug() << boost::math::pdf( nd, to_time( 500 ) ) / max * 100; qDebug() << boost::math::pdf( nd, to_time( 500 - 5 ) ) / max * 100; qDebug() << boost::math::pdf( nd, to_time( 500 - 10 ) ) / max * 100; for ( int i = 0; i < 1000; ++i ) { x_.push_back( to_time( i ) ); double y = boost::math::pdf( nd, to_time( i ) ) / max; y0_.push_back( y ); y1_.push_back( int( y * b18fs ) / double(b18fs) ); } plot_->setData( &x_[0], &y0_[0], x_.size(), 0 ); plot_->setData( &x_[0], &y1_[0], x_.size(), 1 ); zoomer_->setZoomBase(); }
NeuralNetwork::NeuralNetwork(std::vector<int> sizes) : nlayers_(sizes.size()), sizes_(sizes) { random_gen_ = boost::mt11213b(time(0)); // 高斯分布生成器 boost::normal_distribution<> nd(0.0, 1.0); boost::variate_generator<boost::mt11213b&, boost::normal_distribution<>> var_gen(random_gen_, nd); // 初始化基向量 for (size_t i = 1; i < sizes_.size(); ++i) { // 每一层都有一个基向量,向量中的元素对应这一层的每一个神经元 Vector biases(sizes_[i]); for (int j = 0; j < sizes_[i]; ++j) { biases(j) = var_gen(); } biases_.push_back(biases); } // 初始化权重向量 for (size_t i = 0; i < sizes_.size() - 1; ++i) { // 相邻两个层之间都有一个权重矩阵,如果第 i 层和第 i + 1 层分别有 m 和 n 个神经元, // 那么两层之间的权重矩阵的维度为 m * n Matrix weights(sizes_[i + 1], sizes_[i]); for (int row = 0; row < sizes_[i + 1]; ++row) { for (int col = 0; col < sizes_[i]; ++ col) { weights(row, col) = var_gen(); } } weights_.push_back(weights); } if (biases_.size() != weights_.size()) { std::cout << "Biases and weights size not equal!"; } }
int wb_dbms::del_family(wb_dbms_txn *txn, wb_dbms_cursor *cp, pwr_tOid poid) { int ret = 0; #if 0 dbName name; name.poid = poid; name.normname = ""; pwr_tOid oid = 0; FamilyKey nk(po, ); FamiltData nd(&oid, sizeof(oid)); wb_dbms_cursor *ncp; cp->dup(*ncp, 0); while ((ret = cp->get(&nk, &nd, DB_NEXT)) != DB_NOTFOUND) { del_family(txn, ncp, oid); cp->del(); oh_k ok(nd); oh_d od(); m_dbms_ohead->get(txn, &ok, &od, 0); wb_DbClistK ck(od); m_db_class->del(txn, &ck, 0); wb_DbBodyK bk(od); m_db_obody->del(txn, &bk, 0); m_db_ohead->del(txn, &ok, 0); } ncp->close(); ret = m_db_name->del(txn, &key, 0); #endif return ret; }
void CopulaLatentMarkovNet::predict(vector<double>& prediction) { random_.reset(new CRandomMersenne(FLAGS_ep_randseed)); for (int i = 0; i < num_nodes_; ++i) if (observed_labels_[i] == 0) { // LOG(INFO) << "node " << i << ": " << m_by_ep_[i] << " " << v_by_ep_[i]; boost::math::normal nd(0.0, gmn_marginal_scale_[i]); boost::math::logistic ld(logistic_->get_mean(i), logistic_->scale_); double z = 0.0; for (int sample = 0; sample < FLAGS_num_marginal_samples; ++sample) { double u = random_->Random(); // LOG(INFO) << "sample " << sample << ": " << "u: "; boost::math::normal qnd(m_by_ep_[i], v_by_ep_[i]); double t = quantile(qnd, u); // LOG(INFO) << "t: " << t; u = cdf(nd, t); // LOG(INFO) << "u: " << u; if (u > 1.0-1e-10) u = 1.0 - 1e-10; else if (u < 1e-10) u = 1e-10; z += quantile(ld, u); } prediction.push_back(z/FLAGS_num_marginal_samples); /* double u = cdf(nd, m_by_ep_[i]); if (u > 1.0-1e-10) u = 1.0 - 1e-10; else if (u < 1e-10) u = 1e-10; prediction.push_back(quantile(ld, u)); */ } }
int main() { /** ** The idea is to add one (or more) counter(s) for each loop ** which are (lexicographically) strictly decreasing at each loop iteration ** and then check if their value is bounded from below. **/ int x = nd(); // -- the value of the counter gives a ranking function; // -- in this case the ranking function is a constant int c = 4; while (x >= 0) { // -- the ranking function is strictly decreasing c = c - 1; // -- the ranking function is lower bounded by zero sassert(c >= 0); x = - 2 * x + 10; } return 0; }
int main(int argc, char** argv) { double low_bound = -0.5; double up_bound = 0.5; double scale = 0.00001; boost::mt19937 rng; // I don't seed it on purpouse (it's not relevant) CHECK_LT(low_bound*scale, up_bound*scale)<<"union noise low_bound must less than up_bound"; //boost::normal_distribution<> nd(0.0, 1.0); boost::uniform_real<> nd(low_bound*scale, up_bound*scale); boost::shared_ptr<boost::variate_generator<boost::mt19937&, boost::uniform_real<> > > noise_random_ptr; noise_random_ptr.reset(new boost::variate_generator<boost::mt19937&, boost::uniform_real<> > (rng, nd)); for (int i = 0; i < 10; ++i) { double d = (*(noise_random_ptr))(); std::cout << d << std::endl; } return 0; }
bool Discovery::processMessage(const NodeAddr& remote, const char *message, std::vector<NodeDevice> &newNodes) { bool sendNow = false; std::string act((const char*)message); NodeDevice nd(remote); nd.name = std::string((const char*)&message[act.length() + 1]); nd.id = std::string((const char*)&message[act.length() + nd.name.length() + 2]); nd.sinceVitalSign = 0; // check if we somehow received our own broadcast if (nd.id == getHwAddress()) { return false; } if ("ULLTRA_DEV_R" == act) { // register auto ex = m_discovered.find(nd.id); if (ex != m_discovered.end()) { // already there? ex->second.sinceVitalSign = 0; return false; } LOG(logINFO) << "Discovered node " << nd; m_discovered.insert(std::pair<std::string, NodeDevice>(nd.id, nd)); send("ULLTRA_DEV_R", nd); // send a discovery message back sendNow = true; newNodes.push_back(m_discovered.find(nd.id)->second); } else if ("ULLTRA_DEV_Z" == act) { // unregister auto it = m_discovered.find(nd.id); if (it != m_discovered.end()) { LOG(logINFO) << "Lost node " << nd; if (onNodeLost) onNodeLost(it->second); m_discovered.erase(m_discovered.find(nd.id)); sendNow = true; } } else if ("ULLTRA_DEV_U" == act) { // heartbeat LOG(logINFO) << "Heartbeat node " << nd; //ProcessHeartbeat(remote.sin_addr, ((HeartBeatData*)&recvBuffer[10])); } else if (m_customHandlers.find(act) != m_customHandlers.end() && m_customHandlers[act]) { auto ex = m_discovered.find(nd.id); if (ex == m_discovered.end()) { LOG(logDEBUG1) << "Received custom message " << act << " from unknown " << nd; return false; } ex->second.sinceVitalSign = 0; LOG(logINFO) << "Custom handler for message " << act << " from node " << nd; m_customHandlers[act](ex->second); } else { LOG(logDEBUG1) << "Received unknown message from " << nd; } return sendNow; }
void my_interf::push() { system("cls"); if (tryb == 0) { node nd(0, "", 0, 0); cin >> nd; vect.push(nd); }
//================================================= void DynamicalGraph::AddRandomInteractions(Species* s) { std::uniform_real_distribution<double> uniform(0.0,1.0); std::normal_distribution<double> nd; for( auto pSpecies : m_species ) { if( uniform(*pRnd) < m_connectance ) { s->MakeInteractionWith( pSpecies, nd(*pRnd) ); } if( uniform(*pRnd) < m_connectance ) { pSpecies->MakeInteractionWith( s, nd(*pRnd) ); if( pSpecies->IsGoingExtinct() ) { m_dyingSpecies.insert(pSpecies); } } } if( s->IsGoingExtinct() ) { m_dyingSpecies.insert(s); } }
void foo () { for (int i=0; i < N; i++) { if (nd ()) p[i] = new B (3*i, 3*i); else p[i] = new C (5*i, 5*i); } }
int main() { int x = nd(); while (x <= 10) x = x + 1; return 0; }
void initLight(){ boost::mt19937 rng; // I don't seed it on purpouse (it's not relevant) boost::normal_distribution<> nd(0.0, 1.0); boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor(rng, nd); for (int i=0; i < 1024; ++i){ _aLight[i] = float(abs(var_nor())); } }
// To test loops int main(int argc, char**argv) { int i; int n = nd (); assume (n > 0); int * a = (int*) malloc(sizeof(int) * n); int *p; p = a; for (i = 0; i < n; i++) { p[i] = i; } // trick llvm so that it cannot detect overflow read(p[(nd () > 0 ? i-1 : i)]); return 0; }
void ParseTreeLablerForm::updateTypeCounts(string fullname) { Node nd(fullname); if(nd.updateTypeCounts(typeMaxId)) { widget.comboBox->addItem(QString(nd.type.data())); cerr<<"WARN:new types added from parsetree"<<endl; } }
double rnorm(double mean, double sd, boost::mt19937& rng) { boost::normal_distribution<> nd(mean, sd); boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor(rng, nd); double val = -1.0; while(val < 0) { val = var_nor(); } return val; }
pcl::PointCloud<pcl::PointXYZ> generateData(int data) { //normal distribution boost::mt19937 rng; rng.seed(clock()); boost::normal_distribution<> nd(0, 0.1*data); boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor(rng, nd); pcl::PointCloud<pcl::PointXYZ> pc; for(int i=0; i<100; i++) { pcl::PointXYZ p; p.x=(i-50)/50.; p.y=0; p.z=1; ADD_NOISE(); pc.push_back(p); p.y=1; ADD_NOISE(); pc.push_back(p); p.z=2; ADD_NOISE(); pc.push_back(p); p.y=0; ADD_NOISE(); pc.push_back(p); } for(int i=1; i<99; i++) { pcl::PointXYZ p; p.x=-1; p.y=0; p.z=1+i/100.; ADD_NOISE(); pc.push_back(p); p.x=1; ADD_NOISE(); pc.push_back(p); p.y=1; ADD_NOISE(); pc.push_back(p); p.x=-1; ADD_NOISE(); pc.push_back(p); } return pc; }
int main (){ int n = nd (); assume (n > 0); bar l (n); for (int i=0; i<l._n;i++) { l.update (i, 8); } return 0; }