本文整理汇总了Python中mathics.core.rules.Pattern类的典型用法代码示例。如果您正苦于以下问题:Python Pattern类的具体用法?Python Pattern怎么用?Python Pattern使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Pattern类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: init
def init(self, expr):
super(PatternObject, self).init(expr)
if self.arg_counts is not None:
if len(expr.leaves) not in self.arg_counts:
self.error_args(len(expr.leaves), *self.arg_counts)
self.expr = expr
self.head = Pattern.create(expr.head)
self.leaves = [Pattern.create(leaf) for leaf in expr.leaves]
开发者ID:fleeaway,项目名称:Mathics,代码行数:8,代码来源:base.py
示例2: apply
def apply(self, f, x, evaluation):
'D[f_, x_?NotListQ]'
if f == x:
return Integer(1)
elif not f.is_atom() and len(f.leaves) == 1 and f.leaves[0] == x:
return Expression(
Expression(Expression('Derivative', Integer(1)), f.head), x)
elif not f.is_atom() and len(f.leaves) == 1:
g = f.leaves[0]
return Expression(
'Times', Expression('D', Expression(f.head, g), g),
Expression('D', g, x))
elif not f.is_atom() and len(f.leaves) > 1:
def summand(leaf, index):
if leaf.same(x):
result = Expression(Expression(
Expression(
'Derivative',
*([Integer(0)] * (index) + [Integer(1)] +
[Integer(0)] * (len(f.leaves) - index - 1))),
f.head), *f.leaves)
else:
result = Expression('D', f, leaf)
return Expression('Times', result, Expression('D', leaf, x))
x_pattern = Pattern.create(x)
result = Expression(
'Plus', *[
summand(leaf, index) for index, leaf in enumerate(f.leaves)
if not leaf.is_free(x_pattern, evaluation)])
if len(result.leaves) == 1:
return result.leaves[0]
else:
return result
开发者ID:ashtonbaker,项目名称:Mathics,代码行数:34,代码来源:calculus.py
示例3: apply
def apply(self, expr, patterns, f, evaluation):
'Reap[expr_, {patterns___}, f_]'
patterns = patterns.get_sequence()
sown = [(Pattern.create(pattern), []) for pattern in patterns]
def listener(e, tag):
result = False
for pattern, items in sown:
if pattern.does_match(tag, evaluation):
for item in items:
if item[0].same(tag):
item[1].append(e)
break
else:
items.append((tag, [e]))
result = True
return result
evaluation.add_listener('sow', listener)
try:
result = expr.evaluate(evaluation)
items = []
for pattern, tags in sown:
list = Expression('List')
for tag, elements in tags:
list.leaves.append(Expression(f, tag, Expression('List', *elements)))
items.append(list)
return Expression('List', result, Expression('List', *items))
finally:
evaluation.remove_listener('sow', listener)
开发者ID:bwright,项目名称:Mathics,代码行数:31,代码来源:lists.py
示例4: apply
def apply(self, expr, pattern, test, evaluation):
'ArrayQ[expr_, pattern_, test_]'
pattern = Pattern.create(pattern)
dims = [len(expr.get_leaves())] # to ensure an atom is not an array
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
if not check(0, expr):
return Symbol('False')
depth = len(dims) - 1 # None doesn't count
if not pattern.does_match(Integer(depth), evaluation):
return Symbol('False')
return Symbol('True')
开发者ID:KarlHeitmann,项目名称:Mathics,代码行数:34,代码来源:tensors.py
示例5: apply
def apply(self, expr, form, evaluation):
'FreeQ[expr_, form_]'
form = Pattern.create(form)
if expr.is_free(form, evaluation):
return Symbol('True')
else:
return Symbol('False')
开发者ID:Darkoe,项目名称:Mathics,代码行数:8,代码来源:structure.py
示例6: apply
def apply(self, expr, form, evaluation):
'FreeQ[expr_, form_]'
"""def is_free(sub):
for vars, rest in form.match(sub, {}, evaluation, fully=False):
return False
if sub.is_atom():
return True
else:
return is_free(sub.head) and all(is_free(leaf) for leaf in sub.leaves)"""
form = Pattern.create(form)
if expr.is_free(form, evaluation):
return Symbol('True')
else:
return Symbol('False')
开发者ID:TheJokerX47,项目名称:Mathics,代码行数:16,代码来源:structure.py
示例7: apply
def apply(self, eqs, vars, evaluation):
'Solve[eqs_, vars_]'
vars_original = vars
head_name = vars.get_head_name()
if head_name == 'List':
vars = vars.leaves
else:
vars = [vars]
for var in vars:
if (var.is_atom() and not var.is_symbol()) or head_name in ('Plus', 'Times', 'Power'):
evaluation.message('Solve', 'ivar', vars_original)
return
eqs_original = eqs
if eqs.get_head_name() in ('List', 'And'):
eqs = eqs.leaves
else:
eqs = [eqs]
sympy_eqs = []
sympy_denoms = []
for eq in eqs:
symbol_name = eq.get_name()
if symbol_name == 'True':
#return Expression('List', Expression('List'))
pass
elif symbol_name == 'False':
return Expression('List')
elif not eq.has_form('Equal', 2):
return evaluation.message('Solve', 'eqf', eqs_original)
else:
left, right = eq.leaves
left = left.to_sympy()
right = right.to_sympy()
#vars_sympy = [var.to_sympy() for var in vars]
eq = left - right
eq = sympy.together(eq)
eq = sympy.cancel(eq)
sympy_eqs.append(eq)
numer, denom = eq.as_numer_denom()
sympy_denoms.append(denom)
#eqs = actual_eqs
#left, right = eq.leaves
vars_sympy = [var.to_sympy() for var in vars]
# delete unused variables to avoid SymPy's
# PolynomialError: Not a zero-dimensional system
# in e.g. Solve[x^2==1&&z^2==-1,{x,y,z}]
all_vars = vars[:]
all_vars_sympy = vars_sympy[:]
#for index, var in enumerate(all_vars):
# if eqs_original.is_free(var, evaluation):
vars = []
vars_sympy = []
for var, var_sympy in zip(all_vars, all_vars_sympy):
pattern = Pattern.create(var)
if not eqs_original.is_free(pattern, evaluation):
vars.append(var)
vars_sympy.append(var_sympy)
def transform_dict(sols):
#print "Transform %s" % sols
if not sols:
yield sols
for var, sol in sols.iteritems():
rest = sols.copy()
del rest[var]
rest = transform_dict(rest)
if not isinstance(sol, (tuple, list)):
#print "Convert %s (type %s)" % (sol, type(sol))
sol = [sol]
if not sol:
for r in rest:
#print "Yield %s" % r
yield r
else:
for r in rest:
for item in sol:
#print "Yield %s with new %s" % (r, item)
new_sols = r.copy()
new_sols[var] = item
yield new_sols
break
def transform_solution(sol):
#if isinstance(sol, (list, tuple)):
if not isinstance(sol, dict):
if not isinstance(sol, (list, tuple)):
sol = [sol]
sol = dict(zip(vars_sympy, sol))
#return sol
return transform_dict(sol)
if not sympy_eqs:
sympy_eqs = True
elif len(sympy_eqs) == 1:
sympy_eqs = sympy_eqs[0]
#eq = left - right
#.........这里部分代码省略.........
开发者ID:cjiang,项目名称:Mathics,代码行数:101,代码来源:calculus.py
注:本文中的mathics.core.rules.Pattern类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论