Docsity

Ada Program to Invert a 3x3 Matrix: Algorithm, Procedures and Functions, Exercises of Aeronautical Engineering

Toggle sidebar
bg1
C –15 Solutions
1. Invert a 3x3 Matrix
Data Structure
A new type my_3x3_matrix as a real array (1..3, 1..3)
Subprograms
1. Function to create the matrix
2. Procedure to display the matrix
3. Function to compute the determinant
4. Function to compute the inverse
5. Main program to invert the matrix
Algorithm
Create_Matrix:
For I in 1 .. 3
For J in 1 .. 3
Accept Matrix(I,J)
Return Matrix to the user
Display_Matrix:
For I in 1.. 3
For J in 1.. 3
Display Matrix (I,J);
New_Line
Compute_Determinant:
1. Convert the matrix into a real_matrix (as defined in
Generic_Real_arrays)
2. Compute the determinant using the det function defined in
Generic_Real_Arrays.Operations.
3. Return the computed Value
Compute_Inverse:
1. Convert the matrix into a real_matrix (as defined in
Generic_Real_arrays)
2. Compute the inverse using the Inverse function defined in
Generic_Real_Arrays.Operations.
docsity.com
pf3
pf4

Partial preview of the text

Download Ada Program to Invert a 3x3 Matrix: Algorithm, Procedures and Functions and more Exercises Aeronautical Engineering in PDF only on Docsity!

C –15 Solutions

1. Invert a 3x3 Matrix

Data Structure

A new type my_3x3_matrix as a real array (1..3, 1..3)

Subprograms

1. Function to create the matrix

2. Procedure to display the matrix

3. Function to compute the determinant

4. Function to compute the inverse

5. Main program to invert the matrix

Algorithm

Create_Matrix:

For I in 1 .. 3

For J in 1 .. 3

Accept Matrix(I,J)

Return Matrix to the user

Display_Matrix:

For I in 1.. 3

For J in 1.. 3

Display Matrix (I,J);

New_Line

Compute_Determinant:

1. Convert the matrix into a real_matrix (as defined in

Generic_Real_arrays)

2. Compute the determinant using the det function defined in

Generic_Real_Arrays.Operations.

3. Return the computed Value

Compute_Inverse:

1. Convert the matrix into a real_matrix (as defined in

Generic_Real_arrays)

2. Compute the inverse using the Inverse function defined in

Generic_Real_Arrays.Operations.

3. Convert the real_matrix back into the user defined type

4. Return the inverted matrix

Main Program:

1. Prompt the user to enter a matrix

2. Display accepted matrix to the user

3. Check if matrix is singular by computing the determinant.

4. If matrix is not singular (determinant /= 0)

a. Compute the inverse

b. Display inverse to the user

5. Else, Display “Cannot Invert”

2. Code Listing

Compiling: c:/docume~2/joeb/desktop/16070c~1/matrix/pset_4_inversion.adb (source file time stamp: 2003-10-08 14:37:14)


  1. -- Program to invert a 3x3 matrix
  2. -- Programmer : Joe B
  3. -- Date Last Modified : 10/07/

  4. with Ada.Text_Io;
  5. with Ada.Float_Text_Io;
  6. with Generic_Real_Arrays;
  7. with Generic_Real_Arrays.Operations;
  8. with Generic_Real_Arrays.Array_Io;
  9. procedure Pset_4_Inversion is
  10. -- create instances of the generic matrix packages.
  11. package My_Real_Array is new Generic_Real_Arrays(Float);
  12. package My_Real_Array_Operations is new My_Real_Array.Operations;
  13. -- create a user defined 3x3 matrix
  14. type My_3x3_Matrix is new My_Real_Array.Real_Matrix
  15. (1 .. 3, 1 .. 3);
  16. -- declare local variables for matrices and determinant
  17. A,
  18. B : My_3x3_Matrix;
  19. Det : Float;
  20. -- user function to create the matrix
  21. function Create_My_Matrix return My_3x3_Matrix is
  22. Input_Matrix : My_3x3_Matrix;
  23. begin
  24. for I in 1 .. 3 loop
  1. Input_Matrix : My_3x3_Matrix )
  2. return Float is
  3. My_Real_Matrix : My_Real_Array.Real_Matrix (1 .. 3, 1 .. 3);
  4. begin
  5. -- do type conversion from user defined type to package type
  6. for I in 1.. 3 loop
  7. for J in 1 .. 3 loop
  8. My_Real_Matrix(I,J) := Input_Matrix(I,J);
  9. end loop;
  10. end loop;
  11. -- compute the determinant and return to user
  12. return (My_Real_Array_Operations.Det(My_Real_Matrix));
  13. end My_Determinant;
  14. begin
  15. -- create and display the matrix
  16. Ada.Text_Io.Put_Line("Please Enter the Matrix : ");
  17. A := Create_My_Matrix;
  18. Ada.Text_Io.Put_Line("Created Matrix : ");
  19. Display_My_Matrix(A);
  20. Ada.Text_Io.New_Line;
  21. -- compute determinant
  22. Det := My_Determinant(A);
  23. -- check for singularity
  24. if Det = 0.0 then
  25. Ada.Text_Io.Put_Line("Cannot invert the matrix");
  26. else
  27. -- compute inverse and display
  28. B := My_Inverse(A);
  29. Ada.Text_Io.New_Line;
  30. Ada.Text_Io.Put_Line("Inverted Matrix : ");
  31. Display_My_Matrix(B);
  32. end if;
  33. end Pset_4_Inversion;

131 lines: No errors