![]() |
Blitz Support : |
From: Julian Cummings (cummings_at_[hidden])
Date: 2003-12-11 16:10:37
Andrius,
My point was that there is an inherent difference between a function
"argument" and a function "parameter". An "argument" is a piece of data
that the function applies to, whereas a parameter is a constant value
needed by the function to do its thing. In Navneet's example of the
gauss function, only the first argument to gauss() was a true
"argument", while the second and third arguments were really parameters.
Since a function does not have state (except for compile-time
constants), you have to pass these parameters as arguments when you call
the function. But a functor can have state, so you can pass the
parameters once to the functor's constructor, and then just pass the
data when applying the functor.
Just to be clear, I would have written Navneet's example like this:
template <typename T>
class gauss {
public:
gauss(T mean, T var) : mean_(mean), var_(var) {}
T operator()(T val) { /* gauss function implementation */ }
BZ_DECLARE_FUNCTOR(gauss)
private:
T mean_, val_;
};
int main() {
Array<double,1> a(10);
Array<float,1> b(10);
// initialize arrays
Array<double,1> resA(10);
gauss<double> gaussFunc1(0, 10);
resA = gaussFunc1(a);
Array<float,1> resB(10);
gauss<float> gaussFunc2(2, 5);
resB = gaussFunc2(b);
}
Of course, you could specialize the gauss functor class template for
types float and double if distinct implementations were needed. The
main point is that this is a unary function, not a ternary function.
Regards, Julian C.
Andrius Kurtinaitis wrote:
> Hello,
>
> I just looked into <blitz/array/functorExpr.h> and i did not see any
> difference between blitz-declared functions and functors except that
> when when using functors, you can use class or instance methods (which
> also operate on scalars). Also, in the case of instance methods you
> can use the instance variables when evaluating the expression.
> Am i right?
>
> Andrius
>
> Julian Cummings wrote:
>
>> Just an aside: this example is not a proper trinary function. A
>> trinary function would act on three arrays of data. What you have
>> here is a function that acts on a single array of data and requires
>> two additional scalar input parameters. Such a function would best
>> be captured in blitz by implementing it as a functor object that is
>> initialized with values for the two scalar parameters. The functor
>> would store these parameters as member data and operate on an array
>> by providing an operator() method. See <blitz/array/functorExpr.h>
>> for details.
>>
>> -- Julian C.
>>
>> P.S. Apparently, trinary and ternary are synonyms, but ternary is
>> the more commonly used term for an operator taking three arguments.
>>
>
> _______________________________________________
> Blitz-support mailing list
> Blitz-support_at_[hidden]
> http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
-- Dr. Julian C. Cummings E-mail: cummings_at_[hidden] California Institute of Technology Phone: 626-395-2543 1200 E. California Blvd., Mail Code 158-79 Fax: 626-584-5917 Pasadena, CA 91125