PROGRAM 6
-- Filename: Matrix.ads, Matrix.adb, Program6.cpp Author: Anthony F. Ortiz
-- This program computes the determinant and inverse of a 2 by 2 matrix,
-- adds, subtracts, and multiplies two 2 by 2 matrix, and prints the
-- matrix with it's inverse.
with Text_IO; use Text_IO;
with Matrix; use Matrix;
procedure Main is
package Flt_IO is new Float_IO (Float); use Flt_IO;
A : Flt_Array;
B : Flt_Array;
C : Flt_Array;
D : Flt_Array;
E : Flt_Array;
Inverse : Flt_Array;
Det_A : Integer;
-- Print a 2 by 2 matrix;
procedure Print_Matrix (A : in out Flt_Array) is
begin
for I in 1..2 loop
for J in 1..2 loop
Put (A (I, J), 10, 2, 0);
end loop;
New_Line;
end loop;
end Print_Matrix;
-- Print the determinant.
procedure Print_Determinant (Det_A : in Integer) is
begin
Put ("The determinant of the matrix is ");
Put (Det_A, 10, 2, 0);
end Print_Determinant;
begin
A := Create_Matrix (1.0, 2.0, 3.0, 4.0);
B := Create_Matrix (2.0, 3.0, 4.0, 5.0);
Det_A := Compute_Determinant (A);
Inverse := Compute_Inverse (A, Det_A);
C := Add_Matrix (A, B);
D := Subtract_Matrix (A, B);
E := Multiply_Matrix (A, B);
Put ("Matrix A: ");
New_Line;
Print_Matrix (A);
Put ("Matrix B: ");
New_Line;
Print_Matrix (B);
Put ("Matrix C: ");
New_Line;
Print_Matrix (C);
Put ("Matrix D: ");
New_Line;
Print_Matrix (D);
Put ("Matrix E: ");
New_Line;
Print_Matrix (E);
Put ("Inverse C: ");
New_Line;
Print_Matrix (Inverse);
Print_Determinant (Det_A);
end Main;
-- Filename: Matrix.ads, Matrix.adb, Program6.cpp Author: Anthony F. Ortiz
-- Declaration of a package called matrix. This package contains functions
-- for computing the determinant and inverse of a 2 by 2 matrix, and perform
-- addition, subtraction, and mutliplication for two 2 by 2 matrix.
package Matrix is
-- Holds the type the matrix cell holds.
type Flt_Array is array (1..2, 1..2) of Float;
-- Creates a 2 by 2 matrix.
function Create_Matrix (C1 : in Float; C2 : in Float; C3 : in Float; C4 : in Float)
return Flt_Array;
-- Computes the determinant of a 2 by 2 matrix.
function Compute_Determinant (A : in Flt_Array) return Float;
-- Compute the inverse of a 2 by 2 matrix.
function Compute_Inverse (A : in Flt_Array; Det_A : in Float) return Flt_Array;
-- Add two 2 by 2 matrix.
function Add_Matrix (A : in Flt_Array; B : in Flt_Array) return Flt_Array;
-- Subtract two 2 by 2 matrix.
function Subtract_Matrix (A : in Flt_Array; B : in Flt_Array) return Flt_Array;
-- Mutlipty two 2 by 2 matrix.
function Multiply_matrix (A : in Flt_Array; B : in Flt_Array) return Flt_Array;
end Matrix;
-- Filename: Matrix.ads, Matrix.adb, Program6.cpp Author: Anthony F. Ortiz
-- Implementation of a package called matrix. This package contains functions
-- for computing the determinant and inverse of a 2 by 2 matrix, and perform
-- addition, subtraction, and mutliplication for two 2 by 2 matrix.
package body Matrix is
-- Create a 2 by 2 matrix.
function Create_Matrix(C1 : in Float; C2 : in Float; C3 : in Float;
C4 : in Float) return Flt_Array is
C : Flt_Array;
begin
C (1, 1) := C1;
C (1, 2) := C2;
C (2, 1) := C3;
C (2, 2) := C4;
return C;
end Create_Matrix;
-- Compute the determinant of a 2 by 2 matrix.
function Compute_Determinant(A : in Flt_Array) return Float is
Determinant : Float;
begin
Determinant := A (1, 1) * A (2, 2) - A (1, 2) * A (2, 1);
return Determinant;
end Compute_Determinant;
-- Compute the inverse of a 2 by 2 matrix.
function Compute_Inverse(A : in Flt_Array;
Det_A : in Float) return Flt_Array is
Inverse : Flt_Array;
Count : Integer;
begin
Count := 2;
for I in 1..2 loop
for J in 1..2 loop
if (I = J) then
Inverse (I, J) := 1.0 / Det_A * A (Count, Count);
Count := Count - 1;
else
Inverse (I, J) := -1.0 / Det_A * A (I, J);
end if;
end loop;
end loop;
return Inverse;
end Compute_Inverse;
-- Add two 2 by 2 matrix.
function Add_Matrix(A : in Flt_Array;
B : in Flt_Array) return Flt_Array is
C : Flt_Array;
begin
for I in 1..2 loop
for J in 1..2 loop
C (I, J) := A (I, J) + B (I, J);
end loop;
end loop;
return C;
end Add_Matrix;
-- Subtract two 2 by 2 matrix.
function Subtract_Matrix(A : in Flt_Array ;
B : in Flt_Array) return Flt_Array is
C : Flt_Array;
begin
for I in 1..2 loop
for J in 1..2 loop
C (I, J) := A (I, J) - B (I, J);
end loop;
end loop;
return C;
end Subtract_Matrix;
-- Multiply two 2 by 2 matrix.
function Multiply_Matrix(A : in Flt_Array ;
B : in Flt_Array) return Flt_Array is
C : Flt_Array;
begin
for I in 1..2 loop
for J in 1..2 loop
C (I, J) := A (I, 1) * B(1, J) + A (I, 2) * B (2, J);
end loop;
end loop;
end Multiply_Matrix;
end Matrix
-- outfile: prog6.out
Matrix A
1.00 2.00
3.00 4.00
Matrix B
2.00 3.00
4.00 5.00
Matrix C
3.00 5.00
7.00 9.00
Matrix D
-1.00 -1.00
-1.00 -1.00
Matrix E
10.00 13.00
22.00 29.00
Inverse C
-1.13 0.63
0.88 -0.38
The determinant of the matrix is -0.13
BACK TO CS3120 PAGE.