/** * direction: 0 -- copy values from @values to @ptr, * 1 -- copy values from @ptr to @values... */ SEXP assignDoubleMatrix(SEXP ptr, SEXP obsIndexes, SEXP varIndexes, SEXP values, SEXP direction){ //flush(cout); unsigned long varNum, obsNum, obsIndexNum, varIndexNum; AbstractMatrix * p = getAbstractMatrixFromSEXP(ptr); double coeff = 1. * length(obsIndexes) / p->getNumObservations(); unsigned long dir = (unsigned long) INTEGER(direction)[0]; double *currentValues = 0; if (!(coeff < WRITE_SPEED_PROPORTION)) { currentValues = new double[p->getNumObservations()]; } unsigned long varIndexesLength = length(varIndexes); unsigned long obsIndexesLength = length(obsIndexes); for(varIndexNum = 0; varIndexNum < varIndexesLength; varIndexNum ++){ varNum = (unsigned long)INTEGER(varIndexes)[varIndexNum]-1; if ( coeff < WRITE_SPEED_PROPORTION) { for(obsIndexNum = 0; obsIndexNum < obsIndexesLength; obsIndexNum ++){ obsNum = (unsigned long)INTEGER(obsIndexes)[obsIndexNum]-1; try { if (dir==0) { double value = REAL(values)[varIndexNum *obsIndexesLength + obsIndexNum]; p->writeElementAs(varNum, obsNum,value); } else { double value; p->readElementAs(varNum, obsNum,value); REAL(values)[varIndexNum *obsIndexesLength + obsIndexNum] = value; } } catch(int errorCode) { return R_NilValue; } } } else { try { if (dir==0) { p->readVariableAs(varNum, currentValues); for(obsIndexNum = 0; obsIndexNum < obsIndexesLength; obsIndexNum ++){ obsNum = (unsigned long)INTEGER(obsIndexes)[obsIndexNum] - 1; currentValues[obsNum] = REAL(values)[varIndexNum*obsIndexesLength+obsIndexNum]; } p->writeVariableAs(varNum, currentValues); } else { p->readVariableAs(varNum, currentValues); for(obsIndexNum = 0; obsIndexNum < obsIndexesLength; obsIndexNum ++){ obsNum = (unsigned long)INTEGER(obsIndexes)[obsIndexNum] - 1; REAL(values)[varIndexNum*obsIndexesLength+obsIndexNum] = currentValues[obsNum]; } } } catch(int errorCode){ delete [] currentValues; return R_NilValue; } } } if (!(coeff < WRITE_SPEED_PROPORTION)) { delete [] currentValues; } SEXP ret; PROTECT(ret = allocVector(LGLSXP, 1)); LOGICAL(ret)[0] = TRUE; UNPROTECT(1); //flush(cout); return ret; }
void CorrectnessTest::testSubMatrix() { string fileName = getInputFileName(); string subMatrixFileName = fileName + string(".fvf_submatrix"); string obsFileName = fileName + string(".fvf_obsnames"); string varFileName = fileName + string(".fvf_varnames"); testDbg << "obsFileName = " << obsFileName << endl; testDbg << "subMatrixFileName = " << subMatrixFileName << endl; AbstractMatrix *data = new FileVector ( fileName, 64 ); ifstream subMatrixData(subMatrixFileName.c_str()); ifstream obsNamesData(obsFileName.c_str()); ifstream varNamesData(varFileName.c_str()); CPPUNIT_ASSERT(subMatrixData.good()); CPPUNIT_ASSERT(obsNamesData.good()); CPPUNIT_ASSERT(varNamesData.good()); testDbg << "Reading file:" << fileName << endl; unsigned long numVariables = data->getNumVariables(); unsigned long numObservations = data->getNumObservations(); unsigned long i; testDbg << "Reading observations' names from " << obsFileName << endl; map<string, unsigned long> obsToIdx; for (i=0; i<numObservations; i++) { string obsName; obsNamesData >> obsName; obsToIdx[obsName] = i; } testDbg << "Reading variables' names from " << varFileName << endl; map<string, unsigned long> varToIdx; for (i=0; i<numVariables; i++) { string varName; varNamesData >> varName; varToIdx[varName] = i; } // indexes in order, specified in _submatrix file. vector<string> obsIdxesNames; testDbg << "Matrix size is " << data->getNumObservations() << " x " << data->getNumVariables() << endl; string obsNames; getline(subMatrixData, obsNames); tokenize(obsNames, obsIdxesNames); testDbg << "Submatrix width is:" << obsIdxesNames.size() << endl; vector<unsigned long> obsIdexes; for (i=0; i<obsIdxesNames.size(); i++){ obsIdexes.push_back(obsToIdx[obsIdxesNames[i]]); } vector<unsigned long> varIdxes; string subMatrixString; while (getline(subMatrixData, subMatrixString)) { string varName; vector<string> subMatrixElements; tokenize(subMatrixString, subMatrixElements); varName = subMatrixElements[0]; unsigned long varIdx = varToIdx[varName]; for (i = 0; i < obsIdxesNames.size(); i++) { double matrixElem; double submatrixElem; submatrixElem = atof(subMatrixElements[i+1].c_str()); data->readElementAs(varIdx, obsIdexes[i], matrixElem); double relDiff = TestUtil::relativeDifference(matrixElem, submatrixElem); CPPUNIT_ASSERT( relDiff = 0./0. || relDiff < 1E-4 ); } } delete data; }