Skip to content

diagflat() yields incorrect results for large vectors on Windows10 #18449

@markwainger1

Description

@markwainger1

matrx= np.diagflat(vect) should always generate a symmetric matrix with the input vector on the diagonal and zeros everywhere else. For vectors larger than length 46340 on Windows10, the matrix has some off-diagonal elements filled with random numbers. This behavior reliably happens on multiple PCs running Windows10, and does not ever happen on Linux.

Reproducing code example:

import numpy as np
for k in range(46339,46343):
        zzz= np.random.randn(1,k)
        print("zzz ",zzz.shape)
        yyy= np.diagflat(zzz)
        okay= np.all(yyy==yyy.T)
        print("yyy ",yyy.shape,", npall=",okay)

Error message:

zzz (1, 46339)
yyy (46339, 46339) , npall= True
zzz (1, 46340)
yyy (46340, 46340) , npall= True
zzz (1, 46341)
yyy (46341, 46341) , npall= False
zzz (1, 46342)
yyy (46342, 46342) , npall= False

Result of np.all(yyy == yyy.T) should always be True. The random non-zero entries always start appearing at the same locations in the output matrix. For the case of k=46341, the nonzero elements with lowest [i,j] are:

i= 14232 j= 16472 yyy[i,j]= 1.6592703450619213 , yyy[j,i]= 0.0
i= 14233 j= 16473 yyy[i,j]= -1.8024626780833979 , yyy[j,i]= 0.0
i= 14234 j= 16474 yyy[i,j]= 2.5861122962016725 , yyy[j,i]= 0.0
i= 14235 j= 16475 yyy[i,j]= 1.7072455932249944 , yyy[j,i]= 0.0
i= 14236 j= 16476 yyy[i,j]= 1.1587226504308519 , yyy[j,i]= 0.0
i= 14237 j= 16477 yyy[i,j]= 0.5920055429795094 , yyy[j,i]= 0.0
i= 14238 j= 16478 yyy[i,j]= 0.38220918166731577 , yyy[j,i]= 0.0
i= 14239 j= 16479 yyy[i,j]= -0.02773795748618439 , yyy[j,i]= 0.0

NumPy/Python version information:

1.20.1 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions