0

I am trying to understand the code here: http://devernay.free.fr/vision/src/prosac.c

Mainly because I want to translate it into python.

Here is a snippet:

for(n_test = N, I_n_test = I_N; n_test > m; n_test--) { 
  // Loop invariants:
  // - I_n_test is the number of inliers for the n_test first correspondences
  // - n_best is the value between n_test+1 and N that maximizes the ratio                       
  I_n_best/n_best
  assert(n_test >= I_n_test);
  ...
  ...
  // prepare for next loop iteration
  I_n_test -= isInlier[n_test-1];
} // for(n_test ...

So what does this do I_n_test = I_N; in the loop statement ?

Is it a stopping condition ? Shouldn't it be "==" then ?

3 Answers3

2

You can read

 for(n_test = N, I_n_test = I_N; n_test > m; n_test--)

as

 for (initialization ; loop-checking condition; increment/decrement)

From the spec, chapter §6.8.5.3, C11

The statement

  for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.

So, as per the syntax, n_test = N, I_n_test = I_N is the expression containing the initialization statements. They are separated by comma operator.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

for(n_test = N, I_n_test = I_N; n_test > m; n_test--)

it initialize I_n_test = I_N & n_test = N and both variable must be separated by "," not with ";" . If you separate with semicolon then compiler treated as I_n_test = I_n as a condition but not initialization even though you initialize I_n_test = I_n. So it simply works like variable Assignment inside the loop.

dragon
  • 132
  • 6
0

"Is it (I_n_test = I_N;) a stopping condition?"

No, it is not part of the condition.

"Shouldn't it be "==" then?"

No, = is fully appropriate.

"So what does this I_n_test = I_N; do in the loop statement?"

I_n_test = I_N is just an initialization/assignment of the variable I_n_test as it is part of the first of the three sections of a for loop header separated by ;.

Here is an unfolded presentation of the syntax:

for (
      initializations (optional)  
      ;
      condition (obliged if it shouldn't be an endless loop)  
      ;
      in- and/or decrementation (optional if the condition can be influenced 
    )                           inside the loop´s body or as side effect of 
                                the condition itself) 

Since there are two initializations, they need to be separated by the , comma operator in the first section. Else it would be a syntactical error.

To find out more about the comma operator, take a look at:

What does the comma operator , do?


Since n_test and I_n_test are both declared before the loop,

for (n_test = N, I_n_test = I_N; n_test > m; n_test--) { ... 

is basically equivalent to:

n_test = N;
I_n_test = I_N;

for (; n_test > m; n_test--) { ...

But the assignments are brought into the loop header to show that the variables n_test and I_n_test have at least one of their main purposes in the context of the loop.