Example #1
0
def get_log_color_gray(image_array, x, y, iterations, max_iterations):
    if iterations == max_iterations:
        return

    k = real_log(float64(iterations)) / real_log(float64(max_iterations) - 1.0)

    image_array[y, x, 0] = int8(255 * k)
    image_array[y, x, 1] = int8(255 * k)
    image_array[y, x, 2] = int8(255 * k)
Example #2
0
def get_log_color_rgb(image_array, x, y, iterations, max_iterations):
    if iterations == max_iterations:
        return

    k = 3.0 * real_log(float64(iterations)) / real_log(float64(max_iterations))

    if k < 1:
        image_array[y, x, 0] = int8(255 * k)
        image_array[y, x, 1] = 0
        image_array[y, x, 2] = 0
    elif k < 2:
        image_array[y, x, 0] = 255
        image_array[y, x, 1] = int8(255 * (k - 1))
        image_array[y, x, 2] = 0
    else:
        image_array[y, x, 0] = 255
        image_array[y, x, 1] = 255
        image_array[y, x, 2] = int8(255 * (k - 2))
Example #3
0
def abx(encoded, is_protected, n_sample=10**7):
    ans = numba.int64(0)
    pos = np.nonzero(is_protected)[0]
    neg = np.nonzero(~is_protected)[0]
    for _ in prange(n_sample):
        a = encoded[np.random.choice(pos)]
        b = encoded[np.random.choice(neg)]
        x = encoded[np.random.choice(pos)]
        ans += numba.int8(dist(a, x) < dist(b, x))
    return ans / n_sample - 0.5
Example #4
0
    numpy matrix of integers for fast comparison.

    Requires all seqs to have the same length."""
    L1 = len(seqs[0])
    mat = np.zeros((len(seqs), L1), dtype=np.int8)
    for si, s in enumerate(seqs):
        assert L1 == len(
            s
        ), "All sequences must have the same length: L1 = %d, but L%d = %d" % (
            L1, si, len(s))
        for aai, aa in enumerate(s):
            mat[si, aai] = AA2CODE[aa]
    return mat


@nb.jit(nb.int8(nb.char[:], nb.char[:]), nopython=True)
def nb_hamming_distance(str1, str2):
    tot = 0
    for s1, s2 in zip(str1, str2):
        if s1 != s2:
            tot += 1
    return tot


def trunc_hamming(seq1, seq2, maxDist=2, **kwargs):
    """Truncated hamming distance
    d = hamming() if d<maxDist else d = maxDist"""
    d = hamming_distance(seq1, seq2)
    return maxDist if d >= maxDist else d

Example #5
0
 def f(x, out):
     # Explicit coercion of intenums to ints
     if x > int16(RequestError.internal_error):
         out[0] = x - int32(RequestError.not_found)
     else:
         out[0] = x + int8(Shape.circle)
Example #6
0
from time import time
from random import random
import numpy as np
from numba import jit,  guvectorize, complex128, int8, int32

#---------------------   Les constantes 

X_MIN,Y_MIN,X_MAX,Y_MAX = -2,-1,1,1 # Valeurs min et max pour les parties reelles et imaginaires
MAX_ITER = 500 # Le nombre d'itérations maximum avant de considérer que la suite ne diverge pas vers l'infini
NB_POINTS = 1000000 # Nombres de points aléatoires choisis pour la méthode de Monte Carlo

#---------------------   Les fonctions

@jit(int8(complex128),nopython=True)
def mandelbrot(c):
    """
    Renvoie 0 si la suite diverge avant MAX_ITER itérations et 1 sinon.
    Permet de compter les points de non divergence vers l'infini.
    """
    # On regarde d'abord si c est dans la cardioide ou le cercle principal
    p = abs(c-0.25)
    if c.real < p - 2*p*p + 0.25 or (c.real+1)**2 + c.imag**2 < 0.0625 : return 1
    # Sinon, on étudie la suite
    r,i=0,0
    for n in range(MAX_ITER):
        r2 = r*r
        i2 = i*i
        if r2 + i2 > 4.0:
            return 0
        i = 2* r*i + c.imag
        r = r2 - i2 + c.real
Example #7
0
from math import asin, atan2, ceil, cos, degrees, radians, sin, sqrt

from numba import float64, int8, int32, jit, typeof
# from numba.pycc import CC

# cc = CC('compiled_helpers', )
# Uncomment the following line to print out the compilation steps
# # cc.verbose = True

dtype_3floattuple = typeof((1.0, 1.0, 1.0))
dtype_2floattuple = typeof((1.0, 1.0))


# @cc.export('position_to_line', int8(int32, int32, int32, int32, int32, int32))
@jit(int8(int32, int32, int32, int32, int32, int32), nopython=True, cache=True)
def position_to_line(x, y, x1, x2, y1, y2):
    """tests if a point pX(x,y) is Left|On|Right of an infinite line from p1 to p2
            Return: -1 for pX left of the line from! p1 to! p2
                    0 for pX on the line [is not needed]
                    1 for pX  right of the line
                    this approach is only valid because we already know that y lies within ]y1;y2]
        """

    if x1 < x2:
        # p2 is further right than p1
        if x > x2:
            # pX is further right than p2,
            if y1 > y2:
                return -1
            else:
Example #8
0
# -*- coding: utf-8 -*-
# http://numba.pydata.org/numba-doc/latest/user/examples.html

from __future__ import print_function, division, absolute_import

from timeit import default_timer as timer
from matplotlib.pylab import imshow, jet, show, ion
import numpy as np

from numba import jit, int32, int8, float64


@jit(int8(float64, float64, int32), nopython=True, nogil=True)
def mandel(x, y, max_iters):
    """
    Given the real and imaginary parts of a complex number,
    determine if it is a candidate for membership in the Mandelbrot
    set given a fixed number of iterations.
    """
    i = 0
    c = complex(x, y)
    z = 0.0j
    for i in range(max_iters):
        z = z * z + c
        if (z.real * z.real + z.imag * z.imag) >= 4:
            return i

    return 255


@jit(nopython=True, nogil=True)
Example #9
0
    return vec
def seqs2mat(seqs):
    """Convert a collection of AA sequences into a
    numpy matrix of integers for fast comparison.

    Requires all seqs to have the same length."""
    L1 = len(seqs[0])
    mat = np.zeros((len(seqs), L1), dtype = np.int8)
    for si, s in enumerate(seqs):
        assert L1 == len(s), "All sequences must have the same length: L1 = %d, but L%d = %d" % (L1, si, len(s))
        for aai, aa in enumerate(s):
            mat[si, aai] = AA2CODE[aa]
    return mat


@nb.jit(nb.int8(nb.char[:], nb.char[:]), nopython = True)
def nb_hamming_distance(str1, str2):
    tot = 0
    for s1, s2 in zip(str1, str2):
        if s1 != s2:
            tot += 1
    return tot

def trunc_hamming(seq1,seq2,maxDist=2,**kwargs):
    """Truncated hamming distance
    d = hamming() if d<maxDist else d = maxDist"""
    d = hamming_distance(seq1, seq2)
    return maxDist if d >= maxDist else d

def dichot_hamming(seq1,seq2,mmTolerance=1,**kwargs):
    """Dichotamized hamming distance.
Example #10
0
def int_cast_usecase(x):
    # Explicit coercion of intenums to ints
    if x > int16(RequestError.internal_error):
        return x - int32(RequestError.not_found)
    else:
        return x + int8(Shape.circle)