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

Nathaniel Smith njs at pobox.com
Fri Mar 14 16:00:02 CET 2014


On Fri, Mar 14, 2014 at 11:25 AM, Robert Kern <robert.kern at gmail.com> wrote:
> On 2014-03-14 10:16, M.-A. Lemburg wrote:
>> Now since this is all about syntactic sugar, we also need to look at
>> some code examples:
[...]
>> 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']

For those skimming along, note that there's also a more detailed
example in the draft PEP, looking at a few different aspects of code
usability:
  https://github.com/numpy/numpy/pull/4351/files#diff-dd521035cec59cd5fb2e040f0703742bR230

-n


More information about the Python-ideas mailing list