473,382 Members | 1,526 Online

# 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
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.

Jul 22 '05 #1
7 5030
"seia0106" <mi*******@yahoo.com> wrote in message
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.

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
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
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.

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

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

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

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.