Valarray and gcc

Hi,

The page root.cern.ch/root/Cint.phtml?faq
states that valarray STL container is supported only for
VC and BC. However in ROOT 5.05/01 I was able to generate
a dictionary for std::valarray class and some functions
of valarray (e.g. pow, sqrt) myself. Unfortunately I can’t
do this for the 5.10/00 version. Now evaluation of e.g.
v2 = cos(v1);
leads to the same value of all elements of v2.

I hoped to find a native ROOT class to replace valarray
(see root.cern.ch/root/roottalk/roottalk05/2600.html )
I agree that TVectorT provides more or less similar
functionality, but with valarrays my code was more readable.
I don’t like that EmentMult(v1, v2) modifies the first argument,
also to have a squared vector one needs to copy it. Assuming
v1, v2, v3 are valarrays, the expression
std::valarray<Double_t> v4 = sqrt(pow(v1, 2) + pow(v2,2) + 1./v3);
rewritten in terms of TVectorT become much longer.

So I wonder whether it is possible to have valarray working
in ROOT 5.10/00 or current CVS version.

Some details. I have Fedora Core 2 running on a Pentium IV box.

Thank you,
Maxim Nikulin

Hi Maxim,

[quote=“nikulin”]The page root.cern.ch/root/Cint.phtml?faq
states that valarray STL container is supported only for
VC and BC. However in ROOT 5.05/01 I was able to generate
a dictionary for std::valarray class and some functions
of valarray (e.g. pow, sqrt) myself. Unfortunately I can’t
do this for the 5.10/00 version. Now evaluation of e.g.
v2 = cos(v1);
leads to the same value of all elements of v2.[/quote]I will have a look. A data analysis program should probably support valarray.

[quote=“nikulin”]I don’t like that EmentMult(v1, v2) modifies the first argument,
also to have a squared vector one needs to copy it.[/quote]The advantage is that you see that you need to copy one of the vectors, if you don’t want to change them. Most of the operators create temporaries (there’s no way out), which makes them really a lot slower.

[quote=“nikulin”]Assuming
v1, v2, v3 are valarrays, the expression
std::valarray<Double_t> v4 = sqrt(pow(v1, 2) + pow(v2,2) + 1./v3);
rewritten in terms of TVectorT become much longer.[/quote]But this is a perfect example of why one should not use operators. You create (if I counted correctly) 6 temporaries with this expression, where you could survive with 2. Assuming that your goal is maximum performance, not minimal code size.

Cheers, Axel.

Hi Axel,

[quote=“nikulin”]The page root.cern.ch/root/Cint.phtml?faq
states that valarray STL container is supported only for VC and BC.
…[/quote]

Thank you, it would be nice.

[quote=“nikulin”]I don’t like that EmentMult(v1, v2) modifies the first argument,
also to have a squared vector one needs to copy it.[/quote]

[quote=“Axel”]The advantage is that you see that you need to copy
one of the vectors, if you don’t want to change them.
Most of the operators create temporaries (there’s no way out),
which makes them really a lot slower.[/quote]
valarray allows both ways:
v1 = v2; // or
valarray<Double_t> v3 = v1
v2;

I suppose TVectorT<> mixes linear algebra and per-element
operations, thus sometimes it’s too general to be convenient
in particular cases.

[quote=“nikulin”]Assuming v1, v2, v3 are valarrays, the expression
std::valarray<Double_t> v4 = sqrt(pow(v1, 2) + pow(v2,2) + 1./v3);
rewritten in terms of TVectorT become much longer.[/quote]

[quote=“Axel”]But this is a perfect example of why one should not use operators.
You create (if I counted correctly) 6 temporaries with this expression,
where you could survive with 2. Assuming that your goal is maximum
performance, not minimal code size.[/quote]
Even if CINT do no optimization (compiler is likely more smart)
I believe I can waste extra 120 numbers after iterating
over a million of chain entries. Actually this time the performance
critical part is the discussion, but I hope you will excuse
my desire to have a nice script next time.

One line expressions are superior for calculation directly
from the command prompt too. I don’t share opinion that
everything must be done from scripts.

Maxim Nikulin

[quote=“Axel”]But this is a perfect example of why one should not use operators. You create (if I counted correctly) 6 temporaries with this expression, where you could survive with 2. Assuming that your goal is maximum performance, not minimal code size.
Cheers, Axel.[/quote]

Hello, Axel. std::valarray is a very specific class, it was developed to be
hardly optimized by compiler/library developers. Look, for example, at gcc’s implementation of valarray, it’s not as straiforward as Dinkumware’s, it’s very complex and clever, it uses interesting technics to avoid unnecessary temporaries. So overloaded operators is not a problem, the problem is programmers’ laziness :slight_smile: