Am 06.03.21 um 23:14 schrieb Allin Cottrell:
On Sat, 6 Mar 2021, Artur Tarassow wrote:
> Hi,
>
> I just found that an "exported" 1by1 matrix becomes a scalar once it
> is a bundle member.
It's a matrix as a bundle member, but it becomes a scalar when extracted
with no type specifier.
<hansl>
matrix m = {7}
bundle b
b.m = m
eval typeof(b.m)
x = b.m
eval typeof(x)
</hansl>
Ah, I think understand the problem now. So the "default" data type of a
1-by-1 matrix is "scalar" as long as the data type is not explicitly
declared, e.g. as in <matrix x = b.m>.
> I guess that's a bug as the data type changes but I may be
wrong.
I'd say it's a bug only if the extracted scalar won't function as if it
were a 1x1 matrix. I think that's covered now, but maybe not in all cases?
I wanted to do some assertion of individual vector values. Here is a
simplified toy example (in my application I have a more general
architecture avoiding those two loops) but it illustrates the "issue".
So, the point is in looping through all vector values by indexing each
value. However, indexing a scalar (of what I though is a matrix) is (of
course) not possible. Thus, the script fails here. The solution is to
test the data type of "m*" and deciding whether one needs a loop or not.
<hansl>
clear
set verbose
matrix m1 = mnormal(1, 1)
matrix m2 = mnormal(2, 1)
bundle B = _(m1, m2)
matrix expected_m1 = {-3.2}
matrix expected_m2 = {-3.2; 5}
print "Compare m2 values"
loop i=1..rows(expected_m1)
assert(B.m2[i] == expected_m2[i])
endloop
# FAILS as indexing for the actual scalar m1 is not possible
print "Compare m1 values"
loop i=1..rows(expected_m1)
assert(B.m1[i] == expected_m1[i])
endloop
print "finished"
</hansl>
But I don't want to push this too far. It the spec is currently such
that <x = b.m> becomes a by default a scalar (as long as not explicitly
declared as a "matrix" type), this is fine as long as it is intended by
the specs ;-)
Artur