Tableau de nombres complexes

Fermé
ismailk Messages postés 1 Date d'inscription vendredi 17 avril 2015 Statut Membre Dernière intervention 17 avril 2015 - 17 avril 2015 à 12:18
mamiemando Messages postés 33123 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 mai 2024 - 8 mai 2015 à 01:26
Bonjour ,
J'essaie de creer un tableau avec des nombres complexes , le probleme c'est que mes nombres complexes sont generes dans une boucle for , et cela me rend l'erreur que pour utiliser 'complex<double>' mes arguments doivent etre constants , sauriez-vous comment remedier au probleme ?
Je vous joins mon code , et merci !!

 BYTE *img;  
    int tmp_Width, tmp_Height, tmp_Depth;
    Initialize_IMG_Buffer_With_Current_Setting(&img, &tmp_Width, &tmp_Height, &tmp_Depth);
    Get_Current_Image(img);

    
    BYTE *img_Buffer = new BYTE[tmp_Width * tmp_Height * tmp_Depth]; 
    BYTE *img_Buffer1 = new BYTE[tmp_Width * tmp_Height * tmp_Depth];

	complex<double> tab[512][512];
	complex<double> tab2[512][512];

	for(int m=0;m<tmp_Height;m++){
		for(int l=0;l<tmp_Width;l++){
			for(int n=0;n<tmp_Width/2;n++){
				int i = m*tmp_Width*tmp_Depth + n*tmp_Depth;
				int i2= m*tmp_Width*tmp_Depth + (n+tmp_Width/2)*tmp_Depth;
				complex<double> wnl1=polar(cos((-4*PI*l*n)/512),sin((-4*PI*l*n)/512));
				complex<double> wnl2=polar(cos((-2*PI-4*PI*l*n)/512),sin((-2*PI-4*PI*l*n)/512));
				if(l%2==0){
					tab[l][m]=tab[l][m]+(img[i]+img[i2])*wnl1;
				}
				else if(l%2==1){
					tab[l][m]=tab[l][m]+(img[i]-img[i2])*wnl2;
				}
			}
		}
	}
	for(int l=0;l<tmp_Width;l++){
		for(int k=0;k<tmp_Height;k++){
			for(int m=0;m<tmp_Heigt/2;m++){
				complex<double> wmk3=(cos((-4*PI*k*m)/512),sin((-4*PI*k*m)/512));
				complex<double> wmk4=(cos((-2*PI-4*PI*k*m)/512),sin((-2*PI-4*PI*k*m)/512));
				if(k%2==0){
					tab2[l][k]=tab2[l][k]+(tab[l][m]+tab[l][m+256])*wmk3;
				}
				else if(k%2==1){
					tab2[l][k]=tab2[l][k]+(tab[l][m]-tab[l][m+256])*wmk4;
				}
			}
		}
	}
	for(int k=0;k<tmp_Height;k++){
		for(int l=0;l<tmp_Width;l++){
			int i= k*tmp_Width*tmp_Depth + l*tmp_Depth;
			int mod = (int)abs(tab2[l][k]);
			int arg = (int)arg(tab2[l][k]);
			img_Buffer[i]=mod;
			img_Buffer1[i]=arg;
		}
	}

	Display_Current_Image_Output(img_Buffer, tmp_Width, tmp_Height, tmp_Depth, _T("mod"));
	Display_Current_Image_Output(img_Buffer1, tmp_Width, tmp_Height, tmp_Depth, _T("arg"));
    Destroy_IMG_Buffer_With_Current_Setting(&img);
    Destroy_IMG_Buffer_With_Current_Setting(&img_Buffer);
    Destroy_IMG_Buffer_With_Current_Setting(&img_Buffer1);

1 réponse

mamiemando Messages postés 33123 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 mai 2024 7 753
Modifié par mamiemando le 8/05/2015 à 01:34
Ton erreur est due à la manière dont tu initialises tes nombres complexes. Par exemple pour créer le nombre complexe z = a + i.b il ne faut pas écrire :

complex<double> z = (a, b) FAUX


... mais plutôt :

complex<double> z (a, b) // a + ib // ok, on appelle le constructeur


En effet dans le premier cas tu recopies de quelque chose qui est vu comme un int égal à b dans un complexe. Concrètement le complexe vaut alors z = b. Dans le second tu construis bien z = a + i.b.

Pour t'en convaincre :

#include <iostream>
#include <cmath>
#include <cstdint>
#include <complex>

template <typename T>
std::ostream & operator << (
    std::ostream & os,
    const std::complex<T> & z
) {
    os << std::real(z) << " + " << std::imag(z) << 'i';
    return os;
}

int main() {
    std::complex<double> z1 (10.0, 1.0);   // OK
    std::complex<double> z2 = (10.0, 1.0); // Faux
    std::cout << "z1 = " << z1 << std::endl
              << "z2 = " << z2 << std::endl;
    return 0;
}


... produit :

z1 = 10 + 1i
z2 = 1 + 0i


Du coup ensuite, pas spécialement de problème pour écrire par exemple :

#include <iostream>
#include <cmath>
#include <cstdint>
#include <complex>

template <typename T>
std::ostream & operator << (
    std::ostream & os,
    const std::complex<T> & z
) {
    os << std::real(z) << " + " << std::imag(z) << 'i';
    return os;
}

int main() {
    for (unsigned i = 0; i < 8; i++) {
        double angle = M_PI * i / 4;
        std::complex<double> z (cos(angle), sin(angle));
        std::cout << z << std::endl;
    }
    return 0;
}


... qui produit (presque) le résultat attendu :


1 + 0i
0.707107 + 0.707107i
6.12323e-17 + 1i
-0.707107 + 0.707107i
-1 + 1.22465e-16i
-0.707107 + -0.707107i
-1.83697e-16 + -1i
0.707107 + -0.707107i


... c'est à dire successivement 1, sqrt(2) * (1 + i) / 2, i, sqrt(2) * (-1 + i) / 2, -1, etc.

Bonne chance
0