from numpy import * def kron(a,b): """kronecker product of a and b Kronecker product of two matrices is block matrix [[ a[ 0 ,0]*b, a[ 0 ,1]*b, ... , a[ 0 ,n-1]*b ], [ ... ... ], [ a[m-1,0]*b, a[m-1,1]*b, ... , a[m-1,n-1]*b ]] """ if not a.flags['CONTIGUOUS']: a = reshape(a, a.shape) if not b.flags['CONTIGUOUS']: b = reshape(b, b.shape) o = outerproduct(a,b) o=o.reshape(a.shape + b.shape) return concatenate(concatenate(o, axis=1), axis=1) def repmat0(a, m, n): b = a.view(mat) origrows = b.shape[0] origcols = b.shape[1] rows = b.shape[0] * m cols = b.shape[1] * n c = b.reshape(b.size).A.repeat(m, 0).reshape(rows, origcols).repeat(n, 0) return c.reshape(rows, cols) def repmat1(a, m, n): b = a.view(mat) return hstack([vstack([b]*m)]*n) def repmat2(a, m, n): b = a.view(mat) return concatenate([concatenate([b]*m)]*n, axis=1) def repmat3(a, m, n): return outer(ones((m,1)), outer(ones((1,n)), a)) def repmat4(a, m, n): b = a.view(mat).A (origrows, origcols) = b.shape rows = origrows * m cols = origcols * n c = b.reshape(1,b.size).repeat(m, 0).reshape(rows, origcols).repeat(n,0) return c.reshape(rows, cols) def repmat5(a, m, n): return kron(ones((m,n)),a) def repmat6(a, m, n): a = eval('r_['+m*'a,'+']') return eval('c_['+n*'a,'+']')