From: Adam Beneschan on 2 Feb 2010 20:24 On Feb 2, 2:23 pm, Jerry <lancebo...(a)qwest.net> wrote: > On Feb 2, 1:52 am, Jean-Pierre Rosen <ro...(a)adalog.fr> wrote: > > > > > > > Jerry a écrit :> I've never understood why Ada does not allow slicing in > > > multidimensional arrays. What are the safety issues involved? And how > > > is it safe to force the programmer into ad hoc methods? > > > One-dimensional slices are simple and efficient. Multidimensional slices > > are a can of worms. > Well, yes, I was thinking of rectangular slices. No doubt the (cost of > implementation) / usefulness is high (and usage difficult) for non- > rectangular parts, but that is far less common than rectangular parts. > Python, Matlab/Octave, Igor Pro... all pull it off without too much > hassle (although Python asks you to imagine addressing the array by > the "cracks" between elements, as I recall--probably a disease of C- > style counting). I don't know anything about any of those languages. What I know about Ada is that when multi-dimensional arrays are passed as parameters to procedures, they're normally passed by reference (if they're large enough). Some arrays are required to be passed by reference; in other cases, it's unspecified, but I'd expect any array to be passed by reference unless it's a packed array of 32 Booleans or something. So say you have a procedure: type Matrix is array (Natural range <>, Natural range <>) of Float; procedure Operation_On_Matrix (M : in out Matrix) is ... You later declare a matrix: X : Matrix (1..10, 1..10); and want to call the operation on a rectangular slice. If the "slice" consists of an entire row, or one or more consecutive rows: Operation_On_Matrix (X (3..4, 1..10)); this could be done as efficiently as if all of X were passed, since the procedure would see it as an array of 20 consecutive floats (in a typical implementation). However, a slice consisting of one or more columns: Operation_On_Matrix (X (1..10, 5..6)); or some smaller rectangle: Operation_On_Matrix (X (4..6, 4..6)); would be tricky, since the procedure now has to be told that there are gaps between the "rows" of the array that it's seeing as a parameter. This means making Operation_On_Matrix less efficient, since it has to be given more information about each Matrix that comes in; and the inefficiency has to be put there even if no caller ever used a 2-D rectangular slice, ever. Maybe the efficiency hit isn't all that large---I don't know. It wouldn't really work to fix the language to say that rectangular slices are allowed only if they include the entire index range in all dimensions but the first, since that would fail for array types declared with the Fortran convention. I realize that the original question was about using assignment to copy slices. I suppose that, theoretically, the language could be changed to allow multi-dimensional slices in assignments but not as subprogram parameters. Yuck. I wouldn't want to add that kind of inconsistency to the language. Anyway, I don't know how serious these issues are, but this seems to me to be a possible reason why adding this feature isn't as simple as it sounds. -- Adam
From: Hibou57 (Yannick Duchêne) on 3 Feb 2010 23:13 On 1 fév, 03:11, "Peter C. Chapin" <pcc482...(a)gmail.com> wrote: > Apparently the component type of an array needs to be fully constrained (which > again makes sense) yet I don't know the size That's what generics are made for. But you will have a single matrix type of a fixed size. I you don't want a fixed size type, the best I will think about, would be to create an abstract matrix type which will be implemented on a one dimensional array. You could use slice in the implementation. Ex. for a 2x2 matrix, you will use a 1 .. 4 array of Float. You may define a swap row method which will move items from 1 .. 2 to 3 .. 4 and vice-versa. You will be able to use slice optimized operations. Tell me if you need a more concrete example (I confess I'm a bit terse here).
From: Hibou57 (Yannick Duchêne) on 3 Feb 2010 23:27 On 1 fév, 07:55, Niklas Holsti <niklas.hol...(a)tidorum.invalid> wrote: > Another solution -- which perhaps you have considered -- is to use a > generic package to define row and matrix types: > > generic Size : Positive; > package Matrices > is > type Row is array (1 .. Size) of Float; > type Matrix is array (1 .. Size) of Row; > end Matrices; > > However, this will probably force some other parts of your code to > become generic, too, parametrized either by a Size, or by an instance of > Matrices. I was just thinking while I was reading you : if he use generics, then the generic package should define two matrix types, one for source matrix and one for result matrix of some matrix functions (which returns matrix with reversed dimensions). One Matrix(X,Y) and one Matrix(Y,X)
From: Hibou57 (Yannick Duchêne) on 3 Feb 2010 23:42 On 2 fév, 09:52, Jean-Pierre Rosen <ro...(a)adalog.fr> wrote: > When designing a programming language, you have to stop at some point. You have to stop to primitives (like rendezvous for tasking), things which are a pain to simulate if not supported by the language (like post/pre condition which will come with the next revision), consistency (like my suggestion to make object declared with Constant overloadable) and constructs with overall properties (like the loop construct and its immutable variant instead of the goto-made-loop). For every thing else (starting with personal-need-of-the-day), there are generics and generic instantiations (and a language is not a library, it's a paradigm)
From: Dmitry A. Kazakov on 4 Feb 2010 04:10
On Wed, 3 Feb 2010 20:13:20 -0800 (PST), Hibou57 (Yannick Duch�ne) wrote: > On 1 f�v, 03:11, "Peter C. Chapin" <pcc482...(a)gmail.com> wrote: >> Apparently the component type of an array needs to be fully constrained (which >> again makes sense) yet I don't know the size > That's what generics are made for. > But you will have a single matrix type of a fixed size. Generics per design are incapable to express that the thing is variable. You cannot make a generic variable-dimension matrix, a generic string, or for that matter even a generic number: generic Value : WHAT? package Integer is ... end Integer; That does not work. > I you don't want a fixed size type, the best I will think about, would > be to create an abstract matrix type which will be implemented on a > one dimensional array. You could use slice in the implementation. This does not work either, I mean at the user interface level, because slice does not have a type. In Ada types system you cannot express "the subtype S is a vector of the type M". Therefore making it abstract types you will loose most of the comfort built-in arrays offer. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de |