[Numpy-discussion] fastest way to do multiplication with diagonal matrices from left or right

Robert Kern robert.kern@gmail....
Mon Mar 26 12:00:55 CDT 2007


daniel.egloff@zkb.ch wrote:
> Dear list
> 
> what is the fastet way to multiply with a diagonal matrix from left or
> right and without to build a square matrix from the diagonal.

Use broadcasting to do your work for you.

  from numpy import array, newaxis

  diags = array([...])
  mymatrix = array([[...]])

  # From the right:
  mymatrix * diags

  # From the left:
  diags[:,newaxis] * mymatrix

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list