linalg.py 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#  Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ..helper import is_complex, is_real, complex_variable_exists
16 17
from ....fluid.framework import ComplexVariable
from ....fluid import layers
18 19 20 21 22 23 24 25 26 27 28

__all__ = ['matmul', ]


def matmul(x, y, transpose_x=False, transpose_y=False, alpha=1.0, name=None):
    """
    Applies matrix multiplication to two complex number tensors. See the 
    detailed description in :ref:`api_fluid_layers_matmul`.

    Args:
        x (ComplexVariable|Variable): The first input, can be a ComplexVariable 
29
            with data type complex64 or complex128, or a Variable with data type 
30 31
            float32 or float64.
        y (ComplexVariable|Variable): The second input, can be a ComplexVariable 
32
            with data type complex64 or complex128, or a Variable with data type 
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
            float32 or float64.
        transpose_x (bool): Whether to transpose :math:`x` before multiplication.
        transpose_y (bool): Whether to transpose :math:`y` before multiplication.
        alpha (float): The scale of output. Default 1.0.
        name(str|None): A name for this layer(optional). If set None, the layer
            will be named automatically.
   
    Returns:
        ComplexVariable: The product result, with the same data type as inputs.

    Examples:
        .. code-block:: python

            import numpy as np
            import paddle
            import paddle.fluid.dygraph as dg
            with dg.guard():
                x = np.array([[1.0 + 1j, 2.0 + 1j], [3.0+1j, 4.0+1j]])
                y = np.array([1.0 + 1j, 1.0 + 1j])
                x_var = dg.to_variable(x)
                y_var = dg.to_variable(y)
                result = paddle.complex.matmul(x_var, y_var)
                print(result.numpy())
                # [1.+5.j 5.+9.j]         
    """
    # x = a + bi, y = c + di
59
    # P1 = ac; P2 = (a + b)(c + d); P3 = bd; then mm(x, y) = (P1-P3) + (P2-P1-P3)j
60 61 62
    complex_variable_exists([x, y], "matmul")
    a, b = (x.real, x.imag) if is_complex(x) else (x, None)
    c, d = (y.real, y.imag) if is_complex(y) else (y, None)
63
    P1 = layers.matmul(a, c, transpose_x, transpose_y, alpha, name)
64
    if is_real(b) and is_real(d):
65 66 67 68
        P2 = layers.matmul(a + b, c + d, transpose_x, transpose_y, alpha, name)
        P3 = layers.matmul(b, d, transpose_x, transpose_y, alpha, name)
        real = P1 - P3
        imag = P2 - P1 - P3
69
    elif is_real(b):
70
        real = P1
71 72
        imag = layers.matmul(b, c, transpose_x, transpose_y, alpha, name)
    else:
73
        real = P1
74 75
        imag = layers.matmul(a, d, transpose_x, transpose_y, alpha, name)
    return ComplexVariable(real, imag)