Ada 中的 HowTo 和多维矩阵
HowTo & multidimensional matrix in Ada
我想知道如何使用 Ada 语言创建多维矩阵,我的意思是,堆叠 i 个 j x k 维矩阵:
我发现的一种方法是使用多维数组,非常简单。
还有其他方法可以实现吗?也许使用 Ada.Containers.Vectors 或在数组声明中混合使用它?有正规的图书馆吗?
谢谢
您也可以尝试标准的 Ada 包 Real Vectors and Matrices or Complex Vectors and Matrices,它也提供一些矩阵运算。这可能是完成它的最简单方法。
这里有两个可能有用的例子。
第一个例子展示了多维数组数组的使用。使用此方法时,有时需要固定矩阵堆栈的大小(此处:在 [=13= 的声明期间,大小固定为 4)。内存已预先分配以容纳所有四个矩阵。
第二个示例显示了可以添加矩阵的 Ada 向量容器的使用。向量是“动态数组”。添加矩阵时分配内存。
GNAT 编译器(最新版本)附带的 Ada 标准库包含正式的容器包(另请参阅 here)。
example_1.adb
with Ada.Text_IO; use Ada.Text_IO;
procedure Example_1 is
type Rows is new Natural range 0 .. 2;
type Cols is new Natural range 0 .. 2;
type Matrix is array (Rows, Cols) of Integer;
-- A 2D matrix.
type Stack is array (Natural range <>) of Matrix;
-- Stack of 2D matrices.
S : constant Stack (1 .. 4) :=
(1 => (others => (others => 1)),
2 => (others => (others => 2)),
3 => (others => (others => 3)),
4 => (others => (others => 4)));
begin
for Mtx of S loop -- using "of", not "in", such to iterate over the elements.
for R in Rows loop
Put ("[");
for C in Cols loop
Put (Mtx (R, C)'Image);
end loop;
Put_Line (" ]");
end loop;
New_Line;
end loop;
end Example_1;
example_2.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
procedure Example_2 is
type Rows is new Natural range 0 .. 2;
type Cols is new Natural range 0 .. 2;
type Matrix is array (Rows, Cols) of Integer;
-- A 2D matrix.
package Stacks is new Ada.Containers.Vectors (Natural, Matrix);
use Stacks;
subtype Stack is Stacks.Vector;
-- Stack of 2D matrices.
Empty_Stack : constant Stack := Stacks.Empty_Vector;
-- An empty stack.
S : Stack;
-- Instead of using Append (..) shown below, you can also
-- initialize the stack using:
--
-- S : Stack := Empty_Stack
-- & (others => (others => 1))
-- & (others => (others => 2))
-- & (others => (others => 3))
-- & (others => (others => 4));
begin
S.Append ((others => (others => 1)));
S.Append ((others => (others => 2)));
S.Append ((others => (others => 3)));
S.Append ((others => (others => 4)));
-- ...
for Mtx of S loop -- using "of", not "in", such to iterate over the elements.
for R in Rows loop
Put ("[");
for C in Cols loop
Put (Mtx (R, C)'Image);
end loop;
Put_Line (" ]");
end loop;
New_Line;
end loop;
end Example_2;
输出(两个例子相同)
[ 1 1 1 ]
[ 1 1 1 ]
[ 1 1 1 ]
[ 2 2 2 ]
[ 2 2 2 ]
[ 2 2 2 ]
[ 3 3 3 ]
[ 3 3 3 ]
[ 3 3 3 ]
[ 4 4 4 ]
[ 4 4 4 ]
[ 4 4 4 ]
我想知道如何使用 Ada 语言创建多维矩阵,我的意思是,堆叠 i 个 j x k 维矩阵:
我发现的一种方法是使用多维数组,非常简单。
还有其他方法可以实现吗?也许使用 Ada.Containers.Vectors 或在数组声明中混合使用它?有正规的图书馆吗?
谢谢
您也可以尝试标准的 Ada 包 Real Vectors and Matrices or Complex Vectors and Matrices,它也提供一些矩阵运算。这可能是完成它的最简单方法。
这里有两个可能有用的例子。
第一个例子展示了多维数组数组的使用。使用此方法时,有时需要固定矩阵堆栈的大小(此处:在 [=13= 的声明期间,大小固定为 4)。内存已预先分配以容纳所有四个矩阵。
第二个示例显示了可以添加矩阵的 Ada 向量容器的使用。向量是“动态数组”。添加矩阵时分配内存。
GNAT 编译器(最新版本)附带的 Ada 标准库包含正式的容器包(另请参阅 here)。
example_1.adb
with Ada.Text_IO; use Ada.Text_IO;
procedure Example_1 is
type Rows is new Natural range 0 .. 2;
type Cols is new Natural range 0 .. 2;
type Matrix is array (Rows, Cols) of Integer;
-- A 2D matrix.
type Stack is array (Natural range <>) of Matrix;
-- Stack of 2D matrices.
S : constant Stack (1 .. 4) :=
(1 => (others => (others => 1)),
2 => (others => (others => 2)),
3 => (others => (others => 3)),
4 => (others => (others => 4)));
begin
for Mtx of S loop -- using "of", not "in", such to iterate over the elements.
for R in Rows loop
Put ("[");
for C in Cols loop
Put (Mtx (R, C)'Image);
end loop;
Put_Line (" ]");
end loop;
New_Line;
end loop;
end Example_1;
example_2.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
procedure Example_2 is
type Rows is new Natural range 0 .. 2;
type Cols is new Natural range 0 .. 2;
type Matrix is array (Rows, Cols) of Integer;
-- A 2D matrix.
package Stacks is new Ada.Containers.Vectors (Natural, Matrix);
use Stacks;
subtype Stack is Stacks.Vector;
-- Stack of 2D matrices.
Empty_Stack : constant Stack := Stacks.Empty_Vector;
-- An empty stack.
S : Stack;
-- Instead of using Append (..) shown below, you can also
-- initialize the stack using:
--
-- S : Stack := Empty_Stack
-- & (others => (others => 1))
-- & (others => (others => 2))
-- & (others => (others => 3))
-- & (others => (others => 4));
begin
S.Append ((others => (others => 1)));
S.Append ((others => (others => 2)));
S.Append ((others => (others => 3)));
S.Append ((others => (others => 4)));
-- ...
for Mtx of S loop -- using "of", not "in", such to iterate over the elements.
for R in Rows loop
Put ("[");
for C in Cols loop
Put (Mtx (R, C)'Image);
end loop;
Put_Line (" ]");
end loop;
New_Line;
end loop;
end Example_2;
输出(两个例子相同)
[ 1 1 1 ]
[ 1 1 1 ]
[ 1 1 1 ]
[ 2 2 2 ]
[ 2 2 2 ]
[ 2 2 2 ]
[ 3 3 3 ]
[ 3 3 3 ]
[ 3 3 3 ]
[ 4 4 4 ]
[ 4 4 4 ]
[ 4 4 4 ]