Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > transform on valarray

Reply
Thread Tools

transform on valarray

 
 
Gary Wessle
Guest
Posts: n/a
 
      08-18-2006
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
 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      08-18-2006

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;
}

 
Reply With Quote
 
 
 
 
Pierre Barbier de Reuille
Guest
Posts: n/a
 
      08-18-2006
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
 
Reply With Quote
 
Stefan Naewe
Guest
Posts: n/a
 
      08-18-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM
Why not use valarray<valarray<T> > ? Jim West C++ 2 12-23-2003 03:54 PM
Enhancing valarray with "normal" arithmetic operators =?ISO-8859-1?Q?Christian_Brechb=FChler?= C++ 6 09-14-2003 06:39 PM
Re: valarray resize Marc Schellens C++ 5 07-30-2003 07:06 AM
New Altivec-optimized valarray implementation Glen Low C++ 3 06-26-2003 05:07 AM



Advertisments