コード例 #1
0
def log_add_double(x, y):
    """
    Return log(x + y) given log(x) and log(y); see [1].

    [1] Digital Filtering Using Logarithmic Arithmetic.
        Kingsbury and Rayner, 1970.
    """

    if "log_add_d" in get_qy().module.global_variables:
        log_add_d = Function.get_named("log_add_d")
    else:

        @Function.define(float, [float, float])
        def log_add_d(x_in, y_in):
            s = x_in >= y_in
            a = qy.select(s, x_in, y_in)

            @qy.if_else(a == -numpy.inf)
            def _(then):
                if then:
                    qy.return_(-numpy.inf)
                else:
                    qy.return_(a +
                               qy.log1p(qy.exp(qy.select(s, y_in, x_in) - a)))

    return log_add_d(x, y)
コード例 #2
0
def binomial_log_pdf(k, p, n):
    """
    Compute the binomial PMF.
    """

    name = "binomial_log_pdf_ddd"

    if name in get_qy().module.global_variables:
        pdf = Function.get_named(name)
    else:

        @Function.define(float, [float, float, float])
        def binomial_log_pdf_ddd(k, p, n):
            from qy.math import ln_choose

            @qy.if_(k > n)
            def _():
                qy.return_(-numpy.inf)

            @qy.if_(p == 0.0)
            def _():
                qy.return_(qy.select(k == 0.0, 0.0, -numpy.inf))

            @qy.if_(p == 1.0)
            def _():
                qy.return_(qy.select(k == n, 0.0, -numpy.inf))

            qy.return_(
                ln_choose(n, k) + k * qy.log(p) + (n - k) * qy.log1p(-p))

    return binomial_log_pdf_ddd(k, p, n)
コード例 #3
0
ファイル: binomial.py プロジェクト: borg-project/cargo
def binomial_log_pdf(k, p, n):
    """
    Compute the binomial PMF.
    """

    name = "binomial_log_pdf_ddd"

    if name in get_qy().module.global_variables:
        pdf = Function.get_named(name)
    else:
        @Function.define(float, [float, float, float])
        def binomial_log_pdf_ddd(k, p, n):
            from qy.math import ln_choose

            @qy.if_(k > n)
            def _():
                qy.return_(-numpy.inf)

            @qy.if_(p == 0.0)
            def _():
                qy.return_(qy.select(k == 0.0, 0.0, -numpy.inf))

            @qy.if_(p == 1.0)
            def _():
                qy.return_(qy.select(k == n, 0.0, -numpy.inf))

            qy.return_(ln_choose(n, k) + k * qy.log(p) + (n - k) * qy.log1p(-p))

    return binomial_log_pdf_ddd(k, p, n)
コード例 #4
0
def binomial_pdf(k, p, n):
    """
    Compute the binomial PDF function.
    """

    name = "gsl_ran_binomial_pdf"

    if name in get_qy().module.global_variables:
        pdf = Function.get_named(name)
    else:
        import llvm.core

        from ctypes import c_uint

        pdf = Function.named(name, float, [c_uint, float, c_uint])

        pdf._value.add_attribute(llvm.core.ATTR_READONLY)
        pdf._value.add_attribute(llvm.core.ATTR_NO_UNWIND)

    return pdf(k, p, n)
コード例 #5
0
ファイル: categorical.py プロジェクト: borg-project/cargo
def binomial_pdf(k, p, n):
    """
    Compute the binomial PDF function.
    """

    name = "gsl_ran_binomial_pdf"

    if name in get_qy().module.global_variables:
        pdf = Function.get_named(name)
    else:
        import llvm.core

        from ctypes import c_uint

        pdf = Function.named(name, float, [c_uint, float, c_uint])

        pdf._value.add_attribute(llvm.core.ATTR_READONLY)
        pdf._value.add_attribute(llvm.core.ATTR_NO_UNWIND)

    return pdf(k, p, n)
コード例 #6
0
ファイル: real.py プロジェクト: bsilverthorn/qy
    def to_python(self):
        """
        Emit conversion of this value to a Python object.
        """

        from qy import (
            Function,
            object_ptr_type,
            )

        float_from_double = Function.named("PyFloat_FromDouble", object_ptr_type, [float])

        return float_from_double(self._value)
コード例 #7
0
ファイル: py_object.py プロジェクト: bsilverthorn/qy
    def from_string(string):
        """
        Build a Object for a Python string object.
        """

        py_from_string = \
            Function.named(
                "PyString_FromString",
                object_ptr_type,
                [llvm.Type.pointer(llvm.Type.int(8))],
                )

        return Object(py_from_string(qy.string_literal(string))._value)
コード例 #8
0
ファイル: py_object.py プロジェクト: bsilverthorn/qy
        def invoke_python(*inner_arguments):
            from qy import constant_pointer_to

            call_object = \
                Function.named(
                    "PyObject_CallObject",
                    object_ptr_type,
                    [object_ptr_type, object_ptr_type],
                    )

            argument_tuple = qy.py_tuple(*inner_arguments[1:])
            call_result    = call_object(inner_arguments[0], argument_tuple)

            qy.py_dec_ref(argument_tuple)
            qy.py_check_null(call_result)
            qy.py_dec_ref(call_result)
            qy.return_()
コード例 #9
0
ファイル: py_object.py プロジェクト: bsilverthorn/qy
    def get(self, name):
        """
        Get an attribute.
        """

        object_ptr_type = qy.object_ptr_type

        get_attr = \
            Function.named(
                "PyObject_GetAttrString",
                object_ptr_type,
                [object_ptr_type, llvm.Type.pointer(llvm.Type.int(8))],
                )

        result = get_attr(self, qy.string_literal(name))

        qy.py_check_null(result)

        return Object(result._value)
コード例 #10
0
ファイル: mixture.py プロジェクト: borg-project/cargo
def log_add_double(x, y):
    """
    Return log(x + y) given log(x) and log(y); see [1].

    [1] Digital Filtering Using Logarithmic Arithmetic.
        Kingsbury and Rayner, 1970.
    """

    if "log_add_d" in get_qy().module.global_variables:
        log_add_d = Function.get_named("log_add_d")
    else:
        @Function.define(float, [float, float])
        def log_add_d(x_in, y_in):
            s = x_in >= y_in
            a = qy.select(s, x_in, y_in)

            @qy.if_else(a == -numpy.inf)
            def _(then):
                if then:
                    qy.return_(-numpy.inf)
                else:
                    qy.return_(a + qy.log1p(qy.exp(qy.select(s, y_in, x_in) - a)))

    return log_add_d(x, y)