transform on valarray

Discussion in 'C++' started by Gary Wessle, Aug 18, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    I have 2 valarray<double> a and b, how can I make a = 1/b, that is
    each element of a is 1 / each element of b?

    I have been reading around and could not understand how it is done.
    let a and b have the same size.
    transform(a.begin(), a.end(), &b[0], operation)
    or
    b = a /=(1);

    not sure.

    thanks
    Gary Wessle, Aug 18, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Guest

    Gary Wessle wrote:
    > Hi
    >
    > I have 2 valarray<double> a and b, how can I make a = 1/b, that is
    > each element of a is 1 / each element of b?
    >
    > I have been reading around and could not understand how it is done.
    > let a and b have the same size.
    > transform(a.begin(), a.end(), &b[0], operation)
    > or
    > b = a /=(1);
    >
    > not sure.
    >
    > thanks


    Well, it is a better idea to switch to vectors - as valarrays don't
    have iterators, and thren transform is not usefull. An example then
    would be:

    #include <iostream>
    #include <algorithm>
    #include <vector>

    double reciproke(const double& d)
    {
    return 1.0/d;
    }

    int main()
    {
    std::vector<double> a(2), b(2);
    a[0] = 2.0;
    a[1] = 4.0;

    std::transform(a.begin(), a.end(), b.begin(), reciproke);

    for(std::vector<double>::iterator it = a.begin(); it !=
    a.end();++it)
    {
    std::cout << *it << std::endl;
    }

    for(std::vector<double>::iterator it = b.begin(); it !=
    b.end();++it)
    {
    std::cout << *it << std::endl;
    }

    return 0;
    }
    , Aug 18, 2006
    #2
    1. Advertising

  3. Gary Wessle wrote:
    > Hi
    >
    > I have 2 valarray<double> a and b, how can I make a = 1/b, that is
    > each element of a is 1 / each element of b?
    >
    > I have been reading around and could not understand how it is done.
    > let a and b have the same size.
    > transform(a.begin(), a.end(), &b[0], operation)
    > or
    > b = a /=(1);
    >
    > not sure.
    >
    > thanks


    And why not simply :

    a = 1.0 / b;

    ??

    This operation is defined in the standard ...

    Pierre
    Pierre Barbier de Reuille, Aug 18, 2006
    #3
  4. Gary Wessle

    Stefan Naewe Guest

    Gary Wessle schrieb:
    > Hi
    >
    > I have 2 valarray<double> a and b, how can I make a = 1/b, that is
    > each element of a is 1 / each element of b?


    What's wrong with

    valarray<double> b(2.0, 10);
    valarray<double> a(1.0/b);

    ??



    /S
    --
    Stefan Naewe
    stefan_DOT_naewe_AT_atlas_DOT_de
    Stefan Naewe, Aug 18, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glen Low
    Replies:
    3
    Views:
    968
    Glen Low
    Jun 26, 2003
  2. Marc Schellens

    Re: valarray resize

    Marc Schellens, Jul 29, 2003, in forum: C++
    Replies:
    5
    Views:
    1,272
    Marc Schellens
    Jul 30, 2003
  3. =?ISO-8859-1?Q?Christian_Brechb=FChler?=

    Enhancing valarray with "normal" arithmetic operators

    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Sep 12, 2003, in forum: C++
    Replies:
    6
    Views:
    945
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Sep 14, 2003
  4. Jim West

    const and valarray reference

    Jim West, Oct 23, 2003, in forum: C++
    Replies:
    4
    Views:
    1,124
    Victor Bazarov
    Oct 24, 2003
  5. Jim West
    Replies:
    2
    Views:
    486
    Jim West
    Dec 23, 2003
Loading...

Share This Page