Ada:访问Real_Matrix中的第一个元素,其中包含一行和一列

evi*_*ate 2 ada

问题陈述

我有一个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;
Run Code Online (Sandbox Code Playgroud)

Sim*_*ght 5

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

Put_Line
  (Float'Image 
     (Real_Matrix'((Transpose(?) * ?) / (Transpose(?) * ?))
        (Integer'First, Integer'First)));
Run Code Online (Sandbox Code Playgroud)

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

? : constant Real_Matrix := (Transpose(?) * ?) / (Transpose(?) * ?);
Run Code Online (Sandbox Code Playgroud)

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

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

该函数返回矩阵B,使得A*B(几乎)等于单位矩阵.结果的索引范围是A'Range(2)和A'Range(1).如果A'Length(1)不等于A'Length(2),则引发Constraint_Error.如果矩阵A病态,则引发Constraint_Error.

和GNAT的实现是

function Inverse (A : Real_Matrix) return Real_Matrix is
  (Solve (A, Unit_Matrix (Length (A))));
Run Code Online (Sandbox Code Playgroud)

其中Solve(同一参考文献,(70))说

该函数返回矩阵Y,使得X(几乎)等于A*Y.这是求解几组线性方程的标准数学运算.结果的索引范围是A'Range(2)和X'Range(2).如果A'Length(1),A'Length(2)和X'Length(1)不相等,则引发Constraint_Error.如果矩阵A病态,则引发Constraint_Error.

Unit_Matrix(相同的参考,(79))是

function Unit_Matrix (Order            : Positive;
                      First_1, First_2 : Integer := 1) return Real_Matrix;
Run Code Online (Sandbox Code Playgroud)

注意First_1,First_2!的默认值!