next up previous contents
Next: Vecteurs de longueur Up: Algèbres et Types Previous: Algèbres et Types

Une classe de Complexes

Consiérons d'abord la classe des complexes:

class Complex      { public: float re,im;      
  Complex(int i)      {re = i; im = 0;}
  Complex(float r=0, float i =0)      {re =r; im =i;}  
  Complex(const Complex& a)       {re =a.re; im =a.im;}  
  Complex& operator=(const Complex& a) { re =a.re; im = a.im; return *this; }   
  Complex& operator+=(const Complex& a)
              { re +=a.re; im += a.im; return *this; }   
  friend Complex operator+(const Complex& a, const Complex& b) 
              { Complex c(a); c += b; return c; }   
  Complex& operator-=(const Complex& a)
              { re -=a.re; im -= a.im; return *this; }   
  friend Complex operator-(const Complex& a, const Complex& b)  
              { Complex c(a); c -= b; return c; }   
  friend Complex operator*(const Complex& a, const Complex& b) 
        { Complex c(a.re*b.re-a.im*b.im,a.re*b.im + a.im*b.re); return c; }   
  friend Complex operator /(const Complex& a, const Complex& b)  
          { 
            Complex c; float xl= b.re*b.re + b.im*b.im; 
            c.re =(a.re*b.re+a.im*b.im)/xl; c.im =(b.re*a.im-b.im*a.re)/xl; 
          return c;
          }
  friend float norm2(const Complex& b) { return b.re*b.re + b.im*b.im;}
  friend float real(const Complex& b) { return b.re;}
  friend float imag(const Complex& b) { return b.im;}
};
Ainsi a = b * c + d marchera dans les 2 cas de réels ou de nombres complexes. Continuons maintenant avec des vecteurs et des matrices de tailles N et NxN. Commes leurs éléments peuvent être un corps quelconques, nous le laissons indéterminé; ce sera le type T.



Pironneau Olivier
Jeudi 26 juin 1997 07:15:20