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

Robert Kern robert.kern at gmail.com
Mon Mar 26 13:00:55 EDT 2007


daniel.egloff at 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