Beispiel #1
0
def zeros(shape, dtype=float, bohrium=True):
    """
    Return a matrix of given shape and type, filled with zeros.

    Parameters
    ----------
    shape : int or sequence of ints
        Shape of the matrix
    dtype : data-type, optional
        The desired data-type for the matrix, default is float.
    bohrium : boolean, optional
        Determines whether it is a Bohrium-enabled array or a regular NumPy array

    Returns
    -------
    out : matrix
        Zero matrix of given shape, dtype, and order.

    See Also
    --------
    numpy.zeros : Equivalent array function.
    matlib.ones : Return a matrix of ones.

    Notes
    -----
    The order of the data in memory is always row-major (C-style).

    If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``,
    `out` becomes a single row matrix of shape ``(1,N)``.

    Examples
    --------
    >>> import numpy.matlib
    >>> np.matlib.zeros((2, 3))
    matrix([[ 0.,  0.,  0.],
            [ 0.,  0.,  0.]])

    >>> np.matlib.zeros(2)
    matrix([[ 0.,  0.]])

    """
    if bohrium and not dtype_support(dtype):
        _warn_dtype(dtype, 3)
        return numpy.zeros(shape, dtype=dtype)

    a = empty(shape, dtype=dtype, bohrium=bohrium)
    a[...] = a.dtype.type(0)
    return a
Beispiel #2
0
def random_system(x_max, y_max, z_max, n, b, dtype=npf.float):
    """Generate a galaxy of random bodies"""

    solarmass = 1.98892e30

    def circlev(rx, ry, rz):
        """Helper function..."""
        r2 = npf.sqrt(rx * rx + ry * ry + rz * rz)
        numerator = (6.67e-11) * 1e6 * solarmass
        return npf.sqrt(numerator / r2)

    solarsystem = {}

    solarsystem['x'] = npf.random.random(n)
    solarsystem['y'] = npf.random.random(n)
    solarsystem['z'] = npf.random.random(n) * .01
    dist = (1.0 / npf.sqrt(solarsystem['x']**2 + solarsystem['y']**2 +
                           solarsystem['z']**2)) - (0.8 -
                                                    npf.random.random() * .1)

    solarsystem['x'] = x_max * solarsystem['x'] * dist * npf.sign(
        .5 - npf.random.random(n))
    solarsystem['y'] = y_max * solarsystem['y'] * dist * npf.sign(
        .5 - npf.random.random(n))
    solarsystem['z'] = z_max * solarsystem['z'] * dist * npf.sign(
        .5 - npf.random.random(n))
    magv = circlev(solarsystem['x'], solarsystem['y'], solarsystem['z'])

    absangle = npf.arctan(npf.absolute(solarsystem['y'] / solarsystem['x']))
    thetav = npf.pi / 2 - absangle
    solarsystem['vx'] = -1 * npf.sign(
        solarsystem['y']) * npf.cos(thetav) * magv
    solarsystem['vy'] = npf.sign(solarsystem['x']) * npf.sin(thetav) * magv
    solarsystem['vz'] = npf.zeros(n)
    solarsystem['m'] = npf.random.random(n) * solarmass * 10 + 1e20

    solarsystem['m'][0] = 1e6 * solarmass
    solarsystem['x'][0] = 0
    solarsystem['y'][0] = 0
    solarsystem['z'][0] = 0
    solarsystem['vx'][0] = 0
    solarsystem['vy'][0] = 0
    solarsystem['vz'][0] = 0

    asteroids = {}
    asteroids['x'] = npf.random.random(b)
    asteroids['y'] = npf.random.random(b)
    asteroids['z'] = npf.random.random(b) * .01
    dist = (1.0 / npf.sqrt(asteroids['x']**2 + asteroids['y']**2 +
                           asteroids['z']**2)) - (npf.random.random() * .2)
    asteroids['x'] = x_max * asteroids['x'] * dist * npf.sign(
        .5 - npf.random.random(b))
    asteroids['y'] = y_max * asteroids['y'] * dist * npf.sign(
        .5 - npf.random.random(b))
    asteroids['z'] = z_max * asteroids['z'] * dist * npf.sign(
        .5 - npf.random.random(b))
    magv = circlev(asteroids['x'], asteroids['y'], asteroids['z'])

    absangle = npf.arctan(npf.absolute(asteroids['y'] / asteroids['x']))
    thetav = npf.pi / 2 - absangle
    asteroids['vx'] = -1 * npf.sign(asteroids['y']) * npf.cos(thetav) * magv
    asteroids['vy'] = npf.sign(asteroids['x']) * npf.sin(thetav) * magv
    asteroids['vz'] = npf.zeros(b)
    asteroids['m'] = npf.random.random(b) * solarmass * 10 + 1e14

    ss = {}
    for key in solarsystem:
        ss[key] = np.array(solarsystem[key].astype(dtype))
    a = {}
    for key in asteroids:
        a[key] = np.array(asteroids[key].astype(dtype))

    return ss, a