Nombres complexes C: avec class Cmplx

Description

Bonjour,

Le code présenté ici est le "frère" de celui de "Nombres complexes B: avec struct Cmplx".

La différence principale entre "struct" et "class" est la possibilité de "gérer" la protection de l'accès à leurs données et fonctions.
Cela est souvent nécessaire afin de garantir une certaine cohérence des données.

De plus, la classe Cmplx a été augmentée de la fonction:
void cbrt(const Cmplx& z, Cmplx x[3]) {
  x[0] = exp(log(z)/3);
  x[1] = x[0]*Cmplx(-0.5, sqrt(0.75));
  x[2] = x[0]*Cmplx(-0.5,-sqrt(0.75));
} // x[0]³ = z, x[1]³ = z, x[2]³ = z.
qui calcule les 3 racines cubiques du nombre complexe z.

Les fichiers Cmplx.h et Cmplx.cpp séparent la définition de la classe de nombres complexes du programme main contenu dans CmplxClass.cpp.

class cmplx dans le fichier Cmplx.h:
#define Reel double

class Cmplx {
private:
  Reel R, I;

public:
  // constructeurs
  Cmplx(); // default
  Cmplx(Reel re, Reel im);
  Cmplx(const Cmplx& z);

  // affectation
  Cmplx& operator=(const Reel r);

  // affectations avec opération (Cmplx)
  Cmplx& operator += (const Cmplx& z);
  Cmplx& operator -= (const Cmplx& z);
  Cmplx& operator *= (const Cmplx& z);
  Cmplx& operator /= (const Cmplx& z);

  // affectations avec opération (Reel)
  Cmplx& operator += (const Reel d);
  Cmplx& operator -= (const Reel d);
  Cmplx& operator *= (const Reel d);
  Cmplx& operator /= (const Reel d);

  // fonctions réelles standards
  friend Reel real(const Cmplx& z);
  friend Reel imag(const Cmplx& z);
  friend Reel norm(const Cmplx& z);
  friend Reel abs(const Cmplx& z);
  friend Reel arg(const Cmplx& z);

  // opérateurs de comparaison
  friend bool operator==(const Cmplx y,const Cmplx& z);
  friend bool operator!=(const Cmplx y,const Cmplx& z);

  // opérateurs unaires
  friend Cmplx operator+(const Cmplx& z);
  friend Cmplx operator-(const Cmplx& z);

  // opérateurs binaires
  friend Cmplx& operator+(Cmplx y,const Cmplx& z);
  friend Cmplx& operator+(const Reel a,Cmplx z);
  friend Cmplx& operator+(Cmplx z,const Reel a);
  friend Cmplx& operator-(Cmplx y,const Cmplx& z);
  friend Cmplx  operator-(const Reel a,const Cmplx& z);
  friend Cmplx& operator-(Cmplx z,const Reel a);
  friend Cmplx& operator*(Cmplx y,const Cmplx& z);
  friend Cmplx& operator*(const Reel a,Cmplx z);
  friend Cmplx& operator*(Cmplx z,const Reel a);
  friend Cmplx& operator/(Cmplx y,const Cmplx& z);
  friend Cmplx  operator/(Reel a,const Cmplx& z);
  friend Cmplx& operator/(Cmplx z,const Reel a);

  // fonctions complexes  standards
  friend Cmplx conj(const Cmplx& z);
  friend Cmplx cos (const Cmplx& z);
  friend Cmplx cosh(const Cmplx& z);
  friend Cmplx exp (const Cmplx& z);
  friend Cmplx log (const Cmplx& z);
  friend Cmplx log10(const Cmplx& z);
  friend Cmplx polar(const Reel r,const Reel t);
  friend Cmplx pow (const Cmplx& z,Reel d);
  friend Cmplx pow (const Cmplx& z,const Cmplx& x);
  friend Cmplx sin (const Cmplx& z);
  friend Cmplx sinh(const Cmplx& z);
  friend Cmplx sqrt(const Cmplx& z);
  friend Cmplx tan (const Cmplx& z);
  friend Cmplx tanh(const Cmplx& z);

  // fonctions complexes suplémentaires
  friend Cmplx& square(const Cmplx& z);
  friend Cmplx& cube  (const Cmplx& z);
};

En consultant Cmplx.cpp, on constate qu'on y utilise les mêmes "algorithmes".

Le résultat de la compilation et de l'exécution des fichiers du Zip permet une vérification sommaire:
Nombres complexes C: avec class Cmplx

A: x = [-5+i·4], y = [3+i·2.1], z = [7-i·4.88]
B: w=17 = [17+i·0], w = z = [7-i·4.88]
C: x==y ? false, x!=y ? true
D: +z = [7-i·4.88], -z = [-7+i·4.88]
E: x+y = [-2+i·6.1]; w=z, w+=x = [2-i·0.88]; w=z, w+=6 = [13-i·4.88]
E': 3+x = [-2+i·4], x+4 = [-1+i·4]
F: x-y = [-8+i·1.9]; w=z, w-=x = [12-i·8.88]; w=z, w-=6 = [1-i·4.88]
F': 3-x = [8-i·4], x-1 = [-6+i·4]
G: x*y = [-23.4+i·1.5]; w=z, w*=x = [-15.48+i·52.4]; w=z, w*=6 = [42-i·29.28]
G': 3*x = [-15+i·12], x*4 = [-20+i·16]
H: x/y = [-0.49217+i·1.67785]; w=z, w/=x = [-1.32976-i·0.0878049]; w=z, w/=6 = [1.16667-i·0.813333]
H': 3/x = [-0.365854-i·0.292683], x/2.5 = [-2+i·1.6]
I': 3+1/x+z/y-5*x*y = [120.68-i·9.78548]
J: norm(z) = 72.8144, abs(z) = 8.53314, arg(z) = -0.608806
K: conj(z) = [7+i·4.88]
L: cos(z) = [49.6212+i·43.2373], cosh(z) = [91.4743+i·540.632]
M: exp(z) = [182.948+i·1081.27]
N: log(z) = [2.14396-i·0.608806], log10(z) = [0.931109-i·0.264401]
O': polar(5, pi/6) = [4.33013+i·2.5]
P: pow(z, 3) = [-157.102-i·601.146], pow(z, 1.5) = [15.2354-i·19.7286], pow(z, x) = [0.000147522-i·0.00020478]
Q: sin(z) = [43.2423-i·49.6155], sinh(z) = [91.4741+i·540.633]
R: w = sqrt(z) = [-2.78686+i·0.875539], w*w = [7-i·4.88]
S: tan(z) = [0.000114343-i·0.999984], tanh(z) = [1+i·5.47112e-07]
T: square(z) = [25.1856-i·68.32], pow(z,2) = [25.1856-i·68.32]
U: cube(z) = [-157.102-i·601.146], pow(z,3) = [-157.102-i·601.146]
V: cbrt(z, r); r[0] = [2.00154-i·0.411853], r[1] = [-0.644096+i·1.93931], r[2] = [-1.35745-i·1.52746]
   cube(r[0]) = [7-i·4.88], cube(r[1]) = [7-i·4.88], cube(r[2]) = [7-i·4.88]

Equation du second degre a coefficients complexes:  a*x*x + b*x + c = 0.
Avec a = [-5+i·4], b = [6+i·3], c = [1-i·7]
  posons D = b*b - 4*a*c = [-65-i·120], srD = sqrt(D) = [-5.97802+i·10.0368]
  x1 = (-b + srD)/(2*a) = [1.07362+i·0.155222]
  x2 = (-b - srD)/(2*a) = [-0.6346+i·0.795997]
Control 1: a*x1*x1 + b*x1 + c = [-1.77636e-15-i·8.88178e-16]
Control 2: a*x2*x2 + b*x2 + c = [0-i·1.77636e-15]

 
Bonne lecture ...
 

Liens

CodeS-SourceS: Nombres complexes A: avec la bibliothèque logicielle std complex
CodeS-SourceS: Nombres complexes B: avec struct Cmplx
 

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.