コード例 #1
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def norm(x, order=None):
    '''Matrix or vector norm
    x -- input array
    ord -- None  = Frobenius               | 2-norm
           'fro' = Frobenius               | n/a
           inf   = max(sum(abs(x),axis=1)) | max(abs(x))
           -inf  = min(sum(abs(x),axis=1)) | min(abs(x))
           0     = n/a                     | sum(x != 0)
           1     = max(sum(abs(x),axis=0)) | as below
           -1    = min(sum(abs(x),axis=0)) | as below
           2     = 2-norm (largest s.v.)   | as below
           -2    = smallest singular value | as below
           other = n/a                     | sum(abs(x)**ord)**(1./ord)
    Return norm
    '''
    if order is None:
        return _linalg.norm(x)
    if order == 'fro':
        order = _normorder.FROBENIUS  # @UndefinedVariable
    elif _isinf(order):
        if order > 0:
            order = _normorder.POS_INFINITY  # @UndefinedVariable
        else:
            order = _normorder.NEG_INFINITY  # @UndefinedVariable

    try:
        return _linalg.norm(x, order)
    except Exception, e:
        raise LinAlgError(e)
コード例 #2
0
def norm(x, order=None):
    '''Matrix or vector norm
    x -- input array
    ord -- None  = Frobenius               | 2-norm
           'fro' = Frobenius               | n/a
           inf   = max(sum(abs(x),axis=1)) | max(abs(x))
           -inf  = min(sum(abs(x),axis=1)) | min(abs(x))
           0     = n/a                     | sum(x != 0)
           1     = max(sum(abs(x),axis=0)) | as below
           -1    = min(sum(abs(x),axis=0)) | as below
           2     = 2-norm (largest s.v.)   | as below
           -2    = smallest singular value | as below
           other = n/a                     | sum(abs(x)**ord)**(1./ord)
    Return norm
    '''
    if order is None:
        return _linalg.norm(x)
    if order == 'fro':
        order = _normorder.FROBENIUS # @UndefinedVariable
    elif _isinf(order):
        if order > 0:
            order = _normorder.POS_INFINITY # @UndefinedVariable
        else:
            order = _normorder.NEG_INFINITY # @UndefinedVariable

    try:
        return _linalg.norm(x, order)
    except Exception, e:
        raise LinAlgError(e)
コード例 #3
0
def tensordot(a, b, axes=2):
    '''Tensor dot product of two arrays
    '''
    if isinstance(axes, int):
        bx = range(axes)
        ao = a.getRank() - axes
        ax = [ ao + i for i in bx ]
    else:
        t = type(axes)
        if t is _types.ListType or t is _types.TupleType:
            if len(axes) == 0:
                raise ValueError, "Given axes sequence should be non-empty"

            if len(axes) == 1:
                ax = axes[0]
                bx = axes[0]
            else:
                ax = axes[0]
                bx = axes[1]

            ta = type(ax)
            tal = ta is _types.ListType or ta is _types.TupleType
            tb = type(bx)
            tbl = tb is _types.ListType or tb is _types.TupleType
            if tal != tbl:
                if tal:
                    bx = list(bx)
                else:
                    ax = list(ax)
        else:
            raise ValueError, "Given axes has wrong type"

    return _linalg.tensorDotProduct(a, b, ax, bx)
コード例 #4
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def tensordot(a, b, axes=2):
    '''Tensor dot product of two arrays
    '''
    if isinstance(axes, int):
        bx = list(range(axes))
        ao = a.getRank() - axes
        ax = [ao + i for i in bx]
    else:
        if isinstance(axes, (list, tuple)):
            if len(axes) == 0:
                raise ValueError("Given axes sequence should be non-empty")

            if len(axes) == 1:
                ax = axes[0]
                bx = axes[0]
            else:
                ax = axes[0]
                bx = axes[1]

            tal = isinstance(ax, (list, tuple))
            tbl = isinstance(bx, (list, tuple))
            if tal != tbl:
                if tal:
                    bx = list(bx)
                else:
                    ax = list(ax)
        else:
            raise ValueError("Given axes has wrong type")

    return _linalg.tensorDotProduct(a, b, ax, bx)
コード例 #5
0
def eigvals(a):
    '''Eigenvalues
    '''
    try:
        return _linalg.calcEigenvalues(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #6
0
def qr(a, mode='full'):
    '''QR decomposition
    '''
    try:
        return _linalg.calcQRDecomposition(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #7
0
def cholesky(a):
    '''Cholesky decomposition
    '''
    try:
        return _linalg.calcCholeskyDecomposition(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #8
0
def tensordot(a, b, axes=2):
    '''Tensor dot product of two arrays
    '''
    if isinstance(axes, int):
        bx = range(axes)
        ao = a.getRank() - axes
        ax = [ ao + i for i in bx ]
    else:
        t = type(axes)
        if t is _types.ListType or t is _types.TupleType:
            if len(axes) == 0:
                raise ValueError, "Given axes sequence should be non-empty"

            if len(axes) == 1:
                ax = axes[0]
                bx = axes[0]
            else:
                ax = axes[0]
                bx = axes[1]

            ta = type(ax)
            tal = ta is _types.ListType or ta is _types.TupleType
            tb = type(bx)
            tbl = tb is _types.ListType or tb is _types.TupleType
            if tal != tbl:
                if tal:
                    bx = list(bx)
                else:
                    ax = list(ax)
        else:
            raise ValueError, "Given axes has wrong type"

    return _linalg.tensorDotProduct(a, b, ax, bx)
コード例 #9
0
def svd(a, full_matrices=1, compute_uv=1):
    '''Singular value decomposition
    '''
    try:
        return _linalg.calcSingularValueDecomposition(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #10
0
def pinv(a, rcond=1e-15):
    '''Pseudo-inverse of array
    '''
    try:
        return _linalg.calcPseudoInverse(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #11
0
def solve(a, b):
    '''Solve equation a x = b
    '''
    try:
        return _linalg.solve(a, b)
    except Exception, e:
        raise LinAlgError(e)
コード例 #12
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def eig(a):
    '''Eigen decomposition
    '''
    try:
        return _linalg.calcEigenDecomposition(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #13
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def eigvals(a):
    '''Eigenvalues
    '''
    try:
        return _linalg.calcEigenvalues(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #14
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def pinv(a, rcond=1e-15):
    '''Pseudo-inverse of array
    '''
    try:
        return _linalg.calcPseudoInverse(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #15
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def matrix_power(a, n):
    '''Raise matrix to given power
    '''
    try:
        return _linalg.power(a, n)
    except Exception, e:
        raise LinAlgError(e)
コード例 #16
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def inv(a):
    '''Inverse of square array
    '''
    try:
        return _linalg.calcInverse(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #17
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def solve(a, b):
    '''Solve equation a x = b
    '''
    try:
        return _linalg.solve(a, b)
    except Exception, e:
        raise LinAlgError(e)
コード例 #18
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def det(a):
    '''Determinant
    '''
    try:
        return _linalg.calcDeterminant(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #19
0
def eig(a):
    '''Eigen decomposition
    '''
    try:
        return _linalg.calcEigenDecomposition(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #20
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def cholesky(a):
    '''Cholesky decomposition
    '''
    try:
        return _linalg.calcCholeskyDecomposition(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #21
0
def matrix_power(a, n):
    '''Raise matrix to given power
    '''
    try:
        return _linalg.power(a, n)
    except Exception, e:
        raise LinAlgError(e)
コード例 #22
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def qr(a, mode='full'):
    '''QR decomposition
    '''
    try:
        return _linalg.calcQRDecomposition(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #23
0
def inv(a):
    '''Inverse of square array
    '''
    try:
        return _linalg.calcInverse(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #24
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def svd(a, full_matrices=1, compute_uv=1):
    '''Singular value decomposition
    '''
    try:
        return _linalg.calcSingularValueDecomposition(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #25
0
def det(a):
    '''Determinant
    '''
    try:
        return _linalg.calcDeterminant(a)
    except Exception, e:
        raise LinAlgError(e)
コード例 #26
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def trace(a, offset=0, axis1=0, axis2=1, dtype=None):
    t = _linalg.trace(a, offset)

    dtype = _translatenativetype(dtype)
    if dtype is not None:
        t = t.cast(dtype.value)
    return t
コード例 #27
0
def trace(a, offset=0, axis1=0, axis2=1, dtype=None):
    dtype = _translatenativetype(dtype)
    if dtype is None:
        dtval = a.getDType()
    else:
        dtval = dtype.value

    return _linalg.trace(a, offset).cast(dtval)
コード例 #28
0
def trace(a, offset=0, axis1=0, axis2=1, dtype=None):
    dtype = _translatenativetype(dtype)
    if dtype is None:
        dtval = a.getDType()
    else:
        dtval = dtype.value

    return _linalg.trace(a, offset).cast(dtval)
コード例 #29
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
    '''Cross product of two (arrays of vectors)
    
    a -- first vector
    b -- second vector
    axisa -- axis of a that defines the vector(s)
    axisb -- axis of b that defines the vector(s)
    axisc -- axis of c that will contain the cross product(s)
    axis -- override all values of axis values
    '''
    if axis is not None:
        axisa = axisb = axisc = axis

    return _linalg.crossProduct(a, b, axisa, axisb, axisc)
コード例 #30
0
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
    '''Cross product of two (arrays of vectors)
    
    a -- first vector
    b -- second vector
    axisa -- axis of a that defines the vector(s)
    axisb -- axis of b that defines the vector(s)
    axisc -- axis of c that will contain the cross product(s)
    axis -- override all values of axis values
    '''
    if axis is not None:
        axisa = axisb = axisc = axis

    return _linalg.crossProduct(a, b, axisa, axisb, axisc)
コード例 #31
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def matmul(a, b):
    '''Matrix product of two datasets
    '''
    a = _asarray(a)
    b = _asarray(b)
    prepend = a.rank == 1
    if prepend:
        a.shape = 1, a.shape[0]
    append = b.rank == 1
    if append:
        b.shape = b.shape[0], 1
    m = _asarray(_linalg.matrixProduct(a._jdataset(), b._jdataset()))
    if prepend:
        m.shape = m.shape[1:]
    if append:
        m.shape = m.shape[:-1]
    return m
コード例 #32
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def matmul(a, b):
    '''Matrix product of two datasets
    '''
    a = _asarray(a)
    b = _asarray(b)
    prepend = a.rank == 1
    if prepend:
        a.shape = 1,a.shape[0] 
    append = b.rank == 1
    if append:
        b.shape = b.shape[0],1 
    m = _asarray(_linalg.matrixProduct(a._jdataset(), b._jdataset()))
    if prepend:
        m.shape = m.shape[1:]
    if append:
        m.shape = m.shape[:-1]
    return m
コード例 #33
0
ファイル: jylinalg.py プロジェクト: belkassaby/scisoft-core
def cond(a, order=None):
    '''Condition number
    x -- input array
    ord -- None  = 2-norm computed directly using SVD
           'fro' = Frobenius norm
           inf   = max(sum(abs(x),axis=1))
           -inf  = min(sum(abs(x),axis=1))
           0     = n/a                     | sum(x != 0)
           1     = max(sum(abs(x),axis=0))
           -1    = min(sum(abs(x),axis=0))
           2     = 2-norm (largest s.v.)  
           -2    = smallest singular value
           other = n/a                    
    Return norm
    '''
    if order is None:
        try:
            return _linalg.calcConditionNumber(_jinput(a))
        except Exception, e:
            raise LinAlgError(e)
コード例 #34
0
def cond(a, order=None):
    '''Condition number
    x -- input array
    ord -- None  = 2-norm computed directly using SVD
           'fro' = Frobenius norm
           inf   = max(sum(abs(x),axis=1))
           -inf  = min(sum(abs(x),axis=1))
           0     = n/a                     | sum(x != 0)
           1     = max(sum(abs(x),axis=0))
           -1    = min(sum(abs(x),axis=0))
           2     = 2-norm (largest s.v.)  
           -2    = smallest singular value
           other = n/a                    
    Return norm
    '''
    if order is None:
        try:
            return _linalg.calcConditionNumber(_jinput(a))
        except Exception, e:
            raise LinAlgError(e)
コード例 #35
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def dot(a, b):
    '''Dot product of two arrays'''
    return _linalg.dotProduct(a, b)
コード例 #36
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def vdot(a, b):
    '''Dot product of two vectors with first vector conjugated if complex'''
    return _linalg.dotProduct(conjugate(a.flatten()), b.flatten())
コード例 #37
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def inner(a, b):
    '''Inner product of two arrays (sum product over last dimensions)'''
    return _linalg.tensorDotProduct(a, b, -1, -1)
コード例 #38
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def outer(a, b):
    '''Outer product of two arrays'''
    return _linalg.outerProduct(a, b)
コード例 #39
0
def inner(a, b):
    '''Inner product of two arrays (sum product over last dimensions)'''
    return _linalg.tensorDotProduct(a, b, -1, -1)
コード例 #40
0
def dot(a, b):
    '''Dot product of two arrays'''
    return _linalg.dotProduct(a, b)
コード例 #41
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def kron(a, b):
    '''Kronecker product of two arrays'''
    return _linalg.kroneckerProduct(a, b)
コード例 #42
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def kron(a, b):
    '''Kronecker product of two arrays'''
    return _linalg.kroneckerProduct(a, b)
コード例 #43
0
ファイル: jymaths.py プロジェクト: DawnScience/scisoft-core
def outer(a, b):
    '''Outer product of two arrays'''
    return _linalg.outerProduct(a, b)
コード例 #44
0
def vdot(a, b):
    '''Dot product of two vectors with first vector conjugated if complex'''
    return _linalg.dotProduct(conjugate(a.flatten()), b.flatten())