[Gretl-devel] argument type problem when 1x1 matrix is coerced to scalar

Sven Schreiber svetosch at gmx.net
Thu Jul 16 12:10:21 EDT 2015

while I was testing the SVAR addon I ran into a bug that has its roots 
in gretl. The code below shows the problem, namely that even though the 
function hey() officially returns a matrix, if it's a 1x1 matrix _and_ 
if the caller does not explicitly cast the output to a matrix type (with 
the 'matrix' alias for genr), then gretl puts the return value into a 
scalar type. And if the caller then wants to use this variable as a 
matrix (because that's what it's supposed to be), it fails.

So I think gretl should honor the return type of a user-defined function 
always. I know that sometimes gretl has to convert a 1x1 matrix to a 
scalar, for example if vec(a)'vec(a) is treated by the programmer as a 
number for further stuff. But somehow gretl does it prematurely here.


function void hu(matrix *m)
   print "ouf"
end function

function matrix hey(bool in)
   if in
     return zeros(2,2)
     return zeros(1,1)
end function

out2 = hey(1)	
hu(&out2)	# works

matrix out3 = hey(0)
hu(&out3)	# works

out = hey(0)
hu(&out)	# fails, wrong argument type

More information about the Gretl-devel mailing list