gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Dynamic mutidimensional arrays


From: Michael Riedl
Subject: Dynamic mutidimensional arrays
Date: Tue, 28 Mar 2023 00:22:46 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:102.0) Gecko/20100101 Thunderbird/102.8.0

Gaius, all,

I have a questions / suggest about a possible extension of GM2.

Would it be possible to have dynamic multidimensional arrays implemented ?

We have multidimensional open array as procedure parameter, but if we come to the main module we already need to know the (maximal) dimension of e.g. a matrix - that does not make a lot of sense in my view. Especially if you have more complex programs where you do not know upfront the dimensions of the problem you would like to handle.

A small sample code illustrating the approach below - the code should work with AWD Modula, XDS M2 and the P1 compiler - it's the way Oberon addresses the issue (without the "DISPOSE").

Thanks for some feedback

Michael

----------------------------------------------------------------------

MODULE DynArray;

TYPE  tVECTOR    = ARRAY OF REAL;      (* 1-dim open array *)
      tMATRIX    = ARRAY OF tVECTOR;   (* 2-dim open array *)

      tPtrVECTOR = POINTER TO tVECTOR; (* pointer to open 1-dim array *)
      tPtrMATRIX = POINTER TO tMATRIX; (* pointer to open 2-dim array *)

PROCEDURE MultMatVek(    M,N : CARDINAL;
                     VAR A   : ARRAY OF ARRAY OF REAL;   (* in *)
                     VAR B   : ARRAY OF REAL;            (* in *)
                     VAR C   : ARRAY OF REAL);           (* out *)

          (* multiply matrix A[M,N] times vector B[N], result is vector C[M] *)

          VAR i,j : CARDINAL;
              s   : REAL;
BEGIN
      FOR i:=0 TO M-1 DO
        s := 0.0;
        FOR j:=0 TO N-1 DO s:=s + A[i,j]*B[j]; END;
        C[i] := s;
      END; (* FOR i *)
END MultMatVek;

VAR   A       : tPtrMATRIX;
      B,C     : tPtrVECTOR;
      M,N,i,j : CARDINAL;

BEGIN
      M := 4;
      N := 3; (* suppose M and N are read in from some (external) source ... *)

      NEW(A,M,N);
      NEW(B,N);
      NEW(C,M);

      FOR i:=0 TO M-1 DO (* some code to initialize A,B ... *)
        FOR j:=0 TO N-1 DO A^[i,j] := VAL(REAL,i+j); END;
      END;
      FOR i:=0 TO N-1 DO B^[i] := VAL(REAL,i); END;

      MultMatVek(M,N,A^,B^,C^);

      (* Some code to output the result C ... *)

      DISPOSE(A);
      DISPOSE(B);
      DISPOSE(C);
END DynArray.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]