Example #1
0
    def test_ufunc_out(self):
        from _numpypy import array, negative, zeros, sin
        from math import sin as msin
        a = array([[1, 2], [3, 4]])
        c = zeros((2, 2, 2))
        b = negative(a + a, out=c[1])
        #test for view, and also test that forcing out also forces b
        assert (c[:, :, 1] == [[0, 0], [-4, -8]]).all()
        assert (b == [[-2, -4], [-6, -8]]).all()
        #Test broadcast, type promotion
        b = negative(3, out=a)
        assert (a == -3).all()
        c = zeros((2, 2), dtype=float)
        b = negative(3, out=c)
        assert b.dtype.kind == c.dtype.kind
        assert b.shape == c.shape
        a = array([1, 2])
        b = sin(a, out=c)
        assert (c == [[msin(1), msin(2)]] * 2).all()
        b = sin(a, out=c + c)
        assert (c == b).all()

        #Test shape agreement
        a = zeros((3, 4))
        b = zeros((3, 5))
        raises(ValueError, 'negative(a, out=b)')
        b = zeros((1, 4))
        raises(ValueError, 'negative(a, out=b)')
Example #2
0
    def test_ufunc_out(self):
        from numpypy import array, negative, zeros, sin
        from math import sin as msin
        a = array([[1, 2], [3, 4]])
        c = zeros((2,2,2))
        b = negative(a + a, out=c[1])
        #test for view, and also test that forcing out also forces b
        assert (c[:, :, 1] == [[0, 0], [-4, -8]]).all()
        assert (b == [[-2, -4], [-6, -8]]).all()
        #Test broadcast, type promotion
        b = negative(3, out=a)
        assert (a == -3).all()
        c = zeros((2, 2), dtype=float)
        b = negative(3, out=c)
        assert b.dtype.kind == c.dtype.kind
        assert b.shape == c.shape
        a = array([1, 2])
        b = sin(a, out=c)
        assert(c == [[msin(1), msin(2)]] * 2).all()
        b = sin(a, out=c+c)
        assert (c == b).all()

        #Test shape agreement
        a = zeros((3,4))
        b = zeros((3,5))
        raises(ValueError, 'negative(a, out=b)')
        b = zeros((1,4))
        raises(ValueError, 'negative(a, out=b)')
Example #3
0
def sin(v):
	from math import sin as msin
	if isinstance(v, int) or isinstance(v, float):
		return msin(v)
	elif isinstance(v, Vector):
		return Vector(array=[msin(x) for x in v.list])
	else:
		raise Exception("Must provide either an int, a float, or a vector.")
Example #4
0
def rungeKutta(x_cur, u_next, u_cur):
    h = 0.25
    xj = [0, 0, 0, 0]

    up = (u_next + u_cur) * 0.5

    k11 = x_cur[1]
    k12 = u_cur - x_cur[1]
    k12 = 10 * k12
    k13 = x_cur[3]
    k14 = 0.0004 * (-4905 * msin(x_cur[2]) - 500 * x_cur[3] + 5000 *
                    (u_cur - x_cur[1]) * mcos(x_cur[2]))

    x2p = x_cur[1] + k12 * h * 0.5
    x3p = x_cur[2] + k13 * h * 0.5
    x4p = x_cur[3] + k14 * h * 0.5

    k21 = x2p
    k22 = up - x2p
    k22 = 10 * k22
    k23 = x4p
    k24 = 0.0004 * (-4905 * msin(x3p) - 500 * x4p + 5000 *
                    (up - x2p) * mcos(x3p))

    x2q = x_cur[1] + k22 * h * 0.5
    x3q = x_cur[2] + k23 * h * 0.5
    x4q = x_cur[3] + k24 * h * 0.5

    k31 = x2q
    k32 = up - x2q
    k32 = 10 * k32
    k33 = x4q
    k34 = 0.0004 * (-4905 * msin(x3q) - 500 * x4q + 5000 *
                    (up - x2q) * mcos(x3q))

    x2e = x_cur[1] + k32 * h
    x3e = x_cur[2] + k33 * h
    x4e = x_cur[3] + k34 * h

    k41 = x2e
    k42 = u_next - x2e
    k42 = 10 * k42
    k43 = x4e
    k44 = 0.0004 * (-4905 * msin(x3e) - 500 * x4e + 5000 *
                    (u_next - x2e) * mcos(x3e))

    xj[0] = x_cur[0] + h * (k11 + 2.0 * k21 + 2.0 * k31 + k41) / 6.0
    xj[1] = x_cur[1] + h * (k12 + 2.0 * k22 + 2.0 * k32 + k42) / 6.0
    xj[2] = x_cur[2] + h * (k13 + 2.0 * k23 + 2.0 * k33 + k43) / 6.0
    xj[3] = x_cur[3] + h * (k14 + 2.0 * k24 + 2.0 * k34 + k44) / 6.0
    return xj
Example #5
0
def nebula(output,
           iterations=100,      # (1, 500, 1)
           exposure=1.0,        # (0, 5, 0.01)
           damping=0.8,         # (0, 1.2, 0.01)
           noisy=1.0,           # (0, 10, 0.01)
           fuzz=1.0,            # (0, 10, 0.01)
           intensity=1.0,       # (0, 5, 0.01)
           initial_vx=10,       # (0, 100, 0.01)
           initial_vy=10,       # (0, 100, 0.01)
           spawn=10,            # (1, 100, 1)
           step_sample_rate=10  # Sample rate for each particle render.
          ):
    for radius, colour in [(50, (1.0, 0.1, 0.1)),
                           (100, (1.0, 0.5, 0.1)),
                           (150, (0.3, 1, 0.3)),
                           (200, (0.25, 1, 0.75)),
                           (250, (0.2, 0.2, 1)),
                           (300, (0.75, 0.25, 1))
                          ]:
        LOG.info('# Generating: %s radius:%i colour:%s', 'circle', radius, colour)
        output.write('SIMULATE {} {} {} {} {}\n'.format(iterations, step_sample_rate, damping, noisy, fuzz))
        output.write('COLOUR {} {} {}\n'.format(*tuple(c * intensity for c in colour)))
        for angle in frange(0, tau, 0.5 / radius):
            for _ in range(spawn):
                x, y = 500 + radius * mcos(angle), 500 + radius * msin(angle)
                output.write('PARTICLE {} {} {} {}\n'.format(x, y, fuzzy(initial_vx), fuzzy(initial_vy)))
        output.write('END\n')
    output.write('TONEMAP {}\n'.format(exposure))
Example #6
0
    def py_loop3_2Dsincos(x, y):
        # reverse the order of traversal
        u = zeros((len(x),len(y)), Float)
        from math import sin as msin, cos as mcos

        # x[i], y[j]: coordinates of grid point (i,j)
        for j in xrange(len(y)):
            for i in xrange(len(x)):
                u[i,j] = msin(x[i])*mcos(y[j])
        return u
Example #7
0
    def py_loop2_2Dsincos(x, y):
        # inlined expressions
        u = zeros((len(x),len(y)), Float)
        from math import sin as msin, cos as mcos

        # x[i], y[j]: coordinates of grid point (i,j)
        for i in xrange(len(x)):
            for j in xrange(len(y)):
                u[i,j] = msin(x[i])*mcos(y[j])
        return u
Example #8
0
def sin(a):
    return msin(a)
Example #9
0
 def I(x, y):
     return msin(x)*mcos(y)