Looking at the simple code:
proc main
{
var a : [1..3][1..3] real;
var r = 1..3;
a[r][1] = -3.0;
a[r][2] = 3.0;
a[r][3] = 3.0;
writeln('sliced');
writeln(a[1][1], ' ', a[1][2], ' ', a[1][3]);
writeln(a[2][1], ' ', a[2][2], ' ', a[2][3]);
writeln(a[3][1], ' ', a[3][2], ' ', a[3][3]);
[i in 1..3] a[i][1] = -3.0;
[i in 1..3] a[i][2] = 3.0;
[i in 1..3] a[i][3] = 3.0;
writeln('normal');
writeln(a[1][1], ' ', a[1][2], ' ', a[1][3]);
writeln(a[2][1], ' ', a[2][2], ' ', a[2][3]);
writeln(a[3][1], ' ', a[3][2], ' ', a[3][3]);
}
produces:
sliced
-3.0 -3.0 -3.0
3.0 3.0 3.0
3.0 3.0 3.0
normal
-3.0 3.0 3.0
-3.0 3.0 3.0
-3.0 3.0 3.0
Am I missing something here. Should not the results should agree.
I use array of arrays to be consistent with my own old C++ code. Also, as
Chapel does not seem to allow initialization of a multi-dimensional array,
I avoid them. I raised a related issue in April 2015 in case you want to
cross-reference anything.
Note that the Fortran
a(1:3,1) = -3.0
a(1:3,2) = 3.0
a(1:3,3) = 3.0
or even
a(2:3,1) = -3.0
has worked correctly in various non-standard/standard Fortran compilers
for at least 38 years.
The alternative Chapel
proc main
{
var a : [1..3, 1..3] real;
var r = 1..3;
a[r, 1] = -3.0;
a[r, 2] = 3.0;
a[r, 3] = 3.0;
writeln('sliced');
writeln(a[1, 1], ' ', a[1, 2], ' ', a[1, 3]);
writeln(a[2, 1], ' ', a[2, 2], ' ', a[2, 3]);
writeln(a[3, 1], ' ', a[3, 2], ' ', a[3, 3]);
[i in 1..3] a[i, 1] = -3.0;
[i in 1..3] a[i, 2] = 3.0;
[i in 1..3] a[i, 3] = 3.0;
writeln('normal');
writeln(a[1, 1], ' ', a[1, 2], ' ', a[1, 3]);
writeln(a[2, 1], ' ', a[2, 2], ' ', a[2, 3]);
writeln(a[3, 1], ' ', a[3, 2], ' ', a[3, 3]);
}
Produces consistent results.
Back in April 2015, I was struggling with multi-dimensional arrays and how
to assign then at declaration time (which I do not think was possible at
that time and still is not I believe).
I think you said multidimensional arrays were going to be more consistent
at the time. My examples prove that 3 years later, your advice is still
wise.
But I still cannot initialize my multidimensional array easily.
And even my workaround appears broken in some sense of the word.
The sliced example (of anything less than a full column or row) works
too!
proc main
{
var a : [1..3, 1..3] real;
var r = 1..3;
a[r, 1] = -3.0;
a[r, 2] = 3.0;
a[r, 3] = 3.0;
writeln('sliced');
writeln(a[1, 1], ' ', a[1, 2], ' ', a[1, 3]);
writeln(a[2, 1], ' ', a[2, 2], ' ', a[2, 3]);
writeln(a[3, 1], ' ', a[3, 2], ' ', a[3, 3]);
[i in 1..3] a[i, 1] = -3.0;
[i in 1..3] a[i, 2] = 3.0;
[i in 1..3] a[i, 3] = 3.0;
writeln('normal');
writeln(a[1, 1], ' ', a[1, 2], ' ', a[1, 3]);
writeln(a[2, 1], ' ', a[2, 2], ' ', a[2, 3]);
writeln(a[3, 1], ' ', a[3, 2], ' ', a[3, 3]);
a[1, 1] = -3.0;
a[2..3, 1] = -3.0; // <---------- SUB-SLICE
a[1..3, 2] = 3.0;
a[1..3, 3] = 3.0;
writeln('sub-sliced');
writeln(a[1, 1], ' ', a[1, 2], ' ', a[1, 3]);
writeln(a[2, 1], ' ', a[2, 2], ' ', a[2, 3]);
writeln(a[3, 1], ' ', a[3, 2], ' ', a[3, 3]);
}
It works as expected. If instead of a multidimensional array, you use an
array of arrays, the sub-slicing, i.e. slicing anything other than a full
column or row, fails.
Regards - Damian
Pacific Engineering Systems International, 277-279 Broadway, Glebe NSW 2037
Ph:+61-2-8571-0847 .. Fx:+61-2-9692-9623 | unsolicited email not wanted here
Views & opinions here are mine and not those of any past or present employer
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers