void testIteration() { IntArray a; assert(a.begin() == a.end()); assert(a.rbegin() == a.rend()); a = IntArray(10000); for (int i = 0; i < 10000; i++) { a[i] = i; } int counter = 0; IntArray::iterator aIter; for (aIter = a.begin(); aIter != a.end(); ++aIter) { assert(*aIter == counter); ++counter; } for (IntArray::reverse_iterator aReverseIter = a.rbegin(); aReverseIter != a.rend(); ++aReverseIter) { --counter; --aIter; assert(*aReverseIter == counter); } assert(aIter == a.begin()); }
//显示vector容器中的元素 void put_vector(IntArray v) { IntArray::iterator theIterator; for (theIterator=v.begin();theIterator!=v.end();++theIterator){ cout<<(*theIterator)<<" "; } }
void Galois::mulPoly(IntArray &seki, IntArray &a, IntArray &b) const { fill(seki.begin(),seki.end(), 0); for (unsigned int ia = 0; ia < a.size(); ia++) { if(a[ia] != 0) { int loga = logTbl[a[ia]]; int ib2 = min(b.size(), seki.size() - ia); for(int ib = 0; ib < ib2; ib++) { if(b[ib] != 0) { seki[ia + ib] ^= expTbl[loga + logTbl[b[ib]]]; } } } } }
void testAccess() { const IntArray a = {1, 2, 3}; assert(a[0] == 1); assert(a[1] == 2); assert(a[2] == 3); assert(a.at(0) == 1); assert(a.at(1) == 2); assert(a.at(2) == 3); IntArray b; b.assign({7, 7, 7, 7, 7}); assert(b[4] == 7); assert(b.at(4) == 7); b.assign(a.begin(), a.end()); assert(b == a); b.clear(); assert(b.size() == 0); }
static bool CollideGeoms(const SweepTest* sweep_test, const SweptVolume& volume, const IntArray& geom_stream, const NxExtendedVec3& center, const NxVec3& dir, SweptContact& impact) { impact.mIndex = INVALID_ID; impact.mGeom = NULL; static const NxU32 GeomSizes[] = { sizeof(TouchedUserBox), sizeof(TouchedUserCapsule), sizeof(TouchedMesh), sizeof(TouchedBox), sizeof(TouchedSphere), sizeof(TouchedCapsule), }; bool Status = false; const NxU32* Data = geom_stream.begin(); const NxU32* Last = geom_stream.end(); while(Data!=Last) { TouchedGeom* CurrentGeom = (TouchedGeom*)Data; SweepFunc ST = gSweepMap[volume.GetType()][CurrentGeom->mType]; if(ST) { SweptContact C; C.mDistance = impact.mDistance; // Initialize with current best distance C.mIndex = INVALID_ID; if((ST)(sweep_test, &volume, CurrentGeom, center, dir, C)) { if(C.mDistance<impact.mDistance) { impact = C; impact.mGeom = CurrentGeom; Status = true; } } } NxU8* ptr = (NxU8*)Data; ptr += GeomSizes[CurrentGeom->mType]; Data = (const NxU32*)ptr; } return Status; }
NM_Status InverseIteration :: solve(SparseMtrx &a, SparseMtrx &b, FloatArray &_eigv, FloatMatrix &_r, double rtol, int nroot) { FILE *outStream; if ( a.giveNumberOfColumns() != b.giveNumberOfColumns() ) { OOFEM_ERROR("matrices size mismatch"); } SparseLinearSystemNM *solver = GiveClassFactory().createSparseLinSolver(ST_Direct, domain, engngModel); int nn = a.giveNumberOfColumns(); int nc = min(2 * nroot, nroot + 8); nc = min(nc, nn); //// control of diagonal zeroes in mass matrix, to be avoided //int i; //for (i = 1; i <= nn; i++) { // if (b.at(i, i) == 0) { // b.at(i, i) = 1.0e-12; // } //} FloatArray w(nc), ww(nc), t; std :: vector< FloatArray > z(nc, nn), zz(nc, nn), x(nc, nn); outStream = domain->giveEngngModel()->giveOutputStream(); /* initial setting */ #if 0 ww.add(1.0); for ( int j = 0; j < nc; j++ ) { z[j].add(1.0); } #else { FloatArray ad(nn), bd(nn); for (int i = 1; i <= nn; i++) { ad.at(i) = fabs(a.at(i, i)); bd.at(i) = fabs(b.at(i, i)); } IntArray order; order.enumerate(nn); std::sort(order.begin(), order.end(), [&ad, &bd](int a, int b) { return bd.at(a) * ad.at(b) > bd.at(b) * ad.at(a); }); for (int i = 0; i < nc; i++) { x[i].at(order[i]) = 1.0; b.times(x[i], z[i]); ww.at(i + 1) = z[i].dotProduct(x[i]); } } #endif int it; for ( it = 0; it < nitem; it++ ) { /* copy zz=z */ for ( int j = 0; j < nc; j++ ) { zz[j] = z[j]; } /* solve matrix equation K.X = M.X */ for ( int j = 0; j < nc; j++ ) { solver->solve(a, z[j], x[j]); } /* evaluation of Rayleigh quotients */ for ( int j = 0; j < nc; j++ ) { w.at(j + 1) = zz[j].dotProduct(x[j]); } for ( int j = 0; j < nc; j++ ) { b.times(x[j], z[j]); } for ( int j = 0; j < nc; j++ ) { w.at(j + 1) /= z[j].dotProduct(x[j]); } /* check convergence */ int ac = 0; for ( int j = 1; j <= nc; j++ ) { if ( fabs( ww.at(j) - w.at(j) ) <= fabs( w.at(j) * rtol ) ) { ac++; } ww.at(j) = w.at(j); } //printf ("\n iteration %d %d",it,ac); //w.printYourself(); /* Gramm-Schmidt ortogonalization */ for ( int j = 0; j < nc; j++ ) { if ( j != 0 ) { b.times(x[j], t); } for ( int ii = 0; ii < j; ii++ ) { x[j].add( -x[ii].dotProduct(t), x[ii] ); } b.times(x[j], t); x[j].times( 1.0 / sqrt( x[j].dotProduct(t) ) ); } if ( ac > nroot ) { break; } /* compute new approximation of Z */ for ( int j = 0; j < nc; j++ ) { b.times(x[j], z[j]); } } // copy results IntArray order; order.enumerate(w.giveSize()); std :: sort(order.begin(), order.end(), [&w](int a, int b) { return w.at(a) < w.at(b); }); _eigv.resize(nroot); _r.resize(nn, nroot); for ( int i = 1; i <= nroot; i++ ) { _eigv.at(i) = w.at(order.at(i)); _r.setColumn(x[order.at(i) - 1], i); } if ( it < nitem ) { fprintf(outStream, "InverseIteration :: convergence reached in %d iterations\n", it); } else { fprintf(outStream, "InverseIteration :: convergence not reached after %d iterations\n", it); } return NM_Success; }
//在main()函数中测试find()_end()算法(返回两数组相同元素的在第一个数组中的位置) void main () { //-------------------------------------------- // find_end()算法对普通数组的处理 //--------------------------------------------- int x[ARRAY_SIZE]={1,3,5,7,9,2,4,6,8,10}; cout << "x[]: "; put_array(x,ARRAY_SIZE); cout<<endl; int y[]={5,7,9}; cout << "y[]: "; put_array(y,3); cout<<endl; // find_end()算法查找,并显示查找结果 int *p=find_end(x,x+ARRAY_SIZE,&y[0],&y[2]); if (p != x + ARRAY_SIZE) { //查到 cout << "The first element that matches :" ; put_array(y,3); cout<< " is at location in x" << p - x<< endl; } else { //未查到 cout << "The sequence does not contain any elements"; cout<< " with value " ; put_array(&x[3],3); } //-------------------------------------------- // find_end()算法对vector容器的处理 //--------------------------------------------- //声明intvector容器对象 IntArray intvector; //向intvector容器中插入元素 for (int i=1; i<=10; i++) { intvector.push_back(i); }; //显示intvector容器中的元素值 cout << "intvector: "; put_vector(intvector); cout<<endl; IntArray temp; temp.push_back(5); temp.push_back(6); temp.push_back(7); cout << "temp: "; put_vector(temp); cout<<endl; // find_end()算法查找,并显示查找结果 IntArray::iterator pos; pos=find_end(intvector.begin(),intvector.end(),temp.begin(),temp.end()); if (pos != intvector.end()) { //查到 cout << "The first element that matches "; put_vector(temp); cout<< " is at location in intvector " <<pos - intvector.begin()<< endl; } else { //未查到 cout << "The sequence does not contain any elements"; cout<< " with value "; put_vector(temp); cout<< endl ; } }