4

I was wondering about how to create a multidimensional matrix using Ada language, I mean, stacking i number of matrix of j x k dimension:

enter image description here

One way I have found is using multidimensional arrays, quite easy.

Are there other ways to accomplish it? Maybe using Ada.Containers.Vectors or mixing it within the array declaration? Any formal library?

Thanks

4
  • Can you elaborate on why you are interested in an alternative approach? Do multi-dimensional arrays not suffice or are they hard to use for the particular use case you are facing? Commented Feb 19, 2021 at 8:19
  • Is the dimension, you are referring to, the geometric dimension (like 3 in a 3x3 matrix) or the number of indices in your array? Commented Feb 19, 2021 at 8:37
  • @DeeDee just being curious, I use to stack matrix in other languages into a multidimensional matrix of (m x n) x z dimensions and it came to mi mind how it would be done with such a strong typed language. Commented Feb 19, 2021 at 11:39
  • 1
    @Zerte edited for clarifying I mean stacking matrix, e.g. (m x n) x z dimensions, that is, m x n matrix and z stacked matrix in the same object. Commented Feb 19, 2021 at 11:40

2 Answers 2

4

Here are two examples that might be of use.

  • The first example shows the use of an array of multi-dimensional arrays. When using this method you need, at some point, to fix the size of the stack of matrices (here: during the declaration of S, the size is fixed to 4). Memory is preallocated to hold all four matrices.

  • The second example shows the use of an Ada vector container to which matrices can be added. Vectors are "dynamic arrays". Memory is allocated when matrices added.

The Ada standard library that ships with (recent versions of) the GNAT compiler contains formal containers packages (see also here).

example_1.adb

with Ada.Text_IO; use Ada.Text_IO; procedure Example_1 is type Rows is new Natural range 0 .. 2; type Cols is new Natural range 0 .. 2; type Matrix is array (Rows, Cols) of Integer; -- A 2D matrix. type Stack is array (Natural range <>) of Matrix; -- Stack of 2D matrices. S : constant Stack (1 .. 4) := (1 => (others => (others => 1)), 2 => (others => (others => 2)), 3 => (others => (others => 3)), 4 => (others => (others => 4))); begin for Mtx of S loop -- using "of", not "in", such to iterate over the elements. for R in Rows loop Put ("["); for C in Cols loop Put (Mtx (R, C)'Image); end loop; Put_Line (" ]"); end loop; New_Line; end loop; end Example_1; 

example_2.adb

with Ada.Text_IO; use Ada.Text_IO; with Ada.Containers.Vectors; procedure Example_2 is type Rows is new Natural range 0 .. 2; type Cols is new Natural range 0 .. 2; type Matrix is array (Rows, Cols) of Integer; -- A 2D matrix. package Stacks is new Ada.Containers.Vectors (Natural, Matrix); use Stacks; subtype Stack is Stacks.Vector; -- Stack of 2D matrices. Empty_Stack : constant Stack := Stacks.Empty_Vector; -- An empty stack. S : Stack; -- Instead of using Append (..) shown below, you can also -- initialize the stack using: -- -- S : Stack := Empty_Stack -- & (others => (others => 1)) -- & (others => (others => 2)) -- & (others => (others => 3)) -- & (others => (others => 4)); begin S.Append ((others => (others => 1))); S.Append ((others => (others => 2))); S.Append ((others => (others => 3))); S.Append ((others => (others => 4))); -- ... for Mtx of S loop -- using "of", not "in", such to iterate over the elements. for R in Rows loop Put ("["); for C in Cols loop Put (Mtx (R, C)'Image); end loop; Put_Line (" ]"); end loop; New_Line; end loop; end Example_2; 

output (same for both examples)

[ 1 1 1 ] [ 1 1 1 ] [ 1 1 1 ] [ 2 2 2 ] [ 2 2 2 ] [ 2 2 2 ] [ 3 3 3 ] [ 3 3 3 ] [ 3 3 3 ] [ 4 4 4 ] [ 4 4 4 ] [ 4 4 4 ] 
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry to trouble you DeeDee. You gave a great answer to a question I asked yesterday stackoverflow.com/questions/66291973/… (suggesting Reference_Preserving_Key) and I wanted to accept it, but it has disappeared. Any chance you could put it back? it was really useful!
That was great, what I was looking for. Thank you so much
2

You could also try standard Ada packages Real Vectors and Matrices or Complex Vectors and Matrices which also provide some operations on matrices. It can be the easiest way to accomplish it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.