Example #1
0
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)
Example #2
0
def _leading_trailing(a):
    if a.getrank() == 1:
        if len(a) > 2*_summaryEdgeItems:
            b = _gen.concatenate((a[:_summaryEdgeItems],
                                     a[-_summaryEdgeItems:]))
        else:
            b = a
    else:
        if len(a) > 2*_summaryEdgeItems:
            l = [_leading_trailing(a[i]) for i in range(
                min(len(a), _summaryEdgeItems))]
            l.extend([_leading_trailing(a[-i]) for i in range(
                min(len(a), _summaryEdgeItems),0,-1)])
        else:
            l = [_leading_trailing(a[i]) for i in range(0, len(a))]
        b = _gen.concatenate(tuple(l))
    return b