Ada input format issue
22 March 2023 at 00:35
I'm new to Ada and not sure how to fix this code. It appears it is about format issue,ย but don't know what is causing this. I am trying to read two files called a.txt
and b.txt
then compute the inverse of it. Sorry again am just stuck.
Here is the input files
A.txt
2 -2 4 -2 2 0
1 0 4 -3 0 0
1 1 1 1 1 1
1 0 0 -1 0 0
3 0 0 -3 0 2
1 0 0 0 -2 4
B.txt
13
18
51
82
32
12
here is my code
with Ada.Text_IO, Ada.Float_Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Float_Text_IO, Ada.Integer_Text_IO;
procedure Matrix_Inverse is
type Matrix is array (1 .. 6, 1 .. 6) of Float;
type Vector is array (1 .. 6) of Float;
function Get_Matrix_From_File(Filename : String) return Matrix is
A : Matrix := (others => (others => 0.0));
File : File_Type;
Line : String (1 .. 20);
Last : Natural;
begin
Open (File, In_File, Filename);
for I in A'Range(1) loop
Get_Line (File, Line, Last);
for J in A'Range(2) loop
A (I, J) := Float'Value (Line (1 .. Last));
Get (File, Line);
Get_Line (File, Line, Last);
end loop;
A (I, 6) := Float'Value (Line (1 .. Last));
end loop;
Close (File);
return A;
end Get_Matrix_From_File;
function Get_Vector_From_File(Filename : String) return Vector is
B : Vector := (others => 0.0);
File : File_Type;
begin
Open (File, In_File, Filename);
for I in B'Range loop
Get (File, B (I));
end loop;
Close (File);
return B;
end Get_Vector_From_File;
procedure Print_Matrix(A : Matrix) is
begin
for I in A'Range(1) loop
for J in A'Range(2) loop
Put (A (I, J), 6, 2);
Put (" ");
end loop;
New_Line;
end loop;
end Print_Matrix;
function Inverse(A : Matrix) return Matrix is
Inv : Matrix := (others => (others => 0.0));
Identity : Matrix := (others => (others => 0.0));
begin
for I in Identity'Range(1) loop
Identity (I, I) := 1.0;
end loop;
for I in A'Range(1) loop
if A (I, I) = 0.0 then
for J in I + 1 .. A'Range(1)'Last loop
if A (J, I) /= 0.0 then
for K in A'Range(2) loop
A (I, K) := A (I, K) + A (J, K);
Identity (I, K) := Identity (I, K) + Identity (J, K);
end loop;
exit;
end if;
end loop;
end if;
for J in A'Range(1) loop
if J /= I then
declare
Factor : constant Float := A (J, I) / A (I, I);
begin
for K in A'Range(2) loop
A (J, K) := A (J, K) - Factor * A (I, K);
Identity (J, K) := Identity (J, K) - Factor * Identity (I, K);
end loop;
end;
end if;
end loop;
for I in A'Range(1) loop
declare
Diagonal : constant Float := A (I, I);
begin
for J in A'Range(2) loop
Identity (I, J) := Identity (I, J) / Diagonal;
end loop;
end;
end loop;
return Identity;
end Inverse;
function Solve(A : Matrix; B : Vector) return Vector is
Inv_A : Matrix := Inverse(A);
Result : Vector := (others => 0.0);
begin
for I in Result'Range loop
for J in Result'Range loop
Result (I) := Result (I) + Inv_A (I, J) * B (J);
end loop;
end loop;
return Result;
end Solve;
A : Matrix := Get_Matrix_From_File("a.txt");
B : Vector := Get_Vector_From_File("b.txt");
Solution : Vector := Solve(A, B);
begin
Put_Line("Solution:");
for I in Solution'Range loop
Put(Solution(I), 6, 2);
Put(" ");
end loop;
New_Line;
end Matrix_Inverse;