Numpy dot too clever about symmetric multiplications

后端 未结 2 1114
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 14:33

Anybody know about documentation for this behaviour?

import numpy as np
A  = np.random.uniform(0,1,(10,5))
w  = np.ones(5)
Aw = A*w
Sym1 = Aw.dot(Aw.T)
Sym2          


        
2条回答
  •  猫巷女王i
    2020-12-05 14:56

    I suspect this is to do with promotion of intermediate floating point registers to 80 bit precision. Somewhat confirming this hypothesis is that if we use fewer floats we consistently get 0 in our results, ala

    A  = np.random.uniform(0,1,(4,2))
    w  = np.ones(2)
    Aw = A*w
    Sym1 = Aw.dot(Aw.T)
    Sym2 = (A*w).dot((A*w).T)
    diff = Sym1 - Sym2
    # diff is all 0's (ymmv)
    

提交回复
热议问题