From lukas.stadler at oracle.com  Fri Mar 18 11:09:56 2016
From: lukas.stadler at oracle.com (Lukas Stadler)
Date: Fri, 18 Mar 2016 11:09:56 +0100
Subject: [Rd] formatting of complex matrix
Message-ID: <691AF49D-88A3-4FE5-B66A-8F99347B0F74@oracle.com>

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]]



[Rd] formatting of complex matrix
Lukas Stadler