2

I recently came across a code snippet which is very similar to the one given below :

def abc(a,b,c):
    a1 = a[:, :1]
    b1 = b[:1, :]
    c1 = c[:1, :]
   
    a2 = a1.conj().transpose()
    b2 = c1.conj().transpose()
   
    d = np.linalg.inv(np.sqrt(b1))
   
    e = d @ a2 @ b @ b2 @ d
   
    return e

a,b,c are numpy arrays.

I am trying to understand python decorators and learned a bit from this question.

However I am not able to figure out how the variable e is defined. What exactly is happening ?

I am a beginner in Python. As far as my knowledge, decorators wrap around a function and functions are passed as arguments. But here, these are all numpy arrays.

Any explanation to what exactly is happening when variable e is defined or what is meant by the specific index with multiple decorators in a single line would be very helpful.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    `@` is not decorator here. It's operator - matrix multiplication https://docs.python.org/3/library/operator.html#operator.matmul – Andrej Kesely Oct 28 '20 at 12:37
  • Oh okay. Thanks. Is it equivalent to using np.matmul() ? Or is it just a recommended syntax? – CasualPythoner Oct 28 '20 at 12:43
  • Also, should I just answer my own question or delete it , since it is a wrong question unintentionally? Sorry, new to stack overflow. – CasualPythoner Oct 28 '20 at 12:46
  • Yes, it's like matmul. From [numpy documentation](https://numpy.org/doc/stable/reference/generated/numpy.matmul.html): `The matmul function implements the semantics of the @ operator introduced in Python 3.5 following PEP465.` But personally, I would use `np.matmul` function as it's clearer – Andrej Kesely Oct 28 '20 at 12:46
  • Thanks a lot for your comments. This cleared everything up. – CasualPythoner Oct 28 '20 at 12:46

1 Answers1

3

The line e = d @ a2 @ b @ b2 @ d is not about Python decorators. The @ character here is Python @ operator: https://docs.python.org/3/library/operator.html#operator.matmul

LInk to PEP-465: https://www.python.org/dev/peps/pep-0465/

From numpy documentation link:

The matmul function implements the semantics of the @ operator introduced in Python 3.5 following PEP465.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91