P PointerTable<P, K, HF, KF>::insert(P p, Boolean replace) { size_t h; if (vec_.size() == 0) { vec_.assign(8, P(0)); usedLimit_ = 4; h = startIndex(KF::key(*p)); } else { for (h = startIndex(KF::key(*p)); vec_[h] != 0 ; h = nextIndex(h)) if (KF::key(*vec_[h]) == KF::key(*p)) { if (replace) { P tem(vec_[h]); vec_[h] = p; return tem; } else return vec_[h]; } if (used_ >= usedLimit_) { if (vec_.size() > size_t(-1)/2) { if (usedLimit_ == vec_.size() - 1) abort(); // FIXME throw an exception else usedLimit_ = vec_.size() - 1; } else { // rehash Vector<P> oldVec(vec_.size()*2, P(0)); vec_.swap(oldVec); usedLimit_ = vec_.size() / 2; for (size_t i = 0; i < oldVec.size(); i++) if (oldVec[i] != 0) { size_t j; for (j = startIndex(KF::key(*oldVec[i])); vec_[j] != 0; j = nextIndex(j)) ; vec_[j] = oldVec[i]; } for (h = startIndex(KF::key(*p)); vec_[h] != 0; h = nextIndex(h)) ; } } } used_++; vec_[h] = p; return 0; }
// constructor from data void NewtonEulerDSTest::testBuildNewtonEulerDS1() { std::cout << "--> Test: constructor 1." <<std::endl; SP::NewtonEulerDS ds(new NewtonEulerDS(q0, velocity0, mass, inertia )); double time = 1.5; ds->initialize(time); CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildNewtonEulerDS1A : ", Type::value(*ds) == Type::NewtonEulerDS, true); CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildNewtonEulerDS1B : ", ds->number() == 0, true); CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildNewtonEulerDS1D : ", ds->dimension() == 6, true); CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildNewtonEulerDS1D : ", ds->getqDim() == 7, true); CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildNewtonEulerDS1D : ", ds->scalarMass() == mass, true); SP::SimpleMatrix massMatrix(new SimpleMatrix(6,6)); massMatrix->setValue(0, 0, mass); massMatrix->setValue(1, 1, mass); massMatrix->setValue(2, 2, mass); Index dimIndex(2); dimIndex[0] = 3; dimIndex[1] = 3; Index startIndex(4); startIndex[0] = 0; startIndex[1] = 0; startIndex[2] = 3; startIndex[3] = 3; setBlock(inertia, massMatrix, dimIndex, startIndex); CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildNewtonEulerDS1D : ", *(ds->mass()) == *(massMatrix), true); CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildNewtonEulerDS1D : ", ds->computeKineticEnergy() == 595.0, true); std::cout << "--> Constructor 1 test ended with success." <<std::endl; }
/* * run the cross validation */ void Cvlars::run() { //search the first and last fold with the same size std::vector<int> startIndex(1,0),endIndex(1,k_-1); int k = 0; for(int i = 1; i < k_; i++) { if(sizePartition_[i]!= sizePartition_[startIndex[k]]) { startIndex.push_back(i); endIndex[k] = i-1; endIndex.push_back(k_-1); k++; } } //run for each size of fold for(int i = 0; i < (int) startIndex.size(); i++) subrun(startIndex[i],endIndex[i]); // compute mean prediction error for each index STK::CVectorX one(k_,1); cv_ = (residuals_ * one) / k_; // compute mean standard deviation of cv_ for each index for(int i = 1; i <= (int) index_.size(); i++) residuals_.row(i) -= cv_[i]; residuals_ = residuals_.square(); cvError_ = (residuals_ * one)/(k_-1)/k_; cvError_ = cvError_.sqrt(); }
const P &PointerTable<P, K, HF, KF>::lookup(const K &k) const { if (used_ > 0) { for (size_t i = startIndex(k); vec_[i] != 0; i = nextIndex(i)) if (KF::key(*vec_[i]) == k) return vec_[i]; } return null_; }
/** * Returns the location of the neighborhood in this._values. * * @param machineId Machine's id. * @return Index of its the neightborhood. */ uint getNeighborhoodIndex ( uint machineId ) const { // We jump size uint idx = startIndex() + 1; // We jump the previous machines idx += machineId*getDefinitionSize(); return idx; }
// --------------------------------------------------------------------------- // CSmileyIconRecord::DeleteIconsIn // --------------------------------------------------------------------------- // void CSmileyIconRecord::DeleteIconsIn( TInt aStart, TInt aLength ) { TInt startIndex( FirstIndexIn( aStart, aLength ) ); TInt endIndex( LastIndexIn( aStart, aLength, startIndex ) ); TInt count( endIndex - startIndex ); for ( TInt i( 0 ); startIndex != KErrNotFound && i <= count; i++ ) { CSmileyIcon* icon( iIconArray[startIndex] ); iIconArray.Remove( startIndex ); delete icon; } }
P PointerTable<P, K, HF, KF>::remove(const K &k) { if (used_ > 0) { for (size_t i = startIndex(k); vec_[i] != 0; i = nextIndex(i)) if (KF::key(*vec_[i]) == k) { P p = vec_[i]; do { vec_[i] = P(0); size_t j = i; size_t r; do { i = nextIndex(i); if (vec_[i] == 0) break; r = startIndex(KF::key(*vec_[i])); } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r)); vec_[j] = vec_[i]; } while (vec_[i] != 0); --used_; return p; } } return 0; }
double QxrdAcquisitionExtraInputsChannel::sumChannel() { QVector<double> res = readChannel(); double sum=0; int i0 = startIndex(); int i1 = endIndex(); for(int i=i0; i<i1; i++) { sum += res[i]; } return sum; }
double QxrdAcquisitionExtraInputsChannel::averageChannel() { QVector<double> res = readChannel(); double n=0; double sum=0; int i0 = startIndex(); int i1 = endIndex(); for(int i=i0; i<i1; i++) { sum += res[i]; n += 1; } return (n>0 ? sum/n : 0); }
void CSmileyInfo::SetSmileyText( const TDesC& aText ) { TInt startIndex( KErrNotFound ); for ( TInt i( 0 ); i <= aText.Length(); i++ ) { if ( ( i == aText.Length() || aText[i] == KSpace ) && startIndex != KErrNotFound ) { iStrArray.Append( aText.Mid( startIndex, i - startIndex ) ); startIndex = KErrNotFound; } if ( startIndex == KErrNotFound && i < aText.Length() && aText[i] != KSpace ) { startIndex = i; } } }
bool QxrdAcquisitionExtraInputsChannel::evalTrig(int polarity, bool edgeTrig) { double level = get_TriggerLevel(); double hyst = get_TriggerHysteresis(); bool tres; QVector<double> res = readChannel(); int nlow=0, nhigh=0; double tlevel = level*polarity; double lowlevel = tlevel-fabs(hyst); double highlevel = tlevel+fabs(hyst); int i0 = startIndex(); int i1 = endIndex(); for(int i=i0; i<i1; i++) { double v=res[i]*polarity; if (v < lowlevel) nlow += 1; if (edgeTrig) { if (nlow && (v > highlevel)) nhigh += 1; } else { if (v > highlevel) nhigh += 1; } } set_NLow(nlow); set_NHigh(nhigh); if (edgeTrig) { tres = ((nlow > 0) && (nhigh > 0)); } else { tres = (nhigh > 0); } return tres; }
double QxrdAcquisitionExtraInputsChannel::minimumChannel() { QVector<double> res = readChannel(); double min=0; int i0 = startIndex(); int i1 = endIndex(); for(int i=i0; i<i1; i++) { double v=res[i]; if (i == i0) { min = v; } else { if (v < min) { min = v; } } } return min; }
void ExternalInputSource::pushCharRef(Char ch, const NamedCharRef &ref) { ASSERT(cur() == start()); noteCharRef(startIndex() + (cur() - start()), ref); insertChar(ch); }
/** * Number of processes. * * @return number of processes. */ uint size () const { return _values [ startIndex() ]; }
/** * Returns the index of the service of the given * process. * * @param processId Process' id. * @return Index of process' service. */ uint getServiceStartIndex ( uint processId ) const { return startIndex() + 1 + processId*getDefinitionSize(); }
void Cvlars::run2() { //search the first and last fold with the same size std::vector<int> startIndex(1,0),endIndex(1,k_-1); int k = 0; for(int i = 1; i < k_; i++) { if(sizePartition_[i]!= sizePartition_[startIndex[k]]) { startIndex.push_back(i); endIndex[k] = i-1; endIndex.push_back(k_-1); k++; } } //run for each size of fold //create test and control container #pragma omp parallel { #pragma omp for schedule(dynamic,1) for(int i = 0; i < k_ ; i++) { STK::CArrayXX XControl( n_ - sizePartition_[i], p_); STK::CVectorX yControl( n_ - sizePartition_[i] ); STK::CArrayXX XTest(sizePartition_[i], p_); STK::CVectorX yTest(sizePartition_[i] ); STK::CVectorX yPred(sizePartition_[i] ); //fill the container int index = 1; int index2 = 1; for(int j = 1; j <= n_; j++) { if(partition_[j-1] != i) { yControl[index] = (*p_y_)[j]; XControl.row(index)=p_X_->row(j); index++; } else { yTest[index2] = (*p_y_)[j]; XTest.row(index2)=p_X_->row(j); index2++; } } //run lars on control data set HD::Lars lars(XControl,yControl,maxSteps_,intercept_,eps_); lars.run(); for(int s = 1 ; s <= (int) index_.size(); s++) { //we compute the prediction of the y associated to XTest lars.predict(XTest,index_[s-1], lambdaMode_, yPred); //compute the residuals residuals_(s,i+1) = (yPred-yTest).square().sum()/sizePartition_[i]; } } }//end parallel // compute mean prediction error for each index STK::CVectorX one(k_,1); cv_ = (residuals_ * one) / k_; // compute mean standard deviation of cv_ for each index for(int i = 1; i <= (int) index_.size(); i++) residuals_.row(i) -= cv_[i]; residuals_ = residuals_.square(); cvError_ = (residuals_ * one)/(k_-1)/k_; cvError_ = cvError_.sqrt(); }