Exemplo n.º 1
0
def create_type(name, **kwargs):
    res = S(
        expresso.create_object(TypeInfo(name=name, **kwargs),
                               'pyCAS type ' + name))
    if 'python_type' in kwargs:
        inverse_python_types[kwargs['python_type']] = res
    return res
Exemplo n.º 2
0
def expression_converter(expr):
    if isinstance(expr, expresso.core.Expression):
        return Expression(expr)
    if isinstance(expr, Expression):
        return expr
    if isinstance(expr, bool):
        if expr == True:
            return Expression(expresso.create_object(expr))
        if expr == False:
            return Expression(expresso.create_object(expr))
    if isinstance(expr, (int)):
        expr = Number(expr)
    if isinstance(expr, Number):
        if expr >= 0:
            return Expression(expresso.create_object(expr))
        else:
            expr = abs(expr)
            return negative(expresso.create_object(expr))
    if isinstance(expr, float):
        import fractions
        f = fractions.Fraction(repr(expr))
        if f.denominator == 1:
            return expression_converter(f.numerator)
        if f.numerator == 1:
            return fraction(f.denominator)
        else:
            return f.numerator * fraction(f.denominator)
    if isinstance(expr, complex):
        if expr.real == 0:
            if expr.imag == 0:
                return Zero
            return I * S(float(expr.imag))
        if expr.imag == 0:
            return S(float(expr.real))
        return S(float(expr.real)) + I * S(float(expr.imag))
    if isinstance(expr, tuple):
        return Tuple(*expr)
    raise ValueError('Unsupported expression type: %s (%s)' %
                     (type(expr), expr))
Exemplo n.º 3
0
def expression_converter(expr):
    if isinstance(expr,expresso.core.Expression):
        return Expression(expr)
    if isinstance(expr,Expression):
        return expr
    if isinstance(expr,bool):
        if expr == True:
            return Expression(expresso.create_object(expr))
        if expr == False:
            return Expression(expresso.create_object(expr))
    if isinstance(expr,(int)):
        expr = Number(expr)
    if isinstance(expr, Number):
        if expr >= 0:
            return Expression(expresso.create_object(expr))
        else:
            expr = abs(expr)
            return negative(expresso.create_object(expr))
    if isinstance(expr,float):
        import fractions
        f = fractions.Fraction(repr(expr))
        if f.denominator == 1:
            return expression_converter(f.numerator)
        if f.numerator == 1:
            return fraction(f.denominator)
        else:
            return f.numerator * fraction(f.denominator)
    if isinstance(expr,complex):
        if expr.real == 0:
            if expr.imag == 0:
                return Zero
            return I * S(float(expr.imag))
        if expr.imag == 0:
            return S(float(expr.real))
        return S(float(expr.real)) + I * S(float(expr.imag))
    if isinstance(expr,tuple):
        return Tuple(*expr)
    raise ValueError('Unsupported expression type: %s (%s)' % (type(expr),expr))
Exemplo n.º 4
0
def custom_function(name, argc=None, return_type=None, **kwargs):
    class CustomFunctionData(object):
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)

    func_obj = expresso.create_object(CustomFunctionData(name=name, **kwargs),
                                      name)

    if argc is not None and not isinstance(argc, (tuple, list)):
        argc = [argc]

    class CustomFunctionDelegate(object):
        def __init__(self, func_obj, name, argc):
            self.func_obj = func_obj
            self.name = name
            self.argc = argc

        def __repr__(self):
            return name

        def __call__(self, *args):
            if self.argc != None and len(args) not in self.argc:
                raise ValueError(
                    '%s takes %s arguments' %
                    (self.name, ' or '.join([str(s) for s in self.argc])))
            args = [self.func_obj] + list(args)
            return CustomFunction(*args)

    res = CustomFunctionDelegate(func_obj, name, argc)

    if return_type != None:
        if not argc:
            raise ValueError(
                'argc needs to be defined to register result type')

        # TODO: register in local context, not global evaluators
        from evaluators.type_evaluator import evaluator
        for c in argc:
            evaluator.add_rule(
                Type(res(*[Wildcard(str(s)) for s in range(c)])), return_type)

    return res
Exemplo n.º 5
0
def custom_function(name,argc = None,return_type = None,**kwargs):
    
    class CustomFunctionData(object):
        def __init__(self,**kwargs):
            self.__dict__.update(kwargs)

    func_obj = expresso.create_object(CustomFunctionData(name=name,**kwargs),name)
    
    if argc is not None and not isinstance(argc,(tuple,list)):
        argc = [argc]
        
    class CustomFunctionDelegate(object):
        
        def __init__(self,func_obj,name,argc):
            self.func_obj = func_obj
            self.name = name
            self.argc = argc
        
        def __repr__(self):
            return name
            
        def __call__(self,*args):
            if self.argc != None and len(args) not in self.argc:
                raise ValueError('%s takes %s arguments' % (self.name,' or '.join([str(s) for s in self.argc])))
            args = [self.func_obj] + list(args)
            return CustomFunction(*args)

    res = CustomFunctionDelegate(func_obj,name,argc)

    if return_type != None:
        if not argc:
            raise ValueError('argc needs to be defined to register result type')

        # TODO: register in local context, not global evaluators
        from evaluators.type_evaluator import evaluator
        for c in argc:
            evaluator.add_rule(Type(res(*[Wildcard(str(s)) for s in range(c)])),return_type)

    return res
Exemplo n.º 6
0
            return long(v)
        except:
            raise RuntimeError('expression %s is not convertable to long' % self)

locals().update(expresso.WrappedExpressionTypes(Expression).__dict__)

class Context(ReplaceEvaluator):

    def add_definition(self,search,replacement):
        self.add_replacement(search,replacement)

global_context = Context()

One = S(1)
Zero = S(0)
NaN = S( expresso.create_object(float('nan'),'undefined value') )
I = S( expresso.create_object(1j,'imaginary unit') )

addition = BinaryOperator("+",expresso.associative,expresso.commutative,-11)
negative = UnaryOperator("-",expresso.prefix,-12)
multiplication = BinaryOperator("*",expresso.associative,expresso.commutative,-13)
fraction = UnaryOperator("1/",expresso.prefix,-14)
exponentiation = BinaryOperator("**",-15)

addition_group = Group(addition,negative,Zero)
multiplication_group = Group(multiplication,fraction,One)
real_field = Field(addition_group,multiplication_group)
complex_field = Field(addition_group,multiplication_group)

Or = BinaryOperator("|",expresso.associative,expresso.commutative,-3)
And = BinaryOperator("&",expresso.associative,expresso.commutative,-3)
Exemplo n.º 7
0
 def __init__(self, name, array):
     self.name = name
     self.array_obj = expresso.create_object(
         array, '%s__id_%r' % (name, id(array)))
     self.argc = len(array.shape)
Exemplo n.º 8
0
def create_symbolic_constant(name):
    return S(
        expresso.create_object(SymbolicConstant(name),
                               'symbolic constant %s' % name))
Exemplo n.º 9
0
                               self)


locals().update(expresso.WrappedExpressionTypes(Expression).__dict__)


class Context(ReplaceEvaluator):
    def add_definition(self, search, replacement):
        self.add_replacement(search, replacement)


global_context = Context()

One = S(1)
Zero = S(0)
NaN = S(expresso.create_object(float('nan'), 'undefined value'))
I = S(expresso.create_object(1j, 'imaginary unit'))

addition = BinaryOperator("+", expresso.associative, expresso.commutative, -11)
negative = UnaryOperator("-", expresso.prefix, -12)
multiplication = BinaryOperator("*", expresso.associative,
                                expresso.commutative, -13)
fraction = UnaryOperator("1/", expresso.prefix, -14)
exponentiation = BinaryOperator("**", -15)

addition_group = Group(addition, negative, Zero)
multiplication_group = Group(multiplication, fraction, One)
real_field = Field(addition_group, multiplication_group)
complex_field = Field(addition_group, multiplication_group)

Or = BinaryOperator("|", expresso.associative, expresso.commutative, -3)
Exemplo n.º 10
0
def create_type(name,**kwargs):
    res = S(expresso.create_object(TypeInfo(name=name,**kwargs),'pyCAS type ' + name))
    if 'python_type' in kwargs:
        inverse_python_types[kwargs['python_type']] = res
    return res
Exemplo n.º 11
0
 def __init__(self,name,array):
     self.name = name
     self.array_obj = expresso.create_object(array,'%s__id_%r' % (name,id(array)))
     self.argc = len(array.shape)
Exemplo n.º 12
0
def create_symbolic_constant(name):
    return S(expresso.create_object(SymbolicConstant(name),'symbolic constant %s' % name))