From murdoch.duncan at gmail.com  Fri Mar 18 11:24:08 2016
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Fri, 18 Mar 2016 06:24:08 -0400
Subject: [Rd] formatting of complex matrix
In-Reply-To: <691AF49D-88A3-4FE5-B66A-8F99347B0F74@oracle.com>
References: <691AF49D-88A3-4FE5-B66A-8F99347B0F74@oracle.com>
Message-ID: <56EBD748.204@gmail.com>

On 18/03/2016 6:09 AM, Lukas Stadler wrote:
> 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)) )

Thanks, I'll take a look.

Duncan Murdoch



[Rd] formatting of complex matrix
Duncan Murdoch