void RadialBasisInterpolation<KDDim,RBF>::interpolate_field_data (const std::vector<std::string> &field_names, const std::vector<Point> &tgt_pts, std::vector<Number> &tgt_vals) const { START_LOG ("interpolate_field_data()", "RadialBasisInterpolation<>"); libmesh_experimental(); const unsigned int n_vars = this->n_field_variables(), n_src_pts = this->_src_pts.size(), n_tgt_pts = tgt_pts.size(); libmesh_assert_equal_to (_weights.size(), this->_src_vals.size()); libmesh_assert_equal_to (field_names.size(), this->n_field_variables()); // If we already have field variables, we assume we are appending. // that means the names and ordering better be identical! if (this->_names.size() != field_names.size()) { libMesh::err << "ERROR: when adding field data to an existing list the\n" << "varaible list must be the same!\n"; libmesh_error(); } for (unsigned int v=0; v<this->_names.size(); v++) if (_names[v] != field_names[v]) { libMesh::err << "ERROR: when adding field data to an existing list the\n" << "varaible list must be the same!\n"; libmesh_error(); } RBF rbf(_r_bbox); tgt_vals.resize (n_tgt_pts*n_vars); /**/ std::fill (tgt_vals.begin(), tgt_vals.end(), Number(0.)); for (unsigned int tgt=0; tgt<n_tgt_pts; tgt++) { const Point &p (tgt_pts[tgt]); for (unsigned int i=0; i<n_src_pts; i++) { const Point &x_i(_src_pts[i]); const Real r_i = (p - x_i).size(), phi_i = rbf(r_i); for (unsigned int var=0; var<n_vars; var++) tgt_vals[tgt*n_vars + var] += _weights[i*n_vars + var]*phi_i; } } STOP_LOG ("interpolate_field_data()", "RadialBasisInterpolation<>"); }
TestKernelWindow::TestKernelWindow() { std::cout<<" test kernel "; std::cout<<"\n build X"; DenseMatrix<float> X(30,1); linspace<float>(X.column(0), -1.f, 1.f, 30); std::cout<<"\n build kernel"; RbfKernel<float> rbf(0.33); Covariance<float, RbfKernel<float> > cov; cov.create(X, rbf); m_kernView = new KernelWidget(this); m_kernView->plotK(&cov.K()); setCentralWidget(m_kernView); QDateTime local(QDateTime::currentDateTime()); qDebug() << "Local time is:" << local; srand (local.toTime_t() ); m_smpdlg = new SampleKernelDialog(cov, this); setWindowTitle(tr("RBF Kernel")); createActions(); createMenus(); qRegisterMetaType<QImage>("QImage"); //connect(m_thread, SIGNAL(sendInitialDictionary(QImage)), // this, SLOT(recvInitialDictionary(QImage))); m_smpdlg->show(); m_smpdlg->move(0,0); }
Tchar getch0(void) { Tchar i; again: if (pbp > lastpbp) i = *--pbp; else if (ip) { /* i = rbf(); */ i = rbf0(ip); if (i == 0) i = rbf(); else { ++ip; if (pastend(ip)) { --ip; rbf(); } } } else { if (donef || ndone) done(0); if (nx || 1) { /* BUG: was ibufp >= eibuf, so EOF test is wrong */ if (nfo < 0) ERROR "in getch0, nfo = %d", nfo WARN; if (nfo == 0) { g0: if (nextfile()) { if (ip) goto again; } } nx = 0; #ifdef UNICODE if (MB_CUR_MAX > 1) i = get1ch(ifile); else #endif /*UNICODE*/ i = getc(ifile); if (i == EOF) goto g0; if (ip) goto again; } if (i >= 040) /* zapped: && i < 0177 */ goto g4; i = ifilt[i]; } if (cbits(i) == IMP && !raw) goto again; if (i == 0 && !init && !raw) { /* zapped: || i == 0177 */ goto again; } g4: if (ismot(i)) return i; if (copyf == 0 && sfbits(i) == 0) i |= chbits; if (cbits(i) == eschar && !raw) setcbits(i, ESC); return(i); }
int main( int argc, char* argv[] ) { Eigen::MatrixXf A = Eigen::MatrixXf::Zero(size,size); Eigen::VectorXf b = Eigen::VectorXf::Zero(size); boost_mat_dense A2(size,size); boost_Vector b2(size); for(unsigned int i = 0;i<size;i++){ glm::vec4 p(rand() / float(RAND_MAX),rand() / float(RAND_MAX),rand() / float(RAND_MAX),0.5); p *= 2; p -= 1; float r = rand() / float(RAND_MAX); r = r * 0.2f - 0.1f; p = glm::normalize(p) * r; p.w = r; points.push_back(p); } for(unsigned int i = 0;i<size;i++){ b(i) = points[i].w; b2(i) = points[i].w; for(int j = i;j<size;j++){ A(i,j) = rbf(glm::distance(glm::vec3(points[i]),glm::vec3(points[j]))); A(j,i) = A(i,j); A2(i,j) = A(i,j); A2(j,i) = A(i,j); } } std::vector<Result> res; /* { StopClock sc(true); auto v2 = testGMRES(A2,b2); sc.stop(); Result r; r.name = "GMRES"; r.seconds = sc.getFractionElapsedSeconds(); boost_Vector result = boost::numeric::ublas::prod( A2,v2) - b2; r.error = boost::numeric::ublas::norm_2(result); r.relError = r.error / b.norm(); //res.push_back(r); } ///*/ //ConjugateGradient { StopClock sc(true); auto v = testEigen2(A,b); sc.stop(); Result r; r.name = "Eigen::ConjugateGradient"; r.seconds = sc.getFractionElapsedSeconds(); r.error = (A*v - b).norm(); r.relError = r.error / b.norm(); //res.push_back(r); } { StopClock sc(true); auto v = testEigen<Eigen::PartialPivLU<Eigen::MatrixXf>>(A,b); sc.stop(); Result r; r.name = "Eigen::PartialPivLU"; r.seconds = sc.getFractionElapsedSeconds(); r.error = (A*v - b).norm(); r.relError = r.error / b.norm(); //res.push_back(r); } { StopClock sc(true); auto v = testEigen<Eigen::FullPivLU<Eigen::MatrixXf>>(A,b); sc.stop(); Result r; r.name = "Eigen::FullPivLU"; r.seconds = sc.getFractionElapsedSeconds(); r.error = (A*v - b).norm(); r.relError = r.error / b.norm(); //res.push_back(r); } { StopClock sc(true); auto v = testEigen<Eigen::HouseholderQR<Eigen::MatrixXf>>(A,b); sc.stop(); Result r; r.name = "Eigen::HouseholderQR"; r.seconds = sc.getFractionElapsedSeconds(); r.error = (A*v - b).norm(); r.relError = r.error / b.norm(); //res.push_back(r); } { StopClock sc(true); auto v = testEigen<Eigen::ColPivHouseholderQR<Eigen::MatrixXf>>(A,b); sc.stop(); Result r; r.name = "Eigen::ColPivHouseholderQR"; r.seconds = sc.getFractionElapsedSeconds(); r.error = (A*v - b).norm(); r.relError = r.error / b.norm(); //res.push_back(r); } { StopClock sc(true); auto v = testEigen<Eigen::FullPivHouseholderQR<Eigen::MatrixXf>>(A,b); sc.stop(); Result r; r.name = "Eigen::FullPivHouseholderQR"; r.seconds = sc.getFractionElapsedSeconds(); r.error = (A*v - b).norm(); r.relError = r.error / b.norm(); //res.push_back(r); } { StopClock sc(true); auto v = testEigen<Eigen::LLT<Eigen::MatrixXf>>(A,b); sc.stop(); Result r; r.name = "Eigen::LLT"; r.seconds = sc.getFractionElapsedSeconds(); r.error = (A*v - b).norm(); r.relError = r.error / b.norm(); //res.push_back(r); } { StopClock sc(true); auto v = testEigen<Eigen::LDLT<Eigen::MatrixXf>>(A,b); sc.stop(); Result r; r.name = "Eigen::LDLT"; r.seconds = sc.getFractionElapsedSeconds(); r.error = (A*v - b).norm(); r.relError = r.error / b.norm(); //res.push_back(r); } return 0; }
void UnitCoarsening::compute( std::shared_ptr<RBFFunctionInterface> rbfFunction, std::unique_ptr<ElDistVector> positions, std::unique_ptr<ElDistVector> positionsInterpolation ) { // selection is only performed once. Thereafter, the selected points are used for the // interpolation. selectedPositions.clear(); // An initial selection is needed before the greedy algorithm starts // adding points to the selection. // The first point is the point with the largest radius from the origin. // The second point is the point with the largest distance from the // first point. { // Find first point: largest radius from origin ElDistVector norms( positions->Grid() ); norms.AlignWith( *positions ); El::RowTwoNorms( *positions, norms ); El::Entry<double> locMax = El::MaxAbsLoc( norms ); selectedPositions.push_back( locMax.i ); // Find second point: largest distance from the first point ElDistVector distance = *positions; ElDistVector tmp( distance.Grid() ); tmp.AlignWith( distance ); El::Ones( tmp, distance.Height(), distance.Width() ); for ( int iColumn = 0; iColumn < tmp.Width(); iColumn++ ) { ElDistVector view( tmp.Grid() ); El::View( view, tmp, 0, iColumn, tmp.Height(), 1 ); El::Scale( positions->Get( locMax.i, iColumn ), view ); } El::Axpy( -1, tmp, distance ); El::RowTwoNorms( distance, norms ); locMax = El::MaxAbsLoc( norms ); selectedPositions.push_back( locMax.i ); } int maxPoints = std::min( this->maxPoints, positions->Height() ); int minPoints = std::min( this->minPoints, positions->Height() ); double largestError = 0; for ( int i = 0; i < maxPoints; i++ ) { // Build the matrices for the RBF interpolation std::unique_ptr<ElDistVector> positionsCoarse( new ElDistVector( positions->Grid() ) ); std::unique_ptr<ElDistVector> valuesCoarse( new ElDistVector( positions->Grid() ) ); positionsCoarse->AlignWith( *positions ); valuesCoarse->AlignWith( *positions ); El::Ones( *valuesCoarse, selectedPositions.size(), positions->Width() ); El::Zeros( *positionsCoarse, selectedPositions.size(), positions->Width() ); selectData( positions, positionsCoarse ); // Perform the RBF interpolation std::unique_ptr<ElDistVector> positionsInterpolationCoarse( new ElDistVector( positions->Grid() ) ); *positionsInterpolationCoarse = *positions; ElRBFInterpolation rbf( rbfFunction, std::move( positionsCoarse ), std::move( positionsInterpolationCoarse ) ); std::unique_ptr<ElDistVector> result = rbf.interpolate( valuesCoarse ); assert( result->Height() == positions->Height() ); assert( result->Width() == positions->Width() ); // Compute the error ElDistVector diff( result->Grid() ); diff.AlignWith( *result ); El::Ones( diff, result->Height(), result->Width() ); El::Axpy( -1, *result, diff ); ElDistVector errors( diff.Grid() ); errors.AlignWith( diff ); El::RowTwoNorms( diff, errors ); // Get location of max error El::Entry<double> locMax = El::MaxAbsLoc( errors ); largestError = locMax.value; // Break if maximum points are reached if ( int( selectedPositions.size() ) >= maxPoints ) break; bool convergence = locMax.value < tol && int( selectedPositions.size() ) >= minPoints; if ( convergence ) break; selectedPositions.push_back( locMax.i ); } if ( El::mpi::Rank() == 0 ) { std::cout << "RBF interpolation coarsening: selected "; std::cout << selectedPositions.size() << "/" << positions->Height() << " points,"; std::cout << " error = " << largestError << ", tol = " << tol << std::endl; } std::unique_ptr<ElDistVector> positionsCoarse( new ElDistVector( positions->Grid() ) ); positionsCoarse->AlignWith( *positions ); El::Zeros( *positionsCoarse, selectedPositions.size(), positions->Width() ); selectData( positions, positionsCoarse ); rbf.compute( rbfFunction, std::move( positionsCoarse ), std::move( positionsInterpolation ) ); }
void RadialBasisInterpolation<KDDim,RBF>::prepare_for_use() { // Call base class methods for prep InverseDistanceInterpolation<KDDim>::prepare_for_use(); InverseDistanceInterpolation<KDDim>::construct_kd_tree(); #ifndef LIBMESH_HAVE_EIGEN libmesh_error_msg("ERROR: this functionality presently requires Eigen!"); #else START_LOG ("prepare_for_use()", "RadialBasisInterpolation<>"); // Construct a bounding box for our source points _src_bbox.invalidate(); const unsigned int n_src_pts = this->_src_pts.size(); const unsigned int n_vars = this->n_field_variables(); libmesh_assert_equal_to (this->_src_vals.size(), n_src_pts*this->n_field_variables()); { Point &p_min(_src_bbox.min()), &p_max(_src_bbox.max()); for (unsigned int p=0; p<n_src_pts; p++) { const Point &p_src(_src_pts[p]); for (unsigned int d=0; d<LIBMESH_DIM; d++) { p_min(d) = std::min(p_min(d), p_src(d)); p_max(d) = std::max(p_max(d), p_src(d)); } } } libMesh::out << "bounding box is \n" << _src_bbox.min() << '\n' << _src_bbox.max() << std::endl; // Construct the Radial Basis Function, giving it the size of the domain if(_r_override < 0) _r_bbox = (_src_bbox.max() - _src_bbox.min()).size(); else _r_bbox = _r_override; RBF rbf(_r_bbox); libMesh::out << "bounding box is \n" << _src_bbox.min() << '\n' << _src_bbox.max() << '\n' << "r_bbox = " << _r_bbox << '\n' << "rbf(r_bbox/2) = " << rbf(_r_bbox/2) << std::endl; // Construct the projection Matrix typedef Eigen::Matrix<Number, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> DynamicMatrix; //typedef Eigen::Matrix<Number, Eigen::Dynamic, 1, Eigen::ColMajor> DynamicVector; DynamicMatrix A(n_src_pts, n_src_pts), x(n_src_pts,n_vars), b(n_src_pts,n_vars); for (unsigned int i=0; i<n_src_pts; i++) { const Point &x_i (_src_pts[i]); // Diagonal A(i,i) = rbf(0.); for (unsigned int j=i+1; j<n_src_pts; j++) { const Point &x_j (_src_pts[j]); const Real r_ij = (x_j - x_i).size(); A(i,j) = A(j,i) = rbf(r_ij); } // set source data for (unsigned int var=0; var<n_vars; var++) b(i,var) = _src_vals[i*n_vars + var]; } // Solve the linear system x = A.ldlt().solve(b); //x = A.fullPivLu().solve(b); // save the weights for each variable _weights.resize (this->_src_vals.size()); for (unsigned int i=0; i<n_src_pts; i++) for (unsigned int var=0; var<n_vars; var++) _weights[i*n_vars + var] = x(i,var); STOP_LOG ("prepare_for_use()", "RadialBasisInterpolation<>"); #endif }
static void tmtmcwr(int ab, int tmc, int wr, int ep, int tmm) { const char tmtab[] = { 'a',000,000,000,000,000,000,000, 000,000,000,000,000,000,000,000, '{','}','&',000,'%','c','e',' ', '!',000,000,000,000,000,000,'~', 000 }; struct contab *cp; register int i, j; tchar c; char tmbuf[NTM]; filep savip = ip; int discard = 0; lgf++; if (tmm) { if (skip(1) || (i = getrq(0)) == 0) return; if ((cp = findmn(i)) == NULL || !cp->mx) { nosuch(i); return; } savip = ip; ip = (filep)cp->mx; app++; copyf++; } else { copyf++; if (skip(0) && ab) errprint("User Abort"); } loop: for (i = 0; i < NTM - 5 - mb_cur_max; ) { if (tmm) { if ((c = rbf()) == 0) { ip = savip; tmm = 0; app--; break; } } else c = getch(); if (discard) { discard--; continue; } if (c == '\n') { tmbuf[i++] = '\n'; break; } c: j = cbits(c); if (iscopy(c)) { int n; if ((n = wctomb(&tmbuf[i], j)) > 0) { i += n; continue; } } if (xflag == 0) { tmbuf[i++] = c; continue; } if (ismot(c)) continue; tmbuf[i++] = '\\'; if (c == (OHC|BLBIT)) j = ':'; else if (istrans(c)) j = ')'; else if (j >= 0 && j < sizeof tmtab && tmtab[j]) j = tmtab[j]; else if (j == ACUTE) j = '\''; else if (j == GRAVE) j = '`'; else if (j == UNDERLINE) j = '_'; else if (j == MINUS) j = '-'; else { i--; if (c == WORDSP) j = ' '; else if (j == WORDSP) continue; else if (j == FLSS) { discard++; continue; } } if (j == XFUNC) switch (fbits(c)) { case CHAR: c = charout[sbits(c)].ch; goto c; default: continue; } tmbuf[i++] = j; } if (i == NTM - 2) tmbuf[i++] = '\n'; if (tmc) i--; tmbuf[i] = 0; if (ab) /* truncate output */ obufp = obuf; /* should be a function in n2.c */ if (ep) { flusho(); errprint("%s", tmbuf); } else if (wr < 0) { flusho(); fdprintf(stderr, "%s", tmbuf); } else if (i) write(wr, tmbuf, i); if (tmm) goto loop; copyf--; lgf--; }
void pchar1(register tchar i) { static int _olt; tchar _olp[1]; register int j; filep savip; extern void ptout(tchar); j = cbits(i); if (dip != &d[0]) { if (i == FLSS) dip->flss++; else if (dip->flss > 0) dip->flss--; else if (!ismot(i) && (cbits(i) > 32 || cbits(i) == XFUNC) && !tflg) i |= DIBIT; wbf(i); dip->op = offset; return; } if (!tflg && !print) { if (j == '\n') dip->alss = dip->blss = 0; return; } if (no_out) return; if (tflg) { /* transparent mode, undiverted */ outtp(i); return; } if (cbits(i) == XFUNC) { switch (fbits(i)) { case OLT: olt = realloc(olt, (nolt + 1) * sizeof *olt); _olt = 1; return; case CHAR: #ifndef NROFF if (!ascii) break; #endif /* !NROFF */ savip = ip; ip = charout[sbits(i)].op; app++; fmtchar++; while ((i = rbf()) != 0 && cbits(i) != '\n' && cbits(i) != FLSS) pchar(i); fmtchar--; app--; ip = savip; return; } } if (cbits(i) == 'x') fmtchar = fmtchar; if (_olt) { _olp[0] = i; olt[nolt++] = fetchrq(_olp); _olt = 0; } #ifndef NROFF if (ascii) outascii(i); else #endif ptout(i); }