I see –– thanks for the clarification (it is indeed what Dirk really
had suggested, the accidental shortcut was mine). This passing by
reference seems very interesting (I pass several large complex arma
matrices in other functions), but I need to work out how it works
inside the functions.

Thanks again for the precious comments,

baptiste

PS: my package is now on r-forge, hoping to see if it compiles there too:

https://r-forge.r-project.org/scm/viewvc.php/pkg/cda/?root=photonics


On 10 August 2010 01:38, Davor Cubranic <cubra...@stat.ubc.ca> wrote:
> On 2010-08-07, at 7:03 PM, baptiste auguie wrote:
>
>> Thanks Davor, Douglas and Dirk –– all your comments have been very
>> helpful and I used them to improve my code (along with getting a
>> better understanding of C++). The only thing I haven't tried yet is
>> avoid duplicating the objects in memory using const (seems tricky from
>> what I read on the net).
>
> Using 'const' won't save you from duplicating objects: all it does is mark a 
> variable/parameter as a constant, so that you can't accidentally change it. 
> What does save you from duplication is passing arguments by reference. And 
> passing arguments by reference is often combined with marking them 'const' 
> for safety, because accidentally changing them would introduce wide-ranging 
> bugs into your code that could be difficult to find.
>
> Also, note that passing by const reference only saves space for complex 
> objects, not primitive types like doubles and consts. So for the 'euler' 
> function you used in your example, it would be recommended to make its 
> signature:
>
> arma::mat euler(const double phi, const double theta, const double psi);
> In other words, 'phi', 'theta', and 'psi' are doubles, which I'm passing as 
> constants.
>
> While something like matrix multiply could be:
>
> arma::mat mul(const arma::mat& a, const arma::mat& b);
> Here, 'a' and 'b' are matrix objects, which I'm passing by constant reference.
>
> Hope this helps.
>
> Davor
>
>



-- 
____________________

Dr. Baptiste Auguié

Departamento de Química Física,
Universidade de Vigo,
Campus Universitario, 36310, Vigo, Spain

tel: +34 9868 18617
http://webs.uvigo.es/coloides
____________________
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to