Ada:使用一行和一列访问 Real_Matrix 中的第一个元素

Ada: Access first element in Real_Matrix with one row and one column

问题陈述

我有一个 Real_Matrix 一行一列。我想评估第一行第一列的单个元素的值。当我尝试使用 Matrix(I, J) 语法访问 Matrix 时出现错误。见下文:

代码

with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
with Ada.Text_IO;              use Ada.Text_IO;

procedure Matrix is

   ------------------------------------
   -- Real_Matrix Division Operation --
   ------------------------------------

   function "/" (Left  : Real_Matrix;
                 Right : Real_Matrix) return Real_Matrix
   is
   begin
      return Left * Inverse(Right);
   end "/";

   α : Real_Matrix := ( ( Integer'First => 1.0 ),
                        ( Integer'First => 2.0 ) );
   β : Real_Matrix := ( ( Integer'First => 3.0 ),
                        ( Integer'First => 4.0 ) );

begin

   -- This operation returns an matrix with one row and one column --

   Put_Line(Float'Image(((Transpose(α) * α) / (Transpose(β) * β))(Integer'First, Integer'First))); -- Error: Missing "," --

end Matrix;

我认为您需要一位真正的语言律师来告诉您这是编译器故障还是正确行为,但如果您强制编译器识别 / 操作产生 Real_Matrix:

Put_Line
  (Float'Image 
     (Real_Matrix'((Transpose(α) * α) / (Transpose(β) * β))
        (Integer'First, Integer'First)));

当我尝试这个时,我得到了 Constraint_Error;所以我尝试了@BrianDrummond 的建议,

γ : constant Real_Matrix := (Transpose(α) * α) / (Transpose(β) * β);

结果 γ’First (1) 是 -2147483648,而 γ’First (2) 是 1(这是 macOS Sierra 上的 GNAT GPL 2016)。

进一步调查:我很确定这是 GNAT Inverse 中的一个错误。 ARM G.3.1(72)

This function returns a matrix B such that A * B is (nearly) equal to the unit matrix. The index ranges of the result are A'Range(2) and A'Range(1). Constraint_Error is raised if A'Length(1) is not equal to A'Length(2). Constraint_Error is raised if the matrix A is ill-conditioned.

GNAT 的实现是

function Inverse (A : Real_Matrix) return Real_Matrix is
  (Solve (A, Unit_Matrix (Length (A))));

其中 Solve(相同参考,(70))说

This function returns a matrix Y such that X is (nearly) equal to A * Y. This is the standard mathematical operation for solving several sets of linear equations. The index ranges of the result are A'Range(2) and X'Range(2). Constraint_Error is raised if A'Length(1), A'Length(2), and X'Length(1) are not equal. Constraint_Error is raised if the matrix A is ill-conditioned.

Unit_Matrix(相同的引用,(79))是

function Unit_Matrix (Order            : Positive;
                      First_1, First_2 : Integer := 1) return Real_Matrix;

注意 First_1First_2!

的默认值