1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| PROGRAM determ(input, output);
type
tmat = array [1..10, 1..10] of real;
var
N: integer; { dimension de la matrice }
det: real; { déterminant }
mat: tmat; { matrice à calculer }
I, J: integer;
procedure sous_mat(A: tmat; var B: tmat; ind, N: integer);
{ on supprime la colonne 1 et la ligne ind pour avoir la s/mat de N-1 }
//A matrice de debut B matrice de fin
var
J, I, L: integer;
begin
L := 0;
for I := 1 to N do
begin
if I <> ind then
begin
L := L + 1;
for J := 2 to N do
B[L, J - 1] := A[I, J];
end;
end;
end;
function detn(M: tmat; Nm: integer): real; // M : matrice Nm:ordre de la matrice
{ On cherche l'ordre Nm de la sous matrice en fonction de l'ordre Nm-1 & determinant detn}
var
Res: real; // determinant
mprim: tmat; // matrice intermédiaire
I, S: integer; // I lignes J colonnes S signe des lignes
begin
if Nm = 1 then
detn := M[1, 1];
if Nm = 2 then
detn := M[1, 1] * M[2, 2] - M[2, 1] * M[1, 2];
if Nm = 3 then
detn := M[1, 1] * M[2, 2] * M[3, 3] + M[2, 1] * M[3, 2] * M[1, 3] +
M[1, 2] * M[2, 3] * M[3, 1] - M[3, 1] * M[2, 2] * M[1, 3] -
M[3, 2] * M[2, 3] * M[1, 1] - M[2, 1] * M[1, 2] * M[3, 3]
else
begin
Res := 0;
S := -1;
for I := 1 to Nm do
begin
sous_mat(M, mprim, I, Nm);
S := -S; //change sign every lines
Res := Res + (S * M[I, 1] * detn(mprim, Nm - 1));
end;
detn := Res;
end;
end;
begin
mat[1, 1] := 4; mat[1, 2] := 3; mat[1, 3] := 1; // 4 3 1
mat[2, 1] := 5; mat[2, 2] := 2; mat[2, 3] := 7; // 5 2 7
mat[3, 1] := 8; mat[3, 2] := 0; mat[3, 3] := 2; // 8 0 2
det := detn(mat, 3);//find the determinant
Writeln('Determinant: ', det: 2: 1); // 138
readln;
mat[1, 1] := -1; mat[1, 2] := 0; mat[1, 3] := 1; mat[1, 4] := 1; // -1 0 1 1
mat[2, 1] := 1; mat[2, 2] := -2; mat[2, 3] := 1; mat[2, 4] := -1; // 1 -2 1 -1
mat[3, 1] := 1; mat[3, 2] := 0; mat[3, 3] := -1; mat[3, 4] := 1; // 1 0 -1 1
mat[4, 1] := 1; mat[4, 2] := 0; mat[4, 3] := 1; mat[4, 4] := -1; // 1 0 1 -1
det := detn(mat, 4);//find the determinant
Writeln('Determinant: ', det: 2: 1); //-8
readln;
mat[1, 1] := 2; mat[1, 2] := 3; mat[1, 3] := -1; mat[1, 4] := 3; mat[1, 5] := 1; // 2 3 -1 3 1
mat[2, 1] := 3; mat[2, 2] := 1; mat[2, 3] := -4; mat[2, 4] := 3; mat[2, 5] := -1; // 3 1 -4 3 -1
mat[3, 1] := 1; mat[3, 2] := -2; mat[3, 3] := 3; mat[3, 4] := -4; mat[3, 5] := 2; // 1 -2 3 -4 2
mat[4, 1] := 1; mat[4, 2] := 2; mat[4, 3] := -3; mat[4, 4] := 2; mat[4, 5] := -2; // 1 2 -3 2 -2
mat[5, 1] := 2; mat[5, 2] := 0; mat[5, 3] := 0; mat[5, 4] := 4; mat[5, 5] := -5; // 2 0 0 4 -5
det := detn(mat, 5);//find the determinant
Writeln('Determinant: ', det: 2: 1); //-580
readln;
mat[1, 1] := 7; mat[1, 2] := 2; mat[1, 3] := -8; mat[1, 4] := 4; mat[1, 5] := 6; // 7 2 -8 4 6
mat[2, 1] := -3; mat[2, 2] := -1; mat[2, 3] := 4; mat[2, 4] := -2; mat[2, 5] := -3; // -3 -1 4 -2 -3
mat[3, 1] := 6; mat[3, 2] := 2; mat[3, 3] := 2; mat[3, 4] := 4; mat[3, 5] := 7; // 6 2 2 4 7
mat[4, 1] := 1; mat[4, 2] := 3; mat[4, 3] := 7; mat[4, 4] := 5; mat[4, 5] := 1; // 1 3 7 5 1
mat[5, 1] := -2; mat[5, 2] := 2; mat[5, 3] := 3; mat[5, 4] := 4; mat[5, 5] := 7; // -2 2 3 4 7
det := detn(mat, 5);//find the determinant
Writeln('Determinant: ', det: 2: 1); //-1
readln;
mat[1, 1] := 1; mat[1, 2] := 3; mat[1, 3] := 1; mat[1, 4] := -1; mat[1, 5] := 7; // 1 3 1 -1 7
mat[2, 1] := 2; mat[2, 2] := 2; mat[2, 3] := 2; mat[2, 4] := 0; mat[2, 5] := 2; // 2 2 2 0 2
mat[3, 1] := 3; mat[3, 2] := 1; mat[3, 3] := 3; mat[3, 4] := -8; mat[3, 5] := 1; // 3 1 3 -8 1
mat[4, 1] := 3; mat[4, 2] := 2; mat[4, 3] := 4; mat[4, 4] := 1; mat[4, 5] := 3; // 3 2 4 1 3
mat[5, 1] := 5; mat[5, 2] := 2; mat[5, 3] := 5; mat[5, 4] := 2; mat[5, 5] := 2; // 5 2 5 2 2
det := detn(mat, 5);//find the determinant
Writeln('Determinant: ', det: 2: 1); //-224
readln;
Exit;
Write('Matrix order ');
readln(N);
for I := 1 to N do
begin
writeln('line ', I: 2);
for J := 1 to N do
readln(mat[I, J]);
end;
det := detn(mat, N);//find the determinant
Writeln('Determinant: ', det: 2: 1);
readln;
end. |
Partager