예제 #1
0
import expresso.pycas as pc
from . import rule_symbols as s

evaluator = pc.RewriteEvaluator(recursive=True, split_binary=True)

evaluator.add_rule(s.a * (s.b + s.c), s.a * s.b + s.a * s.c)
evaluator.add_rule(s.a**-1 * (s.b + s.c)**-1, (s.a * s.b + s.a * s.c)**-1)

from .logic_evaluator import is_pure_numeric, is_atomic

is_positive_numeric_integer = pc.And(is_pure_numeric(s.n),
                                     pc.is_in_type(s.n, pc.Types.Integer),
                                     s.n > 1)

evaluator.add_rule((s.a + s.b)**s.n, (s.a + s.b) * (s.a + s.b)**(s.n - 1),
                   condition=is_positive_numeric_integer)
evaluator.add_rule((s.a + s.b)**-s.n,
                   (s.a + s.b)**(-s.n + 1) * (s.a + s.b)**-1,
                   condition=is_positive_numeric_integer)

evaluator.add_rule(s.x * s.x, s.x**2, condition=is_atomic(s.x))
evaluator.add_rule(s.x * s.x**s.n, s.x**(s.n + 1), condition=is_atomic(s.x))
evaluator.add_rule(s.x**s.m * s.x**s.n,
                   s.x**(s.n + s.m),
                   condition=is_atomic(s.x))

evaluator.add_rule((s.a * s.b)**s.c, s.a**s.c * s.b**s.c)

from .canonical_form import canonical_form
from .logic_evaluator import logic_evaluator
from .numeric_evaluator import numeric_evaluator
예제 #2
0
import expresso.pycas as pc
from . import rule_symbols as s
from .logic_evaluator import is_numeric, is_atomic

from .canonical_form import format_evaluator
from .logic_evaluator import evaluator as logic_evaluator

from .logic_evaluator import is_mpmath

compile_evaluator = pc.RewriteEvaluator(recursive=False, split_binary=True)

fold_accuracy = 20


def fold(m):
    try:
        res = m[s.x].N(fold_accuracy, folding=True)
        if type(res) == tuple:
            return False
        m[s.y] = pc.expresso.create_object(res)
    except Exception as e:
        return False


compile_evaluator.add_rule(s.x, s.y, fold)
compile_evaluator.add_rule(s.x**2, s.x * s.x, condition=is_atomic(s.x))

compile_evaluator.add_rule(s.x * abs(s.y),
                           abs(s.x * s.y),
                           condition=is_mpmath(s.x))
compile_evaluator.add_rule(s.x * (s.y + s.z), (s.x * s.y + s.x * s.z),