【问题标题】:HowTo & multidimensional matrix in AdaAda 中的 HowTo 和多维矩阵
【发布时间】:2021-02-19 08:02:05
【问题描述】:

我想知道如何使用 Ada 语言创建多维矩阵,我的意思是,堆叠 i 个 j x k 维的矩阵:

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

还有其他方法可以实现吗?也许使用 Ada.Containers.Vectors 或在数组声明中混合它?有正规的图书馆吗?

谢谢

【问题讨论】:

  • 您能否详细说明您对替代方法感兴趣的原因?多维数组是否不够,或者它们难以用于您面临的特定用例?
  • 您所指的维度是几何维度(如 3x3 矩阵中的 3)还是数组中的索引数?
  • @DeeDee 只是好奇,我用其他语言将矩阵堆叠成 (m x n) x z 维的多维矩阵,我想到了如何使用这种强类型语言来完成。
  • @Zerte 编辑澄清我的意思是堆叠矩阵,例如(m x n) x z维度,即m x n矩阵和z堆叠矩阵在同一个对象中。

标签: matrix multidimensional-array ada


【解决方案1】:

这里有两个可能有用的例子。

  • 第一个例子展示了多维数组的使用。使用此方法时,您有时需要固定矩阵堆栈的大小(此处:在声明S 期间,大小固定为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 ]

【讨论】:

  • 很抱歉给你添麻烦了。您对我昨天提出的问题 stackoverflow.com/questions/66291973/…(建议 Reference_Preserving_Key)给出了很好的回答,我想接受它,但它已经消失了。你有机会把它放回去吗?真的很有用!
  • 太棒了,我一直在寻找。非常感谢
【解决方案2】:

您还可以尝试标准的 Ada 包 Real Vectors and MatricesComplex Vectors and Matrices,它们也提供了一些矩阵运算。这可能是实现它的最简单方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 2013-01-13
    • 2013-05-17
    • 2016-06-09
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多