示例#1
0
def move_point(p, x, unpack=True):
    """Adds a number to each of the coordinates of a point.

    parameters:

      `p`: the point to be moved.
      `x`: the amount the point should be moved.
    """
    tdump = TextDumper()
    tdump.dump(p, "p", ['S', 'Point'])
    tdump.dump(x, "x", ['R', '8'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.move_point_py(c_size_t(len_in),
                      c_char_p(in_str),
                      ctypes.byref(len_out),
                      ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.move_point_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#2
0
def scale_array(v, s, unpack=True):
    """scale a one dimentional array.

    parameters:

      `v`: the array to be scaled.
      `s`: the scalar factor.
    """
    tdump = TextDumper()
    tdump.dump(v, "v", ['T', ['R', '8']])
    tdump.dump(s, "s", ['R', '8'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.scale_array_py(c_size_t(len_in),
                       c_char_p(in_str),
                       ctypes.byref(len_out),
                       ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.scale_array_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#3
0
def cube(x, unpack=True):
    """cube a number

    parameters:

      `x`: number to be cubed.
    """
    tdump = TextDumper()
    tdump.dump(x, "x", ['I', '4'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.cube_py(c_size_t(len_in),
                c_char_p(in_str),
                ctypes.byref(len_out),
                ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.cube_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#4
0
def print_msg(msg, unpack=True):
    """Print a message to the standard error stream.

    parameters:

      `msg`: the message to be printed.
    """
    tdump = TextDumper()
    tdump.dump(msg, "msg", ['W'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.print_msg_py(c_size_t(len_in),
                     c_char_p(in_str),
                     ctypes.byref(len_out),
                     ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.print_msg_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#5
0
def make_line(p1, p2, unpack=True):
    """Given two points, it constructs a line.

    parameters:

      `p1`: the first point.
      `p2`: the second point.
    """
    tdump = TextDumper()
    tdump.dump(p1, "p1", ['S', 'Point'])
    tdump.dump(p2, "p2", ['S', 'Point'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.make_line_py(c_size_t(len_in),
                     c_char_p(in_str),
                     ctypes.byref(len_out),
                     ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.make_line_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#6
0
def scale(v, alpha, unpack=True):
    """Scalar multiplication.

    parameters:

      `v`: the vector to multiply. 
      `alpha`: the scalar.
    """
    tdump = TextDumper()
    tdump.dump(v, "v", ['T', ['R', '8']])
    tdump.dump(alpha, "alpha", ['R', '8'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.scale_py(c_size_t(len_in),
                 c_char_p(in_str),
                 ctypes.byref(len_out),
                 ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.scale_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#7
0
def test_rep(unpack=True):
    """This function will do useless computation for the sake of
    testing the reporter object which will print messages to
    stdout.
    """
    tdump = TextDumper()
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.test_rep_py(c_size_t(len_in),
                    c_char_p(in_str),
                    ctypes.byref(len_out),
                    ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.test_rep_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#8
0
def timestwo(x, unpack=True):
    """Take a scalar and double it.

    parameters:

      `x`: scalar to be doubled.
    """
    tdump = TextDumper()
    tdump.dump(x, "x", ['R', '8'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.timestwo_py(c_size_t(len_in),
                    c_char_p(in_str),
                    ctypes.byref(len_out),
                    ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.timestwo_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#9
0
def square_root(x, x0, iter, unpack=True):
    """Compute the square root of a number using Newton's method.
    This function will print to stdout the amount of time it took to
    execute.

    parameters:

      `x`: the input to the square root function.
      `x0`: initial guess.
      `iter`: number of iterations.
    """
    tdump = TextDumper()
    tdump.dump(x, "x", ['R', '8'])
    tdump.dump(x0, "x0", ['R', '8'])
    tdump.dump(iter, "iter", ['I', '4'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.square_root_py(c_size_t(len_in),
                       c_char_p(in_str),
                       ctypes.byref(len_out),
                       ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.square_root_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#10
0
def rand_array(n, mean, std, unpack=True):
    """Get an array of random numbers from a uniform distribution.

    parameters:

      `n`: amount of random numbers.
      `mean`: the mean of the distribution.
      `std`: the standard deviation of the distribution.
    """
    tdump = TextDumper()
    tdump.dump(n, "n", ['I', '4'])
    tdump.dump(mean, "mean", ['R', '8'])
    tdump.dump(std, "std", ['R', '8'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.rand_array_py(c_size_t(len_in),
                      c_char_p(in_str),
                      ctypes.byref(len_out),
                      ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.rand_array_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#11
0
def sin_vector(v, w, unpack=True):
    """Given a vector of values, it returns the sin of each of its
    elements.

    parameters:

      `v`: input vector
      `w`: input vector
    """
    tdump = TextDumper()
    tdump.dump(v, "v", ['T', ['R', '8']])
    tdump.dump(w, "w", ['T', ['R', '8']])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.sin_vector_py(c_size_t(len_in),
                      c_char_p(in_str),
                      ctypes.byref(len_out),
                      ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.sin_vector_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#12
0
def solve(A, B, unpack=True):
    """Solve a system of linear equations, A*X = B, where X is
    unknown; similar functionality to the \\ operator in
    Matlab/Octave, ie. X = A \\ B

    parameters:

      `A`: matrix A
      `B`: matrix B
    """
    tdump = TextDumper()
    tdump.dump(A, "A", ['T', ['R', '8']])
    tdump.dump(B, "B", ['T', ['R', '8']])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.solve_py(c_size_t(len_in),
                 c_char_p(in_str),
                 ctypes.byref(len_out),
                 ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.solve_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#13
0
def qr(X, unpack=True):
    """Decomposition of X into an orthogonal matrix Q and a right
    triangular matrix R, such that Q*R = X.

    parameters:

      `X`: input matrix
    """
    tdump = TextDumper()
    tdump.dump(X, "X", ['T', ['R', '8']])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.qr_py(c_size_t(len_in),
              c_char_p(in_str),
              ctypes.byref(len_out),
              ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.qr_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#14
0
def inverse_det(A, unpack=True):
    """Find the inverse and determinant of a square matrix using
    the armadillo library.

    parameters:

      `A`: input matrix
    """
    tdump = TextDumper()
    tdump.dump(A, "A", ['T', ['R', '8']])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.inverse_det_py(c_size_t(len_in),
                       c_char_p(in_str),
                       ctypes.byref(len_out),
                       ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.inverse_det_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#15
0
def rand_path(npoints, ntrials, delay, unpack=True):
    """Generate realizations from the Negative Feedback model by
    collecting each point that the DSSA generates. This function
    returns `t` and `x`.

    parameters:

      `npoints`: number of points per realization
      `ntrials`: number of realizations
      `delay`: time by which an event is delayed
    """
    tdump = TextDumper()
    tdump.dump(npoints, "npoints", ['I', '4'])
    tdump.dump(ntrials, "ntrials", ['I', '4'])
    tdump.dump(delay, "delay", ['R', '8'])
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.rand_path_py(c_size_t(len_in),
                     c_char_p(in_str),
                     ctypes.byref(len_out),
                     ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.rand_path_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val
示例#16
0
def gen_tensors(unpack=True):
    """Generate tensors of dimension 4.
    """
    tdump = TextDumper()
    in_str = tdump.close()
    len_in = len(in_str)
    out_str = ctypes.POINTER(c_char)()
    len_out = c_size_t(0)
    LIB.gen_tensors_py(c_size_t(len_in),
                       c_char_p(in_str),
                       ctypes.byref(len_out),
                       ctypes.byref(out_str))
    if out_str[:1] == 'E':
        xc_error_msg = out_str[1:len_out.value]
        raise RuntimeError(xc_error_msg)
    val = TextParser(out_str[:len_out.value]).parse()
    LIB.gen_tensors_py_clear()
    if unpack:
        if val:
            return val.values()[0] if len(val) == 1 else val.values()
        return None
    else:
        return val