Blitz logo

Blitz Support :

From: Navneet Dalal (Navneet.Dalal_at_[hidden])
Date: 2003-12-10 08:54:14


On Wed, 10 Dec 2003 12:24:08 +0200, Andrius Kurtinaitis
<andrius.kurtinaitis_at_[hidden]> wrote:

> Hello,
>
> thank you for the quick reply.
> I have three ternary functions and a long expression where I want to use
> these expressions like that:
>
> result[1] = (a_very_long_expression) + ternary_funtion_1( a1, a2, a3 );
> result[2] = (a_very_long_expression) + ternary_funtion_2( a1, a2, a3 );
> result[3] = (a_very_long_expression) + ternary_funtion_3( a1, a2, a3 );
>
> I do not want to repeat the long expressions like this because they are
> really long and I edit them sometimes and I want to keep them identical.
> I can't define long expressions as functions because they would take
> more than three arguments., a3 );

If this is a very long expression, and I guess that the cost of (cpu time)
computing is quite high.
So it would be better to compute it first and then use it.

I would do this in your place (assuming the expression is same for all
three cases):

  result[1] = (a_very_long_expression);

  result[2] = result[1] + ternary_funtion_2( a1, a2, a3 );
  result[3] = result[1] + ternary_funtion_3( a1, a2, a3 );
  result[1] += ternary_funtion_1( a1, a2, a3 );

Also, I donot know what is the type of result array. But if it is an array
of tinyvector or something similar (with required assignment operator),
you may try to write it in one go as:

  result = (a_very_long_expression);
  result[1] += ternary_funtion_1( a1, a2, a3 );
  result[2] += ternary_funtion_2( a1, a2, a3 );
  result[3] += ternary_funtion_3( a1, a2, a3 );

> I could write that the other way:
>
> for(int i=0; i<3 i++)
> result[i] = (a_very_long_expression);
> result[1] += ternary_funtion_1( a1, a2, a3 );
> result[2] += ternary_funtion_2( a1, a2, a3 );
> result[3] += ternary_funtion_3( a1, a2, a3 );
>
> But then, according to blitz manual, the loops will not be optimized so
> well. Maybe this is not so important at the >3( a1, a2, a3 );

For the two examples you wrote below: there will not be any significant
difference in computation time -- and in my guess will depend upon
compiler to compiler.

> I have another one question. I am coding a leap-frog-like finite
> difference scheme. But I do not see any way in blitz++ to apply a
> stencil to only even (or only odd) components of the Array. Is it not
> possible with blitz++ or I simply did not found it yet?

I am not sure about it - but you want to look at documentation for ways of
changing strides.

> Kind regards,
>
> Andrius
>
> Navneet Dalal wrote:
>> Hi Andrius,
>>
>> By using the BZ_DECLARE_FUNCTION3 macro, overloaded versions of the
>> ternary functions are the provided.
>> Some struct's are also defined but they are internal to the blitz and
>> ideally these should not concern users.
>>
>> Coming to your problerm (if I understood it right) you want to define,
>> let us say, two functions:
>>
>> double gauss(double val, double mean, double var);
>> float gauss(float val, float mean, float var);
>>
>> And then you would like to evaluate gauss on a Array<double,1> or on a
>> Array<float,1>.
>> For this, you just use BZ_DECLARE_FUNCTION3 as:
>>
>> BZ_DECLARE_FUNCTION3(gauss);
>>
>> int main() {
>> Array<double,1> a(10);
>> Array<float ,1> b(10);
>>
>> // initialize arrays
>> Array<double,1> resA(10);
>> resA = gauss(a, 0, 10); // 0 mean and 10 var
>>
>> Array<float ,1> resB(10);
>> resB = gauss(b, 2, 5); // 2 mean and 5 var
>> }
>>
>> If this is not the case, send a complete example of what you want.
>>
>> On Wed, 10 Dec 2003 09:31:22 +0200, Andrius Kurtinaitis
>> <andrius.kurtinaitis_at_[hidden]> wrote:
>>
>>> Hello,
>>> what is the type of the defined ternary functions?
>>> It seems it is a template struct. But is there some short way to
>>> specify the type?
>>> My problem is that I want to choose one of three functions which
>>> should be used in an expression depending on some other parameter.
>>> Andrius
>>>