Originally Posted by
laserlight
In this case, I think it is likely, but that should not really be an important factor to begin with. If you are going to write a function to apply a function to the elements of a container, you might as well use a generic function that applies a function (or function object) to a range.
I agree with this wholeheartedly how does this work in C++?
Is C++ so equipped with appropriate libraries?
For example can I do something like this for some specially defined vectorized log function, or an overloaded log function?
Code:
std::vector<double> myvec ;
... code to initialize my vec ...
std::vector<double> logvec = log(myvec) ;
That would be sweet. Come to think of it, it wouldn't be so hard to overload these functions now would it?
By the way for some reason this code works just fine:
Code:
/* apply a function to all elements of the array and
return a copy of a new array */
cvm::rmatrix &rapply(cvm::rmatrix &rm, double (*dfunc)(double))
{
cvm::rmatrix::iterator q, i ;
cvm::rmatrix *x = new (GC) cvm::rmatrix(rm.msize(), rm.nsize()) ;
i = x->begin() ;
for (q = rm.begin() ; q != rm.end() ; ++q) {
*i = (*dfunc) (*q) ;
++i ;
}
return(*x) ;
}
So if I assign data via the pointer (i) to an allocated variable and return it. No problem. I could of course write vectorized versions of various functions via overloading, using this code as a template I suppose.
However in the original case (rapp2self()) of passing an rmatrix by reference and trying to update it's values internally fails for some reason. I'll have to assume I'm doing something wrong, but the call I'm using to the rapp2self() function looks like this:
Code:
rapp2self(theta_nm_aoa, prin_value) ;
where theta_nm_aoa is of type rmatrix. Not much to it really. Walking through the code with a debugger suggests internally that rapp2self is doing everything it's supposed to. It's only in the scope of the callee, where theta_nm_aoa remains unchanged. It's got to be something I'm doing that's stupid, but I haven't found it yet.