Ignore:
Timestamp:
May 23, 2008, 9:17:19 AM (17 years ago)
Author:
Frederik Heber <heber@…>
Children:
6145aa7
Parents:
6c96f4
Message:

Lots of for loops now count in reverse order where it does not matter, some 3 -> NDIM

for(i=0;i<var;i++) is slower than for (i=var;i--;) if the order of the i's is not important (note: i-- is also a value and it stops when on i == 0 automatically)
in builder.cpp there were some remnant 3 actually meant to be NDIM

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/vector.cpp

    r6c96f4 rf75030  
    2525{
    2626  double res = 0.;
    27   for (int i=0;i<NDIM;i++)
     27  for (int i=NDIM;i--;)
    2828    res += (x[i]-y->x[i])*(x[i]-y->x[i]);
    2929  return (res); 
     
    5555        // create the translation vector
    5656        TranslationVector.Zero();
    57         for (int i=0;i<NDIM;i++)
     57        for (int i=NDIM;i--;)
    5858          TranslationVector.x[i] = (double)N[i];
    5959        TranslationVector.MatrixMultiplication(matrix);
     
    8484  TestVector.CopyVector(this);
    8585  TestVector.InverseMatrixMultiplication(matrix);
    86   for(int i=0;i<NDIM;i++) { // correct periodically
     86  for(int i=NDIM;i--;) { // correct periodically
    8787    if (TestVector.x[i] < 0) {  // get every coefficient into the interval [0,1)
    8888      TestVector.x[i] += ceil(TestVector.x[i]);
     
    106106{
    107107  double res = 0.;
    108   for (int i=0;i<NDIM;i++)
     108  for (int i=NDIM;i--;)
    109109    res += x[i]*y->x[i];
    110110  return (res); 
     
    126126{
    127127  double res = 0.;
    128   for (int i=0;i<NDIM;i++)
     128  for (int i=NDIM;i--;)
    129129    res += this->x[i]*this->x[i];
    130130  return (sqrt(res)); 
     
    136136{
    137137  double res = 0.;
    138   for (int i=0;i<NDIM;i++)
     138  for (int i=NDIM;i--;)
    139139    res += this->x[i]*this->x[i];
    140140  res = 1./sqrt(res);
     
    146146void vector::Zero()
    147147{
    148   for (int i=0;i<NDIM;i++)
     148  for (int i=NDIM;i--;)
    149149    this->x[i] = 0.;
    150150};
     
    259259void vector::Scale(double **factor)
    260260{
    261   for (int i=0;i<NDIM;i++)
     261  for (int i=NDIM;i--;)
    262262    x[i] *= (*factor)[i];
    263263};
     
    265265void vector::Scale(double *factor)
    266266{
    267   for (int i=0;i<NDIM;i++)
     267  for (int i=NDIM;i--;)
    268268    x[i] *= *factor;
    269269};
     
    271271void vector::Scale(double factor)
    272272{
    273   for (int i=0;i<NDIM;i++)
     273  for (int i=NDIM;i--;)
    274274    x[i] *= factor;
    275275};
     
    280280void vector::Translate(const vector *trans)
    281281{
    282   for (int i=0;i<NDIM;i++)
     282  for (int i=NDIM;i--;)
    283283    x[i] += trans->x[i];
    284284};
     
    295295  C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
    296296  // transfer the result into this
    297   for (int i=0;i<NDIM;i++)
     297  for (int i=NDIM;i--;)
    298298    x[i] = C.x[i];
    299299};
     
    327327    C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
    328328    // transfer the result into this
    329     for (int i=0;i<NDIM;i++)
     329    for (int i=NDIM;i--;)
    330330      x[i] = C.x[i];
    331331  } else {
     
    344344void vector::LinearCombinationOfVectors(const vector *x1, const vector *x2, const vector *x3, double *factors)
    345345{
    346   for(int i=0;i<NDIM;i++)
     346  for(int i=NDIM;i--;)
    347347    x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
    348348};
     
    359359  Output((ofstream *)&cout);
    360360  cout << "\t";
    361   for (int i=0;i<NDIM;i++)
     361  for (int i=NDIM;i--;)
    362362    x[i] -= 2.*projection*n->x[i];
    363363  cout << "Projected vector: ";
     
    446446  x1.Scale(x1.Projection(this));
    447447  SubtractVector(&x1);
    448   for (int i=0;i<NDIM;i++)
     448  for (int i=NDIM;i--;)
    449449          result = result || (fabs(x[i]) > MYEPSILON);
    450450
     
    468468  vector->Output((ofstream *)&cout);
    469469  cout << endl;
    470   for (j=0;j<NDIM;j++)
     470  for (j=NDIM;j--;)
    471471    Components[j] = -1;
    472472  // find two components != 0
     
    537537         par.num = num;
    538538       
    539          for (i=0;i<NDIM;i++)
     539         for (i=NDIM;i--;)
    540540                gsl_vector_set(x, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
    541541         
     
    573573   while (status == GSL_CONTINUE && iter < 100);
    574574 
    575   for (i=0;i<(size_t)np;i++)
     575  for (i=(size_t)np;i--;)
    576576    this->x[i] = gsl_vector_get(s->x, i);
    577577   gsl_vector_free(x);
     
    587587void vector::AddVector(const vector *y)
    588588{
    589   for (int i=0;i<NDIM;i++)
     589  for (int i=NDIM;i--;)
    590590    this->x[i] += y->x[i];
    591591}
     
    596596void vector::SubtractVector(const vector *y)
    597597{
    598   for (int i=0;i<NDIM;i++)
     598  for (int i=NDIM;i--;)
    599599    this->x[i] -= y->x[i];
    600600}
     
    605605void vector::CopyVector(const vector *y)
    606606{
    607   for (int i=0;i<NDIM;i++)
     607  for (int i=NDIM;i--;)
    608608    this->x[i] = y->x[i];
    609609}
     
    783783    cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
    784784    // apply sign matrix
    785     for (j=0;j<NDIM;j++)
     785    for (j=NDIM;j--;)
    786786      x[j] *= sign[j];
    787787    // calculate angle and check
     
    792792    }
    793793    // unapply sign matrix (is its own inverse)
    794     for (j=0;j<NDIM;j++)
     794    for (j=NDIM;j--;)
    795795      x[j] *= sign[j];
    796796  }
Note: See TracChangeset for help on using the changeset viewer.