def main():
    # This precomputes the color palette for maximum speed
    # You could change it to compute the color palette of your choice
    w = [colorwheel(i) for i in range(256)]

    # This sets up the initial wave as a smooth gradient
    u = np.zeros(num_pixels)
    um = np.zeros(num_pixels)
    f = np.zeros(num_pixels)

    slope = np.linspace(0, 256, num=num_pixels)
    th = 1

    # the first time is always random (is that a contradiction?)
    r = 0

    while True:

        # Some of the time, add a random new wave to the mix
        # increase .15 to add waves more often
        # decrease it to add waves less often
        if r < .01:
            ii = random.randrange(1, num_pixels - 1)
            # increase 2 to make bigger waves
            f[ii] = (random.random() - .5) * 2

        # Here's where to change dx, dt, and c
        # try .2, .02, 2 for relaxed
        # try 1., .7, .2 for very busy / almost random
        u, um = step(u, um, f, num_pixels, .1, .02, 1), u

        v = u * 200000 + slope + th
        for i, vi in enumerate(v):
            # Scale up by an empirical value, rotate by th, and look up the color
            pixels[i] = w[round(vi) % 256]

        # Take away a portion of the energy of the waves so they don't get out
        # of control
        u = u * .99

        # incrementing th causes the colorwheel to slowly cycle even if nothing else is happening
        th = (th + .25) % 256
        pixels.show()

        # Clear out the old random value, if any
        f[ii] = 0

        # and get a new random value
        r = random.random()
Beispiel #2
0
print(np.log2(2**1))

print(np.log10(10**1))
print(np.exp(1) - np.expm1(1))

x = np.array([-1, +1, +1, -1])
y = np.array([-1, -1, +1, +1])
result = (np.arctan2(y, x) * 180 / np.pi)
ref_result = np.array([-135.0, -45.0, 45.0, 135.0], dtype=np.float)
cmp_result = []
for i in range(len(x)):
    cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
print(cmp_result)

x = np.linspace(-2*np.pi, 2*np.pi, 5)
result = np.sin(x)
ref_result = np.array([2.4492936e-16, -1.2246468e-16,  0.0000000e+00,  1.2246468e-16, -2.4492936e-16], dtype=np.float)
cmp_result = []
for i in range(len(x)):
    cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
print(cmp_result)

result = np.cos(x)
ref_result = np.array([1., -1.,  1., -1.,  1.], dtype=np.float)
cmp_result = []
for i in range(len(x)):
    cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
print(cmp_result)

result = np.tan(x)
Beispiel #3
0
p = [1, 1, 1, 0]
x = [0, 1, 2, 3, 4]
result = np.polyval(p, x)
ref_result = np.array([0, 3, 14, 39, 84])
for i in range(len(x)):
    print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))

a = np.array(x)
result = np.polyval(p, a)
ref_result = np.array([0, 3, 14, 39, 84])
for i in range(len(x)):
    print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))

# linear fit
x = np.linspace(-10, 10, 20)
y = 1.5 * x + 3
result = np.polyfit(x, y, 1)
ref_result = np.array([1.5, 3.0])
for i in range(2):
    print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))

# 2nd degree fit
x = np.linspace(-10, 10, 20)
y = x * x * 2.5 - x * 0.5 + 1.2
result = np.polyfit(x, y, 2)
ref_result = np.array([2.5, -0.5, 1.2])
for i in range(3):
    print(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))

# 3rd degree fit
Beispiel #4
0
    def __getitem__(self, key: Union[_RClassKeyType, Tuple[_RClassKeyType,
                                                           ...]]):

        if not isinstance(key, tuple):
            key = (key, )

        objs: List[np.ndarray] = []
        scalars: List[int] = []
        arraytypes: List[_DType] = []
        scalartypes: List[_DType] = []

        # these may get overridden in following loop
        axis = 0

        for idx, item in enumerate(key):
            scalar = False

            try:
                if isinstance(item, np.ndarray):
                    newobj = item

                elif isinstance(item, slice):
                    step = item.step
                    start = item.start
                    stop = item.stop
                    if start is None:
                        start = 0
                    if step is None:
                        step = 1
                    if isinstance(step, complex):
                        size = int(abs(step))
                        newobj: np.ndarray = np.linspace(start, stop, num=size)
                    else:
                        newobj = np.arange(start, stop, step)

                # if is number
                elif isinstance(item, (int, float, bool)):
                    newobj = np.array([item])
                    scalars.append(len(objs))
                    scalar = True
                    scalartypes.append(newobj.dtype())

                else:
                    newobj = np.array(item)

            except TypeError:
                raise Exception(
                    "index object %s of type %s is not supported by r_[]" %
                    (str(item), type(item)))

            objs.append(newobj)
            if not scalar and isinstance(newobj, np.ndarray):
                arraytypes.append(newobj.dtype())

        # Ensure that scalars won't up-cast unless warranted
        final_dtype = min(arraytypes + scalartypes)
        for idx, obj in enumerate(objs):
            if obj.dtype != final_dtype:
                objs[idx] = np.array(objs[idx], dtype=final_dtype)

        return np.concatenate(tuple(objs), axis=axis)
Beispiel #5
0
try:
    from ulab import numpy as np
except ImportError:
    import numpy as np

x = np.linspace(0, 9, num=10)
y = x * x
print(np.trapz(y))
print(np.trapz(y, x=x))
Beispiel #6
0
try:
    from ulab import numpy as np
except:
    import numpy as np


def print_as_buffer(a):
    print(len(memoryview(a)), list(memoryview(a)))


print_as_buffer(np.ones(3))
print_as_buffer(np.zeros(3))
print_as_buffer(np.eye(4))
print_as_buffer(np.ones(1, dtype=np.int8))
print_as_buffer(np.ones(2, dtype=np.uint8))
print_as_buffer(np.ones(3, dtype=np.int16))
print_as_buffer(np.ones(4, dtype=np.uint16))
print_as_buffer(np.ones(5, dtype=np.float))
print_as_buffer(np.linspace(0, 1, 9))
print(np.eye(3, M=4, k=1))
print(np.eye(3, M=4, k=2))
print(np.eye(3, M=4, k=3))
print(np.eye(4, M=4))
print(np.eye(4, M=3, k=0))
print(np.eye(4, M=3, k=-1))
print(np.eye(4, M=3, k=-2))
print(np.eye(4, M=3, k=-3))
print(np.eye(4, M=3, k=1))
print(np.eye(4, M=3, k=2))
print(np.eye(4, M=3, k=3))
print("Array creation using FULL:")
print(np.full((2, 4), 3, dtype=np.float))
print(np.full((2, 4), 3, dtype=np.uint8))
print("Array creation using LINSPACE:")
print(np.linspace(0, 10, num=5))
print(np.linspace(0, 10, num=5, endpoint=False))
print(np.linspace(0, 10, num=5, endpoint=True))
print(np.linspace(0, 10, num=5, endpoint=False, dtype=np.uint8))
print(np.linspace(0, 10, num=5, endpoint=False, dtype=np.uint16))
print(np.linspace(0, 10, num=5, endpoint=False, dtype=np.int8))
print(np.linspace(0, 10, num=5, endpoint=False, dtype=np.int16))
print("Array creation using LOGSPACE:")
print(np.logspace(0, 10, num=5))
print(np.logspace(0, 10, num=5, endpoint=False))
print(np.logspace(0, 10, num=5, endpoint=True))
print(np.logspace(0, 10, num=5, endpoint=False, dtype=np.uint8))
print(np.logspace(0, 10, num=5, endpoint=False, dtype=np.uint16))
print(np.logspace(0, 10, num=5, endpoint=False, dtype=np.int8))
print(np.logspace(0, 10, num=5, endpoint=False, dtype=np.int16))
print("Array creation using ZEROS:")
Beispiel #8
0
from ulab import numpy as np

print(np.ones(3))
print(np.ones((2, 3)))
print(np.zeros(3))
print(np.zeros((2, 3)))
print(np.eye(3))
print(np.ones(1, dtype=np.int8))
print(np.ones(2, dtype=np.uint8))
print(np.ones(3, dtype=np.int16))
print(np.ones(4, dtype=np.uint16))
print(np.ones(5, dtype=np.float))
print(np.linspace(0, 1, 9))