如何在Ada中使用多维数组

3

我在想如何使用 Ada 语言创建一个多维矩阵,也就是将 i 个 j x k 维的矩阵堆叠起来:

enter image description here

我找到的一种方法是使用多维数组,非常简单。

还有其他实现方式吗?也许可以使用 Ada.Containers.Vectors 或在数组声明中混合使用?是否有官方库可以使用?

谢谢。


你能详细说明一下为什么你对另一种方法感兴趣吗?多维数组不够用还是在你面临的特定用例中难以使用? - DeeDee
你所指的维度是指几何维度(例如3x3矩阵中的3)还是数组中的索引数量? - Zerte
@DeeDee 只是好奇,我曾经在其他语言中将矩阵堆叠成一个(m x n) x z维的多维矩阵,现在想知道在这样一种强类型语言中该如何实现。 - Albatros23
1
@Zerte编辑以澄清我的意思是堆叠矩阵,例如(m x n) x z维度,即在同一对象中的m x n矩阵和z堆叠矩阵。 - Albatros23
2个回答

4
这里有两个可能有用的例子。
第一个例子展示了使用多维数组的数组。当使用此方法时,您需要在某个时候固定矩阵堆栈的大小(在此处:在声明S期间,大小被固定为4)。预先分配内存以容纳所有四个矩阵。
第二个例子展示了使用Ada向量容器添加矩阵。向量是“动态数组”。添加矩阵时分配内存。
随着GNAT编译器(最新版本)一起提供的Ada标准库包含正式的容器包(请参见此处)。

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 ]

抱歉打扰你,DeeDee。昨天我问了一个问题,你给出了一个很好的答案 (建议使用Reference_Preserving_Key),我想采纳它,但是它已经消失了。你有可能把它找回来吗?它真的很有用! - smirkingman
1
那太好了,正是我在寻找的。非常感谢你。 - Albatros23

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接