PEquationsData = ^TEquationsData;
TEquationsData = array[0..0] of Double;
// 线性回归
TLinearRegression = class(TObject)
private
FData: PEquationsData; LinearRegression中有函数:
procedure TLinearRegression.SetData(const AData; const ARowCount, AColCount: Integer);
begin
if (ARowCount < 2) or (AColCount < 2) then
raise Exception.Create(SMatrixSizeError);
//SetSize(ARowCount, AColCount);
Move(AData, FData^, FRowCount * FColCount * Sizeof(Double));
end;
调用时:
LinearRegression.setdata()
adata传递为常量数组:
data1: array[1..12, 1..2] of Double = (
// X Y
( 187.1, 25.4 ),
( 179.5, 22.8 ),
( 157.0, 20.6 ),
( 197.0, 21.8 ),
( 239.4, 32.4 ),
( 217.8, 24.4 ),
( 227.1, 29.3 ),
( 233.4, 27.9 ),
( 242.0, 27.8 ),
( 251.9, 34.2 ),
( 230.0, 29.2 ),
( 271.8, 30.0 )
);
一切正常,
当参数为动态数组TDoubleArray =array of array of Double; 二维动态数组时 赋值不成功。
TemA:Tdoublearray;
// SetLength(TemA,FTotal_Process_Count);
// for I := 0 to FTotal_Process_Count - 1 do
// SetLength(TemA[i],x_count+1);
// for j := 0 to X_Count do
// begin
// for I := 0 to FTotal_Process_Count - 1 do
// begin
// if TempDataGrid.Cells[TempArry[j], i + 1] <> '' then
// TemA[i, j] := StrToFloat((TempDataGrid.Cells[TempArry[j], i + 1]))
// else
// TemA[i, j] := 0;
// // mmo1.Lines.Add(floattostr(x[i,j])+'----'+inttostr(i)+'-'+inttostr(j));
// end;
请高手帮忙看看谢谢了!
TDoubleArray = array of array of Double;
var
x: TDoubleArray;
begin
SetLength(x, A, B);这时,x[0]是一个指向 array Of Double 的指针,x[0][0]是该指针指向的首元素
正如 a, b: array of Double 中,a的元素和b的元素不是连续的一样,x[0]和x[1]中的元素也是不连续的
如果想使用高维动态数组,那么你就必须重新设计 SetData 中的实现方式了btw, 在delphi中,动态数组是由rtl使用引用计数管理生存期的,这样复制来复制去的不是什么高效的办法,还不如直接 FData:=TempA 呢
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TDoubleArray = array of array of Double; TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure SetData(const AData: TDoubleArray; const ARowCount, AColCount: Integer);
// procedure SetData(const AData; const ARowCount, AColCount: Integer);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.SetData(const AData: TDoubleArray; const ARowCount, AColCount: Integer);
//procedure TForm1.SetData(const AData; const ARowCount, AColCount: Integer);
var
i, j: Integer;
begin
//
for i := 0 to ARowCount - 1 do
begin
for j := 0 to AColCount - 1 do
Memo1.Lines.Add(FormatFloat('0.00', AData[i][j]));
Memo1.Lines.Add(Char(VK_RETURN));
end;
end;procedure TForm1.Button1Click(Sender: TObject);
const
data1: array[1..12, 1..2] of Double = (
// X Y
( 187.1, 25.4 ),
( 179.5, 22.8 ),
( 157.0, 20.6 ),
( 197.0, 21.8 ),
( 239.4, 32.4 ),
( 217.8, 24.4 ),
( 227.1, 29.3 ),
( 233.4, 27.9 ),
( 242.0, 27.8 ),
( 251.9, 34.2 ),
( 230.0, 29.2 ),
( 271.8, 30.0 )
);
var
i, j: Integer;
data2: TDoubleArray;
begin
SetLength(data2, length(data1), length(data1[1]));
for i := 1 to length(data1) do
for j := 1 to length(data1[1]) do
data2[i-1][j-1] := data1[i][j];
SetData(TDoubleArray(data2), length(data2), length(data2[0]));
end;end.