473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

transform on valarray

Hi

I have 2 valarray<doublea 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
Aug 18 '06 #1
3 1606

Gary Wessle wrote:
Hi

I have 2 valarray<doublea 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<doublea(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 '06 #2
Gary Wessle wrote:
Hi

I have 2 valarray<doublea 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
Aug 18 '06 #3
Gary Wessle schrieb:
Hi

I have 2 valarray<doublea 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<doubleb(2.0, 10);
valarray<doublea(1.0/b);

??

/S
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
Aug 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. ...
6
by: Steven T. Hatton | last post by:
I bought Josuttis's book on the repeated recommendations of people in this newsgroup. http://www.josuttis.com/libbook/ One of the first things I looked up was the std::valarray<>. And what I...
3
by: Peter | last post by:
Hi everybody, I am unfortunately stuck with a probably very simple problem. I made a class called Particle with a valarray (STL) as a class member. The code for the class goes like this: ...
4
by: Steven T. Hatton | last post by:
This code works for dividing each element of a boost::array<> by a value of its element type: template <typename T, size_t S> inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs,...
2
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
1
by: Dack | last post by:
Hi, I want to track memory leaks in my application (that is using <valarray>). I used the following code: #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> But then, when I...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
2
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
43
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.