def toepsol(a, b, w=None): """...""" m = _import_module(v.dtype) x = vector(a.dtype, a.length()) if not w: w = vector(a.dtype, a.length()) m.covsol(a.block, b.block, w.block, x.block) return x
def toepsol(a, b, w=None): """Solve a real symmetric or complex Hermitian positive definite Toeplitz linear system.""" m = _import_module(v.dtype) x = vector(a.dtype, a.length()) if not w: w = vector(a.dtype, a.length()) m.covsol(a.block, b.block, w.block, x.block) return x
def decompose(self, m): x = vector(m.dtype, min(m.rows(), m.cols())) if not self.impl_.decompose(m.block, x.block): raise ValueError, "something went wrong" else: return x
def freqswap(v): m = _import_module(v.dtype) if isinstance(v, vector): vout = vector(v.dtype, v.length()) else: vout = matrix(v.dtype, v.rows(), v.cols()) m.freqswap(v.block, vout.block) return vout
def clip(v, lt, 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.clip(v.block, vout.block, lt, ut, lc, uc) return vout
def invclip(v, lt, mt, ut, lc, uc): m = _import_module(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 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 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(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 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 """ m = _import_module(dtype) return vector(block=m.kaiser(N, beta))
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: """ m = _import_module(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})""" m = _import_module(dtype) return vector(block=m.blackman(N))
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}))""" m = _import_module(dtype) return vector(block=m.hanning(N))
def randn(self, *shape): """Produce normal-distributed numbers from the open interval :math:`(0,1)` arguments: :shape: the shape of the object to be returned: - no argument: return a single number - a single number: return a vector of the given length - two numbers: return a matrix of the given number of rows and columns """ if len(shape) == 0: return self._rand.randn() elif len(shape) == 1: return vector(block=self._rand.randn(shape[0])) else: return matrix(block=self._rand.randn(shape[0], shape[1]))
# # Copyright (c) 2014 Stefan Seefeld # All rights reserved. # # This file is part of OpenVSIP. It is made available under the # license contained in the accompanying LICENSE.BSD file. import numpy as np from numpy import array from vsip import vector, matrix from vsip.math import reductions as red a = np.arange(16, dtype=float) v = vector(array=a) assert array(red.meanval(v) == np.mean(a)).all() assert array(red.maxval(v)[0] == np.max(a)).all() assert array(red.minval(v)[0] == np.min(a)).all() assert array(red.sumval(v) == np.sum(a)).all()
# ... and feed it some input ... input = "Hello, World !" inbuf = cl.buffer(cxt, 32) outbuf = cl.buffer(cxt, 32) queue = cl.default_queue() queue.write(input, inbuf) kernel(queue, 32, inbuf, outbuf) output = queue.read_string(outbuf, len(input)) # ...finally test that the output is equal to the input assert input == output print 'copying string PASSED' # Now try a copy operation on vectors v1 = vector(array=numpy.arange(16, dtype=numpy.float32)) v2 = vector(length=16, dtype=numpy.float32) v1_data = dda.dda(v1.block, dda.sync_in) v2_data = dda.dda(v2.block, dda.sync_out) kernel(queue, 32, v1_data.buf(), v2_data.buf()) # force the data back to the host v2_data.sync_out(); assert v1 == v2 print 'copying buffers PASSED' # now do the same using the wrapper kernel = wrap(program.create_kernel("copy"), (1,0,1)) v2 = vector(length=16, dtype=numpy.float32) v3 = vector(length=16, dtype=numpy.float32) v1[:] = 1. logging.info('copying v1->v2')
def ramp(dtype, start, increment, length): """Produce a vector whose values are :math:`v_i = start + increment * i`.""" import numpy a = start + increment * numpy.arange(length, dtype=dtype) return vector(array=a)
# # Copyright (c) 2013 Stefan Seefeld # All rights reserved. # # This file is part of OpenVSIP. It is made available under the # license contained in the accompanying LICENSE.BSD file. from vsip import vector from vsip import matrix from vsip.selgen.generation import ramp from vsip.math import elementwise as elm from vsip.signal import * from vsip.signal.fft import * import numpy as np #from matplotlib.pyplot import * v1 = ramp(float, 0, 0.1, 1024) v1 = elm.sin(v1) fwd_fft = fft(float, fwd, 1024, 1., 1, alg_hint.time) inv_fft = fft(float, inv, 1024, 1./1024, 1, alg_hint.time) v2 = vector(complex, 513) fwd_fft(v1, v2) v3 = vector(float, 1024) inv_fft(v2, v3) assert np.isclose(v1, v3).all() #plot(v1) #plot(v3) #show()
#!/usr/bin/env python """ Description Define simple step-function and display its fourier transform.""" from vsip import vector from vsip.signal import * from vsip.signal.fft import fft from matplotlib.pyplot import * import numpy fft_fwd = fft(numpy.float64, fwd, 1000, 1., 0, alg_hint.time) input = vector(numpy.float64, 1000) input[100:] = 0 input[:100] = 1 output = vector(complex, 501) fft_fwd(input, output) plot(output) show()
# ... and feed it some input ... input = "Hello, World !" inbuf = cl.buffer(cxt, 32) outbuf = cl.buffer(cxt, 32) queue = cl.default_queue() queue.write(input, inbuf) kernel(queue, 32, inbuf, outbuf) output = queue.read_string(outbuf, len(input)) # ...finally test that the output is equal to the input assert input == output print 'copying string PASSED' # Now try a copy operation on vectors v1 = vector(array=numpy.arange(16, dtype=numpy.float32)) v2 = vector(length=16, dtype=numpy.float32) v1_data = dda.dda(v1.block, dda.sync_in) v2_data = dda.dda(v2.block, dda.sync_out) kernel(queue, 32, v1_data.buf(), v2_data.buf()) # force the data back to the host v2_data.sync_out() assert v1 == v2 print 'copying buffers PASSED' # now do the same using the wrapper kernel = wrap(program.create_kernel("copy"), (1, 0, 1)) v2 = vector(length=16, dtype=numpy.float32) v3 = vector(length=16, dtype=numpy.float32) v1[:] = 1. logging.info('copying v1->v2')
# # Copyright (c) 2014 Stefan Seefeld # All rights reserved. # # This file is part of OpenVSIP. It is made available under the # license contained in the accompanying LICENSE.BSD file. import numpy as np from numpy import array from vsip import vector, matrix from vsip.math import elementwise as elm a1 = np.arange(16, dtype=float) v1 = vector(array=a1) # Unary functions assert array(elm.cos(v1) == np.cos(a1)).all() assert array(elm.sin(v1) == np.sin(a1)).all() a2 = np.arange(16, dtype=float) v2 = vector(array=a2) # Binary functions assert array(elm.mul(v1, v2) == a1*a2).all() a3 = np.arange(16, dtype=float) v3 = vector(array=a3)
#! /usr/bin/env python from numpy import array, arange from vsip import vector from vsip.cuda.compiler import SourceModule # Create 1D float array a = arange(8, dtype=float) # Wrap it in a vector input = vector(a) print input.array() output = vector(float, 8) mod = SourceModule(""" __global__ void doublify(float *in, float *out) { int idx = threadIdx.x + threadIdx.y*4; out[idx] = 2 * in[idx]; } """) func = mod.get_function("doublify") func(input, output, block=(4,4,1)) print "original array:" print input.array() print "doubled with kernel:" print output.array()
#! /usr/bin/env python from numpy import array, arange from vsip import vector from vsip.cuda.compiler import SourceModule # Create 1D float array a = arange(8, dtype=float) # Wrap it in a vector input = vector(a) print input.array() output = vector(float, 8) mod = SourceModule(""" __global__ void doublify(float *in, float *out) { int idx = threadIdx.x + threadIdx.y*4; out[idx] = 2 * in[idx]; } """) func = mod.get_function("doublify") func(input, output, block=(4, 4, 1)) print "original array:" print input.array() print "doubled with kernel:" print output.array()
from numpy import array, arange from vsip import vector # Create vector from scratch v = vector(float, 8) # Access array a = v.array() v[0] = 3 # Make sure v and a are referring to the same memory. assert v[0] == a[0] == 3 # Create 1D float array a = arange(8, dtype=float) # Wrap it in a vector v = vector(a) v[0] = 3 assert v[0] == a[0] == 3 # Test slicing assert (v[1:3] == a[1:3]).all() assert (v[1:5:2] == a[1:5:2]).all() assert (v[5:1:-2] == a[5:1:-2]).all() assert (v[1::2] == a[1::2]).all() assert (v[:5:2] == a[:5:2]).all() assert (v[1:-2:2] == a[1:-2:2]).all() assert (v[-1::-2] == a[-1::-2]).all()
#!/usr/bin/env python """ Description Define simple difference operator and convolve simple input with it.""" from vsip import vector from vsip.signal import * from vsip.signal.conv import convolution from matplotlib.pyplot import * import numpy # define differentiation operator K = vector(numpy.array([-1., 0., 1.])) # define tics for the X axis X = vector(numpy.arange(1024, dtype=numpy.float64)) # set up input array input = vector(numpy.sin(X/numpy.float64(100.))) # set up output array output = vector(numpy.float64, 1022) # create convolution object conv = convolution(K, symmetry.none, 1024, 1, support_region.min, 0, alg_hint.time) # run convolution conv(input, output) # scale output *= numpy.float64(50.) # plot input and output plot(X, input) plot(X[:1022], output) show()
# Copyright (c) 2013 Stefan Seefeld # All rights reserved. # # This file is part of OpenVSIP. It is made available under the # license contained in the accompanying LICENSE.BSD file. from vsip import vector from vsip import matrix from vsip.selgen.generation import ramp from vsip.math.matvec import dot, prod import numpy as np from numpy import array as A v1 = ramp(float, 0, 1, 8) v2 = ramp(float, 1, 2, 8) d = dot(v1, v2) assert d == np.dot(A(v1), A(v2)) v1 = vector(array=np.arange(4, dtype=float)) m1 = matrix(array=np.arange(16, dtype=float).reshape(4, 4)) m2 = matrix(array=np.arange(16, dtype=float).reshape(4, 4)) # vector * matrix v3 = prod(v1, m1) assert (A(v3) == np.tensordot(A(v1), A(m1), (0, 0))).all() # matrix * vector v3 = prod(m1, v1) assert (A(v3) == np.tensordot(A(m1), A(v1), (1, 0))).all() # matrix * matrix m3 = prod(m1, m2) assert (A(m3) == np.tensordot(A(m1), A(m2), (1, 0))).all()
#!/usr/bin/env python """ Description Define simple difference operator and convolve simple input with it.""" from vsip import vector from vsip.signal import * from vsip.signal.conv import convolution from matplotlib.pyplot import * import numpy # define differentiation operator K = vector(numpy.array([-1., 0., 1.])) # define tics for the X axis X = vector(numpy.arange(1024, dtype=numpy.float64)) # set up input array input = vector(numpy.sin(X / numpy.float64(100.))) # set up output array output = vector(numpy.float64, 1022) # create convolution object conv = convolution(K, symmetry.none, 1024, 1, support_region.min, 0, alg_hint.time) # run convolution conv(input, output) # scale output *= numpy.float64(50.) # plot input and output plot(X, input) plot(X[:1022], output) show()
# # Copyright (c) 2014 Stefan Seefeld # All rights reserved. # # This file is part of OpenVSIP. It is made available under the # license contained in the accompanying LICENSE.BSD file. import numpy as np from numpy import array from vsip import vector, matrix from vsip.math import elementwise as elm a1 = np.arange(16, dtype=float) v1 = vector(array=a1) # Unary functions assert array(elm.cos(v1) == np.cos(a1)).all() assert array(elm.sin(v1) == np.sin(a1)).all() a2 = np.arange(16, dtype=float) v2 = vector(array=a2) # Binary functions assert array(elm.mul(v1, v2) == a1 * a2).all() a3 = np.arange(16, dtype=float) v3 = vector(array=a3) # Ternary functions
# # Copyright (c) 2013 Stefan Seefeld # All rights reserved. # # This file is part of OpenVSIP. It is made available under the # license contained in the accompanying LICENSE.BSD file. from numpy import array, arange from vsip import vector # Create vector from scratch v = vector(float, 8) # Access as array (by-reference) a = array(v, copy=False) v[0] = 3 # Make sure v and a are referring to the same memory. assert v[0] == a[0] == 3 # Create 1D float array a = arange(8, dtype=float) # Wrap it in a vector v = vector(array=a) v[0] = 3 a[0] = 3 assert v[0] == 3 # Test slicing access assert array(v[1:3] == a[1:3]).all() assert array(v[1:5:2] == a[1:5:2]).all() assert array(v[5:1:-2] == a[5:1:-2]).all() assert array(v[1::2] == a[1::2]).all()
# # This file is part of OpenVSIP. It is made available under the # license contained in the accompanying LICENSE.BSD file. from vsip import vector from vsip import matrix from vsip.selgen.generation import ramp from vsip.math.matvec import dot, prod import numpy as np from numpy import array as A v1 = ramp(float, 0, 1, 8) v2 = ramp(float, 1, 2, 8) d = dot(v1, v2) assert d == np.dot(A(v1), A(v2)) v1 = vector(array=np.arange(4, dtype=float)) m1 = matrix(array=np.arange(16, dtype=float).reshape(4,4)) m2 = matrix(array=np.arange(16, dtype=float).reshape(4,4)) # vector * matrix v3 = prod(v1, m1) assert (A(v3) == np.tensordot(A(v1), A(m1), (0, 0))).all() # matrix * vector v3 = prod(m1, v1) assert (A(v3) == np.tensordot(A(m1), A(v1), (1, 0))).all() # matrix * matrix m3 = prod(m1, m2) assert (A(m3) == np.tensordot(A(m1), A(m2), (1, 0))).all()