gm2
[Top][All Lists]
Advanced

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

Re: Dynamic mutidimensional arrays


From: Fischlin Andreas
Subject: Re: Dynamic mutidimensional arrays
Date: Tue, 4 Apr 2023 16:18:23 +0000

Dear Michael,

Whatever the availability of some of the features are that you envisage to need, it struck me that your sample code seems to suffer from the fact that you squeeze everything into the same program module, while the services you seem to look for typically should go into a library module. By this approach you seem to fully miss out on one of the fundamental features of M2. Notably data abstraction by using e.g. an opaque type exported from a library module. It seems to me before calling for language extensions it would be advisable to first try to fully exploit the features the language already offers. Perhaps I am wrong with this impression, I just wanted to share it with you as it may possibly help.

Now of course, as Benjamin rightly pointed out, one disadvantage of PIM M2 is that you need "procedures like WriteThis, WriteThat, WriteYetAnotherThat.” for the functionality of Write, which is also true of course for any functionality such as matrix algebra.

Yet, I would argue that even classical M2 offers quite attractive solutions. Perhaps not what you would be happy with, but I’d say at least usable. Look e.g. for our modules LgMatricesLgMatCalc, or LgMatInv from our SciLib (home pagequick reference) all from RAMSES.

Andreas


ETH Zurich
Prof. em. Dr. Andreas Fischlin
IPCC Vice-Chair WGII
Systems Ecology - Institute of Biogeochemistry and Pollutant Dynamics
CHN E 24
Universitaetstrasse 16
8092 Zurich
SWITZERLAND


+41 44 633-6090 phone
+41 44 633-1136 fax
+41 79 595-4050 mobile

             Make it as simple as possible, but distrust it!
________________________________________________________________________









On Tue, 28.03.23, at 00:22, Michael Riedl <udo-michael.riedl@t-online.de> wrote:

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.




Attachment: smime.p7s
Description: S/MIME cryptographic signature


reply via email to

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