Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-06

Initializing dynamic arrays

4 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

I am writing a unit for dealing with matrix algebra with arbitrary sized matrices (as opposed to the FPC unit that deals with a few fixed size ones).

I declare the matrix type

type
	Matrix = array of array of Real;

By using Length or High, I can loop through the array as I please, and I can make operator overloading and functions for most operations I need.

There is just one thing that I can't do: I can't initialize a matrix in any elegant way! I would like to do like this:

	m1: Matrix = ((1, 2, 3), (2, 3, 4));

and get a properly sized dynamic array matching the matrix given.

This is obviously not supported, but what can I do? I can build an infinite number of functions, taking fixed-sized arrays as input and returning a dynamic array, but then I need one function for each combination of height and width! That is OK for sizes up to 3 or 4 but then it becomes ridiculous.

Is there a better way to solve this problem?

/Ingemar

Reply by email

Jonas Maebe
to Unknown author

ingemar wrote on Mon, 04 Jun 2012:

Show quoted text 14 lines
There is just one thing that I can't do: I can't initialize a matrix in any elegant way! I would like to do like this: m1: Matrix = ((1, 2, 3), (2, 3, 4)); and get a properly sized dynamic array matching the matrix given. This is obviously not supported, but what can I do? I can build an infinite number of functions, taking fixed-sized arrays as input and returning a dynamic array, but then I need one function for each combination of height and width! That is OK for sizes up to 3 or 4 but then it becomes ridiculous. Is there a better way to solve this problem?

I think this is a case where a pointer combined with a width and height parameter is probably the best solution. The more Pascal-like and cleaner approach using open array parameters is only generic if you limit yourself to 1-dimensional arrays.

Jonas

Reply by email

Marco van de Voort
to Jonas Maebe

In our previous episode, Jonas Maebe said:

Show quoted text 16 lines
> m1: Matrix = ((1, 2, 3), (2, 3, 4)); > > and get a properly sized dynamic array matching the matrix given. > > This is obviously not supported, but what can I do? I can build an > infinite number of functions, taking fixed-sized arrays as input and > returning a dynamic array, but then I need one function for each > combination of height and width! That is OK for sizes up to 3 or 4 but > then it becomes ridiculous. > > Is there a better way to solve this problem? I think this is a case where a pointer combined with a width and height parameter is probably the best solution. The more Pascal-like and cleaner approach using open array parameters is only generic if you limit yourself to 1-dimensional arrays.

Numlib does this, though dimensions (1 or 2) must be passed as separate arguments, and instead of a pointer often a var variable of the cell type is used.

determinants, eigen values, quite some is there.

Reply by email

Paulo Costa
to Unknown author: I am writing a unit for dealing with matrix algebra with arbitrary sized matrices (as…
I am writing a unit for dealing with matrix algebra with arbitrary sized matrices (as opposed to the FPC unit that deals with a few fixed size ones).

If you are writing that for your fun and to learn, go ahead, if what you really want is to have a unit to deal with arbitrary sized matrices, just go to:
http://wiki.freepascal.org/5dpo
and install the SdpoDynmatrix package.
There you have automatically ref counted arbitrary sized matrices with overloaded operators to add, subtract, multiply and invert them.

As for the initialization of a matrix you can do the, somewhat ugly but effective, trick of creating a function like:

// Converts a String representation to a TDMatrix
// a11 a12 a13; a12 a22 a23;
function StringToDMatrix(str: string): TDMatrix;

and then write:

A := StringToDMatrix('1 2 3; 4 5 6;');

Paulo Costa

Reply by email