---
title: multiply
description: "将输入参数逐元素相乘。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC2/accel/kunpengaccel_kml_16_0521.html
sourcePath: /source/zh/kunpengboostkithistory/240RC2/accel/kunpengaccel_kml_16_0521.html
indexId: 052736a22159f630d8a4a07b2fd2109e623892f6cc4bba20f4b479faec54e18976
---
# multiply

#### 功能描述

将输入参数逐元素相乘。


#### 必选输入参数

| 参数名 | 类型 | 说明 |
| --- | --- | --- |
| x1，x2 | array\_like | 相乘的数组或标量，如果x1.shape!=x2.shape，则它们必须能广播至相同的形状。 |


#### 可选输入参数

| 参数名 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| out | ndarray/ndarray的元组 | None | 计算结果保存的位置。如果提供，其形状必须与输入数组广播后的形状一致。未指定时，返回一个新的数组。 |
| where | array\_like | None | 将被广播至输入的条件数组。在条件为真处的元素，其计算结果会存入out中，其余位置保留原有值。 |
| \*\*kwargs | \- | \- | 其他关键字参数，请参见NumPy官方文档Universal functions (ufunc)(https://numpy.org/doc/stable/reference/ufuncs.html\#ufuncs\-kwargs)。 |


#### 返回数据

| 类型 | 说明 |
| --- | --- |
| ndarray/scalar | x1和x2逐元素相乘的结果。 |


#### 示例

```
>>> import numpy as np
>>> np.multiply(1.1, 5.5)
6.050000000000001
>>> x1 = np.arange(9.0).reshape((3,3))
>>> x2 = np.arange(3.0)
>>> x1
array([[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]])
>>> x2
array([0., 1., 2.])
>>>
>>> np.multiply(x1, x2)
array([[ 0.,  1.,  4.],
[ 0.,  4., 10.],
[ 0.,  7., 16.]])
>>
#  若x1和x2均为ndarray，那么还可以使用“*”来替代np.multiply
>>> x1 = np.arange(0, 4).reshape((2,2))
>>> x1
array([[0, 1],
[2, 3]])
>>> x2 = np.arange(4, 8).reshape((2,2))
>>> x2
array([[4, 5],
[6, 7]])
>>>
>>> x1 * x2
array([[ 0,  5],
[12, 21]])
>>>
```
