Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CN doc for matrix_power #3727

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/paddle/Overview_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ tensor线性代数相关
" :ref:`paddle.dot <cn_api_paddle_tensor_linalg_dot>` ", "计算向量的内积"
" :ref:`paddle.histogram <cn_api_tensor_histogram>` ", "计算输入张量的直方图"
" :ref:`paddle.matmul <cn_api_tensor_matmul>` ", "计算两个Tensor的乘积,遵循完整的广播规则"
" :ref:`paddle.matrix_power <cn_api_tensor_matrix_power>` ", "计算一个(或一批)方阵的 n 次幂"
" :ref:`paddle.mv <cn_api_tensor_mv>` ", "计算矩阵 x 和向量 vec 的乘积"
" :ref:`paddle.norm <cn_api_tensor_norm>` ", "计算给定Tensor的矩阵范数(Frobenius 范数)和向量范数(向量1范数、2范数、或者通常的p范数)"
" :ref:`paddle.rank <cn_api_fluid_layers_rank>` ", "计算输入Tensor的维度(秩)"
Expand Down
9 changes: 9 additions & 0 deletions docs/api/paddle/Tensor_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,15 @@ matmul(y, transpose_x=False, transpose_y=False, name=None)

请参考 :ref:`cn_api_tensor_matmul`

matrix_power(x, n, name=None)
:::::::::

返回:经过矩阵幂运算后的Tensor

返回类型:Tensor

请参考 :ref:`cn_api_tensor_matrix_power`

max(axis=None, keepdim=False, name=None)
:::::::::

Expand Down
57 changes: 57 additions & 0 deletions docs/api/paddle/matrix_power_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.. _cn_api_tensor_matrix_power:

matrix_power
-------------------------------

.. py:function:: paddle.matrix_power(x, n, name=None)


计算一个或一批方阵的 ``n`` 次幂。

记 :math:`X` 为一个或一批方阵,:math:`n` 为幂次,则公式为:

.. math::
Out = X ^ {n}

特别地,

- 如果 ``n > 0`` , 则返回计算 ``n`` 次幂后的一个或一批矩阵。

- 如果 ``n = 0`` , 则返回一个或一批单位矩阵。

- 如果 ``n < 0`` , 则返回每个矩阵的逆(若矩阵可逆)的 ``abs(n)`` 次幂。

参数
:::::::::
- **x** (Tensor) : 输入的欲进行 ``n`` 次幂运算的一个或一批方阵, 类型为 Tensor。 ``x`` 的形状应为 ``[*, M, M]``, 其中 ``*`` 为零或更大的批次维度, 数据类型为float32, float64。
- **n** (int) : 输入的幂次, 类型为 int。 它可以是任意整数。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name` ,一般无需设置,默认值为None。

返回:
:::::::::
- Tensor, 这个(或这批)矩阵 ``x`` 经过 ``n`` 次幂运算后的结果, 数据类型和输入 ``x`` 的一致。

代码示例
::::::::::

.. code-block:: python

import paddle

x = paddle.to_tensor([[1, 2, 3],
[1, 4, 9],
[1, 8, 27]], dtype='float64')
print(paddle.matrix_power(x, 2))
# [[6. , 34. , 102.],
# [14. , 90. , 282.],
# [36. , 250., 804.]]

print(paddle.matrix_power(x, 0))
# [[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]]

print(paddle.matrix_power(x, -2))
# [[ 12.91666667, -12.75000000, 2.83333333 ],
# [-7.66666667 , 8. , -1.83333333 ],
# [ 1.80555556 , -1.91666667 , 0.44444444 ]]