[Python-ideas] [RFC] draft PEP: Dedicated infix operators for matrix multiplication and matrix power

M.-A. Lemburg mal at egenix.com
Fri Mar 14 12:48:13 CET 2014


On 14.03.2014 12:25, Robert Kern wrote:
> On 2014-03-14 10:16, M.-A. Lemburg wrote:
>> 4. Another very common operation needed in vector/matrix calculation
>>     is transposition. This is usually written as superscript "T" or "t"
>>     ("ᵀ" in Unicode). Wouldn't this operator be needed as well, to
>>     make the picture complete ? OTOH, we currently don't have postfix
>>     operators in Python, so I guess writing this as A.transpose()
>>     comes close enough ;-)
> 
> Indeed. Numpy already uses a .T property for this.

Ah, good trick :-)

>> Now since this is all about syntactic sugar, we also need to look at
>> some code examples:
>>
>>
>> I == A @@ -1 @ A
>> vs.
>> I == A ·· -1 · A
>> vs.
>> I == A.inverse().dot(A)
>>
>>
>> (A @ B).transpose() == B.transpose() @ A.transpose()
>> vs.
>> (A · B).transpose() == B.transpose() · A.transpose()
>> vs.
>> A.dot(B).transpose() == B.transpose().dot(A.transpose())
> 
> (A @ B).T == B.T @ A.T
> (A · B).T == B.T · A.T
> A.dot(B).T == B.T.dot(A.T)
> 
> (FWIW, I didn't notice the math error until I wrote out the @ version.)

Thanks; I should have proofread the email before hitting the send button.

I've correct the quoted version above to have the comparisons
return True for all A and B instead of just for a select few :-)

>> c = A @ v
>> vs.
>> c = A · v
>> vs.
>> c = A.dot(v)
>>
>>
>> Hmm, even though I'd love to see matrix operators in Python,
>> I don't think they really add clarity to the syntax of matrix
>> calculations -  a bit disappointing, I must say :-(
> 
> Some more from real code:
> 
> RSR = R.dot(var_beta.dot(R.T))
> RSR = R @ var_beta @ R.T
> 
> xx_inv.dot(xeps.dot(xx_inv))
> xx_inv @ xeps @ xx_inv
> 
> dF2lower_dper.dot(F2lower.T) + F2lower.dot(dF2lower_dper.T) - 4/period*F2lower.dot(F2lower.T)
> dF2lower_dper @ F2lower.T + F2lower @ dF2lower_dper.T - 4/period*(F2lower @ F2lower.T)
> 
> dFX_dper.dot(Gi.dot(FX2.T)) - FX.dot(Gi.dot(dG_dper.dot(Gi.dot(FX2.T)))) + FX.dot(Gi.dot(dFX2_dper.T))
> (dFX_dper @ Gi @ FX2.T) - (FX @ Gi @ dG_dper @ Gi @ FX2.T) + (FX @ G @ dFX2_dper.T)
> 
> torient_inv.dot(tdof).dot(torient).dot(self.vertices[parent].meta['key']))
> (((torient_inv @ tdof) @ torient) @ self.vertices[parent].meta['key']

This doesn't look very readable to me - the operator saves you
a few parens in some situations, but as in the last example, it can
also require adding new ones.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Mar 14 2014)
>>> Python Projects, Consulting and Support ...   http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2014-03-29: PythonCamp 2014, Cologne, Germany ...          15 days to go
2014-04-09: PyCon 2014, Montreal, Canada ...               26 days to go

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/


More information about the Python-ideas mailing list