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

daniel.egloff@z... daniel.egloff@z...
Fri Mar 23 09:13:04 CDT 2007



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.
Here it what I am looking for:

import numpy as N

def diagmult(X, Y):
    """
    Matrix multiplication X*Y where either X or Y is a diagonal matrix.
    """
    if X.ndim == 1 and Y.ndim == 2:
        R = Y.copy()
        for i, d in enumerate(X):
            R[i,:] *= d
        return R
    elif X.ndim == 2 and Y.ndim == 1:
        R = X.copy()
        for i, d in enumerate(Y):
            R[:,i] *= d
        return R
    elif X.ndim == 1 and Y.ndim == 1:
        return X*Y
    else
        raise ValueError('diagmult dimension mismatch X.ndim = %d, Y.ndim =
%d' % (X.ndim, Y.ndim))

Freundliche Grüsse
Daniel Egloff
Zürcher Kantonalbank
Leiter(in) Financial Computing, ZEF

Josefstrasse 222, 8005 Zürich
Telefon 044 292 45 33, Fax 044 292 45 95
Briefadresse: Postfach, 8010 Zürich, http://www.zkb.ch
___________________________________________________________________

Disclaimer:


Diese Mitteilung ist nur fuer die Empfaengerin / den Empfaenger bestimmt.

Fuer den Fall, dass sie von nichtberechtigten Personen empfangen wird,
bitten wir diese hoeflich, die Mitteilung an die ZKB zurueckzusenden und
anschliessend die Mitteilung mit allen Anhaengen sowie allfaellige Kopien
zu vernichten bzw. zu loeschen. Der Gebrauch der Information ist verboten.


This message is intended only for the named recipient and may contain
confidential or privileged information.

If you have received it in error, please advise the sender by return e-mail
and delete this message and any attachments. Any unauthorised use or
dissemination of this information is strictly prohibited.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070323/91ccae95/attachment-0001.html 


More information about the Numpy-discussion mailing list