如何在Pascal中实现矩阵运算?

发布于 2024-07-18 04:20:52 字数 361 浏览 7 评论 0原文

我需要用矩阵实现运算,并且矩阵的大小必须是可变的。 我想出的唯一解决方案是使用链表:

[pointer to this row, pointer to another row] -> [element 1,1; link to another element] -> [element 1,2,  link to another element] -> .... -> [nil]
     |
     v
[pointer to this row, pointer to another row] ...
     ...

但在我看来有点复杂..有更好(更简单)的解决方案吗?

感谢你们!

I need to implement operations with matrices and size of matrix has to be variable. The only solution I came up with is to use linked list:

[pointer to this row, pointer to another row] -> [element 1,1; link to another element] -> [element 1,2,  link to another element] -> .... -> [nil]
     |
     v
[pointer to this row, pointer to another row] ...
     ...

But it seems to me a little bit complex.. Is there a better (and easier) solution?

Thank you guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁对谁错谁最难过 2024-07-25 04:20:52

一种方法是使用 GetMem 来分配足够的内存。 GetMem 似乎得到了广泛的支持。

const
    MAXMATRIXDATA: Word = 10000;
type
    TMatrixDataType = Word;
    TMatrixData = array[0..MAXMATRIXDATA] of TMatrixDataType;
    PMatrixData = ^TMatrixData;
    TMatrix = record
        Rows, Cols: Word;
        MatrixData: PMatrixData;
        end;
    PMatrix = ^TMatrix;

function CreateMatrix(Rows, Cols: Word): PMatrix;
var
    Ret: PMatrix;
begin
    New(Ret);
    Ret^.Rows := Rows;
    Ret^.Cols := Cols;
    GetMem(Ret^.MatrixData,Rows*Cols*SizeOf(TMatrixDataType));
    CreateMatrix := Ret;
end;

function GetMatrixData(Matrix: PMatrix; Row, Col: Word): TMatrixDataType;
begin
    GetMatrixData := Matrix^.MatrixData^[(Row*Matrix^.Cols)+Col];
end;

procedure SetMatrixData(Matrix: PMatrix; Row, Col: Word; Val: TMatrixDataType);
begin
    Matrix^.MatrixData^[(Row*Matrix^.Cols)+Col] := Val;
end;

One approach would be to use GetMem to allocate exactly enough memory. GetMem seems widely supported.

const
    MAXMATRIXDATA: Word = 10000;
type
    TMatrixDataType = Word;
    TMatrixData = array[0..MAXMATRIXDATA] of TMatrixDataType;
    PMatrixData = ^TMatrixData;
    TMatrix = record
        Rows, Cols: Word;
        MatrixData: PMatrixData;
        end;
    PMatrix = ^TMatrix;

function CreateMatrix(Rows, Cols: Word): PMatrix;
var
    Ret: PMatrix;
begin
    New(Ret);
    Ret^.Rows := Rows;
    Ret^.Cols := Cols;
    GetMem(Ret^.MatrixData,Rows*Cols*SizeOf(TMatrixDataType));
    CreateMatrix := Ret;
end;

function GetMatrixData(Matrix: PMatrix; Row, Col: Word): TMatrixDataType;
begin
    GetMatrixData := Matrix^.MatrixData^[(Row*Matrix^.Cols)+Col];
end;

procedure SetMatrixData(Matrix: PMatrix; Row, Col: Word; Val: TMatrixDataType);
begin
    Matrix^.MatrixData^[(Row*Matrix^.Cols)+Col] := Val;
end;
请叫√我孤独 2024-07-25 04:20:52

任何现代 pascal 变体(Delphi)都可以让您创建动态(运行时大小)数组。

如果该语言不支持多维动态数组,您可以自己处理寻址:

var
  rows, cols, total, i, j : integer;
  cell : datatype;
begin
  rows := ...;
  cols := ...;
  total := rows * cols;
  matrix := ...(total);

  cell := matrix[i * cols + j]; // matrix[row=i,col=j]

end;

这种寻址将比以下链接列表快得多。

Any modern pascal variant (Delphi) will let you create dynamic (runtime sized) arrays.

If the language doesn't support multidimensional dynamic arrays you can take care of the addressing yourself:

var
  rows, cols, total, i, j : integer;
  cell : datatype;
begin
  rows := ...;
  cols := ...;
  total := rows * cols;
  matrix := ...(total);

  cell := matrix[i * cols + j]; // matrix[row=i,col=j]

end;

This kind of addressing will be a lot faster than following linked lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文