How to use the cython.declare function in Cython

To help you get started, we’ve selected a few Cython examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github erdc / proteus / proteus / WaveTools.py View on Github external
def fastsinh_test(k,Z,fast=True):
    """Fast hyperbolic sine function with Taylor approximation - TO BE USED FOR TESTING"
    Parameters
    ----------
    k : double
        Wavenumber
    Z : double
        Z coordinate
    Returns
    --------
    sinh(k*z)

    """
    cython.declare(xx=cython.double[2])
    fastcosh(xx,k,Z,fast)
    return xx[1]
github man-group / mdf / mdf / context.py View on Github external
def _get_shift_key(self, ctx):
        try:
            return self._shift_keys[ctx._id_obj]
        except KeyError:
            pass

        # create the net shift of self and the context's shift set (applying
        # self to the ctx's shift set so any re-shifted nodes take the values
        # from the shift set).
        net_shift_set = cython.declare(dict)
        net_shift_set = dict(ctx._shift_set)
        net_shift_set.update(self)

        # get the shift key and add it to the cache
        shift_key = _make_shift_key(net_shift_set)
        self._shift_keys[ctx._id_obj] = shift_key
        return shift_key
github man-group / mdf / mdf / nodetypes.py View on Github external
def send(self, value):
        if self.is_float:
            value_f = cython.declare(cython.double, value)

            # advance previous to the current value and update current
            # value with the new value unless it's nan (in which case we
            # leave it as it is - ie fill forward).
            self.prev_value_f = self.current_value_f
            if value_f == value_f:
                self.current_value_f = value_f

            self.return_f = (self.current_value_f / self.prev_value_f) - 1.0
            if np.isnan(self.return_f):
                self.return_f = 0.0
            return self.return_f

        # advance prev_value and update current value with any new
        # non-nan values
        mask = ~np.isnan(value)
github man-group / mdf / mdf / context.py View on Github external
def ppstats(self):
        """
        print out some profiling stats
        """
        if not _profiling_enabled:
            print ("*** MDF profiling not enabled ***\n" +
                   "Use the --mdf-profile command line option " +
                   "to enable profiling")
            return

        ctx = cython.declare(MDFContext)

        nodes_without_value = set(_all_nodes.itervalues())
        nodes_with_value = set()

        all_timers = {}
        num_shifts = len(self.get_shifted_contexts())

        for ctx in itertools.chain([self], self.get_shifted_contexts()):
            for node in list(nodes_without_value):
                if node.has_value(ctx):
                    nodes_with_value.add(node)
                    nodes_without_value.remove(node)

            for obj, timer in ctx._timers.iteritems():
                if isinstance(obj, MDFNodeBase):
                    name = obj.name
github man-group / mdf / mdf / context.py View on Github external
if shifted_ctx is not self:
                        all_contexts[num_contexts] = shifted_ctx
                        num_contexts += 1
                        if shifted_ctx._has_nodes_requiring_set_date_callback:
                            contexts_with_set_date_callbacks.append(shifted_ctx)
                            have_on_set_date_callbacks = True

        # trim any unused slots
        all_contexts = all_contexts[:num_contexts]

        # call the 'on_set_date' callback on any nodes needing it before
        # actually setting the date on the context.
        # If on_set_date returns True that indicates the node will become dirty
        # once the date has been changed.
        on_set_date_dirty = cython.declare(list)
        on_set_date_dirty_count = cython.declare(int, 0)
        if have_on_set_date_callbacks:
            on_set_date_dirty = []

            for ctx in contexts_with_set_date_callbacks:
                # get the calling node and activate the context once and for all nodes
                calling_node = ctx._get_calling_node(prev_ctx)
                cookie = ctx._activate(prev_ctx, thread_id)
                try:
                    # call the callbacks (this may call other nodes and so might
                    # modify the set of nodes with callbacks)
                    for node in ctx._nodes_requiring_set_date_callback.keys():
                        cqueue_push(ctx._node_eval_stack, node)
                        try:
                            with ctx._profile(node) as timer:
                                dirty = node.on_set_date(ctx, date)
                            if dirty:
github erdc / proteus / proteus / WaveTools.py View on Github external
Parameters
        ----------
        x : numpy.ndarray
            Position vector
        t : float
            Time variable

        Returns
        --------
        numpy.ndarray
            Velocity vector as 1D array

        """

        cython.declare(xx=cython.double[3])
        cython.declare(cppU=cython.double[3])
        for ii in range(3):
            xx[ii] = x[ii]
            cppU[ii] = 0.
        U = np.zeros(3,)
        self._cpp_uDirect(cppU,xx,t)            
        U[0] = cppU[0]
        U[1] = cppU[1]
        U[2] = cppU[2]

        return U
github man-group / mdf / mdf / nodes.py View on Github external
are not dirtied.
        """
        if _trace_enabled:
            ctx = _get_context(node_state.ctx_id)
            _logger.info("%s[%s] touched (%s)" % (
                            self.name,
                            ctx,
                            DIRTY_FLAGS.to_string(flags)))

        # clear the flags
        node_state.dirty_flags &= ~flags

        if not _quiet:
            # mark any calling nodes as dirty
            caller = cython.declare(MDFNode)
            caller_state = cython.declare(NodeState)
            for ctx_id, callers in node_state.callers.iteritems():
                for caller in callers:
                    try:
                        caller_state = caller._states[ctx_id]
                        caller._set_dirty(caller_state, flags, _depth+1)
                    except KeyError:
                        continue
github man-group / mdf / mdf / nodes.py View on Github external
to the override_node whenever its evaluated in ``ctx``.
        """
        shift_set = ctx.get_shift_set()
        if shift_set and override_node is not shift_set.get(self, None):
            raise Exception("Nodes can only be overriden in the root context, "
                            "or if the context is shifted by the node being overriden")       

        # get the root context the override is being applied to
        root_ctx = cython.declare(MDFContext)
        root_ctx = ctx.get_parent() or ctx
        if self in shift_set:
            # the the context only shifted by this node (ctx could be shifted by other nodes as well)
            root_ctx = root_ctx.shift({self : override_node})

        # early out if the context is already shifted by override_node
        node_state = cython.declare(NodeState)
        node_state = self._get_state(root_ctx)
        if node_state.override is override_node:
            return

        # set the override node to be used by get_value
        node_state.override = override_node

        # reset the state for the root context and all shifts of it
        all_contexts = set([root_ctx])
        if root_ctx.get_parent() is not None:
            all_contexts.update(root_ctx.get_parent().get_shifted_contexts())

        for ctx in all_contexts:
            if ctx is root_ctx \
            or ctx.is_shift_of(root_ctx):
                # let any dependencies know the value of this node is invalid
github man-group / mdf / mdf / nodes.py View on Github external
# the original context we're trying to get the value in.
        all_shifted_ctxs = cqueue()
        cqueue_push(all_shifted_ctxs, parent)
        for shifted_ctx in parent.iter_shifted_contexts():
            if shifted_ctx is ctx \
            or shifted_ctx.is_shift_of(ctx) \
            or ctx.is_shift_of(shifted_ctx):
                cqueue_push(all_shifted_ctxs, shifted_ctx)

        # sort so the least shifted are at the start. This should be more optimal
        # for the next loop than if they were in a random order.
        def get_shift_degree(x):
            return len(x.get_shift_set())
        cqueue_sort(all_shifted_ctxs, get_shift_degree, False)

        best_match_state = cython.declare(NodeState)
        best_match_state = self._get_state(best_match)

        shift_set = cython.declare(dict)
        shift_set = {}
        for shifted_node, shifted_value in ctx_shift_set.iteritems():
            # if the context is shifted by self then include self in the shift set
            if shifted_node is self:
                shift_set[shifted_node] = shifted_value
                continue

            # if self[best_match] depends on shifted_node[best_match] or
            # on shifted_node[shifted_ctx] where shifted_ctx is any shift
            # of the original ctx then we have to include the shifted node in the
            # shift set
            for shifted_ctx in all_shifted_ctxs:
                if self._depends_on(best_match_state, shifted_node, shifted_ctx._id_obj):
github catboost / catboost / contrib / tools / cython / Cython / Compiler / Scanning.py View on Github external
# cython: infer_types=True, language_level=3, py2_import=True, auto_pickle=False
#
#   Cython Scanner
#

from __future__ import absolute_import

import cython
cython.declare(make_lexicon=object, lexicon=object,
               print_function=object, error=object, warning=object,
               os=object, platform=object)

import os
import platform

from .. import Utils
from ..Plex.Scanners import Scanner
from ..Plex.Errors import UnrecognizedInput
from .Errors import error, warning
from .Lexicon import any_string_prefix, make_lexicon, IDENT
from .Future import print_function

debug_scanner = 0
trace_scanner = 0
scanner_debug_flags = 0