Numpy Articles

Page 25 of 81

Raise a square matrix to the power n in Linear Algebra in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To raise a square matrix to the power n in Linear Algebra, use the numpy.linalg.matrix_power() function in Python. For positive integers n, the power is computed by repeated matrix squarings and matrix multiplications. If n == 0, the identity matrix of the same shape is returned. If n < 0, the inverse is computed and then raised to the abs(n). Syntax numpy.linalg.matrix_power(a, n) Parameters The function accepts the following parameters ? a − A square matrix to be raised to the power n − The exponent that can be any integer (positive, ...

Read More

Evaluate the lowest cost contraction order for an einsum expression in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 239 Views

To find the optimal contraction order for an einsum expression, use numpy.einsum_path(). This function analyzes different ways to perform the tensor contractions and returns the most efficient path to minimize computational cost. What is einsum_path()? The einsum_path() function evaluates different contraction orders for Einstein summation operations. It returns a tuple containing the optimal contraction path and detailed information about the optimization process. Syntax numpy.einsum_path(subscripts, *operands, optimize='greedy') Parameters subscripts − String specifying the subscripts for summation using Einstein notation operands − Input arrays for the operation optimize − Optimization strategy ('greedy', 'optimal', ...

Read More

Tensor contraction with Einstein summation convention in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 475 Views

Tensor contraction with Einstein summation convention is a powerful technique for performing complex multi-dimensional array operations. Python's numpy.einsum() method provides an elegant way to implement tensor contractions using subscript notation. Understanding Einstein Summation The Einstein summation convention allows you to represent complex tensor operations using subscript labels. When indices appear in both input tensors but not in the output, they are automatically summed over (contracted). Syntax numpy.einsum(subscripts, *operands) Parameters: subscripts − String specifying the subscripts for summation as comma-separated labels operands − Input arrays for the operation Example: Tensor Contraction ...

Read More

Vector outer product with Einstein summation convention in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 674 Views

The outer product of two vectors creates a matrix where each element is the product of corresponding elements from the input vectors. NumPy's einsum() function provides an elegant way to compute outer products using Einstein summation notation. Understanding Einstein Summation The Einstein summation convention uses subscripts to specify which dimensions to multiply and sum. For outer products, we use the notation 'i, j' where i and j represent different dimensions that remain separate (no summation). Basic Outer Product Example import numpy as np # Create two vectors vector1 = np.array([1, 2]) vector2 = np.array([0, ...

Read More

Matrix Vector multiplication with Einstein summation convention in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 777 Views

Matrix Vector multiplication with Einstein summation convention uses the numpy.einsum() method in Python. This method evaluates the Einstein summation convention on operands, allowing many common multi-dimensional linear algebraic operations to be represented in a simple fashion. Syntax numpy.einsum(subscripts, operands) Parameters: subscripts − String specifying the subscripts for summation as comma separated list of subscript labels operands − Arrays for the operation Understanding Einstein Summation For matrix-vector multiplication, the notation 'ij, j' means: i represents rows of the matrix j represents columns of the matrix and elements of the ...

Read More

Vector inner product with Einstein summation convention in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 405 Views

To compute the inner product of vectors with Einstein summation convention, use the numpy.einsum() method in Python. The first parameter is the subscript string that specifies the summation pattern, and the second parameter contains the arrays for the operation. The einsum() method evaluates the Einstein summation convention on the operands. Using the Einstein summation convention, many common multi-dimensional, linear algebraic array operations can be represented in a simple fashion. In implicit mode einsum computes these values automatically. In explicit mode, einsum provides further flexibility to compute other array operations that might not be considered classical Einstein summation operations, ...

Read More

Return the cumulative product treating NaNs as one but change the type of result in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 216 Views

To return the cumulative product of array elements over a given axis treating NaNs as one, use the nancumprod() method. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. Ones are returned for slices that are all-NaN or empty. The method returns a new array holding the result unless out is specified. Cumulative works like: 5, 5*10, 5*10*15, 5*10*15*20. The dtype parameter allows you to change the type of the returned array from the original array's data type. Syntax numpy.nancumprod(a, axis=None, dtype=None, out=None) Parameters a: ...

Read More

Compute the tensor dot product in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

The tensor dot product is a generalization of matrix multiplication that allows you to sum products of tensor elements over specified axes. NumPy's tensordot() function computes this operation by taking two tensors and summing their products over the axes you specify. Syntax numpy.tensordot(a, b, axes=2) Parameters a, b: Input tensors to compute the dot product axes: Can be an integer N (sum over last N axes of a and first N axes of b) or a tuple of two arrays specifying which axes to sum over Basic Example Let's start with a ...

Read More

Get the Outer product of an array and a scalar in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 288 Views

To get the outer product of an array and a scalar, use the numpy.outer() method in Python. The outer product creates a matrix where each element of the first input is multiplied by each element of the second input. Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product is ? [[a0*b0 a0*b1 ... a0*bN ] [a1*b0 a1*b1 ... a1*bN ] [ ... ... ... ... ] [aM*b0 aM*b1 ... aM*bN ]] Syntax numpy.outer(a, b, out=None) ...

Read More

Get the Outer Product of an array with vector of letters in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 191 Views

The outer product of two vectors creates a matrix where each element is the product of corresponding elements from the input vectors. When working with letters and numbers, NumPy treats the operation as string repetition. Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product is − [[a0*b0 a0*b1 ... a0*bN ] [a1*b0 . [ ... . [aM*b0 aM*bN ]] Syntax To get the outer product of an array with vector of letters, use the numpy.outer() method − ...

Read More
Showing 241–250 of 802 articles
« Prev 1 23 24 25 26 27 81 Next »
Advertisements