Complex struct or Array

S

seia0106

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
 
J

Jakob Bieling

seia0106 said:
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
 
R

Rolf Magnus

seia0106 said:
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.
 
J

Jerry Coffin

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

James Moughan

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
 
J

John Harrison

seia0106 said:
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
 
S

seia0106

John Harrison said:
seia0106 said:
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
 
E

E. Robert Tisdale

seia0106 said:
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]; }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top