473,382 Members | 1,526 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,382 software developers and data experts.

Complex struct or Array

Hello,

Writing a program in c++ that should use complex numbers I have two
choices before me.

1- define a struct for complex data i.e
struct {float real,imag;
}ComplexNum;

2-use an array of float type
float Array[]
and consider its even and odd indices as real and imaginary parts of
data respectively.

I must add that the program loops have to do lot of iterations for
multiplications and additions of complex numbers.

Right now choice one seems to have one advantage that it would easy to
see real and imag seperately instead of worrying about the correct
value of indices. But clearity alone is not all that is needed in this
case.

Would someone please comment on the above two approaches, their
differences, effect on performance/speed, and their pros and cons.

Thanks in advance
Jul 22 '05 #1
7 5030
"seia0106" <mi*******@yahoo.com> wrote in message
news:4f**************************@posting.google.c om...
Hello,

Writing a program in c++ that should use complex numbers I have two
choices before me.

1- define a struct for complex data i.e
struct {float real,imag;
}ComplexNum;

2-use an array of float type
float Array[]
and consider its even and odd indices as real and imaginary parts of
data respectively.

Third and probably best choice: Use the 'complex' datatype provided by
C++, by including <complex>.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #2
seia0106 wrote:
Hello,

Writing a program in c++ that should use complex numbers I have two
choices before me.

1- define a struct for complex data i.e
struct {float real,imag;
}ComplexNum;

2-use an array of float type
float Array[]
and consider its even and odd indices as real and imaginary parts of
data respectively.
What about

3- use the std::complex template.

? Btw, is there a reson why you use float instead of double?
I must add that the program loops have to do lot of iterations for
multiplications and additions of complex numbers.

Right now choice one seems to have one advantage that it would easy to
see real and imag seperately instead of worrying about the correct
value of indices. But clearity alone is not all that is needed in this
case.
What is needed that would justify the use of the less clear array?
Would someone please comment on the above two approaches, their
differences, effect on performance/speed, and their pros and cons.


I don't see a reason why they should differ in performance at all. OTOH,
you never know what the compiler makes out of it, so if performance is
so critical to your program, just write some test programs and measure
the time they take.

Jul 22 '05 #3
mi*******@yahoo.com (seia0106) wrote in message news:<4f**************************@posting.google. com>...
Hello,

Writing a program in c++ that should use complex numbers I have two
choices before me.

1- define a struct for complex data i.e
struct {float real,imag;
}ComplexNum;

2-use an array of float type
float Array[]
and consider its even and odd indices as real and imaginary parts of
data respectively.


3-#include <complex> and use the complex class the standard already defines.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #4
mi*******@yahoo.com (seia0106) wrote in message news:<4f**************************@posting.google. com>...
Hello,

Writing a program in c++ that should use complex numbers I have two
choices before me.

1- define a struct for complex data i.e
struct {float real,imag;
}ComplexNum;

2-use an array of float type
float Array[]
and consider its even and odd indices as real and imaginary parts of
data respectively.

I must add that the program loops have to do lot of iterations for
multiplications and additions of complex numbers.

Right now choice one seems to have one advantage that it would easy to
see real and imag seperately instead of worrying about the correct
value of indices. But clearity alone is not all that is needed in this
case.

Would someone please comment on the above two approaches, their
differences, effect on performance/speed, and their pros and cons.

Thanks in advance


Any reason you can't use the STL complex type?

#include <complex>
....
std::complex<float> c(1.5f, 2.1f);

You get all the advantages of objects and overloaded operators built
in. If your comiler is any good then the STL normally produces
near-optimal code, so it should be the fastest way to do things, too.

Jam
Jul 22 '05 #5

"seia0106" <mi*******@yahoo.com> wrote in message
news:4f**************************@posting.google.c om...
Hello,

Writing a program in c++ that should use complex numbers I have two
choices before me.

1- define a struct for complex data i.e
struct {float real,imag;
}ComplexNum;

2-use an array of float type
float Array[]
and consider its even and odd indices as real and imaginary parts of
data respectively.

I must add that the program loops have to do lot of iterations for
multiplications and additions of complex numbers.

Right now choice one seems to have one advantage that it would easy to
see real and imag seperately instead of worrying about the correct
value of indices. But clearity alone is not all that is needed in this
case.

Would someone please comment on the above two approaches, their
differences, effect on performance/speed, and their pros and cons.


I would expect them to be exactly the same speed, in fact I can't see why
you think they would be different.

If you are really concerned about speed then you should know that double is
faster than float on many platforms.

In any case the only way to find out about speed issues is to try both
techniques and time them. I'd be very surprised to see any difference so I
would always go for option one without thinking.

john
Jul 22 '05 #6
"John Harrison" <jo*************@hotmail.com> wrote in message news:<c6************@ID-196037.news.uni-berlin.de>...
"seia0106" <mi*******@yahoo.com> wrote in message
news:4f**************************@posting.google.c om...
Hello,

Writing a program in c++ that should use complex numbers I have two
choices before me.

1- define a struct for complex data i.e
struct {float real,imag;
}ComplexNum;

2-use an array of float type
float Array[]
and consider its even and odd indices as real and imaginary parts of
data respectively.

I must add that the program loops have to do lot of iterations for
multiplications and additions of complex numbers.

Right now choice one seems to have one advantage that it would easy to
see real and imag seperately instead of worrying about the correct
value of indices. But clearity alone is not all that is needed in this
case.

Would someone please comment on the above two approaches, their
differences, effect on performance/speed, and their pros and cons.


I would expect them to be exactly the same speed, in fact I can't see why
you think they would be different.

If you are really concerned about speed then you should know that double is
faster than float on many platforms.

In any case the only way to find out about speed issues is to try both
techniques and time them. I'd be very surprised to see any difference so I
would always go for option one without thinking.

john

Thanks for all the replies.
It was about integrating given C programs into a larger C++ program
and typedef has been used in given C routines. Using double instead of
float is a good tip although would have to make many changes in the
program then.
Regarding Rolf's question, speed is of utmost importance to me.

regards
Jul 22 '05 #7
seia0106 wrote:


Writing a program in c++ that should use complex numbers
I have two choices before me.

1- define a struct for complex data i.e

typedef struct {float real, imag; } ComplexNum;
Why not

typedef struct {float modulus, argument; } ComplexNum;
2-use an array of float type float Array[]
and consider its even and odd indices
as real and imaginary parts of data respectively.
Why not store the real and imaginary parts in separate real arrays?
Like MATLAB.
I must add that the program loops have to do lot of iterations
for multiplications and additions of complex numbers.
Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/
Right now choice one seems to have one advantage
that it would easy to see real and imag separately
instead of worrying about the correct value of indices.


Small advantage since the representation should be private
and only accessible through member functions:

class floatComplex {
private:
// representation
float X[2];
public:
const
float& real(void) const { return X[0]; }
float& real(void) { return X[0]; }
const
float& imag(void) const { return X[1]; }
float& imag(void) { return X[1]; }
Jul 22 '05 #8

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

Similar topics

1
by: Dennis Gavrilov | last post by:
Hi, All! I have two questions: strategic and technical. Technical one first: I need to share an array of objects (implemented as hashes, having references to other objects and hashes, sharing...
0
by: Ivan | last post by:
Hi All, I have a problem with marshaling complex structures (containing numbers, strings, arrays of another structures) to native C function in dll. I have already posted same question to...
5
by: spasmous | last post by:
I'm looking into upgrading from version 2 to version 3 of the FFT code package FFTW (www.fftw.org). The two versions are incompatible - a lot of it has to do with changing from a complex struct...
0
by: Ivan | last post by:
Hi All, I have a problem with marshaling complex structures (containing numbers, strings, arrays of another structures) to native C function in dll. I have already posted same question to...
2
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and...
2
by: ma740988 | last post by:
I'm looking for an equivalent approach to what I would do in C++ such to create a vector of vector of complex. I would do : typedef std::vector < std::complex < double complex_dvec; typedef...
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
2
by: Andreas Lundgren | last post by:
Hi! I want to put some values in static memory, and then be able to reference this data from my code in a decent way by referencing AllData.Data. (All code beneth is placed outside functions.) ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.