[Rd] formatting of complex matrix

Lukas Stadler lukas.stadler at oracle.com
Fri Mar 18 11:09:56 CET 2016


While working on the printing code, my colleague Zbyněk Šlajchrt noticed that complex matrixes are sometimes misaligned:

> { matrix(1i,2,13) }
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,] 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i
[2,] 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i 0+1i

(the values in the last four columns should be prefixed by two spaces instead of one)
while the formatting is fine, e.g., for real values:

> { matrix(1,2,13) }
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,]    1    1    1    1    1    1    1    1    1     1     1     1     1
[2,]    1    1    1    1    1    1    1    1    1     1     1     1     1

The problem seems to be in printarray.c:275 (https://github.com/wch/r-source/blob/trunk/src/main/printarray.c#L275):
EncodeComplex(x[i + j * r],
    wr[j] + R_print.gap, dr[j], er[j],
    wi[j], di[j], ei[j], OutDec)) )

The width of the real part wr[j] + the width of the imaginary part wi[j] + R_print.gap doesn’t always add up to the width of the column w[j].

As far as we can see, calculating the width of the real part on the fly fixes the problem:
EncodeComplex(x[i + j * r],
    w[j] - wi[j] - 2, dr[j], er[j],
    wi[j], di[j], ei[j], OutDec)) )

Regards,
 Lukas
	[[alternative HTML version deleted]]



More information about the R-devel mailing list