Пример #1
0
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)))
Пример #2
0
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)))
Пример #3
0
def prodt(v, w):
    """Return the matrix product of v and the transpose of w."""

    b1 = v.block
    b2 = w.block
    if b1.dtype == b2.dtype:
        mod = _import_module(b1.dtype)
        return matrix(block=mod.prodt(b1, b2))
    else:
        import numpy
        mod = _import_module(b2.dtype)
        return matrix(
            array=numpy.tensordot(array(b1), array(mod.trans(b2)), (1, 0)))
Пример #4
0
def prodt(v, w):
    """Return the matrix product of v and the transpose of w."""

    b1 = v.block
    b2 = w.block
    if b1.dtype == b2.dtype:
        mod = _import_module(b1.dtype)
        return matrix(block=mod.prodt(b1, b2))
    else:
        import numpy

        mod = _import_module(b2.dtype)
        return matrix(array=numpy.tensordot(array(b1), array(mod.trans(b2)), (1, 0)))
Пример #5
0
    def rsol(self, b, alpha):

        x = matrix(b.dtype, b.rows(), b.cols())
        if not self.impl_.rsol(b.block, alpha, x.block):
            raise ValueError, "something went wrong."
        else:
            return x
Пример #6
0
def covsol(a, b):
    """..."""

    m = _import_module(v.dtype)
    x = matrix(b.dtype, b.rows(), b.cols())
    m.covsol(a.block, b.block, x.block)
    return x
Пример #7
0
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
Пример #8
0
    def lsqsol(self, b):

        x = matrix(b.dtype, self._cols, b.cols())
        if not self.impl_.lsqsol(b.block, x.block):
            raise ValueError, "something went wrong."
        else:
            return x
Пример #9
0
    def solve(self, b):

        x = matrix(b.dtype, b.rows(), b.cols())
        if not self.impl_.solve(b.block, x.block):
            raise ValueError, "something went wrong."
        else:
            return x
Пример #10
0
def covsol(a, b):
    """..."""

    m = _import_module(v.dtype)
    x = matrix(b.dtype, b.rows(), b.cols())
    m.covsol(a.block, b.block, x.block)
    return x
Пример #11
0
    def solve(self, b):

        x = matrix(b.dtype, b.rows(), b.cols())
        if not self.impl_.solve(b.block, x.block):
            raise ValueError, "something went wrong."
        else:
            return x
Пример #12
0
    def lsqsol(self, b):

        x = matrix(b.dtype, self._cols, b.cols())
        if not self.impl_.lsqsol(b.block, x.block):
            raise ValueError, "something went wrong."
        else:
            return x
Пример #13
0
def llsqsol(a, b):
    """..."""

    m = _import_module(a.dtype)
    x = matrix(b.dtype, a.cols(), b.cols())
    m.llsqsol(a.block, b.block, x.block)
    return x
Пример #14
0
    def rsol(self, b, alpha):

        x = matrix(b.dtype, b.rows(), b.cols())
        if not self.impl_.rsol(b.block, alpha, x.block):
            raise ValueError, "something went wrong."
        else:
            return x
Пример #15
0
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
Пример #16
0
    def prodv(self, op, side, b):

        shape = self._produv_shape(op, side, b)
        x = matrix(b.dtype, shape[0], shape[1])
        if not self.impl_.prodv(op, side, b.block, x.block):
            raise ValueError, "something went wrong"
        else:
            return x
Пример #17
0
    def v(self, low, high):

        shape = (self._cols, high - low + 1)
        x = matrix(self.dtype, shape[0], shape[1])
        if not self.impl_.v(low, high, x.block):
            raise ValueError, "something went wrong."
        else:
            return x
Пример #18
0
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
Пример #19
0
    def prodv(self, op, side, b):

        shape = self._produv_shape(op, side, b)
        x = matrix(b.dtype, shape[0], shape[1])
        if not self.impl_.prodv(op, side, b.block, x.block):
            raise ValueError, "something went wrong"
        else:
            return x
Пример #20
0
    def v(self, low, high):

        shape = (self._cols, high - low + 1)
        x = matrix(self.dtype, shape[0], shape[1])
        if not self.impl_.v(low, high, x.block):
            raise ValueError, "something went wrong."
        else:
            return x
Пример #21
0
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
Пример #22
0
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
Пример #23
0
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
Пример #24
0
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
Пример #25
0
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
Пример #26
0
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
Пример #27
0
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
Пример #28
0
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(vb.dtype)
        return matrix(block=mod.vmmul(axis, vb, mb))
    else:
        pass
Пример #29
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('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)))
Пример #30
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)))
Пример #31
0
    def prodq(self, op, side, b):

        if self.qstorage == qrd.saveq1:
            shape = (self._rows, self._cols)
        else:
            shape = (self._rows, self._rows)
        if op in (mat_op.trans, mat_op.herm):
            shape = (shape[1], shape[0])
        if side == product_side.lside:
            shape = (shape[0], b.cols())
        else:
            shape = (b.rows(), shape[1])

        x = matrix(b.dtype, shape[0], shape[1])
        if not self.impl_.prodq(op, side, b.block, x.block):
            raise ValueError, "something went wrong"
        else:
            return x
Пример #32
0
    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]))
Пример #33
0
    def prodq(self, op, side, b):

        if self.qstorage == qrd.saveq1:
            shape = (self._rows, self._cols)
        else:
            shape = (self._rows, self._rows)
        if op in (mat_op.trans, mat_op.herm):
            shape = (shape[1], shape[0])
        if side == product_side.lside:
            shape = (shape[0], b.cols())
        else:
            shape = (b.rows(), shape[1])

        x = matrix(b.dtype, shape[0], shape[1])
        if not self.impl_.prodq(op, side, b.block, x.block):
            raise ValueError, "something went wrong"
        else:
            return x
Пример #34
0
    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]))
Пример #35
0
from numpy import array, arange
from vsip import matrix

# Create matrix from scratch
m = matrix(float, 4, 4)
# Access array
a = m.array()
m[0, 0] = 1
# Make sure m and a are referring to the same memory.
assert m[0,0] == a[0,0] == 1

# Create 1D float array
a = arange(16, dtype=float)
# Reshape into 2D array
a.shape = (4,4)
# Wrap it in a matrix
m = matrix(a)
m[0,0] = 3
assert m[0,0] == a[0,0] == 3

# Test rows access
assert (m[1:3] == a[1:3]).all()
assert (m[1:3:2] == a[1:3:2]).all()
assert (m[3:1:-2] == a[3:1:-2]).all()
assert (m[1::2] == a[1::2]).all()
assert (m[:3:2] == a[:3:2]).all()
assert (m[1:-2:2] == a[1:-2:2]).all()
assert (m[-1::-2] == a[-1::-2]).all()

# Test column slice access
assert (m[1:3,-2] == a[1:3,-2]).all()
Пример #36
0
#
# 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()

Пример #37
0
# 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()
Пример #38
0
# 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 scipy import linalg
from vsip import vector, matrix
from vsip import random
from vsip.selgen import generation as gen
from vsip.math import elementwise as elm
from vsip.math import reductions as red
from vsip.math.solvers import llsqsol
#import matplotlib.pyplot as plt


# Define 'A' to be a two-column matrix containing Y[X] and X
A = matrix(float, 10, 2)
X = gen.ramp(float, 0.1, 0.1, 10)
A[:,0] = elm.exp(-X)
A[:,1] = X

c1,c2= 5.0,2.0
Y = c1*elm.exp(-X) + c2*X

Z = matrix(float, 10, 1)
Z[:,0] = Y + 0.05*red.maxval(Y)[0]*random.rand(float).randn(Y.length())

R = llsqsol(A, Z)
c,resid,rank,sigma = linalg.lstsq(A, Z)

# Compare results of llsqsol with results from linalg.lstsq
assert np.isclose(R, c).all()
Пример #39
0
def trans(m):
    """Return the transpose of matrix 'm'"""

    mod = _import_module(m.block.dtype)
    return matrix(array=mod.trans(m.block))
Пример #40
0
from vsip import matrix
from vsip.math.solvers import svd
from vsip.math.matvec import prod, trans

import numpy as np

A = matrix(array=[[1.,2.,3.],[4.,5.,6.]])
M,N = A.rows(), A.cols()
print A

svd = svd(float, M, N, svd.uvfull, svd.uvfull)

s = svd.decompose(A)
S = matrix(float, M, N)

S.diag()[:] = s
U = svd.u(0, M-1)
V = svd.v(0, N-1)
C = prod(prod(U, S), trans(V))
print C
assert np.isclose(C, A).all()
Пример #41
0
#
# 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 matrix
from sys import exit

# Create matrix from scratch
m = matrix(float, 4, 4)
# Access array
a = array(m, copy=False)
m[0, 0] = 1
# Make sure m and a are referring to the same memory.
assert m[0,0] == a[0,0] == 1

# Create 1D float array
a = arange(16, dtype=float)
# Reshape into 2D array
a.shape = (4,4)
# Wrap it in a matrix
m = matrix(array=a)
m[0,0] = 3
a[0,0] = 3
assert m[0,0] == 3

# Test rows access
assert array(m[1:3] == a[1:3]).all()
Пример #42
0
#
# 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.fftm import *
import numpy as np
#from matplotlib.pyplot import *

v1 = ramp(float, 0, 0.1, 1024)
v1 = elm.sin(v1)
m1 = matrix(float, 16, 1024)
for r in range(16):
    m1[r,:] = ramp(float, r, 0.1, 1024)
fwd_fftm = fftm(float, fwd, 16, 1024, 1., 0, 1, alg_hint.time)
inv_fftm = fftm(float, inv, 16, 1024, 1./1024, 0, 1, alg_hint.time)
m2 = matrix(complex, 16, 513)
fwd_fftm(m1, m2)
m3 = matrix(float, 16, 1024)
inv_fftm(m2, m3)

assert np.isclose(m1, m3).all()

#plot(v1)
#plot(v3)
#show()
Пример #43
0
        
    ax.autoscale_view()
    ax.invert_yaxis()


# Create a map...
if num_processors() > 1:
    nr = num_processors() // 2
    nc = num_processors() // nr
    m = map(nr, nc)
else:
    m = map(1)
comm = m.communicator()

# ...and a distributed matrix with it...
m = matrix(block=block(int, (SIZE,SIZE), 0., m))
# ...as well as a local one in the rank=1 process
lm = matrix(block=block(int, (SIZE,SIZE), 0., map(1)))

# Now manipulate the distributed matrix...
for (y, x) in iterate(m):
    m[y, x] = x + y * m.cols()

# ...and assign it to local.
lm[:,:] = m

# Now print all the local (sub-)matrices...
f1 = plt.figure(1)
plot(m.local(), proc=local_processor())
f1.show()
# ...as well as the distributed matrix
Пример #44
0
from vsip import matrix
from vsip.math.solvers import svd
from vsip.math.matvec import prod, trans

import numpy as np

A = matrix(array=[[1., 2., 3.], [4., 5., 6.]])
M, N = A.rows(), A.cols()
print A

svd = svd(float, M, N, svd.uvfull, svd.uvfull)

s = svd.decompose(A)
S = matrix(float, M, N)

S.diag()[:] = s
U = svd.u(0, M - 1)
V = svd.v(0, N - 1)
C = prod(prod(U, S), trans(V))
print C
assert np.isclose(C, A).all()
Пример #45
0
    ax.autoscale_view()
    ax.invert_yaxis()


# Create a map...
if num_processors() > 1:
    nr = num_processors() // 2
    nc = num_processors() // nr
    m = map(nr, nc)
else:
    m = map(1)
comm = m.communicator()

# ...and a distributed matrix with it...
m = matrix(block=block(int, (SIZE, SIZE), 0., m))
# ...as well as a local one in the rank=1 process
lm = matrix(block=block(int, (SIZE, SIZE), 0., map(1)))

# Now manipulate the distributed matrix...
for (y, x) in iterate(m):
    m[y, x] = x + y * m.cols()

# ...and assign it to local.
lm[:, :] = m

# Now print all the local (sub-)matrices...
f1 = plt.figure(1)
plot(m.local(), proc=local_processor())
f1.show()
# ...as well as the distributed matrix
Пример #46
0
def trans(m):
    """Return the transpose of matrix 'm'"""

    mod = _import_module(m.block.dtype)
    return matrix(array=mod.trans(m.block))
Пример #47
0
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))
Пример #48
0
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))
Пример #49
0
def herm(m):
    """Return the Hermitian, i.e. the conjugate transpose of matrix 'm'"""

    mod = _import_module(m.block.dtype)
    return matrix(array=mod.herm(m.block))
Пример #50
0
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))
Пример #51
0
from numpy import array, arange
from vsip import matrix

# Create matrix from scratch
m = matrix(float, 4, 4)
# Access array
a = m.array()
m[0, 0] = 1
# Make sure m and a are referring to the same memory.
assert m[0, 0] == a[0, 0] == 1

# Create 1D float array
a = arange(16, dtype=float)
# Reshape into 2D array
a.shape = (4, 4)
# Wrap it in a matrix
m = matrix(a)
m[0, 0] = 3
assert m[0, 0] == a[0, 0] == 3

# Test rows access
assert (m[1:3] == a[1:3]).all()
assert (m[1:3:2] == a[1:3:2]).all()
assert (m[3:1:-2] == a[3:1:-2]).all()
assert (m[1::2] == a[1::2]).all()
assert (m[:3:2] == a[:3:2]).all()
assert (m[1:-2:2] == a[1:-2:2]).all()
assert (m[-1::-2] == a[-1::-2]).all()

# Test column slice access
assert (m[1:3, -2] == a[1:3, -2]).all()