Up ! Mathematical - Matrice

Ce programme présente l'usage des matrices de réels.

Le fichier source est ${UPS_HOME}/upsvtm/demo/${UPS_LANGUAGE}/matrice.upl.

Cet exemple utilise les instructions de l'extension d'Up ! 5GL pour Up ! Mathematical.

Pour analyser le fichier source, UpsMat.upi doit être déclaré parmi les modules importés dans le fichier ${UPS_HOME}/ini/${UPS_USER}/upsp5l.ini.

Mode compilé

Commande de compilation

upscmp Source=matrice.upl

Commande d'exécution

matrice

Mode interprété

upssng Source=matrice.upl

Fichier source

Source Composant "Exemple d'emploi du type Matrice" Version 4.0.0;

Procedure EcrireMatrice(Libelle : Caractere, M : Matrice[?,?] De Nul Ou Reel)
/***************************************************************************/
Variable
/******/
Debut
Ecran.Ecrire(Libelle);
Ecran.Ecrire("-------------");
Pour I=0 JusquA M.NbLignes-1 Faire Fin Pour
Fin Procedure

Procedure Appliquer1(R : Nul Ou Reel)
/***********************************/
Debut
Ecran.Ecrire(Caractere(R, "-&&&.&&&.&&&.&&#,#&&"));
Fin Procedure

Procedure Appliquer2(R1 : Nul Ou Reel, R2 : Nul Ou Reel)
/******************************************************/
Debut
Ecran.Ecrire(Caractere(R1, "-&&&.&&&.&&&.&&#,#&&")+"+"+Caractere(R2, "-&&&.&&&.&&&.&&#,#&&"));
Fin Procedure

Fonction Appliquer3(R : Nul Ou Reel) Retourner Nul Ou Reel
/********************************************************/
Debut Retourner -R;
Fin Fonction

Fonction Appliquer4(R1 : Nul Ou Reel, R2 : Nul Ou Reel) Retourner Nul Ou Reel
/***************************************************************************/
Debut
Retourner -(R1+R2);
Fin Fonction

Principal
/*******/
Variable
/******/
Debut
A[0,0]=1.0;
A[0,1]=2.0;
A[0,2]=3.0;
A[1,0]=4.0;
A[1,1]=6.0;
A[1,2]=5.0;
A[2,0]=7.0;
A[2,1]=8.0;
A[2,2]=9.0;
EcrireMatrice("A", A);
B={{4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}, {1.0, 2.0, 3.0}};
EcrireMatrice("B", B);
Ecran.Ecrire(");

C=A+B;
EcrireMatrice("A+B", C);
C=-A;
EcrireMatrice("-A", C);
C=A-B;
EcrireMatrice("A-B", C);
C=A*B;
EcrireMatrice("A*B", C);
C=B/A;
EcrireMatrice("B/A", C);
C=A.Cloner();
EcrireMatrice("++C", ++C);
EcrireMatrice("--C", --C);
C=C^4;
EcrireMatrice("C^4", C);
Ecran.Ecrire(");

C=2.0;
EcrireMatrice("C=2.0", C);
C+=3.0;
EcrireMatrice("C+=3.0", C);
C-=3.0;
EcrireMatrice("C-=3.0", C);
C*=3.0;
EcrireMatrice("C*=3.0", C);
C/=3.0;
EcrireMatrice("C/=3.0", C);
C^=4;
EcrireMatrice("C^=4", C);
Ecran.Ecrire(");

C=A.Copier(0,0,3,3);
EcrireMatrice("Copier()", C);
C=0.0;
C.Coller(0, 0, A);
EcrireMatrice("Coller()", C);
C=A.InsererColonne(1, 10.0);
EcrireMatrice("InsererColonne()", C);
C=A.InsererLigne(1, 10.0);
EcrireMatrice("InsererLigne()", C);
C=A.SupprimerColonne(1);
EcrireMatrice("SupprimerColonne()", C);
C=A.SupprimerLigne(1);
EcrireMatrice("SupprimerLigne()", C);
C=A.Copier(0, 0, 3, 3);
C.PermuterColonnes(0, 2);
EcrireMatrice("PermuterColonnes()", C);
C=A.Copier(0, 0, 3, 3);
C.PermuterLignes(0, 2);
EcrireMatrice("PermuterLignes()", C);
C=A.Copier(0, 0, 3, 3);
C.Transposer();
EcrireMatrice("Transposer()", C);
C=A.Copier(0, 0, 3, 3);
C.AppliquerSymetrieVerticale();
EcrireMatrice("AppliquerSymetrieVerticale()", C);
C=A.Copier(0, 0, 3, 3);
C.AppliquerSymetrieHorizontale();
EcrireMatrice("AppliquerSymetrieHorizontale()", C);
Si C.EstDiagonale() Alors Sinon Fin Si
Si C.EstTriangulaireSuperieure() Alors Sinon Fin Si
Si C.EstTriangulaireInferieure() Alors Sinon Ecran.Ecrire("Non EstTriangulaireInferieure"); Fin Si
Si C.EstSymetrique() Alors Sinon Fin Si
Si C.EstAntiSymetrique() Alors Sinon Fin Si
Ecran.Ecrire(");

A.Appliquer(Appliquer1);
A.Appliquer(Appliquer2,B);
C=A.Appliquer(Appliquer3);
EcrireMatrice("Appliquer3()", C); C=A.Appliquer(Appliquer4,B);
EcrireMatrice("Appliquer4()", C);
Ecran.Ecrire(");

A={{1.0, 1.0, 2.0}, {1.0, 0.0, -2.0}, {-1.0, 2.0, 3.0}};
DecomposerQR(A,Q,R);
EcrireMatrice("Q", Q);
EcrireMatrice("R", R);
DecomposerPLU(A, P, L, U);
EcrireMatrice(P", P);
EcrireMatrice("L", L);
EcrireMatrice("U", U);
DecomposerPLDU(A, P, L, D, U);
EcrireMatrice("P", P);
EcrireMatrice("L", L);
EcrireMatrice("D", D);
EcrireMatrice("U", U);
A={{1.0, 2.0, 3.0}, {4.0, 6.0, 5.0}, {7.0, 8.0, 9.0}};
DecomposerVL(A, V ,L);
EcrireMatrice("V", V);
EcrireMatrice("L", L);
C=Inv(A);
EcrireMatrice("Inv(A)", C);
X=Reel(Det(A));
Ecran.Ecrire("Det(A)="+Caractere(X, "-&&&.&&&.&&&.&&#,#&&.&&&"));
X=Reel(Trc(A));
Ecran.Ecrire("Trc(A)="+Caractere(X, "-&&&.&&&.&&&.&&#,#&&.&&&"));
Y=Rng(A);
Ecran.Ecrire("Rng(A)="+Caractere(Y));
A={{5.0, -4.0, 1.0}, {-4.0, 6.0, -4.0}, {0.0, -4.0, 5.0}};
C=Exp(A);
EcrireMatrice("Exp(A)", C);
C=Log(A);
EcrireMatrice("Log(A)", C);
C=Racine(A);
EcrireMatrice("Racine(A)", C);
Ecran.Ecrire(");
Fin Principal

Résultat de l'exécution

A ------------- {{1,0, 2,0, 3,0} {4,0, 6,0, 5,0} {7,0, 8,0, 9,0}} B ------------- {{4,0, 5,0, 6,0} {7,0, 8,0, 9,0} {1,0, 2,0, 3,0}} A+B ------------- {{5,0, 7,0, 9,0} {11,0, 14,0, 14,0} {8,0, 10,0, 12,0}} -A ------------- {{-1,0, -2,0, -3,0} {-4,0, -6,0, -5,0} {-7,0, -8,0, -9,0}} A-B ------------- {{-3,0, -3,0, -3,0} {-3,0, -2,0, -4,0} {6,0, 6,0, 6,0}} A*B ------------- {{21,0, 27,0, 33,0} {63,0, 78,0, 93,0} {93,0, 117,0, 141,0}} B/A ------------- {{0,500.000, -0,0, 0,5} {0,0, -0,0, 1,0} {1,0, -0,0, 0,0}} ++C ------------- {{2,0, 3,0, 4,0} {5,0, 7,0, 6,0} {8,0, 9,0, 10,0}} --C ------------- {{1,0, 2,0, 3,0} {4,0, 6,0, 5,0} {7,0, 8,0, 9,0}} C^4 ------------- {{7.374,0, 9.692,0, 10.186,0} {16.056,0, 21.108,0, 22.182,0} {25.986,0, 34.160,0, 35.902,0}} C=2.0 ------------- {{2,0, 2,0, 2,0} {2,0, 2,0, 2,0} {2,0, 2,0, 2,0}} C+=3.0 ------------- {{5,0, 5,0, 5,0} {5,0, 5,0, 5,0} {5,0, 5,0, 5,0}} C-=3.0 ------------- {{2,0, 2,0, 2,0} {2,0, 2,0, 2,0} {2,0, 2,0, 2,0}} C*=3.0 ------------- {{6,0, 6,0, 6,0} {6,0, 6,0, 6,0} {6,0, 6,0, 6,0}} C/=3.0 ------------- {{2,0, 2,0, 2,0} {2,0, 2,0, 2,0} {2,0, 2,0, 2,0}} C^=4 ------------- {{432,0, 432,0, 432,0} {432,0, 432,0, 432,0} {432,0, 432,0, 432,0}} Copier() ------------- {{1,0, 2,0, 3,0} {4,0, 6,0, 5,0} {7,0, 8,0, 9,0}} Coller() ------------- {{1,0, 2,0, 3,0} {4,0, 6,0, 5,0} {7,0, 8,0, 9,0}} InsererColonne() ------------- {{1,0, 0,0, 2,0, 3,0} {4,0, 10,0, 6,0, 5,0} {7,0, 0,0, 8,0, 9,0}} InsererLigne() ------------- {{1,0, 2,0, 3,0} {0,0, 10,0, 0,0} {4,0, 6,0, 5,0} {7,0, 8,0, 9,0}} SupprimerColonne() ------------- {{1,0, 3,0} {4,0, 5,0} {7,0, 9,0}} SupprimerLigne() ------------- {{1,0, 2,0, 3,0} {7,0, 8,0, 9,0}} PermuterColonnes() ------------- {{3,0, 2,0, 1,0} {5,0, 6,0, 4,0} {9,0, 8,0, 7,0}} PermuterLignes() ------------- {{7,0, 8,0, 9,0} {4,0, 6,0, 5,0} {1,0, 2,0, 3,0}} Transposer() ------------- {{1,0, 4,0, 7,0} {2,0, 6,0, 8,0} {3,0, 5,0, 9,0}} AppliquerSymetrieVerticale() ------------- {{3,0, 2,0, 1,0} {5,0, 6,0, 4,0} {9,0, 8,0, 7,0}} AppliquerSymetrieHorizontale() ------------- {{7,0, 8,0, 9,0} {4,0, 6,0, 5,0} {1,0, 2,0, 3,0}} Non EstDiagonale Non EstTriangulaireSuperieure Non EstTriangulaireInferieure Non EstSymetrique Non EstAntiSymetrique 1,0 2,0 3,0 4,0 6,0 5,0 7,0 8,0 9,0 1,0+4,0 2,0+5,0 3,0+6,0 4,0+7,0 6,0+8,0 5,0+9,0 7,0+1,0 8,0+2,0 9,0+3,0 Appliquer3() ------------- {{-1,0, -2,0, -3,0} {-4,0, -6,0, -5,0} {-7,0, -8,0, -9,0}} Appliquer4() ------------- {{-5,0, -7,0, -9,0} {-11,0, -14,0, -14,0} {-8,0, -10,0, -12,0}} Q ------------- {{0,577.350, 0,617.213, 0,534.522} {0,577.350, 0,154.303, -0,801.783} {-0,577.350, 0,771.516, -0,267.261}} R ------------- {{1,732.050, -0,577.350, -1,732.050} {0,0, 2,160.246, 3,240.370} {0,0, 0,0, 1,870.828}} P ------------- {{1,0, 0,0, 0,0} {0,0, 0,0, 1,0} {0,0, 1,0, 0,0}} L ------------- {{1,0, 0,0, 0,0} {1,0, 1,0, 0,0} {-1,0, -0,333.333, 1,0}} U ------------- {{1,0, 1,0, 2,0} {0,0, 3,0, 5,0} {0,0, 0,0, -2,333.333}} P ------------- {{1,0, 0,0, 0,0} {0,0, 0,0, 1,0} {0,0, 1,0, 0,0}} L ------------- {{1,0, 0,0, 0,0} {1,0, 1,0, 0,0} {-1,0, -0,333.333, 1,0}} D ------------- {{1,0, 0,0, 0,0} {0,0, 3,0, 0,0} {0,0, 0,0, -2,333.333}} U ------------- {{1,0, 1,0, 2,0} {0,0, 1,0, 1,666.666} {0,0, 0,0, 1,0}} V ------------- {{0,234.630, -0,273.177, -0,932.910} {0,510.958, 0,851.087, -0,120.709} {0,826.964, -0,448.356, 0,339.273}} L ------------- {{15,929.059, 0,0, 0,0} {0,0, 1,099.081, 0,0} {0,0, 0,0, -1,028.140}} Inv(A) ------------- {{-0,777.777, -0,333.333, 0,444.444} {0,055.555, 0,666.666, -0,388.888} {0,555.555, -0,333.333, 0,111.111}} Det(A)=18,0 Trc(A)=16,0 Rng(A)=3 Exp(A) ------------- {{24.325,785.242, -33.582,498.530, 20.961,399.981} {-33.582,498.530, 46.429,903.812, -29.038,913.631} {20.961,399.981, -29.038,913.631, 18.217,354.921}} Log(A) ------------- {{0,588.886, -1,776.696, -0,707.839} {-1,776.696, 0,249.103, -1,437.316} {-0,707.839, -1,437.316, 0,953.769}} Racine(A) ------------- {{1,893.215, -1,169.278, -0,155.153} {-1,169.278, 1,927.686, -0,962.855} {-0,155.153, -0,962.855, 2,015.690}}