def covsol(a, b): """Solve a covariance linear system.""" m = import_module('vsip.math.solvers.covsol', dtype) m = import_module(v.dtype) x = matrix(b.dtype, b.rows(), b.cols()) m.covsol(a.block, b.block, x.block) return x
def prodj(v, w): """Return the matrix product of v and the Hermitian of w.""" b1 = v.block b2 = w.block if b1.dtype == b2.dtype: mod = import_module('vsip.math.matvec', b1.dtype) return matrix(block=mod.prodj(b1, b2)) else: import numpy mod = import_module('vsip.math.matvec', b2.dtype) return matrix(array=numpy.tensordot(array(b1), array(mod.conj(b2)), (1,0)))
def kaiser(dtype, N, beta): """Create a vector with Kaiser window weights. arguments: :dtype: data type of the vector (e.g. float) :N: length of the vector """ import_module('vsip.block', dtype) m = import_module('vsip.signal.window', dtype) return vector(block=m.kaiser(N, beta))
def prodh(v, w): """Return the matrix product of v and the Hermitian of w.""" b1 = v.block b2 = w.block if b1.dtype == b2.dtype: mod = import_module('vsip.math.matvec', b1.dtype) return matrix(block=mod.prodh(b1, b2)) else: import numpy mod = import_module('vsip.math.matvec', b2.dtype) return matrix( array=numpy.tensordot(array(b1), array(mod.herm(b2)), (1, 0)))
def __init__(self, dtype, seed=0, portable=False): """Create a random number generator. arguments: :dtype: type of the data to be generated :seed: initial seed :portable: whether or not to use a portable (reproducible) sequence """ # Make sure the proper block module is loaded, too import_module('vsip.block', dtype) mod = import_module('vsip.rand', dtype) self._rand = mod.rand(seed, portable)
def __init__(self, dtype, seed=0, portable=False): """Create a random number generator. arguments: :dtype: type of the data to be generated :seed: initial seed :portable: whether or not to use a portable (reproducible) sequence """ # Make sure the proper block module is loaded, too import_module("vsip.block", dtype) mod = import_module("vsip.rand", dtype) self._rand = mod.rand(seed, portable)
def cheby(dtype, N, ripple): """Create a vector with a Dolph-Chebyshev window of length N. arguments: :dtype: data type of the vector (e.g. float) :N: length of the vector :ripple: """ import_module('vsip.block', dtype) m = import_module('vsip.signal.window', dtype) return vector(block=m.cheby(N, ripple))
def blackman(dtype, N): """Create a vector with blackman window weights. arguments: :dtype: data type of the vector (e.g. float) :N: length of the vector .. math:: y_i = 0.42 * 0.5 * cos(\\frac{2*\pi*i}{N-1}) + 0.08 * cos(\\frac{4*\pi*i}{N-1})""" import_module('vsip.block', dtype) m = import_module('vsip.signal.window', dtype) return vector(block=m.blackman(N))
def llsqsol(a, b): """Solve a linear least squares problem.""" m = import_module('vsip.math.solvers.llsqsol', a.dtype) x = matrix(b.dtype, a.cols(), b.cols()) m.llsqsol(a.block, b.block, x.block) return x
def unary(func, a): b = a.block m = import_module('vsip.math.elementwise', b.dtype) f = getattr(m, func) v = a.__class__ return v(block=f(b))
def __init__(self, dtype, rows, cols, storage): self._rows = rows self._cols = cols self.qstorage = storage mod = import_module('vsip.math.solvers.qrd', dtype) self.impl_ = mod.qrd(rows, cols, storage)
def hanning(dtype, N): """Create a vector with Hanning window weights. arguments: :dtype: data type of the vector (e.g. float) :N: length of the vector .. math:: y_i = \\frac{1}{2}(1 - cos(\\frac{2\pi(i+1)}{N+1}))""" import_module('vsip.block', dtype) m = import_module('vsip.signal.window', dtype) return vector(block=m.hanning(N))
def __init__(self, dtype, symmetry, support, i, decimation, n, hint): self.dtype = dtype self.symmetry = symmetry self.support = support m = import_module('vsip.signal.conv', dtype) self._impl = m.conv(symmetry, i, decimation, support, n, hint)
def __init__(self, dtype, rows, cols, ustorage, vstorage): self.dtype = dtype self._rows = rows self._cols = cols self.ustorage = ustorage self.vstorage = vstorage mod = import_module('vsip.math.solvers.svd', dtype) self.impl_ = mod.svd(rows, cols, ustorage, vstorage)
def invclip(v, lt, mt, ut, lc, uc): m = import_module('vsip.selgen.clip', v.dtype) if isinstance(v, vector): vout = vector(v.dtype, v.length()) else: vout = matrix(v.dtype, v.rows(), v.cols()) m.invclip(v.block, vout.block, lt, mt, ut, lc, uc) return vout
def outer(alpha, v, w): """Return the outer product of v and w.""" b1 = v.block b2 = w.block if b1.dtype == b2.dtype: mod = import_module('vsip.math.matvec', b1.dtype) return mod.outer(alpha, b1, b2) else: pass
def vmmul(axis, v, m): """Return the elementwise multiplication of m with the replication of v.""" vb = v.block mb = m.block if vb.dtype == mb.dtype: mod = import_module('vsip.math.matvec', vb.dtype) return matrix(block=mod.vmmul(axis, vb, mb)) else: pass
def kron(alpha, v, w): """Return the Kronecker product of v and w.""" b1 = v.block b2 = w.block if b1.dtype == b2.dtype: mod = import_module('vsip.math.matvec', b1.dtype) return mod.kron(alpha, b1, b2) else: import numpy return numpy.kron(b1, b2)
def binary(func, a1, a2): b1 = a1.block b2 = a2.block if b1.dtype == b2.dtype: m = import_module('vsip.math.elementwise', b1.dtype) else: raise ValueError, 'Unsupported combination of dtypes (%s, %s)'%(b1.dtype, b2.dtype) f = getattr(m, func) v = a1.__class__ return v(block=f(b1, b2))
def binary(func, a1, a2): b1 = a1.block b2 = a2.block if b1.dtype == b2.dtype: m = import_module('vsip.math.elementwise', b1.dtype) else: raise ValueError, 'Unsupported combination of dtypes (%s, %s)' % ( b1.dtype, b2.dtype) f = getattr(m, func) v = a1.__class__ return v(block=f(b1, b2))
def dot(a, b): """Compute the dot product of a and b. dot(a, b) = a * b""" b1 = a.block b2 = b.block if b1.dtype == b2.dtype: mod = import_module('vsip.math.matvec', b1.dtype) return mod.dot(b1, b2) else: import operator import numpy return reduce(operator.add, map(operator.mul, numpy.array(b1), numpy.array(b2)))
def prod(v, w): """Return the matrix product of v and w.""" b1 = v.block b2 = w.block if b1.dtype == b2.dtype: mod = import_module('vsip.math.matvec', b1.dtype) b3 = mod.prod(b1, b2) if len(b3.shape) == 2: return matrix(block=b3) else: return vector(block=b3) else: import numpy if len(b1.shape) == len(b2.shape): return matrix(array=numpy.tensordot(array(b1), array(b2), (1,0))) elif len(b1.shape) == 1: return vector(array=numpy.tensordot(array(b1), array(b2), (0,0))) else: return vector(array=numpy.tensordot(array(b1), array(b2), (1,0)))
def subblock(parent, slice): """Create a subblock of 'parent'. Arguments: parent: the parent block to reference. slice: the slice of the parent to refer to. """ dtype = parent.dtype mod = import_module("vsip.block", dtype) if len(slice) == 1: return mod.subblock(parent, slice[0]) elif len(slice) == 2: return mod.subblock(parent, slice[0], slice[1]) elif len(slice) == 3: return mod.subblock(parent, slice[0], slice[1], slice[2]) else: raise ValueError, "Unsupported slice %s" % slice
def prod(v, w): """Return the matrix product of v and w.""" b1 = v.block b2 = w.block if b1.dtype == b2.dtype: mod = import_module('vsip.math.matvec', b1.dtype) b3 = mod.prod(b1, b2) if len(b3.shape) == 2: return matrix(block=b3) else: return vector(block=b3) else: import numpy if len(b1.shape) == len(b2.shape): return matrix(array=numpy.tensordot(array(b1), array(b2), (1, 0))) elif len(b1.shape) == 1: return vector(array=numpy.tensordot(array(b1), array(b2), (0, 0))) else: return vector(array=numpy.tensordot(array(b1), array(b2), (1, 0)))
def subblock(parent, slice): """Create a subblock of 'parent'. Arguments: parent: the parent block to reference. slice: the slice of the parent to refer to. """ dtype = parent.dtype mod = import_module('vsip.parallel.block', dtype) if len(slice) == 1: return mod.subblock(parent, slice[0]) elif len(slice) == 2: return mod.subblock(parent, slice[0], slice[1]) elif len(slice) == 3: return mod.subblock(parent, slice[0], slice[1], slice[2]) else: raise ValueError, 'Unsupported slice %s' % slice
def block(*args, **kwargs): """Create a distributed block. Valid argument combinations: block(array, map): create a block from an existing array-like container block(dtype, shape, map): create a block of the given type and shape block(dtype, shape, value, map): create a block of the given type and shape and initialize it to value """ # Unpack constructor arguments array = value = None if len(args) == 2: array, map = args[0], args[1] array = numpy.array(array, copy=False) shape = array.shape elif len(args) == 3: dtype, shape, map = args[0], args[1], args[2] value = None elif len(args) == 4: dtype, shape, value, map = args[0], args[1], args[2], args[3] mod = import_module('vsip.parallel.block', dtype) if 'array' in kwargs: return mod.block(array, map) # Create a new block if len(shape) == 1: return value and mod.block(shape[0], value, map) or mod.block( shape[0], map) elif len(shape) == 2: return value and mod.block(shape[0], shape[1], value, map) or mod.block(shape[0], shape[1], map) elif len(shape) == 3: return value and mod.block(shape[0], shape[1], shape[2], value, map) or mod.block( shape[0], shape[1], shape[2], map) else: raise ValueError, 'Unsupported shape %s' % shape
def block(*args, **kwargs): """Create a distributed block. Valid argument combinations: block(array, map): create a block from an existing array-like container block(dtype, shape, map): create a block of the given type and shape block(dtype, shape, value, map): create a block of the given type and shape and initialize it to value """ # Unpack constructor arguments array = value = None if len(args) == 2: array, map = args[0], args[1] array = numpy.array(array, copy=False) shape = array.shape elif len(args) == 3: dtype, shape, map = args[0], args[1], args[2] value = None elif len(args) == 4: dtype, shape, value, map = args[0], args[1], args[2], args[3] mod = import_module('vsip.parallel.block', dtype) if 'array' in kwargs: return mod.block(array, map) # Create a new block if len(shape) == 1: return value and mod.block(shape[0], value, map) or mod.block(shape[0], map) elif len(shape) == 2: return value and mod.block(shape[0], shape[1], value, map) or mod.block(shape[0], shape[1], map) elif len(shape) == 3: return value and mod.block(shape[0], shape[1], shape[2], value, map) or mod.block(shape[0], shape[1], shape[2], map) else: raise ValueError, 'Unsupported shape %s'%shape
def block(*args, **kwargs): """Create a block. Valid argument combinations: block(dtype, shape): create a block of the given type and shape block(dtype, shape, value): create a block of the given type and shape and initialize it to value block(array=A): create a block from an existing array-like container """ # First figure out the data type so we know which # module to import. if args: dtype = args[0] shape = args[1] value = len(args) == 3 and args[2] or None elif "array" in kwargs: # make sure we have all the required metadata array = numpy.array(kwargs["array"], copy=False) dtype = array.dtype mod = import_module("vsip.block", dtype) if "array" in kwargs: return mod.block(array) # Create a new block if len(shape) == 1: return value and mod.block(shape[0], value) or mod.block(shape[0]) elif len(shape) == 2: return value and mod.block(shape[0], shape[1], value) or mod.block(shape[0], shape[1]) elif len(shape) == 3: return value and mod.block(shape[0], shape[1], shape[2], value) or mod.block(shape[0], shape[1], shape[2]) else: raise ValueError, "Unsupported shape %s" % shape
def herm(m): """Return the Hermitian, i.e. the conjugate transpose of matrix 'm'""" mod = import_module('vsip.math.matvec', m.block.dtype) return matrix(array=mod.herm(m.block))
def __init__(self, dtype, length): m = import_module('vsip.math.solvers.lud', dtype) self.impl_ = mod.lud(length)
def trans(m): """Return the transpose of matrix 'm'""" mod = import_module('vsip.math.matvec', m.block.dtype) return matrix(array=mod.trans(m.block))
def local(self): """Return the local submatrix if this is a distributed matrix, 'self' otherwise.""" import_module('vsip.block', self.block.dtype) return matrix(block=self.block.local())
def local(self): """Return the local subvector if this is a distributed vector, 'self' otherwise.""" import_module('vsip.block', self.block.dtype) return vector(block=self.block.local())
def __init__(self, dtype, uplo, length): mod = import_module('vsip.math.solvers.chold', dtype) self.uplo = uplo self.length = length self.impl_ = mod.chold(uplo, length)