void DeepCopy(BigMatrix *pInMat, BigMatrix *pOutMat, SEXP rowInds, SEXP colInds) { in_BMAccessorType inMat( *pInMat ); out_BMAccessorType outMat( *pOutMat ); double *pRows = NUMERIC_DATA(rowInds); double *pCols = NUMERIC_DATA(colInds); index_type nRows = GET_LENGTH(rowInds); index_type nCols = GET_LENGTH(colInds); if (nRows != pOutMat->nrow()) Rf_error("length of row indices does not equal # of rows in new matrix"); if (nCols != pOutMat->ncol()) Rf_error("length of col indices does not equal # of cols in new matrix"); index_type i = 0; index_type j = 0; in_CType *pInColumn; out_CType *pOutColumn; for (i = 0; i < nCols; ++i) { pInColumn = inMat[static_cast<index_type>(pCols[i])-1]; pOutColumn = outMat[i]; for (j = 0; j < nRows; ++j) { pOutColumn[j] = static_cast<out_CType>( pInColumn[static_cast<index_type>(pRows[j])-1]); } } return; }
SEXP CombineSubMapsTransSimple(BigMatrix *oneVox_allSubs, SEXP allVoxs_allSubs, index_type seed, double *pVoxs, index_type nvoxs, index_type nsubs) { //using namespace Rcpp; BMAccessorType outMat( *oneVox_allSubs ); if (nvoxs != oneVox_allSubs->ncol()) ::Rf_error("nvoxs must equal oneVox_allSubs->ncol"); if (nsubs != oneVox_allSubs->nrow()) ::Rf_error("nsubs must equal oneVox_allSubs->nrow"); // loop through each subject's map index_type s = 0; index_type v = 0; index_type vv = 0; LDOUBLE x = 0; LDOUBLE delta = 0; LDOUBLE mean = 0; LDOUBLE M2 = 0; LDOUBLE stdev = 0; // CType *inCol; CType *outCol; LDOUBLE scaled_x; BigMatrix *allVoxs_oneSub; SEXP Rp; SEXP tmp; //RObject RallVoxs_oneSub; for (s=0; s < nsubs; ++s) { PROTECT(tmp = VECTOR_ELT(allVoxs_allSubs, s)); //RallVoxs_oneSub(tmp); //Rp = RallVoxs_oneSub.slot("address"); PROTECT(Rp = GET_SLOT(tmp, install("address"))); //tmp = allVoxs_allSubs[s]; //RObject RallVoxs_oneSub(tmp); //Rp = RallVoxs_oneSub.slot("address"); allVoxs_oneSub = reinterpret_cast<BigMatrix*>(R_ExternalPtrAddr(Rp)); UNPROTECT(2); BMAccessorType inMat( *allVoxs_oneSub ); for (v=0; v < nvoxs; ++v) { vv = static_cast<index_type>(pVoxs[v]-1); outMat[v][s] = static_cast<CType>(inMat[vv][seed]); } } return R_NilValue; }
void densCalc(NcVar * inVar, DataMatrix<double> & outMat){ int tLen,latLen,lonLen; double invtLen; double selfDiv; tLen = inVar->get_dim(0)->size(); invtLen = 1.0/((double) tLen); latLen = inVar->get_dim(1)->size(); lonLen = inVar->get_dim(2)->size(); DataMatrix3D<double> inMat(tLen,latLen,lonLen); inVar->set_cur(0,0,0); inVar->get((&inMat[0][0][0]), tLen,latLen,lonLen); //DataMatrix<double> outMat(latLen,lonLen); for (int i=0; i<latLen; i++){ for (int j=0; j<lonLen; j++){ outMat[i][j] = 0.0; } } //Divide all cells by self and average over time for (int t=0; t<tLen; t++){ for (int i=0; i<latLen; i++){ for (int j=0; j<lonLen; j++){ if (std::fabs(inMat[t][i][j])>0.0){ outMat[i][j]+=1.0; } } } } for (int i=0; i<latLen; i++){ for (int j=0; j<lonLen; j++){ outMat[i][j]*=invtLen; } } // outVar->set_cur(0,0); //outVar->put((&outMat[0][0]),latLen,lonLen); }
bool floodShouldPushPoint(const cv::Point &np, const cv::Mat &mat) { return inMat(np, mat.rows, mat.cols); }
SEXP CombineSubMaps(BigMatrix *oneVox_allSubs, SEXP allVoxs_allSubs, index_type seed, double *pVoxs, index_type nvoxs, index_type nsubs) { //using namespace Rcpp; BMAccessorType outMat( *oneVox_allSubs ); if (nsubs != oneVox_allSubs->ncol()) Rf_error("nsubs must equal oneVox_allSubs->ncol"); if (nvoxs != oneVox_allSubs->nrow()) Rf_error("nsubs must equal oneVox_allSubs->nrow"); // loop through each subject's map index_type s = 0; index_type v = 0; index_type vv = 0; LDOUBLE x = 0; LDOUBLE delta = 0; LDOUBLE mean = 0; LDOUBLE M2 = 0; LDOUBLE stdev = 0; // CType *inCol; CType *outCol; LDOUBLE scaled_x; BigMatrix *allVoxs_oneSub; SEXP Rp; SEXP tmp; //RObject RallVoxs_oneSub; for (s=0; s < nsubs; ++s) { PROTECT(tmp = VECTOR_ELT(allVoxs_allSubs, s)); //RallVoxs_oneSub(tmp); //Rp = RallVoxs_oneSub.slot("address"); PROTECT(Rp = GET_SLOT(tmp, install("address"))); //tmp = allVoxs_allSubs[s]; //RObject RallVoxs_oneSub(tmp); //Rp = RallVoxs_oneSub.slot("address"); allVoxs_oneSub = reinterpret_cast<BigMatrix*>(R_ExternalPtrAddr(Rp)); UNPROTECT(2); BMAccessorType inMat( *allVoxs_oneSub ); // inCol = inMat[seed]; delta = mean = M2 = stdev = 0; for (v=0; v < nvoxs; ++v) { // todo: add checking for NaN...but shouldn't really have any! // maybe can also pass the exact list of voxs to loop through! // if (!ISNAN(pColumn[curj])) // NA_REAL vv = static_cast<index_type>(pVoxs[v]-1); x = static_cast<LDOUBLE>(inMat[vv][seed]); delta = x - mean; mean = mean + delta/static_cast<LDOUBLE>(v+1); M2 = M2 + delta*(x - mean); } stdev = sqrt(M2/(static_cast<LDOUBLE>(nvoxs-1))); //printf("mean: %f; stdev: %f\n", mean, stdev); outCol = outMat[s]; for (v=0; v < nvoxs; ++v) { vv = static_cast<index_type>(pVoxs[v]-1); scaled_x = (static_cast<LDOUBLE>(inMat[vv][seed])-mean)/stdev; outCol[v] = static_cast<CType>(scaled_x); } } return R_NilValue; }
void expm_eigen(int n, double *rz, double *out) { Eigen::Map< Eigen::MatrixXd > inMat(rz, n, n); Eigen::Map< Eigen::MatrixXd > outMat(out, n, n); outMat = inMat.exp(); }