unit Matr;
interface
/// Упаковка матрицы в последовательность по строкам
function MatrPackRows<T>(a:array[,] of T):sequence of T;
/// Упаковка матрицы в последовательность по столбцам
function MatrPackCols<T>(a:array[,] of T):sequence of T;
/// Формирование матрицы из последовательности построчное
function MatrUnpack<T>(s:sequence of T; cols:integer):array[,] of T;
/// Срез матрицы (пока жесткий)
function Slice<T>(a:array[,] of T; rfrom,rto,cfrom,cto:integer):array[,] of T;
/// Главная диагональ матрицы в последовательность
function MatrMainDiag<T>(a:array[,] of T):sequence of T;
/// Побочная диагональ матрицы в последовательность
function MatrSecondDiag<T>(a:array[,] of T):sequence of T;
implementation
function MatrPackRows<T>(a:array[,] of T):sequence of T;
begin
var jm:=a.ColCount-1;
for var i:=0 to a.RowCount-1 do
for var j:=0 to jm do
yield a[i,j]
end;
function MatrPackRows<T>(Self:array[,] of T):sequence of T; extensionmethod;
begin
var jm:=Self.ColCount-1;
for var i:=0 to Self.RowCount-1 do
for var j:=0 to jm do
yield Self[i,j]
end;
function MatrPackCols<T>(a:array[,] of T):sequence of T;
begin
var im:=a.RowCount-1;
for var j:=0 to a.ColCount-1 do
for var i:=0 to im do
yield a[i,j]
end;
function MatrPackCols<T>(Self:array[,] of T):sequence of T; extensionmethod;
begin
var im:=Self.RowCount-1;
for var j:=0 to Self.ColCount-1 do
for var i:=0 to im do
yield Self[i,j]
end;
function MatrUnpack<T>(s:sequence of T;cols:integer):array[,] of T;
begin
var r:=new T[s.Count div cols,cols];
var n:=r.Length;
var k:=0;
foreach var elem in s do begin
r[k div cols, k mod cols]:=elem;
k+=1;
if k=n then break
end;
Result:=r
end;
function MatrUnpack<T>(Self:sequence of T;cols:integer):array[,] of T;
extensionmethod;
begin
var r:=new T[Self.Count div cols,cols];
var n:=r.Length;
var k:=0;
foreach var elem in Self do begin
r[k div cols, k mod cols]:=elem;
k+=1;
if k=n then break
end;
Result:=r
end;
function Slice<T>(a:array[,] of T;rfrom,rto,cfrom,cto:integer):array[,] of T;
begin
var rows:=a.RowCount;
var cols:=a.ColCount;
Assert((rfrom>=0) and (rfrom<rows),'Недопустимый параметр rfrom');
Assert((rto>=0) and (rto<rows) and (rto>rfrom),'Недопустимый параметр rto');
Assert((cfrom>=0) and (cfrom<cols),'Недопустимый параметр cfrom');
Assert((cto>=0) and (cto<cols) and (cto>cfrom),'Недопустимый параметр cto');
var newrows:=rto-rfrom;
var newcols:=cto-cfrom;
var r:=new T[newrows,newcols];
for var i:=rfrom to rto-1 do
for var j:=cfrom to cto-1 do
r[i-rfrom,j-cfrom]:=a[i,j];
Result:=r
end;
function Slice<T>(Self:array[,] of T;rfrom,rto,cfrom,cto:integer):array[,] of T;
extensionmethod;
begin
var rows:=Self.RowCount;
var cols:=Self.ColCount;
Assert((rfrom>=0) and (rfrom<=rows),'Недопустимый параметр rfrom');
Assert((rto>=0) and (rto<=rows) and (rto>rfrom),'Недопустимый параметр rto');
Assert((cfrom>=0) and (cfrom<=cols),'Недопустимый параметр cfrom');
Assert((cto>=0) and (cto<=cols) and (cto>cfrom),'Недопустимый параметр cto');
var newrows:=rto-rfrom;
var newcols:=cto-cfrom;
var r:=new T[newrows,newcols];
for var i:=rfrom to rto-1 do
for var j:=cfrom to cto-1 do
r[i-rfrom,j-cfrom]:=Self[i,j];
Result:=r
end;
function MatrMainDiag<T>(a:array[,]of T):sequence of T;
begin
var n:=Min(a.RowCount,a.ColCount)-1;
for var i:=0 to n do yield a[i,i]
end;
function MatrMainDiag<T>(Self:array[,]of T):sequence of T; extensionmethod;
begin
var n:=Min(Self.RowCount,Self.ColCount)-1;
for var i:=0 to n do yield Self[i,i]
end;
function MatrSecondDiag<T>(a:array[,]of T):sequence of T;
begin
var n:=Min(a.RowCount,a.ColCount);
for var i:=0 to n-1 do
yield a[i,a.ColCount-i-1]
end;
function MatrSecondDiag<T>(Self:array[,]of T):sequence of T; extensionmethod;
begin
var n:=Min(Self.RowCount,Self.ColCount);
for var i:=0 to n-1 do
yield Self[i,Self.ColCount-i-1]
end;
end;