6

I have this array:

const / var
  _Data : array [0..4] of array [0..3] of Double =
    ((0,0,0,0),
     (0,0,1,1),
     (1,0,1,0),
     (1,1,0,0),
     (1,1,1,1));

I wanna pass it as param value for this procedure:

procedure NN.NetTraining(Data: TDoubleMatrix);

Where:

  TDoubleArray    = array of Double;
  TDoubleMatrix   = array of TDoubleArray;

Is There some manner to cast or convert this static array into dynamic array in Delphi (2009) ?

Thanks in advance.

  • Check here: [http://chee-yang.blogspot.com/2008/10/delphi-2009-array-is-dead-long-live.html](http://chee-yang.blogspot.com/2008/10/delphi-2009-array-is-dead-long-live.html) – Chau Chee Yang Oct 21 '09 at 01:07

4 Answers4

3

While this does not do exactly what you want (for reasons given in Gamecat's answer), it may be a viable work-around for you to initialise your dynamic data array:


var Data:TDoubleMatrix;
begin
  Data:=TDoubleMatrix.create(TDoubleArray.create(0,0,0,0),
                             TDoubleArray.create(0,0,1,1),
                             TDoubleArray.create(1,0,1,0),
                             TDoubleArray.create(1,1,0,0),
                             TDoubleArray.create(1,1,1,1));
end;
PhiS
  • 4,540
  • 25
  • 35
  • I like ther create for dynamic arrays. Unfortunately there is no free. – Toon Krijthe Oct 20 '09 at 10:58
  • 1
    @Gamecat: You don't need a free. Dynamic arrays are freed automatically when they go out of scope, or you can simply set them to nil. The Create() construct simply allows you to initialize the entire array in one shot. – Ken White Oct 20 '09 at 12:42
1

Dynamic arrays differ from normal arrays.

Dynamic arrays are pointers, while normal arrays are blocks of memory. With one dimensional arrays, you can use the address of the array. But with multi dimensional arrays this trick won't work.

In your case, I would use a file to initialize the array. So you can use dynamic arrays 100% of the time. Else you have to write your own conversion which kind of defeats the purpose of dymanic arrays.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
0

You can't cast but you can copy! Something like...

    procedure CopyDMatrix(var Source; LengthX, LengthY: integer; var Dest: TDoubleMatrix);
    const
      SizeOfDouble = SizeOf(Double);
    var
      n  : integer;
      ptr: pointer;
    begin
      SetLength(Dest, LengthX);
      Ptr := @Source;
      for n := 0 to LengthX - 1 do begin
        SetLength(Dest[n], LengthY);
        Move(ptr^, Dest[n][0] , SizeOfDouble * LengthY);
        inc(cardinal(ptr), SizeOfDouble * LengthY);
      end;
    end;

    //...  

    CopyDMatrix(Data, Length(Data), Length(Data[0]), DoubleMatrix);
GJ.
  • 10,810
  • 2
  • 45
  • 62
-1

May be you should use open arrays instead?

Alex
  • 5,477
  • 2
  • 36
  • 56
  • I'm pretty sure open arrays are only one-dimensional. Any further dimensions must be either static or dynamic. Can you post the code to show that it works? – Rob Kennedy Oct 20 '09 at 15:28