# function of 2d array

Discussion in 'C++' started by Andi, Nov 7, 2006.

1. ### AndiGuest

Hi, I want to define the following functions before main(); I work with
2D arrays (fftw_complex) wich are arrays with array[][0] is real and
array[][1] the imaginary part of a complex number. Now I want to do
some operations like norming, swapping, coord transforms as functions.
But I don't know how to handle these 1D/2D arrays. Any advices (maybe a
book etc.?)

// the following 4 work
double getAmplitude (double arg[]) {
double amplitude=sqrt(pow(arg[0],2.)+pow(arg[1],2.));
return amplitude;
}

double getPhase (double arg[]) {
double phase=atan(arg[1]/arg[0]);
return phase;
}

double getRe (double amplitude, double phase) {
double result=amplitude*cos(phase);
return result;
}

double getIm (double amplitude, double phase) {
double result=amplitude*sin(phase);
return result;
}

// functions -- Do not work
void coord_transform (fftw_complex arg[][2]);

void norm (fftw_complex arg[][2]) {
int Dim=((int)(sizeof(arg)/(sizeof(arg[0]))));
for (int i=0;i<Dim;i++) {
double amplitude=getAmplitude(arg);
double phase=getPhase(arg);
double normamplitude=amplitude/Dim;
arg[0]=getRe(normamplitude*phase);
arg[1]=getIm(normamplitude*phase);
}
}

Andi, Nov 7, 2006

2. ### XtremDevGuest

may be you could first create a complex class :

class complex
{
public:

complex();
complex(double p_dRe, double p_dIm);
complex(const complex & p_complex);
~complex();

complex & operator=(const complex & p_complex);

double GetRe() const;
double GetIm() const;

double GetMod() const;
double GetArg() const;

void SetRe(dconst double p_dValue);
void SetIm(const double p_dValue);

etc...... opertor+, operator-, operator +=, operator -=

protected:

double m_dRe;
double m_dIm;
};

and after you could manage a 1D array of complex;
Complex * l_pArrComplex = new Complex[256];

Finally, you could use the complex array pointer in a Fft class or
function like this :

void FftTransform( double * p_pdInSignal, Complex * p_pcplxOutFft );

On 7 nov, 13:30, "Andi" <> wrote:
> Hi, I want to define the following functions before main(); I work with
> 2D arrays (fftw_complex) wich are arrays with array[][0] is real and
> array[][1] the imaginary part of a complex number. Now I want to do
> some operations like norming, swapping, coord transforms as functions.
> But I don't know how to handle these 1D/2D arrays. Any advices (maybe a
> book etc.?)
>
> // the following 4 work
> double getAmplitude (double arg[]) {
> double amplitude=sqrt(pow(arg[0],2.)+pow(arg[1],2.));
> return amplitude;
>
> }double getPhase (double arg[]) {
> double phase=atan(arg[1]/arg[0]);
> return phase;
>
> }double getRe (double amplitude, double phase) {
> double result=amplitude*cos(phase);
> return result;
>
> }double getIm (double amplitude, double phase) {
> double result=amplitude*sin(phase);
> return result;
>
> }// functions -- Do not work
> void coord_transform (fftw_complex arg[][2]);
>
> void norm (fftw_complex arg[][2]) {
> int Dim=((int)(sizeof(arg)/(sizeof(arg[0]))));
> for (int i=0;i<Dim;i++) {
> double amplitude=getAmplitude(arg);
> double phase=getPhase(arg);
> double normamplitude=amplitude/Dim;
> arg[0]=getRe(normamplitude*phase);
> arg[1]=getIm(normamplitude*phase);
> }
>
>
>
> }- Masquer le texte des messages précédents -- Afficher le texte des messages précédents -

XtremDev, Nov 7, 2006

3. ### Daniel T.Guest

"XtremDev" <> wrote:

> may be you could first create a complex class :

'complex' already exists in the standard.

--
To send me email, put "sheltie" in the subject.

Daniel T., Nov 7, 2006
4. ### Ivan VecerinaGuest

"Andi" <> wrote in message
news:...
: Hi, I want to define the following functions before main(); I work
with
: 2D arrays (fftw_complex) wich are arrays with array[][0] is real and
: array[][1] the imaginary part of a complex number. Now I want to do
: some operations like norming, swapping, coord transforms as functions.
:
: // the following 4 work
: double getAmplitude (double arg[])
If the parameter must always be a 2-item array, it
would be pass that array by reference:
double getAmplitude( double const (&arg)[2] )

: {
: double amplitude=sqrt(pow(arg[0],2.)+pow(arg[1],2.));
: return amplitude;
: }
:
: double getPhase (double arg[]) {
Same: double getPhase ( double const (&arg)[2] )

: double phase=atan(arg[1]/arg[0]);
: return phase;
: }
:
: double getRe (double amplitude, double phase) {
: double result=amplitude*cos(phase);
: return result;
: }
:
: double getIm (double amplitude, double phase) {
: double result=amplitude*sin(phase);
: return result;
: }
:
: // functions -- Do not work
: void coord_transform (fftw_complex arg[][2]);
:
: void norm (fftw_complex arg[][2]) {
: int Dim=((int)(sizeof(arg)/(sizeof(arg[0]))));
: for (int i=0;i<Dim;i++) {
: double amplitude=getAmplitude(arg);
: double phase=getPhase(arg);
: double normamplitude=amplitude/Dim;
: arg[0]=getRe(normamplitude*phase);
: arg[1]=getIm(normamplitude*phase);
A simple change to the above lines allows your code to compile:
arg[0]=getRe(normamplitude,phase);
arg[1]=getIm(normamplitude,phase);

: }
: }

This said: working with "naked" array is tough, and calls for
trouble. Why not use a struct, or std::complex ?
[ or even better, an existing FFT implementation ... ]

: But I don't know how to handle these 1D/2D arrays. Any advices (maybe
a
: book etc.?)

I have no specific book advice, but you could study pointers,
references and arrays in your favorite C+ book.
http://accu.org/index.php/book_reviews?url=search.xqy?field=subject&term=beginner's+c%2b%2b

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

Ivan Vecerina, Nov 7, 2006
5. ### Guest

Andi wrote:
> Hi, I want to define the following functions before main(); I work with
> 2D arrays (fftw_complex) wich are arrays with array[][0] is real and
> array[][1] the imaginary part of a complex number. Now I want to do
> some operations like norming, swapping, coord transforms as functions.
> But I don't know how to handle these 1D/2D arrays. Any advices (maybe a
> book etc.?)

The simplest solution is to just take your fftw_complex *arg array and
typecast it to std::complex<double>* (or vice versa). See
http://www.fftw.org/doc/Complex-numbers.html for why this works (in
short, all extant std::complex implementations store as real part
followed by imaginary part, the same as C99, and this is slated to
become part of the standard if it hasn't already).

Note also that your code is wrong; you should use atan2 to get the
(It's also crazy to use the pow function just to square a number, BTW.)
Note also that you don't need to convert a complex number to polar
form just to divide by a real number -- it is much simpler and more
efficient to just divide the real and imaginary parts separately.

You may have a deeper problem if you want to use FFTs but don't know
how to do complex arithmetic given the real and imaginary parts of a
number.

Regards,
Steven G. Johnson

, Nov 7, 2006