![]() |
Blitz Support : |
From: Navneet Dalal (Navneet.Dalal_at_[hidden])
Date: 2004-01-09 13:47:33
Hi Julian,
I am moving this discussion to Blitz support. So that other users can also
comment.
I perfectly remember the mails exchanged few weeks before with Andrius
Kurtinaitis about the use of functors instead of functions for
using scalar arguments in calling functions with array like expressions.
In previous code of blitz, one would have written the above example as in
attached file.
However, if one looks at the syntax and the code that one has to write
(with current cvs version of blitz), a typical example would look like
--------------- Code --------------------------
using namespace blitz;
typedef TinyVector<int,1> VectorType;
typedef Array<int,1> ArrayType;
int gauss(int val, int val2) {
return static_cast<int>(val*val2);
}
class s_testfunc {
public:
s_testfunc(int val2) : val2(val2){}
int operator()(int val) { testfunc(val,val2);}
BZ_DECLARE_FUNCTOR(s_testfunc)
private:
int val2;
};
int main(int argc, char** argv) {
ArrayType a (Range(0,3));
s_testfunc o(2);
a = o(tensor::i);
std::cout << a << std::endl;
return 0;
}
--------------- Code Ends --------------------
First, a user has to create a class explicitly (which was provided by
blitz macros previously) and secondly,
the function calling syntax gets ugly. As the functions and class cannot
have the same name, one would have to invent a new name for the same
concept and use this new name at call times (thus may confuse readers).
> Array-like expressions. If we provide combinations with generic
> arguments
> of type T, then you get ambiguous function overloads. Also, you would
> have
> a problem if the original function testfunc were templated. This is why
> I
> recently modified the expression template system to explicitly handle
> expressions involving the built-in scalar types, rather than relying on a
> general template type T to handle this case. I hope this makes sense.
Related to above mentioned problem in your mail:
I just tried an example of templated functions, with scalar arguments. I
am unable to see the problem. The code compiles without any problem.
Can you provide an example.
In any case, if we are restricting BZ_DECLARE_FUNCTION only for arrays and
not to scalars, I strongly believe that we should have atleast definitions
in newet-macros.h for binary user-defined functions involving explicit
combinations of an Array-like object and a built-in scalar type.
We may not need it for ternary functions as ternary functions are quite
rare compared to binary functions.
regards
-navneet
On Thu, 8 Jan 2004 17:01:12 -0800, Julian C. Cummings
<cummings_at_[hidden]> wrote:
> Hi Navneet,
>
> I think that the compile error reported by gcc is correct and that your
> code
> is in error. The problem is that your test function can only be applied
> to
> Arrays or Array-like objects in an elementwise fashion. You are giving
> an
> IndexPlaceholder and a scalar int, so you do not have two Array-like
> objects. You can convert the scalar int into an Array-like expression
> using
> the asExpr class:
>
> a = testfunc(tensor::i, _bz_ArrayExpr<asExpr<int>::T_expr>(2));
>
> This converts the scalar int "2" into a _bz_ArrayExprConstant and wraps
> it
> in a _bz_ArrayExpr object (an Array expression). I know this a tad ugly,
> but we really need to restrict the use of Array functions to Arrays and
> Array-like expressions. If we provide combinations with generic
> arguments
> of type T, then you get ambiguous function overloads. Also, you would
> have
> a problem if the original function testfunc were templated. This is why
> I
> recently modified the expression template system to explicitly handle
> expressions involving the built-in scalar types, rather than relying on a
> general template type T to handle this case. I hope this makes sense.
>
> Regards, Julian C.
>
> P.S. I could provide definitions in newet-macros.h for binary
> user-defined
> functions involving explicit combinations of an Array-like object and a
> built-in scalar type, like we do for the standard binary math operators
> and
> functions. Then your code would compile as it was originally written.
> Doing this for ternary functions gets complicated because there are so
> many
> combinations of one Array-like object and two scalar types or two
> Array-like
> objects and one scalar type. My original thinking was that users would
> use
> the BZ_DECLARE_FUNCTION macros to be able to apply their functions to
> Arrays, not to scalars. What do you think?
>
>
>
>
>> -----Original Message-----
>> From: Navneet Dalal [mailto:Navneet.Dalal_at_[hidden]]
>> Sent: Wednesday, January 07, 2004 1:30 PM
>> To: Julian Cummings
>> Subject: BZ_DECLARE_FUNCTION2 problem
>>
>>
>> Hi Julian
>>
>> The code in the attached file doesnot compile (with latest version of
>> blitz taken from cvs).
>> I tried to look into the problem, but in vein.
>>
>> It gives the following error:
>> /home/edmund/dalal/code/src/apps/general/test.cpp: In function `int
>> main(int, char**)':
>> /home/edmund/dalal/code/src/apps/general/test.cpp:27: error: cannot
>> convert `blitz::IndexPlaceholder<0>' to `int' for argument
>> `1' to `int
>> testfunc(int, int)'
>>
>> Compiler: gcc-3.3.1.
>> Can you look into it.
>>
>> -navneet