def kroneckerproduct(a,b): '''Computes a otimes b where otimes is the Kronecker product operator. Note: the Kronecker product is also known as the matrix direct product or tensor product. It is defined as follows for 2D arrays a and b where shape(a)=(m,n) and shape(b)=(p,q): c = a otimes b => cij = a[i,j]*b where cij is the ij-th submatrix of c. So shape(c)=(m*p,n*q). >>> print kroneckerproduct([[1,2]],[[3],[4]]) [[3 6] [4 8]] >>> print kroneckerproduct([[1,2]],[[3,4]]) [ [3 4 6 8]] >>> print kroneckerproduct([[1],[2]],[[3],[4]]) [[3] [4] [6] [8]] ''' a, b = _na.asarray(a), _na.asarray(b) if not (len(a.shape)==2 and len(b.shape)==2): raise ValueError, 'Input must be 2D arrays.' if not a.iscontiguous(): a = _gen.reshape(a, a.shape) if not b.iscontiguous(): b = _gen.reshape(b, b.shape) o = outerproduct(a,b) o.shape = a.shape + b.shape return _gen.concatenate(_gen.concatenate(o, axis=1), axis=1)
def kroneckerproduct(a, b): '''Computes a otimes b where otimes is the Kronecker product operator. Note: the Kronecker product is also known as the matrix direct product or tensor product. It is defined as follows for 2D arrays a and b where shape(a)=(m,n) and shape(b)=(p,q): c = a otimes b => cij = a[i,j]*b where cij is the ij-th submatrix of c. So shape(c)=(m*p,n*q). >>> print kroneckerproduct([[1,2]],[[3],[4]]) [[3 6] [4 8]] >>> print kroneckerproduct([[1,2]],[[3,4]]) [ [3 4 6 8]] >>> print kroneckerproduct([[1],[2]],[[3],[4]]) [[3] [4] [6] [8]] ''' a, b = _na.asarray(a), _na.asarray(b) if not (len(a.shape) == 2 and len(b.shape) == 2): raise ValueError, 'Input must be 2D arrays.' if not a.iscontiguous(): a = _gen.reshape(a, a.shape) if not b.iscontiguous(): b = _gen.reshape(b, b.shape) o = outerproduct(a, b) o.shape = a.shape + b.shape return _gen.concatenate(_gen.concatenate(o, axis=1), axis=1)
def outerproduct(array1, array2): """outerproduct(array1, array2) computes the NxM outerproduct of N vector 'array1' and M vector 'array2', where result[i,j] = array1[i]*array2[j]. """ array1=_gen.reshape( _na.asarray(array1), (-1,1)) # ravel array1 into an Nx1 array2=_gen.reshape( _na.asarray(array2), (1,-1)) # ravel array2 into a 1xM return matrixmultiply(array1,array2) # return NxM result
def outerproduct(array1, array2): """outerproduct(array1, array2) computes the NxM outerproduct of N vector 'array1' and M vector 'array2', where result[i,j] = array1[i]*array2[j]. """ array1 = _gen.reshape(_na.asarray(array1), (-1, 1)) # ravel array1 into an Nx1 array2 = _gen.reshape(_na.asarray(array2), (1, -1)) # ravel array2 into a 1xM return matrixmultiply(array1, array2) # return NxM result
def tensormultiply(array1, array2): """tensormultiply returns the product for any rank >=1 arrays, defined as: r_{xxx, yyy} = \sum_k array1_{xxx, k} array2_{k, yyyy} where xxx, yyy denote the rest of the a and b dimensions. """ array1, array2 = _na.asarray(array1), _na.asarray(array2) if array1.shape[-1] != array2.shape[0]: raise ValueError, "Unmatched dimensions" shape = array1.shape[:-1] + array2.shape[1:] return _gen.reshape(dot(_gen.reshape(array1, (-1, array1.shape[-1])), _gen.reshape(array2, (array2.shape[0], -1))), shape)
def tensormultiply(array1, array2): """tensormultiply returns the product for any rank >=1 arrays, defined as: r_{xxx, yyy} = \sum_k array1_{xxx, k} array2_{k, yyyy} where xxx, yyy denote the rest of the a and b dimensions. """ array1, array2 = _na.asarray(array1), _na.asarray(array2) if array1.shape[-1] != array2.shape[0]: raise ValueError, "Unmatched dimensions" shape = array1.shape[:-1] + array2.shape[1:] return _gen.reshape( dot(_gen.reshape(array1, (-1, array1.shape[-1])), _gen.reshape(array2, (array2.shape[0], -1))), shape)