Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.matmul

numpy.matmul(a,b,out=None)

Matrix product of two arrays.

The behavior depends on the arguments in the following way.

  • If both arguments are 2-D they are multiplied like conventionalmatrices.
  • If either argument is N-D, N > 2, it is treated as a stack ofmatrices residing in the last two indexes and broadcast accordingly.
  • If the first argument is 1-D, it is promoted to a matrix byprepending a 1 to its dimensions. After matrix multiplicationthe prepended 1 is removed.
  • If the second argument is 1-D, it is promoted to a matrix byappending a 1 to its dimensions. After matrix multiplicationthe appended 1 is removed.

Multiplication by a scalar is not allowed, use* instead. Note thatmultiplying a stack of matrices with a vector will result in a stack ofvectors, but matmul will not recognize it as such.

matmul differs fromdot in two important ways.

  • Multiplication by scalars is not allowed.
  • Stacks of matrices are broadcast together as if the matriceswere elements.

Warning

This function is preliminary and included in NumPy 1.10.0 for testingand documentation. Its semantics will not change, but the number andorder of the optional arguments will.

New in version 1.10.0.

Parameters:
a:array_like

First argument.

b:array_like

Second argument.

out:ndarray, optional

Output argument. This must have the exact kind that would be returnedif it was not used. In particular, it must have the right type, must beC-contiguous, and its dtype must be the dtype that would be returnedfordot(a,b). This is a performance feature. Therefore, if theseconditions are not met, an exception is raised, instead of attemptingto be flexible.

Returns:
output:ndarray

Returns the dot product ofa andb. Ifa andb are both1-D arrays then a scalar is returned; otherwise an array isreturned. Ifout is given, then it is returned.

Raises:
ValueError

If the last dimension ofa is not the same size asthe second-to-last dimension ofb.

If scalar value is passed.

See also

vdot
Complex-conjugating dot product.
tensordot
Sum products over arbitrary axes.
einsum
Einstein summation convention.
dot
alternative matrix product with different broadcasting rules.

Notes

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

Examples

For 2-D arrays it is the matrix product:

>>>a=[[1,0],[0,1]]>>>b=[[4,1],[2,2]]>>>np.matmul(a,b)array([[4, 1],       [2, 2]])

For 2-D mixed with 1-D, the result is the usual.

>>>a=[[1,0],[0,1]]>>>b=[1,2]>>>np.matmul(a,b)array([1, 2])>>>np.matmul(b,a)array([1, 2])

Broadcasting is conventional for stacks of arrays

>>>a=np.arange(2*2*4).reshape((2,2,4))>>>b=np.arange(2*2*4).reshape((2,4,2))>>>np.matmul(a,b).shape(2, 2, 2)>>>np.matmul(a,b)[0,1,1]98>>>sum(a[0,1,:]*b[0,:,1])98

Vector, vector returns the scalar inner product, but neither argumentis complex-conjugated:

>>>np.matmul([2j,3j],[2j,3j])(-13+0j)

Scalar multiplication raises an error.

>>>np.matmul([1,2],3)Traceback (most recent call last):...ValueError:Scalar operands are not allowed, use '*' instead

Previous topic

numpy.outer

Next topic

numpy.tensordot

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp