本文整理汇总了Python中mathics.core.expression.Expression.evaluate方法的典型用法代码示例。如果您正苦于以下问题:Python Expression.evaluate方法的具体用法?Python Expression.evaluate怎么用?Python Expression.evaluate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mathics.core.expression.Expression
的用法示例。
在下文中一共展示了Expression.evaluate方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: apply
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def apply(self, items, evaluation):
'%(name)s[items__]'
items_sequence = items.get_sequence()
all_numeric = all(item.is_numeric() and item.get_precision() is None for item in items_sequence)
if all_numeric and any(not isinstance(item, Number) for item in items_sequence):
# All expressions are numeric but exact and they are not all numbers,
# so apply N and compare them.
items = items_sequence
n_items = []
for item in items:
if not isinstance(item, Number):
# TODO: use $MaxExtraPrecision insterad of hard-coded 50
n_expr = Expression('N', item, Real(50))
item = n_expr.evaluate(evaluation)
n_items.append(item)
items = n_items
else:
items = items.numerify(evaluation).get_sequence()
wanted = operators[self.get_name()]
prev = None
for item in items:
if item.get_real_value() is None and not item.has_form('DirectedInfinity', None):
return
if prev is not None and do_cmp(prev, item) not in wanted:
return Symbol('False')
prev = item
return Symbol('True')
示例2: apply
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def apply(self, f, expr, test, m, evaluation):
'NestWhile[f_, expr_, test_, Pattern[m,_Integer|All]]'
results = [expr]
while True:
if m.get_name() == 'All':
test_leaves = results
else:
test_leaves = results[-m.value:]
test_expr = Expression(test, *test_leaves)
test_result = test_expr.evaluate(evaluation)
if test_result.is_true():
next = Expression(f, results[-1])
results.append(next.evaluate(evaluation))
else:
break
return results[-1]
示例3: apply_elements
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def apply_elements(self, filename, expr, elems, evaluation, options={}):
"Export[filename_, expr_, elems_List?(AllTrue[#, NotOptionQ]&), OptionsPattern[]]"
# Check filename
if not self._check_filename(filename, evaluation):
return Symbol('$Failed')
# Process elems {comp* format?, elem1*}
leaves = elems.get_leaves()
format_spec, elems_spec = [], []
found_form = False
for leaf in leaves[::-1]:
leaf_str = leaf.get_string_value()
if not found_form and leaf_str in EXPORTERS:
found_form = True
if found_form:
format_spec.append(leaf_str)
else:
elems_spec.append(leaf)
# Infer format if not present
if not found_form:
assert format_spec == []
format_spec = self._infer_form(filename, evaluation)
if format_spec is None:
evaluation.message('Export', 'infer', filename)
return Symbol('$Failed')
format_spec = [format_spec]
else:
assert format_spec != []
# First item in format_spec is the explicit format.
# The other elements (if present) are compression formats
if elems_spec != []: # FIXME: support elems
evaluation.message(
'Export', 'noelem', elems, String(format_spec[0]))
return Symbol('$Failed')
# Load the exporter
exporter_symbol, exporter_options = EXPORTERS[format_spec[0]]
stream_options, custom_options = _importer_exporter_options(
exporter_options.get("System`Options"), options, evaluation)
exporter_function = Expression(
exporter_symbol, filename, expr, *list(chain(stream_options, custom_options)))
if exporter_function.evaluate(evaluation) == Symbol('Null'):
return filename
return Symbol('$Failed')
示例4: apply
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def apply(self, list, expr, evaluation):
'Select[list_, expr_]'
if list.is_atom():
evaluation.message('Select', 'normal')
return
new_leaves = []
for leaf in list.leaves:
test = Expression(expr, leaf)
if test.evaluate(evaluation) == Symbol('True'):
new_leaves.append(leaf)
return Expression(list.head, *new_leaves)
示例5: eval_color
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def eval_color(x, y, v):
v_scaled = (v - v_min) / v_range
if color_function_scaling and color_function_min is not None and color_function_max is not None:
v_color_scaled = color_function_min + v_scaled * color_function_range
else:
v_color_scaled = v
v_lookup = int(v_scaled * 100 + 0.5) # calculate and store 100 different shades max.
value = colors.get(v_lookup)
if value is None:
value = Expression(color_func, Real(v_color_scaled))
value = value.evaluate(evaluation)
colors[v_lookup] = value
return value
示例6: _norm_calc
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def _norm_calc(head, u, v, evaluation):
expr = Expression(head, u, v)
old_quiet_all = evaluation.quiet_all
try:
evaluation.quiet_all = True
expr_eval = expr.evaluate(evaluation)
finally:
evaluation.quiet_all = old_quiet_all
if expr_eval.same(expr):
evaluation.message('Norm', 'nvm')
return None
else:
return expr_eval
示例7: apply
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def apply(self, filename, expr, elems, evaluation):
"Export[filename_, expr_, elems_List]"
# Check filename
if not self._check_filename(filename, evaluation):
return Symbol('$Failed')
## Process elems {comp* format?, elem1*}
leaves = elems.get_leaves()
format_spec, elems_spec = [], []
found_form = False
for leaf in leaves[::-1]:
leaf_str = leaf.get_string_value()
if not found_form and leaf_str in EXPORTERS:
found_form = True
if found_form:
format_spec.append(leaf_str)
else:
elems_spec.append(leaf)
# Infer format if not present
if not found_form:
assert format_spec == []
format_spec = self._infer_form(filename, evaluation)
if format_spec is None:
evaluation.message('Export', 'infer', filename)
return Symbol('$Failed')
format_spec = [format_spec]
else:
assert format_spec != []
# First item in format_spec is the explicit format.
# The other elements (if present) are compression formats
if elems_spec != []: # FIXME: support elems
evaluation.message(
'Export', 'noelem', elems, String(format_spec[0]))
return Symbol('$Failed')
# Load the exporter
exporter_symbol = EXPORTERS[format_spec[0]]
exporter_function = Expression(exporter_symbol, filename, expr)
if exporter_function.evaluate(evaluation) == Symbol('Null'):
return filename
return Symbol('$Failed')
示例8: yield_match
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def yield_match(vars_2, rest):
items = expression.get_sequence()
for item in items:
quick_test = self.quick_pattern_test(item, self.test_name)
if quick_test is not None:
if not quick_test:
break
#raise StopGenerator
else:
test_expr = Expression(self.test, item)
test_value = test_expr.evaluate(evaluation)
if not test_value.is_true():
break
#raise StopGenerator
else:
yield_func(vars_2, None)
示例9: check
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def check(level, expr):
if not expr.has_form('List', None):
test_expr = Expression(test, expr)
if test_expr.evaluate(evaluation) != Symbol('True'):
return False
level_dim = None
else:
level_dim = len(expr.leaves)
if len(dims) > level:
if dims[level] != level_dim:
return False
else:
dims.append(level_dim)
if level_dim is not None:
for leaf in expr.leaves:
if not check(level + 1, leaf):
return False
return True
示例10: apply
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def apply(self, f, x, x0, evaluation):
'FindRoot[f_, {x_, x0_}]'
x0 = Expression('N', x0).evaluate(evaluation)
if not isinstance(x0, Number):
evaluation.message('FindRoot', 'snum', x0)
return
x_name = x.get_name()
if not x_name:
evaluation.message('FindRoot', 'sym', x, 2)
return
count = 0
def diff(evaluation):
return Expression('D', f, x).evaluate(evaluation)
d = dynamic_scoping(diff, {x_name: None}, evaluation)
def sub(evaluation):
d_value = d.evaluate(evaluation)
if d_value == Integer(0):
return None
return Expression('Times', f, Expression('Power', d_value, Integer(-1))).evaluate(evaluation)
while count < 100:
minus = dynamic_scoping(sub, {x_name: x0}, evaluation)
if minus is None:
evaluation.message('FindRoot', 'dsing', x_name, x0)
return
x1 = Expression('Plus', x0, Expression('Times', Integer(-1), minus)).evaluate(evaluation)
if not isinstance(x1, Number):
evaluation.message('FindRoot', 'nnum', x_name, x0)
return
if x1 == x0:
break
x0 = x1.evaluate(evaluation)
count += 1
else:
evaluation.message('FindRoot', 'maxiter')
return Expression('List', Expression('Rule', x, x0))
示例11: numerify_args
# 需要导入模块: from mathics.core.expression import Expression [as 别名]
# 或者: from mathics.core.expression.Expression import evaluate [as 别名]
def numerify_args(items, evaluation):
items_sequence = items.get_sequence()
all_numeric = all(item.is_numeric() and item.get_precision() is None
for item in items_sequence)
# All expressions are numeric but exact and they are not all numbers,
if all_numeric and any(not isinstance(item, Number)
for item in items_sequence):
# so apply N and compare them.
items = items_sequence
n_items = []
for item in items:
if not isinstance(item, Number):
# TODO: use $MaxExtraPrecision insterad of hard-coded 50
n_expr = Expression('N', item, Integer(50))
item = n_expr.evaluate(evaluation)
n_items.append(item)
items = n_items
else:
items = items.numerify(evaluation).get_sequence()
return items