def convert_function_call(cls, function_call, prefix=''): """ Converts a single handed over function call to C++ NEST API syntax. Parameters ---------- function_call : ASTFunctionCall The function call node to convert. prefix : str Optional string that will be prefixed to the function call. For example, to refer to a function call in the class "node", use a prefix equal to "node." or "node->". Predefined functions will not be prefixed. Returns ------- s : str The function call string in C++ syntax. """ function_name = function_call.get_name() if function_name == 'and': return '&&' if function_name == 'or': return '||' if function_name == PredefinedFunctions.TIME_RESOLUTION: return 'nest::Time::get_resolution().get_ms()' if function_name == PredefinedFunctions.TIME_STEPS: return 'nest::Time(nest::Time::ms((double) ({!s}))).get_steps()' if function_name == PredefinedFunctions.CLIP: # warning: the arguments of this function must swapped and # are therefore [v_max, v_min, v], hence its structure return 'std::min({2!s}, std::max({1!s}, {0!s}))' if function_name == PredefinedFunctions.MAX: return 'std::max({!s}, {!s})' if function_name == PredefinedFunctions.MIN: return 'std::min({!s}, {!s})' if function_name == PredefinedFunctions.EXP: return 'std::exp({!s})' if function_name == PredefinedFunctions.LN: return 'std::log({!s})' if function_name == PredefinedFunctions.LOG10: return 'std::log10({!s})' if function_name == PredefinedFunctions.COSH: return 'std::cosh({!s})' if function_name == PredefinedFunctions.SINH: return 'std::sinh({!s})' if function_name == PredefinedFunctions.TANH: return 'std::tanh({!s})' if function_name == PredefinedFunctions.EXPM1: return 'numerics::expm1({!s})' if function_name == PredefinedFunctions.RANDOM_NORMAL: return '(({!s}) + ({!s}) * ' + prefix + 'normal_dev_( nest::kernel().rng_manager.get_rng( ' + prefix + 'get_thread() ) ))' if function_name == PredefinedFunctions.RANDOM_UNIFORM: return '(({!s}) + ({!s}) * nest::kernel().rng_manager.get_rng( ' + prefix + 'get_thread() )->drand())' if function_name == PredefinedFunctions.EMIT_SPIKE: return 'set_spiketime(nest::Time::step(origin.get_steps()+lag+1));\n' \ 'nest::SpikeEvent se;\n' \ 'nest::kernel().event_delivery_manager.send(*this, se, lag)' # suppress prefix for misc. predefined functions # check if function is "predefined" purely based on the name, as we don't have access to the function symbol here function_is_predefined = PredefinedFunctions.get_function( function_name) if function_is_predefined: prefix = '' if ASTUtils.needs_arguments(function_call): n_args = len(function_call.get_args()) return prefix + function_name + '(' + ', '.join( ['{!s}' for _ in range(n_args)]) + ')' return prefix + function_name + '()'
def convert_function_call(self, function_call, prefix=''): """Convert a single function call to C++ GSL API syntax. Parameters ---------- function_call : ASTFunctionCall The function call node to convert. prefix : str Optional string that will be prefixed to the function call. For example, to refer to a function call in the class "node", use a prefix equal to "node." or "node->". Predefined functions will not be prefixed. Returns ------- s : str The function call string in C++ syntax. """ function_name = function_call.get_name() if function_name == PredefinedFunctions.TIME_RESOLUTION: return 'nest::Time::get_resolution().get_ms()' if function_name == PredefinedFunctions.TIME_STEPS: return 'nest::Time(nest::Time::ms((double) {!s})).get_steps()' if function_name == PredefinedFunctions.MAX: return 'std::max({!s}, {!s})' if function_name == PredefinedFunctions.MIN: return 'std::min({!s}, {!s})' if function_name == PredefinedFunctions.CLIP: # warning: the arguments of this function have been swapped and # are therefore [v_max, v_min, v], hence its structure return 'std::min({2!s}, std::max({1!s}, {0!s}))' if function_name == PredefinedFunctions.EXP: if self.is_upper_bound: return 'std::exp(std::min({!s},' + str( self.maximal_exponent) + '))' else: return 'std::exp({!s})' if function_name == PredefinedFunctions.COSH: if self.is_upper_bound: return 'std::cosh(std::min(std::abs({!s}),' + str( self.maximal_exponent) + '))' else: return 'std::cosh({!s})' if function_name == PredefinedFunctions.SINH: if self.is_upper_bound: return 'std::sinh(({!s} > 0 ? 1 : -1)*std::min(std::abs({!s}),' + str( self.maximal_exponent) + '))' else: return 'std::sinh({!s})' if function_name == PredefinedFunctions.TANH: return 'std::tanh({!s})' if function_name == PredefinedFunctions.LOG: return 'std::log({!s})' if function_name == PredefinedFunctions.EXPM1: return 'numerics::expm1({!s})' if function_name == PredefinedFunctions.EMIT_SPIKE: return 'set_spiketime(nest::Time::step(origin.get_steps()+lag+1));\n' \ 'nest::SpikeEvent se;\n' \ 'nest::kernel().event_delivery_manager.send(*this, se, lag)' # suppress prefix for misc. predefined functions function_is_predefined = PredefinedFunctions.get_function( function_name ) # check if function is "predefined" purely based on the name, as we don't have access to the function symbol here if function_is_predefined: prefix = '' if ASTUtils.needs_arguments(function_call): n_args = len(function_call.get_args()) return prefix + function_name + '(' + ', '.join( ['{!s}' for _ in range(n_args)]) + ')' return prefix + function_name + '()'
def convert_function_call(self, function_call, prefix=''): """Convert a single function call to C++ GSL API syntax. Parameters ---------- function_call : ASTFunctionCall The function call node to convert. prefix : str Optional string that will be prefixed to the function call. For example, to refer to a function call in the class "node", use a prefix equal to "node." or "node->". Predefined functions will not be prefixed. Returns ------- s : str The function call string in C++ syntax. """ function_name = function_call.get_name() if function_name == PredefinedFunctions.TIME_RESOLUTION: return 'nest::Time::get_resolution().get_ms()' if function_name == PredefinedFunctions.TIME_STEPS: return 'nest::Time(nest::Time::ms((double) {!s})).get_steps()' if function_name == PredefinedFunctions.MAX: return 'std::max({!s}, {!s})' if function_name == PredefinedFunctions.MIN: return 'std::min({!s}, {!s})' if function_name == PredefinedFunctions.CLIP: # warning: the arguments of this function have been swapped and # are therefore [v_max, v_min, v], hence its structure return 'std::min({2!s}, std::max({1!s}, {0!s}))' if function_name == PredefinedFunctions.EXP: if self.is_upper_bound: return 'std::exp(std::min({!s},' + str( self.maximal_exponent) + '))' else: return 'std::exp({!s})' if function_name == PredefinedFunctions.COSH: if self.is_upper_bound: return 'std::cosh(std::min(std::abs({!s}),' + str( self.maximal_exponent) + '))' else: return 'std::cosh({!s})' if function_name == PredefinedFunctions.SINH: if self.is_upper_bound: return 'std::sinh(({!s} > 0 ? 1 : -1)*std::min(std::abs({!s}),' + str( self.maximal_exponent) + '))' else: return 'std::sinh({!s})' if function_name == PredefinedFunctions.TANH: return 'std::tanh({!s})' if function_name == PredefinedFunctions.LN: return 'std::log({!s})' if function_name == PredefinedFunctions.LOG10: return 'std::log10({!s})' if function_name == PredefinedFunctions.EXPM1: return 'numerics::expm1({!s})' if function_name == PredefinedFunctions.RANDOM_NORMAL: return '(({!s}) + ({!s}) * ' + prefix + 'normal_dev_( nest::get_vp_specific_rng( ' + prefix + 'get_thread() ) ))' if function_name == PredefinedFunctions.RANDOM_UNIFORM: return '(({!s}) + ({!s}) * nest::get_vp_specific_rng( ' + prefix + 'get_thread() )->drand())' if function_name == PredefinedFunctions.EMIT_SPIKE: return 'set_spiketime(nest::Time::step(origin.get_steps()+lag+1));\n' \ 'nest::SpikeEvent se;\n' \ 'nest::kernel().event_delivery_manager.send(*this, se, lag)' if function_name == PredefinedFunctions.DELIVER_SPIKE: return ''' set_delay( {1!s} ); const long __delay_steps = nest::Time::delay_ms_to_steps( get_delay() ); set_delay_steps(__delay_steps); e.set_receiver( *__target ); e.set_weight( {0!s} ); // use accessor functions (inherited from Connection< >) to obtain delay in steps and rport e.set_delay_steps( get_delay_steps() ); e.set_rport( get_rport() ); e(); ''' # suppress prefix for misc. predefined functions # check if function is "predefined" purely based on the name, as we don't have access to the function symbol here function_is_predefined = PredefinedFunctions.get_function( function_name) if function_is_predefined: prefix = '' if ASTUtils.needs_arguments(function_call): n_args = len(function_call.get_args()) return prefix + function_name + '(' + ', '.join( ['{!s}' for _ in range(n_args)]) + ')' return prefix + function_name + '()'
def convert_function_call(cls, function_call, prefix=''): """ Converts a single handed over function call to C++ NEST API syntax. Parameters ---------- function_call : ASTFunctionCall The function call node to convert. prefix : str Optional string that will be prefixed to the function call. For example, to refer to a function call in the class "node", use a prefix equal to "node." or "node->". Predefined functions will not be prefixed. Returns ------- s : str The function call string in C++ syntax. """ function_name = function_call.get_name() if function_name == 'and': return '&&' if function_name == 'or': return '||' if function_name == PredefinedFunctions.TIME_RESOLUTION: return 'nest::Time::get_resolution().get_ms()' if function_name == PredefinedFunctions.TIME_STEPS: return 'nest::Time(nest::Time::ms((double) %s)).get_steps()' if function_name == PredefinedFunctions.POW: return 'std::pow(%s, %s)' if function_name == PredefinedFunctions.MAX or function_name == PredefinedFunctions.BOUNDED_MAX: return 'std::max(%s, %s)' if function_name == PredefinedFunctions.MIN or function_name == PredefinedFunctions.BOUNDED_MIN: return 'std::min(%s, %s)' if function_name == PredefinedFunctions.EXP: return 'std::exp(%s)' if function_name == PredefinedFunctions.LOG: return 'std::log(%s)' if function_name == PredefinedFunctions.EXPM1: return 'numerics::expm1(%s)' if function_name == PredefinedFunctions.EMIT_SPIKE: return 'set_spiketime(nest::Time::step(origin.get_steps()+lag+1));\n' \ 'nest::SpikeEvent se;\n' \ 'nest::kernel().event_delivery_manager.send(*this, se, lag)' # suppress prefix for misc. predefined functions function_is_predefined = PredefinedFunctions.get_function( function_name ) # check if function is "predefined" purely based on the name, as we don't have access to the function symbol here if function_is_predefined: prefix = '' if ASTUtils.needs_arguments(function_call): n_args = len(function_call.get_args()) return prefix + function_name + '(' + ', '.join( ['%s' for _ in range(n_args)]) + ')' return prefix + function_name + '()'