HENRY Renaud
Développeur Informatique

Implémenatation des nombres complexes

Posted 10 Décembre 2017

Objectifs :

Implémentations des nombres complexes dans différents langages de programmation afin de préparer la création de fractales.

Les prérequis

  • Avoir Visuel Studio 2017
  • Savoir Créer un Projet

1) Introduction

L’article est composé de 5 parties. Dans un premier temps nous allons créer un projet UserForm, Afin d’avoir une interface utilisateur pour les tests (IHM : Interface Homme Machine) Puis nous allons créer le constructeur et les propriétés de la class « ComplexNumbers ». Après nous coderons les fonctions d’addition de soustraction … Pour surcharger les opérateur +,- … et ainsi avoir une utilisation plus simple de l’objet nombre complexe. Et enfin nous allons tester la class via l’IHM.

2) Création du projet UserForm «ComplexNumbers»

Dans un premier temps créez un nouveau projet via le menu File/Fichier :

Puis Selectionnez «Windows Form App» dans le language de programation Visuel Basic
Enfin nommez le projet ComplexNumbers

3) le constructeur et les propriétés de la class « ComplexNumbers »

Apres avoir créez la Class ComplexNumbers via Ajout de fichier du projet, vous devez obtenir :

Public Class ComplexNumbers

End Class
                                    
On rajoute les deux Regions (Constructor et Properties) :

Public Class ComplexNumbers
#Region "Constructor"

#End Region
#Region "Properties"

#End Region
End Class
                                    
Nous avons vues dans l'acticle de la semaine dernière que l'on peut utliser la forme algébrique du nombre complex, on a donc en Properties :

Public Class ComplexNumbers
#Region "Constructor"

#End Region
#Region "Properties"
  Private m_Reel As Double
    Public Property Reel() As Double
        Get
            Return m_Reel
        End Get
        Set(ByVal value As Double)
            m_Reel = value
        End Set
    End Property
    Private m_Img As Double
    Public Property Img() As Double
        Get
            Return m_Img
        End Get
        Set(ByVal value As Double)
            m_Img = value
        End Set
    End Property
    #End Region
End Class
                                    
Pour le constructeur nous allons implémenter 2 mehodes, Une avec des scalaires et une avec l'objet ComplexNumbers.
Le constructeur avec des scalaires :

Public Sub New(Reel As Double, Img As Double)
    m_Img = Img
    m_Reel = Reel
End Sub
                                    
Le constructeur avec l'objet ComplexNumbers :

Public Sub New(Z As ComplexeNumbers)
    m_Img = Z.Img
    m_Reel = Z.Reel
End Sub
                                    
Ce qui nous donne :

Public Class ComplexNumbers
#Region "Constructor"
    Public Sub New(Reel As Double, Img As Double)
        m_Img = Img
        m_Reel = Reel
    End Sub
    Public Sub New(Z As ComplexeNumbers)
        m_Img = Z.Img
        m_Reel = Z.Reel
    End Sub
#End Region
#Region "Properties"
  Private m_Reel As Double
    Public Property Reel() As Double
        Get
            Return m_Reel
        End Get
        Set(ByVal value As Double)
            m_Reel = value
        End Set
    End Property
    Private m_Img As Double
    Public Property Img() As Double
        Get
            Return m_Img
        End Get
        Set(ByVal value As Double)
            m_Img = value
        End Set
    End Property
    #End Region
End Class
                                    

4) les fonctions de la class « ComplexNumbers »

Maintenant que nous avons les propriétés et les constructeurs, nous pouvons réaliser des opérations dessus. Dans un premier temps , on réalier les fonction pour passer de la forme algébrique à la forme polaire.

#Region "Functions"
    Public Function r() As Double
        Return Math.Sqrt(m_Img * m_Img + m_Reel * m_Reel)
    End Function


    Public Function Phi_Radian() As Double
        If m_Reel <> 0 And m_Img <> 0 Then
            Return Math.Atan2(m_Img, m_Reel)
        ElseIf m_Reel <> 0 Then
            If m_Reel > 0 Then
                Return Math.Asin(m_Img / r())
            Else
                Return Math.PI - Math.Asin(m_Img / r())
            End If
        ElseIf m_Img <> 0 Then
            If m_Img > 0 Then
                Return Math.Acos(m_Reel / r())
            Else
                Return -Math.Acos(m_Reel / r())
            End If
        Else
            Return 0
        End If
    End Function


    Public Function Phi_degre() As Double
        Return Phi_Radian() / Math.PI * 180
    End Function
#End Region              
                                
Avec ces sortie, on peux passer de la forme algébrique à la forme polaire et inversement.
Donc On peut faire les opérations d'addition de de soustration avec la forme algébrique :

Public Function Addition(Z As ComplexeNumbers) As ComplexeNumbers
        Return New ComplexeNumbers(m_Reel + Z.Reel, m_Img + Z.Img)
End Function

Public Function Soustraction(Z As ComplexeNumbers) As ComplexeNumbers
        Return New ComplexeNumbers(m_Reel - Z.Reel, m_Img - Z.Img)
End Function
                                
Et On peut faire les opérations de multiplication de de divistion avec la forme polaire que l'on convertira en forme algébrique :

Public Function Multiplication(Z As ComplexeNumbers) As ComplexeNumbers
    Dim Phi_Current = Me.Phi_Radian
    Dim R_Current = Me.r

    Return New ComplexeNumbers(R_Current * Z.r * Math.Cos(Phi_Current + Z.Phi_Radian),
                               R_Current * Z.r * Math.Sin(Phi_Current + Z.Phi_Radian))
End Function

Public Function Division(Z As ComplexeNumbers) As ComplexeNumbers
    Dim Phi_Current = Me.Phi_Radian
    Dim R_Current = Me.r

    Return New ComplexeNumbers(R_Current / Z.r * Math.Cos(Phi_Current - Z.Phi_Radian),
                               R_Current / Z.r * Math.Sin(Phi_Current - Z.Phi_Radian))
End Function
                                
On fait de même pour la puissance :

Public Function Pow(n As Double) As ComplexeNumbers
    Dim Phi_Current = Me.Phi_Radian
    Dim R_Current = Me.r

    Return New ComplexeNumbers(Math.Pow(R_Current, n) * Math.Cos(Phi_Current * n),
                                Math.Pow(R_Current, n) * Math.Sin(Phi_Current * n))
End Function
                                

4) les fonctions de la class « ComplexNumbers »

Pour pouvoir utiliser les symboles +,- ..., il faut surcharger ces fonctions :

Public Shared Operator +(ByVal Z1 As ComplexeNumbers, ByVal Z2 As ComplexeNumbers) As ComplexeNumbers
    Return Z1.Addition(Z2)
End Operator

Public Shared Operator -(ByVal Z1 As ComplexeNumbers, ByVal Z2 As ComplexeNumbers) As ComplexeNumbers
    Return Z1.Soustraction(Z2)
End Operator

Public Shared Operator *(ByVal Z1 As ComplexeNumbers, ByVal Z2 As ComplexeNumbers) As ComplexeNumbers
    Return Z1.Multiplication(Z2)
End Operator
Public Shared Operator /(ByVal Z1 As ComplexeNumbers, ByVal Z2 As ComplexeNumbers) As ComplexeNumbers
    Return Z1.Division(Z2)
End Operator
                                

5) To string

Afin de verifier simplement la valeur d'un nombre complexe, on surcharge aussi la fonction toString :

Public Overloads Function ToString(Optional n As Integer = 3) As String
    Return Math.Round(CDec(m_Reel), n).ToString + "+ i*" + Math.Round(CDec(m_Img), n).ToString
End Function
                                

6) Code Complet

On obtient ainsi :

Public Class ComplexeNumbers
#Region "Constructor"
    Public Sub New(Reel As Double, Img As Double)
        m_Img = Img
        m_Reel = Reel
    End Sub
    Public Sub New(Z As ComplexeNumbers)
        m_Img = Z.Img
        m_Reel = Z.Reel
    End Sub

#End Region
#Region "Properties"
    Private m_Reel As Double
    Public Property Reel() As Double
        Get
            Return m_Reel
        End Get
        Set(ByVal value As Double)
            m_Reel = value
        End Set
    End Property
    Private m_Img As Double
    Public Property Img() As Double
        Get
            Return m_Img
        End Get
        Set(ByVal value As Double)
            m_Img = value
        End Set
    End Property

#End Region
#Region "Functions"
    Public Function r() As Double
        Return Math.Sqrt(m_Img * m_Img + m_Reel * m_Reel)
    End Function
    Public Function Phi_Radian() As Double
        If m_Reel <> 0 And m_Img <> 0 Then
            Return Math.Atan2(m_Img, m_Reel)
        ElseIf m_Reel <> 0 Then
            If m_Reel > 0 Then
                Return Math.Asin(m_Img / r())
            Else
                Return Math.PI - Math.Asin(m_Img / r())
            End If
        ElseIf m_Img <> 0 Then
            If m_Img > 0 Then
                Return Math.Acos(m_Reel / r())
            Else
                Return -Math.Acos(m_Reel / r())
            End If
        Else
            Return 0
        End If
    End Function

    Public Function Phi_degre() As Double
        Return Phi_Radian() / Math.PI * 180
    End Function
#End Region
#Region "Operators functions"
    Public Function Addition(Z As ComplexeNumbers) As ComplexeNumbers
        Return New ComplexeNumbers(m_Reel + Z.Reel, m_Img + Z.Img)
    End Function

    Public Function Soustraction(Z As ComplexeNumbers) As ComplexeNumbers
        Return New ComplexeNumbers(m_Reel - Z.Reel, m_Img - Z.Img)
    End Function

    Public Function Multiplication(Z As ComplexeNumbers) As ComplexeNumbers
        Dim Phi_Current = Me.Phi_Radian
        Dim R_Current = Me.r

        Return New ComplexeNumbers(R_Current * Z.r * Math.Cos(Phi_Current + Z.Phi_Radian),
                                   R_Current * Z.r * Math.Sin(Phi_Current + Z.Phi_Radian))
    End Function

    Public Function Division(Z As ComplexeNumbers) As ComplexeNumbers
        Dim Phi_Current = Me.Phi_Radian
        Dim R_Current = Me.r

        Return New ComplexeNumbers(R_Current / Z.r * Math.Cos(Phi_Current - Z.Phi_Radian),
                                   R_Current / Z.r * Math.Sin(Phi_Current - Z.Phi_Radian))
    End Function

    Public Function Pow(n As Double) As ComplexeNumbers
        Dim Phi_Current = Me.Phi_Radian
        Dim R_Current = Me.r

        Return New ComplexeNumbers(Math.Pow(R_Current, n) * Math.Cos(Phi_Current * n),
                                    Math.Pow(R_Current, n) * Math.Sin(Phi_Current * n))
    End Function
#End Region
#Region "Opérators Symboles"
    Public Shared Operator +(ByVal Z1 As ComplexeNumbers, ByVal Z2 As ComplexeNumbers) As ComplexeNumbers
        Return Z1.Addition(Z2)
    End Operator

    Public Shared Operator -(ByVal Z1 As ComplexeNumbers, ByVal Z2 As ComplexeNumbers) As ComplexeNumbers
        Return Z1.Soustraction(Z2)
    End Operator

    Public Shared Operator *(ByVal Z1 As ComplexeNumbers, ByVal Z2 As ComplexeNumbers) As ComplexeNumbers
        Return Z1.Multiplication(Z2)
    End Operator
    Public Shared Operator /(ByVal Z1 As ComplexeNumbers, ByVal Z2 As ComplexeNumbers) As ComplexeNumbers
        Return Z1.Division(Z2)
    End Operator
#End Region
#Region "To string"
    Public Overloads Function ToString(Optional n As Integer = 3) As String
        Return Math.Round(CDec(m_Reel), n).ToString + "+ i*" + Math.Round(CDec(m_Img), n).ToString
    End Function
#End Region
End Class

                                

7) L'IHM

Afin de tester la class, il faut réaliser une Inteface Graphique : Avec le Code ci-dessous :

Public Class IHM_Teste
#Region "Constructor"
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_TextBox_a1.Text = "5,7"
        m_TextBox_a2.Text = "4,4"
        m_TextBox_b1.Text = "1,1"
        m_TextBox_b2.Text = "2,8"
        m_TextBox_power.Text = "2,7"
    End Sub
#End Region
#Region "Properties"
    Private m_Z1 As ComplexeNumbers
    Private m_Z2 As ComplexeNumbers
    Private m_Power As Double
#End Region
#Region "Button"

    Private Sub Button_Compute_Click(sender As Object, e As EventArgs) Handles Button_Compute.Click
        Compute()
    End Sub

    Private Sub Button_Clear_Click(sender As Object, e As EventArgs) Handles Button_Clear.Click
        m_RichTextBox_Output.Text = ""
    End Sub

#End Region
#Region "Function"
    Private Sub Compute()

        Dim a1 As Double = 0
        Dim b1 As Double = 0
        Dim a2 As Double = 0
        Dim b2 As Double = 0
        Dim power As Double = 0


        'Conversion text to Double
        Try
            a1 = CDbl(m_TextBox_a1.Text)
        Catch ex As Exception
            m_RichTextBox_Output.Text += "Erreur a_1" + Chr(10)
            Exit Sub
        End Try

        Try
            a2 = CDbl(m_TextBox_a2.Text)
        Catch ex As Exception
            m_RichTextBox_Output.Text += "Erreur a_2" + Chr(10)
            Exit Sub
        End Try

        Try
            b1 = CDbl(m_TextBox_b1.Text)
        Catch ex As Exception
            m_RichTextBox_Output.Text += "Erreur b_1" + Chr(10)
            Exit Sub
        End Try

        Try
            b2 = CDbl(m_TextBox_b2.Text)
        Catch ex As Exception
            m_RichTextBox_Output.Text += "Erreur b_2" + Chr(10)
            Exit Sub
        End Try

        Try
            power = CDbl(m_TextBox_power.Text)
        Catch ex As Exception
            m_RichTextBox_Output.Text += "Erreur Power" + Chr(10)
            Exit Sub
        End Try

        m_Z1 = New ComplexeNumbers(a1, b1)
        m_Z2 = New ComplexeNumbers(a2, b2)
        m_Power = power

        m_RichTextBox_Output.Text += " Z1 = " + m_Z1.ToString + Chr(10)
        m_RichTextBox_Output.Text += " Z2 = " + m_Z2.ToString + Chr(10)

        m_RichTextBox_Output.Text += " Z1 + Z2 = " + (m_Z1 + m_Z2).ToString + Chr(10)
        m_RichTextBox_Output.Text += " Z1 - Z2 = " + (m_Z1 - m_Z2).ToString + Chr(10)

        m_RichTextBox_Output.Text += " Z1 * Z2 = " + (m_Z1 * m_Z2).ToString + Chr(10)
        m_RichTextBox_Output.Text += " Z1 / Z2 = " + (m_Z1 / m_Z2).ToString + Chr(10)

        m_RichTextBox_Output.Text += " Z1^power = " + (m_Z1.Pow(m_Power)).ToString + Chr(10)
        m_RichTextBox_Output.Text += " Z2^power = " + (m_Z2.Pow(m_Power)).ToString + Chr(10)
    End Sub
#End Region
End Class
                                

8) Conclusion

Maintenant que nous avons la class pour manipuler les nombres complexes, il devient possible de faire des fractales.
La suites au prochain épisode

9) Annexes / Références