Linear Algebra

Matrix and vector products

cupy.dot(a, b, out=None)[source]

Returns a dot product of two arrays.

For arrays with more than one axis, it computes the dot product along the last axis of a and the second-to-last axis of b. This is just a matrix product if the both arrays are 2-D. For 1-D arrays, it uses their unique axis as an axis to take dot product over.

Parameters:
Returns:

The dot product of a and b.

Return type:

cupy.ndarray

See also

numpy.dot()

cupy.vdot(a, b)[source]

Returns the dot product of two vectors.

The input arrays are flattened into 1-D vectors and then it performs inner product of these vectors.

Parameters:
Returns:

Zero-dimensional array of the dot product result.

Return type:

cupy.ndarray

See also

numpy.vdot()

cupy.inner(a, b)[source]

Returns the inner product of two arrays.

It uses the last axis of each argument to take sum product.

Parameters:
Returns:

The inner product of a and b.

Return type:

cupy.ndarray

See also

numpy.inner()

cupy.outer(a, b, out=None)[source]

Returns the outer product of two vectors.

The input arrays are flattened into 1-D vectors and then it performs outer product of these vectors.

Parameters:
Returns:

2-D array of the outer product of a and b.

Return type:

cupy.ndarray

See also

numpy.outer()

cupy.tensordot(a, b, axes=2)[source]

Returns the tensor dot product of two arrays along specified axes.

This is equivalent to compute dot product along the specified axes which are treated as one axis by reshaping.

Parameters:
  • a (cupy.ndarray) – The first argument.
  • b (cupy.ndarray) – The second argument.
  • axes
    • If it is an integer, then axes axes at the last of a and the first of b are used.
    • If it is a pair of sequences of integers, then these two sequences specify the list of axes for a and b. The corresponding axes are paired for sum-product.
  • out (cupy.ndarray) – Output array.
Returns:

The tensor dot product of a and b along the axes specified by axes.

Return type:

cupy.ndarray

Decompositions

cupy.linalg.cholesky(a)[source]

Cholesky decomposition.

Decompose a given two-dimensional square matrix into L * L.T, where L is a lower-triangular matrix and .T is a conjugate transpose operator. Note that in the current implementation a must be a real matrix, and only float32 and float64 are supported.

Parameters:a (cupy.ndarray) – The input matrix with dimension (N, N)
cupy.linalg.qr(a, mode='reduced')[source]

QR decomposition.

Decompose a given two-dimensional matrix into Q * R, where Q is an orthonormal and R is an upper-triangular matrix.

Parameters:
  • a (cupy.ndarray) – The input matrix.
  • mode (str) – The mode of decomposition. Currently ‘reduced’, ‘complete’, ‘r’, and ‘raw’ modes are supported. The default mode is ‘reduced’, and decompose a matrix A = (M, N) into Q, R with dimensions (M, K), (K, N), where K = min(M, N).
cupy.linalg.svd(a, full_matrices=True, compute_uv=True)[source]

Singular Value Decomposition.

Factorizes the matrix a as u * np.diag(s) * v, where u and v are unitary and s is an one-dimensional array of a‘s singular values.

Parameters:
  • a (cupy.ndarray) – The input matrix with dimension (M, N).
  • full_matrices (bool) – If True, it returns U and V with dimensions (M, M) and (N, N). Otherwise, the dimensions of U and V are respectively (M, K) and (K, N), where K = min(M, N).
  • compute_uv (bool) – If True, it only returns singular values.

Norms etc.

cupy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)[source]

Returns the sum along the diagonals of an array.

It computes the sum along the diagonals at axis1 and axis2.

Parameters:
  • a (cupy.ndarray) – Array to take trace.
  • offset (int) – Index of diagonals. Zero indicates the main diagonal, a positive value an upper diagonal, and a negative value a lower diagonal.
  • axis1 (int) – The first axis along which the trace is taken.
  • axis2 (int) – The second axis along which the trace is taken.
  • dtype – Data type specifier of the output.
  • out (cupy.ndarray) – Output array.
Returns:

The trace of a along axes (axis1, axis2).

Return type:

cupy.ndarray

See also

numpy.trace()