Changeset 3f9db6e in sasmodels


Ignore:
Timestamp:
Dec 8, 2017 11:06:05 AM (6 years ago)
Author:
Omer Eisenberg <omereis@…>
Children:
7f79cba
Parents:
71779b2
Message:

supporting all sorts of special power: ± 1/3, ½, 2, 3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/py2c.py

    r71779b2 r3f9db6e  
    113113    return ''.join(generator.c_proc) 
    114114 
     115def isevaluable(s): 
     116    try: 
     117        eval(s) 
     118        return True 
     119    except: 
     120        return False 
     121     
    115122class SourceGenerator(NodeVisitor): 
    116123    """This visitor is able to transform a well formed syntax tree into python 
     
    778785        self.write_python('}') 
    779786 
     787    def get_special_power (self, string): 
     788        function_name = '' 
     789        is_negative_exp = False 
     790        if (isevaluable(str(self.current_statement))): 
     791            exponent = eval(string) 
     792            is_negative_exp = exponent < 0 
     793            abs_exponent = abs(exponent) 
     794            if (abs_exponent == 2): 
     795                function_name = "square" 
     796            elif (abs_exponent == 3): 
     797                function_name = "cube" 
     798            elif (abs_exponent == 0.5): 
     799                function_name = "sqrt" 
     800            elif (abs_exponent == 1.0/3.0): 
     801                function_name = "cbrt" 
     802        if (function_name == ''): 
     803            function_name = "pow" 
     804        return function_name, is_negative_exp 
     805 
    780806    def translate_power (self, node): 
    781807# get exponent by visiting the right hand argument. 
    782808        function_name = "pow" 
    783809        temp_statement = self.current_statement 
     810# 'visit' functions write the results to the 'current_statement' class memnber 
     811# Here, a temporary variable, 'temp_statement', is used, that enables the 
     812# use of the 'visit' function 
    784813        self.current_statement = '' 
    785814        self.visit(node.right) 
    786815        exponent = self.current_statement.replace(' ','') 
    787         exponent = exponent.replace('(','') 
    788         exponent = exponent.replace(')','') 
     816        function_name, is_negative_exp = self.get_special_power (self.current_statement) 
    789817        self.current_statement = temp_statement 
    790 # is the right hand argument, the exponent, a number? 
    791         if (hasattr(node.right, 'n')): # power of constand 
    792             exponent = node.right.n 
    793             if (exponent == 2): 
    794                 function_name = "square" 
    795             elif (exponent == 3): 
    796                 function_name = "cube" 
    797             elif (exponent == 0.5): 
    798                 function_name = "sqrt" 
    799         elif (exponent == "1/2"): 
    800                 function_name = "sqrt" 
     818        if (is_negative_exp): 
     819            self.write_c ("1.0 / (") 
    801820        self.write_c (function_name + " (") 
    802821        self.visit(node.left) 
     
    805824            self.visit(node.right) 
    806825        self.write_c(")") 
     826        if (is_negative_exp): 
     827            self.write_c(")") 
     828        self.write_c(" ") 
    807829 
    808830    def translate_integer_divide (self, node): 
Note: See TracChangeset for help on using the changeset viewer.