I created a matrix object which is a child of Tmemorystream
TMatrix = class(TMemoryStream)
rows,cols:longint;
constructor create(r,c:longint); virtual;
function item(r,c:longint):double;
procedure setitem(r,c:longint;d:double);
procedure setMatrix(r,c:longint);
procedure zero;
Procedure Pivot(r,c:Longint);
Function Average(a:Longint):double;
Function SumSquares(a:longint):double;
Function Variance(a:Longint):double;
function Max(a:LongInt):double;
function Min(a:LongInt):double;
Function StdDeviation(a:LongInt):double;
function stdperiod(col,d1,d2:LongInt):double;
function avgperiod(col,d1,d2:LongInt):double;
function MaxPeriod(col,d1,d2:LongInt):double;
function MinPeriod(col,d1,d2:LongInt):double;
procedure readfile(var f:TFileStream); virtual;
procedure writefile(var f:TFileStream); virtual;
procedure savetofile(s:string);
procedure loadfromfile(s:string)
procedure writetext(s:string);
procedure print(s:String)
end;
with methods looking like ...
constructor TMatrix.create(r,c:longint);
begin
inherited create;
setmatrix(r,c);
end;
procedure TMatrix.SetMatrix(r,c:longint);
var
x:longint;
begin
rows:=r;
cols:=c;
x:=sizeof(double)*r;
x:=x*c;
Clear;
SetSize(x);
zero;
end;
function TMatrix.item(r,c:longint):double;
var
x:longint;
d:double;
begin
x:=((r-1)*cols+(c-1))*sizeof(d);
d:=0;
if x<size then begin
seek(x,0);
read(d,sizeof(d));
end;
item:=d;
end;
procedure TMatrix.setitem(r,c:longint;d:double);
var
x:longint;
begin
x:=((r-1)*cols+(c-1))*sizeof(d);
if x<size then begin
seek(x,0);
write(d,sizeof(d));
end;
end;
procedure TMatrix.Zero;
var
i,j:longint;
begin
for i:=1 to rows do begin
for j:=1 to cols do SetItem(i,j,0);
end;
end;
I have used this object with very large matrix manipulation with pretty good
sucess. Perhaps this approach will suit your needs.
Bill
In article <4j7s2d$...@news1.inlink.com>, jstra...@inlink.com (Joe Stratmann)
wrote:
Quote
>plmi...@ix.netcom.com (Patrick Mills) wrote:
>>Using Delphi 2.0, I need to dynamically allocate memory and reference
>>it as a two-dimensional array. The techinfo.hlp on the CD says use
>>this technique for dynamically allocating an array:
>>>...you need to create an array type using the largest size you
>>>might possibly need. When creating a type, no memory is actually
>>>allocated. If you created a variable of that type, then the
>>>compiler will attempt to allocate the necessary memory for you.
>>>Instead, create a variable which is a pointer to that type. This
>>>causes the compiler to only allocate the four bytes needed for the
>>>pointer.
>>>Before you can use the array, you need to allocate memory for it.
>>>By using AllocMem, you will be able to control exactly how many
>>>bytes are allocated. To determine the number of bytes you'll need
>>>to allocate, simply multiply the array size you want by the size of
>>>the individual array element.
>>This technique seems to work fine for a single-dimensional array, but
>>if I declare the type as a two-dimensional array, I get a
>>memory-protection fault when the outer loop that processes the first
>>dimension of the array moves past the first index value (i.e. from 1
>>to 2).
>>Anyone got any suggestions?
>>Patrick Mills
>>plmi...@ix.netcom.com
>This may be a real dumb question, but when you allocate the array did you
> calculate the size as
>x*y?
>Such as if the array could be myArray[100][100]
>did you do:
>(pseudocode - can't remember the syntax and I'm dead tired)
>myArrayPtr = AllocMem(100*100);
>Joe