Beispiel #1
0
def cross_product(a, b, axis1=-1, axis2=-1):
    """Return the cross product of two vectors.

    The cross product is performed over the last axes of a and b by default,
    and can handle axes with dimensions 2 and 3. For a dimension of 2,
    the z-component of the equivalent three-dimensional cross product is
    returned.
    """
    a = _move_axis_to_0(asarray(a), axis1)
    b = _move_axis_to_0(asarray(b), axis2)
    if a.shape[0] != b.shape[0]:
        raise ValueError("incompatible dimensions for cross product")
    elif a.shape[0] == 2:
        return a[0] * b[1] - a[1] * b[0]
    elif a.shape[0] == 3:
        x = a[1] * b[2] - a[2] * b[1]
        y = a[2] * b[0] - a[0] * b[2]
        z = a[0] * b[1] - a[1] * b[0]
        cp = array([x, y, z])
        if len(cp.shape) == 1:
            return cp
        # we put the result (in the first axis) as the last axis
        axes = range(1, len(cp.shape)) + [0]
        return multiarray.transpose(cp, axes)
    else:
        raise ValueError(
            "can only do cross product for axes with dimensions 2 or 3")
Beispiel #2
0
def solve_linear_equations(a, b):
    one_eq = len(b.shape) == 1
    if one_eq:
        b = b[:, Numeric.NewAxis]
    _assertRank2(a, b)
    _assertSquareness(a)
    n_eq = a.shape[0]
    n_rhs = b.shape[1]
    if n_eq != b.shape[0]:
        raise LinAlgError, 'Incompatible dimensions'
    t =_commonType(a, b)
#    lapack_routine = _findLapackRoutine('gesv', t)
    if _array_kind[t] == 1: # Complex routines take different arguments
        lapack_routine = lapack_lite.zgesv
    else:
        lapack_routine = lapack_lite.dgesv
    a, b = _fastCopyAndTranspose(t, a, b)
    pivots = Numeric.zeros(n_eq, 'l')
    results = lapack_routine(n_eq, n_rhs, a, n_eq, pivots, b, n_eq, 0)
    if results['info'] > 0:
        raise LinAlgError, 'Singular matrix'
    if one_eq:
        return Numeric.ravel(b) # I see no need to copy here
    else:
        return multiarray.transpose(b) # no need to copy
Beispiel #3
0
def cross_product(a, b, axis1=-1, axis2=-1):
    """Return the cross product of two vectors.

    The cross product is performed over the last axes of a and b by default,
    and can handle axes with dimensions 2 and 3. For a dimension of 2,
    the z-component of the equivalent three-dimensional cross product is
    returned.
    """
    a = _move_axis_to_0(asarray(a), axis1)
    b = _move_axis_to_0(asarray(b), axis2)
    if a.shape[0] != b.shape[0]:
        raise ValueError("incompatible dimensions for cross product")
    elif a.shape[0] == 2:
        return a[0]*b[1] - a[1]*b[0]
    elif a.shape[0] == 3:
        x = a[1]*b[2] - a[2]*b[1]
        y = a[2]*b[0] - a[0]*b[2]
        z = a[0]*b[1] - a[1]*b[0]
        cp = array([x,y,z])
        if len(cp.shape) == 1:
            return cp
        # we put the result (in the first axis) as the last axis
        axes = range(1, len(cp.shape)) + [0]
        return multiarray.transpose(cp, axes)
    else:
        raise ValueError("can only do cross product for axes with dimensions 2 or 3")
Beispiel #4
0
def transpose(a, axes=None):
    """transpose(a, axes=None) returns array with dimensions permuted
    according to axes.  If axes is None (default) returns array with
    dimensions reversed.
    """
    #    if axes is None: # this test has been moved into multiarray.transpose
    #        axes = arange(len(array(a).shape))[::-1]
    return multiarray.transpose(a, axes)
Beispiel #5
0
def transpose(a, axes=None):
    """transpose(a, axes=None) returns array with dimensions permuted
    according to axes.  If axes is None (default) returns array with
    dimensions reversed.
    """
#    if axes is None: # this test has been moved into multiarray.transpose
#        axes = arange(len(array(a).shape))[::-1]
    return multiarray.transpose(a, axes)
Beispiel #6
0
def _move_axis_to_0(a, axis):
    if axis == 0:
        return a
    n = len(a.shape)
    if axis < 0:
        axis += n
    axes = range(1, axis+1) + [0,] + range(axis+1, n)
    return multiarray.transpose(a, axes)
Beispiel #7
0
def singular_value_decomposition(a, full_matrices=0):
    _assertRank2(a)
    n = a.shape[1]
    m = a.shape[0]
    t = _commonType(a)
    real_t = _array_type[0][_array_precision[t]]
    a = _fastCopyAndTranspose(t, a)
    if full_matrices:
        nu = m
        nvt = n
        option = 'A'
    else:
        nu = min(n, m)
        nvt = min(n, m)
        option = 'S'
    s = Numeric.zeros((min(n, m), ), real_t)
    u = Numeric.zeros((nu, m), t)
    vt = Numeric.zeros((n, nvt), t)
    iwork = Numeric.zeros((8 * min(m, n), ), 'i')
    if _array_kind[t] == 1:  # Complex routines take different arguments
        lapack_routine = lapack_lite.zgesdd
        rwork = Numeric.zeros((5 * min(m, n) * min(m, n) + 5 * min(m, n), ),
                              real_t)
        lwork = 1
        work = Numeric.zeros((lwork, ), t)
        results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt, work,
                                 -1, rwork, iwork, 0)
        lwork = int(abs(work[0]))
        work = Numeric.zeros((lwork, ), t)
        results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt, work,
                                 lwork, rwork, iwork, 0)
    else:
        lapack_routine = lapack_lite.dgesdd
        lwork = 1
        work = Numeric.zeros((lwork, ), t)
        results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt, work,
                                 -1, iwork, 0)
        lwork = int(work[0])
        work = Numeric.zeros((lwork, ), t)
        results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt, work,
                                 lwork, iwork, 0)
    if results['info'] > 0:
        raise LinAlgError, 'SVD did not converge'
    return multiarray.transpose(u), s, multiarray.transpose(
        vt)  # why copy here?
Beispiel #8
0
def _move_axis_to_0(a, axis):
    if axis == 0:
        return a
    n = len(a.shape)
    if axis < 0:
        axis += n
    axes = range(1, axis + 1) + [
        0,
    ] + range(axis + 1, n)
    return multiarray.transpose(a, axes)
Beispiel #9
0
def singular_value_decomposition(a, full_matrices = 0):
    _assertRank2(a)
    n = a.shape[1]
    m = a.shape[0]
    t =_commonType(a)
    real_t = _array_type[0][_array_precision[t]]
    a = _fastCopyAndTranspose(t, a)
    if full_matrices:
	nu = m
	nvt = n
	option = 'A'
    else:
	nu = min(n,m)
	nvt = min(n,m)
	option = 'S'
    s = Numeric.zeros((min(n,m),), real_t)
    u = Numeric.zeros((nu, m), t)
    vt = Numeric.zeros((n, nvt), t)
    iwork = Numeric.zeros((8*min(m,n),), 'l')
    if _array_kind[t] == 1: # Complex routines take different arguments
        lapack_routine = lapack_lite.zgesdd
        rwork = Numeric.zeros((5*min(m,n)*min(m,n) + 5*min(m,n),), real_t)
        lwork = 1
        work = Numeric.zeros((lwork,), t)
        results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt,
                                 work, -1, rwork, iwork, 0)
        lwork = int(abs(work[0]))
        work = Numeric.zeros((lwork,), t)
        results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt,
                                 work, lwork, rwork, iwork, 0)
    else:
        lapack_routine = lapack_lite.dgesdd
        lwork = 1
        work = Numeric.zeros((lwork,), t)
        results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt,
                                 work, -1, iwork, 0)
        lwork = int(work[0])
        work = Numeric.zeros((lwork,), t)
        results = lapack_routine(option, m, n, a, m, s, u, m, vt, nvt,
                                 work, lwork, iwork, 0)
    if results['info'] > 0:
        raise LinAlgError, 'SVD did not converge'
    return multiarray.transpose(u), s, multiarray.transpose(vt) # why copy here?
Beispiel #10
0
def swapaxes(a, axis1, axis2):
    """swapaxes(a, axis1, axis2) returns array a with axis1 and axis2
    interchanged.
    """
    a = array(a, copy=0)
    n = len(a.shape)
    if n <= 1: return a
    if axis1 < 0: axis1 += n
    if axis2 < 0: axis2 += n
    if axis1 < 0 or axis1 >= n:
        raise ValueError, "Bad axis1 argument to swapaxes."
    if axis2 < 0 or axis2 >= n:
        raise ValueError, "Bad axis2 argument to swapaxes."
    new_axes = arange(n)
    new_axes[axis1] = axis2
    new_axes[axis2] = axis1
    return multiarray.transpose(a, new_axes)
Beispiel #11
0
def swapaxes(a, axis1, axis2):
    """swapaxes(a, axis1, axis2) returns array a with axis1 and axis2
    interchanged.
    """
    a = array(a, copy=0)
    n = len(a.shape)
    if n <= 1: return a
    if axis1 < 0: axis1 += n
    if axis2 < 0: axis2 += n
    if axis1 < 0 or axis1 >= n:
        raise ValueError, "Bad axis1 argument to swapaxes."
    if axis2 < 0 or axis2 >= n:
        raise ValueError, "Bad axis2 argument to swapaxes."
    new_axes = arange(n)
    new_axes[axis1] = axis2
    new_axes[axis2] = axis1
    return multiarray.transpose(a, new_axes)
Beispiel #12
0
"""Numeric module defining a multi-dimensional array and useful procedures for
Beispiel #13
0
# This module is a lite version of LinAlg.py module which contains
Beispiel #14
0
"""Numeric module defining a multi-dimensional array and useful procedures for