Topic: std::valarray


Author: Andre Rabello DOS ANJOS <Andre.dos.Anjos@cern.ch>
Date: Fri, 1 Mar 2002 17:10:08 GMT
Raw View
Hi,
 After some trouble I've found out that gcc 3 compiles, but the
resulting executable with the following prog:

#include <valarray>

typedef std::valarray<int> vint;

int main (void)
{
 vint sample(3, 10); //a sample
 std::valarray<vint> x(sample,4); //four of the sample
 std::valarray<bool> mask(true, x.size());
 mask[1] = false;
 std::valarray<vint> y(x[mask]); ///< crashes
 return 0;
}

 Is a not-so-simple type as the alias 'vint' to be supported
by the template class valarray in a standart conformant compiler?
Couldn't see why not, but I'm surely not an expert on it.

 Regards,

--
Andre Rabello DOS ANJOS, M.Sc.
Signal Processing, Data Analysis and Computing
Office: 32-2-A06, Tel: (+ 41 22) 767 5022
Fax: (+ 41 22) 767 8420
CERN - EP/HC Division
CH-1211 Geneve 23 - Suisse/Switzerland
http://www.lps.ufrj.br/~rabello

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: herwin@gmu.edu (Harry Erwin)
Date: 1998/03/10
Raw View
Gabriel Dos Reis <dosreis@DPTMaths.ENS-Cachan.Fr> wrote:

> >>>>> =ABHarry=BB, Harry Erwin <herwin@gmu.edu> said:
>
> Harry> Gabriel Dos Reis <Gabriel.Dos-Reis@dptmaths.ens-cachan.fr> wrote:
> >> Why do we have valarray::sum() and not valarray::product() ?
> >>
> >> -- Gaby
>
> Harry> valarray is the orphan child of the STL. It's actually defined to
> Harry> support computational work, but the definition isn't that clear, and
> Harry> most compilers are unlikely to optimize its operations correctly (if at
> Harry> all). valarray::sum() is used to sum up the elements of the valarray,
> Harry> something that is done more frequently than doing a product of the
> Harry> elements.
>
> Huh...
> I do have seen real world nuemrical algorithms which made heavy
> use of products on array of numbers (for ex. evaluating a polynomial
> given by its roots...). I don't see any real point in allowing sum()
> and not product().

Like I said, valarray is the orphan child of the STL. In Codewarrior,
they haven't implemented some of the required numerical operations
because they're run into problems with the definitions (and with
limitations of their compiler). The stuff they've implemented hasn't
been really optimized. I'd love to see some discussion here of what the
definition _should_ be and how it should be used. We also need a web
page somewhere to document the right way of using valarray and the slice
family.

--
Harry Erwin, herwin@gmu.edu, http://osf1.gmu.edu/~herwin, Senior
Software Analyst for the FAA, PhD candidate modeling how bats
echolocate and lecturer for CS 211 (data structures and advanced C++).
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: pedwards@cs.wright.edu (Phil Edwards)
Date: 1998/03/06
Raw View
Gabriel Dos Reis  <Gabriel.Dos-Reis@dptmaths.ens-cachan.fr> wrote:
+
+  Why do we have valarray::sum() and not valarray::product() ?
+

Because sequence summations seem to be used in industry and research
more than sequence products are?  It's a guess on my part, though.


If we were discussing a "Container" rather than a valarray, I'd say
that you can easily roll your own using the binary_op form of the
accumulate() in <numeric> by just throwing in the "multiplies" struct
from the <functional> header, thusly:

 int Gamma = accumulate (vala.begin(), vala.end(),
                         1, multiplies<int>());

But accumulate() takes input iterators, which don't seem to be defined
for valarrays, implicitly or explicitly.  I'd assume you could use
&vala[0] and &vala[n] as the endpoints, just like any other use of an
STL algorithm with "dumb" arrays.  But I am not very familiar with the
valarray specs, since none of my compilers have even a field-test
implementation yet.


(Err, perhaps the endpoint should be &vala[n-1] instead, with an
accompanying change to the "init" parameter to pick up the last value?
Does the valarray spec define past-the-end legality like the rest of
the language?  I'd expect so...)


Luck++;
/dev/phil

=> If you post a followup, please don't email a copy to me.  I read news
    often enough that replying to things twice is annoying.  But thanks.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Gabriel Dos Reis <Gabriel.Dos-Reis@dptmaths.ens-cachan.fr>
Date: 1998/03/06
Raw View
>>>>> =ABPhil=BB, Phil Edwards <pedwards@cs.wright.edu> wrote:

Phil> Gabriel Dos Reis  <Gabriel.Dos-Reis@dptmaths.ens-cachan.fr> wrote:
Phil> +=20
Phil> +  Why do we have valarray::sum() and not valarray::product() ?
Phil> +=20

Phil> Because sequence summations seem to be used in industry and researc=
h
Phil> more than sequence products are?  It's a guess on my part, though.

 This is not quite true. I'm doing research on polynomial
systems and I frequently use algorithms where sequence products are
heavly used.=20


Phil> If we were discussing a "Container" rather than a valarray, I'd say

 valarray was designed with the purpose to support numerical
computations. I'ld choose vector if my pupose were to use containers.

[...]

Phil> (Err, perhaps the endpoint should be &vala[n-1] instead, with an
Phil> accompanying change to the "init" parameter to pick up the last val=
ue?
Phil> Does the valarray spec define past-the-end legality like the rest o=
f
Phil> the language?  I'd expect so...)

 The Standard does not define such thing as iterator for class
valarray.

-- Gaby
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Christopher M. Gurnee" <gurnec@nospam.yahoo.com>
Date: 1998/03/07
Raw View
Phil Edwards wrote in message <6dmihn$196$1@alpha.wright.edu>...
> [if valarray where a container, you could:]
>
> int Gamma = accumulate (vala.begin(), vala.end(),
>                         1, multiplies<int>());
>
>But accumulate() takes input iterators, which don't seem to be
defined
>for valarrays, implicitly or explicitly.  I'd assume you could use
>&vala[0] and &vala[n] as the endpoints, just like any other use of an
>STL algorithm with "dumb" arrays.  But I am not very familiar with
the
>valarray specs, since none of my compilers have even a field-test
>implementation yet.
>
>
>(Err, perhaps the endpoint should be &vala[n-1] instead, with an
>accompanying change to the "init" parameter to pick up the last
value?
>Does the valarray spec define past-the-end legality like the rest of
>the language?  I'd expect so...)

Here's what you can use:

int Gamma = accumulate(&vala[0], &vala[0]+vala.size(),
  1, multiplies<int>());

and here's why (this is taken from subclause 26.3.2.3 of CD-2, I don't
know if there are any changes in the FDIS):

1) You can't use &vala[vala.size()] in place of &vala[0]+vala.size().
In the expression, vala[n], these preconditions must hold: n >= 0, n <
vala.size().

2) The standard states that the expression &vala[i+j] == &vala[i] + j
evaluates to true, where i+j < vala.size().  This implies that the
data stored in a valarray is stored in a contiguous piece of memory,
just as though it were an array.  Therefore, &vala[0] + vala.size() is
a pointer pointing to the element past the last element in the
valarray.

-Chris
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Gabriel Dos Reis <dosreis@DPTMaths.ENS-Cachan.Fr>
Date: 1998/03/09
Raw View
>>>>> =ABHarry=BB, Harry Erwin <herwin@gmu.edu> said:

Harry> Gabriel Dos Reis <Gabriel.Dos-Reis@dptmaths.ens-cachan.fr> wrote:
>> Why do we have valarray::sum() and not valarray::product() ?
>>
>> -- Gaby
>>

Harry> valarray is the orphan child of the STL. It's actually defined to
Harry> support computational work, but the definition isn't that clear, and
Harry> most compilers are unlikely to optimize its operations correctly (if at
Harry> all). valarray::sum() is used to sum up the elements of the valarray,
Harry> something that is done more frequently than doing a product of the
Harry> elements.

Huh...
I do have seen real world nuemrical algorithms which made heavy
use of products on array of numbers (for ex. evaluating a polynomial
given by its roots...). I don't see any real point in allowing sum()
and not product().

-- Gaby
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: herwin@gmu.edu (Harry Erwin)
Date: 1998/03/03
Raw View
Gabriel Dos Reis <Gabriel.Dos-Reis@dptmaths.ens-cachan.fr> wrote:

>       Why do we have valarray::sum() and not valarray::product() ?
>
> -- Gaby
>

valarray is the orphan child of the STL. It's actually defined to
support computational work, but the definition isn't that clear, and
most compilers are unlikely to optimize its operations correctly (if at
all). valarray::sum() is used to sum up the elements of the valarray,
something that is done more frequently than doing a product of the
elements.

--
Harry Erwin, herwin@gmu.edu, http://osf1.gmu.edu/~herwin, Senior
Software Analyst for the FAA, PhD candidate modeling how bats
echolocate and lecturer for CS 211 (data structures and advanced C++).
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Gabriel Dos Reis <Gabriel.Dos-Reis@dptmaths.ens-cachan.fr>
Date: 1998/03/02
Raw View

 Why do we have valarray::sum() and not valarray::product() ?

-- Gaby



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]