// [[Rcpp::export]] SEXP HandMadeKalmanFilterConstantCoeffCpp(NumericVector a0 , NumericMatrix P0, NumericMatrix T, NumericMatrix Z , NumericMatrix HH, NumericMatrix GG, NumericMatrix yt) { // convert data to Eigen class Eigen::Map<Eigen::VectorXd > a0e(&a0[0], (size_t)(a0.size())); Eigen::Map<Eigen::VectorXd > P0e(&P0[0], P0.rows(), P0.cols()); Eigen::Map<Eigen::MatrixXd > Te(&T[0], T.rows(), T.cols()); Eigen::Map<Eigen::MatrixXd > Ze(&Z[0], Z.rows(), Z.cols()); Eigen::Map<Eigen::MatrixXd > HHe(&HH[0], HH.rows(), HH.cols()); Eigen::Map<Eigen::MatrixXd > GGe(&GG[0], GG.rows(), GG.cols()); // HMKF initialization block cout << T.rows() << " " << Z.rows() << " " << HH.rows() << " " << GG.rows() << endl; HMKF kf = HMKF(T.rows(), Z.rows()); kf.setModel(Te, Ze, HHe, GGe); kf.initialize(a0e, P0e); // filtering steps NumericVector x(yt.cols()), xp(yt.cols()), V(yt.cols()); for(int i=0; i!=yt.cols(); ++i){ kf.predict(); NumericMatrix::Column col = yt(_,i); // std::cout << "y:" << col[0] << std::endl; kf.filter(Map<Eigen::VectorXd >(&col[0], col.size())); // xp[i]= (kf.getxp())(0); x[i] = (kf.getx())(0); // V[i] = (kf.getV())(0,0); } return List::create(_("x") = x, _("xp") = xp, _("V") = V); }
void append( List fillVecs) { // "append" fill oldmat w/ // we will loop through cols, filling retmat in with the vectors in list // then update retmat_size to index the next free // newLenths isn't used, added for compatibility std::size_t sizeOld, sizeAdd, sizeNew, icol; NumericVector fillTmp; // check that number of vectors match if ( nvec != fillVecs.size()) { throw std::range_error("In append(): dimension mismatch"); } for (icol = 0; icol<nvec; icol++){ // vector to append fillTmp = fillVecs[icol]; // compute lengths sizeOld = lengths[icol]; sizeAdd = fillTmp.size(); sizeNew = sizeOld + sizeAdd; // grow data matrix as needed if ( sizeNew > allocLen) { grow(sizeNew); } // iterator for col to fill NumericMatrix::Column dataCol = data(_, icol); // fill row of return matrix, starting at first non-zero elem std::copy( fillTmp.begin(), fillTmp.end(), dataCol.begin() + sizeOld); // update size of retmat lengths[icol] = sizeNew; } }
// [[Rcpp::export]] double runit_NumericMatrix_column( NumericMatrix m ){ NumericMatrix::Column col = m.column(0) ; return std::accumulate( col.begin(), col.end(), 0.0 ) ; }