Macaulay2 » Documentation
Packages » Python » PythonObject » PythonObject @ PythonObject » PythonObject @= Thing
next | previous | forward | backward | up | index | toc

# PythonObject @= Thing -- augmented matrix multiplication of Python objects

- Operator: @=
- Usage:
    `x @= y`
- Inputs:
  - x, a python object,
  - y, a thing,
- Outputs:
  - a python object, the product of x and y as matrices, assigned to x;

## Description

Multiply two Python objects (e.g., NumPy arrays) as matrices and assign the result to the first argument.

    i1 : installNumPyMethods();

    i2 : x = toPython matrix {{1, 2}, {3, 4}}

    o2 = [[1 2]
          [3 4]]

    o2 : PythonObject of class numpy.ndarray

    i3 : x @= toPython matrix {{5, 6}, {7, 8}}

    o3 = [[19 22]
          [43 50]]

    o3 : PythonObject of class numpy.ndarray

    i4 : x

    o4 = [[19 22]
         [43 50]]

    o4 : PythonObject of class numpy.ndarray

If the right-hand side is a Macaulay2 object, then it is first converted to a Python object before multiplying.

    i5 : x = toPython matrix {{1, 2}, {3, 4}}

    o5 = [[1 2]
          [3 4]]

    o5 : PythonObject of class numpy.ndarray

    i6 : x @= matrix {{8, 7}, {6, 5}}

    o6 = [[20 17]
          [48 41]]

    o6 : PythonObject of class numpy.ndarray

    i7 : x

    o7 = [[20 17]
          [48 41]]

    o7 : PythonObject of class numpy.ndarray

If the Python class of x defines an `__imatmul__` method for in-place matrix multiplication, then it will be called. Otherwise, x and y will be multiplied in the usual way, creating a new Python object that is assigned back to x.

For example, NumPy arrays support in-place matrix multiplication. In the examples above, x was modified directly, and no new objects were created.

## See also

- PythonObject @ PythonObject -- matrix multiplication of Python objects
- augmented assignment
- installNumPyMethods -- install NumPy methods

## Ways to use this method:

- PythonObject @= Thing -- augmented matrix multiplication of Python objects

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Python/doc/arithmetic.m2:375:0.