File : scalvectors.adb


with Ints;
use Ints;

pragma Elaborate (Ints);

package body ScalVectors is

  function Diag(V: ScalVec) return ScalMat is
    A: ScalMat(V'Range,V'Range);
  begin
    SetZero(A);
    for I in V'Range loop
      A(I,I) := V(I);
    end loop;
    return A;
  end Diag;

  procedure SetRow(I: in Integer; V: in ScalVec; A: in out ScalMat) is
    JFirst: constant Integer := IMin(V'First,A'First(2));
    JLast:  constant Integer := IMax(V'Last,A'Last(2));
  begin
    for J in JFirst .. JLast loop
      A(I,J) := V(J);
    end loop;
  end SetRow;

  function GetRow(I: Integer; A: ScalMat) return ScalVec is
     V: ScalVec(A'Range(2));
  begin
    for J in V'Range loop
      V(J) := A(I,J);
    end loop;
    return V;
  end GetRow;

  procedure SetColumn(J: in Integer; V: in ScalVec; A: in out ScalMat) is
    IFirst: constant Integer := IMin(V'First,A'First(1));
    ILast:  constant Integer := IMax(V'Last,A'Last(1));
  begin
    for I in IFirst .. ILast loop
      A(I,J) := V(I);
    end loop;
  end SetColumn;

  function GetColumn(J: Integer; A: ScalMat) return ScalVec is
     V: ScalVec(A'Range(1));
  begin
    for I in V'Range loop
      V(I) := A(I,J);
    end loop;
    return V;
  end GetColumn;

  ------------------------

  procedure Inv2(A: in ScalMat2; Ai: out ScalMat2; Det: out Scalar) is
  begin
    Det := A(1,1)*A(2,2)-A(1,2)*A(2,1);
    Ai(1,1) :=  A(2,2)/Det;
    Ai(2,2) :=  A(1,1)/Det;
    Ai(1,2) := -A(1,2)/Det;
    Ai(2,1) := -A(2,1)/Det;
  end Inv2;

  function Inv2(A: ScalMat2) return ScalMat2 is
    Det: Scalar;
    Ai: ScalMat2;
  begin
    Inv2(A,Ai,Det);
    return Ai;
  end Inv2;

  procedure Inv3(A: in ScalMat3; Ai: out ScalMat3; Det: out Scalar) is
  begin
    Ai(1,1) := A(2,2)*A(3,3)-A(3,2)*A(2,3);
    Ai(1,2) := A(3,2)*A(1,3)-A(1,2)*A(3,3);
    Ai(1,3) := A(1,2)*A(2,3)-A(1,3)*A(2,2);
    Det := Ai(1,1)*A(1,1)+Ai(1,2)*A(2,1)+Ai(1,3)*A(3,1);
    Ai(1,1) := Ai(1,1)/Det;
    Ai(1,2) := Ai(1,2)/Det;
    Ai(1,3) := Ai(1,3)/Det;
    Ai(2,1) := (A(2,3)*A(3,1)-A(2,1)*A(3,3))/Det;
    Ai(2,2) := (A(1,1)*A(3,3)-A(3,1)*A(1,3))/Det;
    Ai(2,3) := (A(2,1)*A(1,3)-A(1,1)*A(2,3))/Det;
    Ai(3,1) := (A(2,1)*A(3,2)-A(2,2)*A(3,1))/Det;
    Ai(3,2) := (A(3,1)*A(1,2)-A(1,1)*A(3,2))/Det;
    Ai(3,3) := (A(1,1)*A(2,2)-A(1,2)*A(2,1))/Det;
  end Inv3;

  function Inv3(A: ScalMat3) return ScalMat3 is
    Det: Scalar;
    Ai: ScalMat3;
  begin
    Inv3(A,Ai,Det);
    return Ai;
  end Inv3;

end ScalVectors;