cast from type double to integer

103 views
Skip to first unread message

Maia Sosa Kapur

unread,
Oct 5, 2021, 3:29:28 PM10/5/21
to TMB Users
I have a matrix of derived quantities inside my model which do not (explicitly) depend on parameter values.

I would like to round these to the nearest integer, stash that output separately, and use the result to index within a different matrix. I currently have a static, externally-generated lookup table but would like to use the "live" values for accuracy.

So far I have tried:

 int temp_ind = int(matrix_of_doubles(x,y,z)); 
 return_val = other_table(temp_ind); // must be an integer

And received error: invalid cast from type 'CppAD::AD<double>' to type 'int'. If I substitute a raw double for  matrix_of_doubles(x,y,z)  (e.g. 3.14159) this error doesn't come up. I suspect TMB is thinking that matrix_of_doubles is deeply related to differentiated parameters, and therefore shouldn't be rounded?

For what it's worth, I'm trying to use the predicted lengths-at-age to look up selectivity values associated with that length bin. In my model, the predicted lengths in question are deterministic (no estimation of growth parameters). 

Is there a way to store the rounded value of a derived double, with no intention of modifying the original quantity? 

Thanks
MK

Maia Sosa Kapur

unread,
Oct 5, 2021, 4:41:14 PM10/5/21
to TMB Users
Thanks to help from Dr. W. Stockhausen, the appropriate syntax is indeed straightforward:

int temp_ind = CppAD::Integer(matrix_of_doubles(x,y,z)); 

Thanks all.
Reply all
Reply to author
Forward
0 new messages