Jump to content

Matrix transposition: Difference between revisions

→‎{{header|Ada}}: Replaced by an implementation based on the standard library
(added GAP)
(→‎{{header|Ada}}: Replaced by an implementation based on the standard library)
Line 2:
 
=={{header|Ada}}==
Transpose is a function of the standard Ada library provided for matrices built upon any floating-point or complex type. The relevant packages are Ada.Numerics.Generic_Real_Arrays and Ada.Numerics.Generic_Complex_Arrays, correspondingly.
This example creates a matrix type of arbitrary length using a generic package. The array element type is any floating point type. The array index type is any discrete value. For this example an enumerated type is used for the index type.
generic
type Element_Type is digits <>;
type Row_Index is (<>);
package Rect_Real_Matrix is
type Rect_Matrix is array (Row_Index range <>, Row_Index range <>) of Element_Type;
function Transpose(Item : Rect_Matrix) return Rect_Matrix;
procedure Print(Item : Rect_Matrix);
end Rect_Real_Matrix;
 
This example illustrates use of Transpose for the matrices built upon the standard type Float:
with Ada.Text_Io;
<ada>
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
with Ada.Text_IO; use Ada.Text_IO;
procedure Matrix_Transpose is
package body Rect_Real_Matrix is
procedure Put (X : Real_Matrix) is
type Fixed is delta 0.01 range -500.0..500.0;
begin
for I in X'Range (1) loop
for J in X'Range (2) loop
Put (Fixed'Image (Fixed (X (I, J))));
end loop;
New_Line;
end loop;
end Put;
Matrix : constant Real_Matrix :=
-----------
( (0.0, 0.1, 0.2, 0.3),
-- Print --
(0.4, 0.5, 0.6, 0.7),
-----------
(0.8, 0.9, 1.0, 1.1)
);
procedure Print(Item : Rect_Matrix) is
begin
package Real_Io is new Ada.Text_Io.Float_Io(Element_Type);
Put_Line ("Before Transposition:");
use Real_Io;
Put (Matrix);
use Ada.Text_Io;
beginNew_Line;
Put_Line ("After Transposition:");
for I in Item'range(1) loop
Put (Transpose (Matrix));
for J in Item'range(2) loop
end Matrix_Transpose;
Put(Item => Item(I,J), Exp => 0, Fore => 5);
</ada>
Put(" ");
Sample output:
end loop;
<pre>
New_Line;
Before Transposition:
end loop;
0.00 0.10 0.20 0.30
end Print;
0.40 0.50 0.60 0.70
0.80 0.90 1.00 1.10
---------------
-- Transpose --
---------------
function Transpose (Item : Rect_Matrix) return Rect_Matrix is
Temp : Rect_Matrix(Item'Range(2), Item'Range(1));
begin
for Col in Item'range(1) loop
for Row in Item'range(2) loop
Temp(Row,Col) := Item(Col,Row);
end loop;
end loop;
return Temp;
end Transpose;
end Rect_Real_Matrix;
 
After Transposition:
with Rect_Real_Matrix;
0.00 0.40 0.80
with Ada.Text_IO; use Ada.Text_IO;
0.10 0.50 0.90
0.20 0.60 1.00
procedure Rect_Mat_Transpose is
0.30 0.70 1.10
type Col_Index is (Severe, High, Moderate, Low, Insignificant);
</pre>
subtype Row_Index is Col_Index range Severe..Low;
package Flt_Mat is new Rect_Real_Matrix(Float, Col_Index);
use Flt_Mat;
M : Rect_Matrix(Col_Index, Row_Index) := ((1.0, 1.0, 1.0, 1.0),
(2.0, 4.0, 8.0, 16.0),
(3.0, 9.0, 27.0, 81.0),
(4.0, 16.0, 64.0, 256.0),
(5.0, 25.0,125.0, 625.0));
begin
Put_line("Before Transposition:");
Print(M);
New_Line;
Put_Line("After Transposition:");
Print(Transpose(M));
end Rect_Mat_Transpose;
 
Output:
 
Before Transposition:
1.00000 1.00000 1.00000 1.00000
2.00000 4.00000 8.00000 16.00000
3.00000 9.00000 27.00000 81.00000
4.00000 16.00000 64.00000 256.00000
5.00000 25.00000 125.00000 625.00000
After Transposition:
1.00000 2.00000 3.00000 4.00000 5.00000
1.00000 4.00000 9.00000 16.00000 25.00000
1.00000 8.00000 27.00000 64.00000 125.00000
1.00000 16.00000 81.00000 256.00000 625.00000
 
=={{header|ALGOL 68}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.