提交 29403884 authored 作者: Olivier Breuleux's avatar Olivier Breuleux

backport to 2.4 and additional documentation for my last commit

上级 4bbac540
...@@ -417,8 +417,10 @@ def stack_search(start, expand, mode='bfs', build_inv = False): ...@@ -417,8 +417,10 @@ def stack_search(start, expand, mode='bfs', build_inv = False):
raise ValueError('mode should be bfs or dfs', mode) raise ValueError('mode should be bfs or dfs', mode)
rval_set = set() rval_set = set()
rval_list = list() rval_list = list()
if mode == 'bfs': start_pop = start.popleft if mode == 'bfs':
else: start_pop = start.pop start_pop = start.popleft
else:
start_pop = start.pop
expand_inv = {} expand_inv = {}
while start: while start:
l = start_pop() l = start_pop()
......
...@@ -1337,7 +1337,16 @@ def _redefine_asRoutine(real_symbol_value): ...@@ -1337,7 +1337,16 @@ def _redefine_asRoutine(real_symbol_value):
return decorator return decorator
def _scal_elemwise_with_nfunc(nfunc, nin, nout): def _scal_elemwise_with_nfunc(nfunc, nin, nout):
"""Replace a symbol definition with an elementwise version of the corresponding scalar Op""" """
Replace a symbol definition with an elementwise version of the
corresponding scalar Op. If it is not None, the nfunc argument
should be a string such that getattr(numpy, nfunc) implements
a vectorized version of the elemwise operation. nin is the number
of inputs expected by that function, and nout is the number of
**destination** inputs it takes. That is, the function should
take nin+nout inputs. nout == 0 means that the numpy function
does not take a numpy array argument to put its result in.
"""
def construct(symbol): def construct(symbol):
symbolname = symbol.__name__ symbolname = symbol.__name__
inplace = symbolname.endswith('_inplace') inplace = symbolname.endswith('_inplace')
...@@ -1350,10 +1359,12 @@ def _scal_elemwise_with_nfunc(nfunc, nin, nout): ...@@ -1350,10 +1359,12 @@ def _scal_elemwise_with_nfunc(nfunc, nin, nout):
if inplace: if inplace:
scalar_op = getattr(scal, symbolname[:-len('_inplace')]) scalar_op = getattr(scal, symbolname[:-len('_inplace')])
inplace_scalar_op = scalar_op.__class__(scal.transfer_type(0)) inplace_scalar_op = scalar_op.__class__(scal.transfer_type(0))
rval = elemwise.Elemwise(inplace_scalar_op, {0: 0}, name=n, nfunc_spec=((nfunc, nin, nout) if nfunc else None)) rval = elemwise.Elemwise(inplace_scalar_op, {0: 0}, name=n,
nfunc_spec = nfunc and (nfunc, nin, nout))
else: else:
scalar_op = getattr(scal, symbolname) scalar_op = getattr(scal, symbolname)
rval = elemwise.Elemwise(scalar_op, name=n, nfunc_spec=((nfunc, nin, nout) if nfunc else None)) rval = elemwise.Elemwise(scalar_op, name=n,
nfunc_spec = nfunc and (nfunc, nin, nout))
if getattr(symbol, '__doc__', False): if getattr(symbol, '__doc__', False):
rval.__doc__ = symbol.__doc__ + '\n' + rval.__doc__ rval.__doc__ = symbol.__doc__ + '\n' + rval.__doc__
...@@ -1993,9 +2004,6 @@ def round(a, mode="half_away_from_zero"): ...@@ -1993,9 +2004,6 @@ def round(a, mode="half_away_from_zero"):
else: else:
raise Exception("round mode %s is not implemented."%mode) raise Exception("round mode %s is not implemented."%mode)
# def __round_half_to_even(a, dest):
# dest[:] = numpy.around(a)
@_scal_elemwise_with_nfunc('around', 1, 0) @_scal_elemwise_with_nfunc('around', 1, 0)
def round_half_to_even(a): def round_half_to_even(a):
"""round_half_to_even(a)""" """round_half_to_even(a)"""
......
...@@ -365,16 +365,6 @@ def _make_nfunc(name, nin, nout): ...@@ -365,16 +365,6 @@ def _make_nfunc(name, nin, nout):
f = getattr(numpy, name) f = getattr(numpy, name)
return f return f
# if name.endswith("*"):
# name = name[:-1]
# f = getattr(numpy, name)
# def fn(*args):
# args[-1][:] = f(*(args[:-1]))
# return fn
# else:
# f = getattr(numpy, name)
# return f
################ ################
### Elemwise ### ### Elemwise ###
...@@ -416,6 +406,13 @@ class Elemwise(Op): ...@@ -416,6 +406,13 @@ class Elemwise(Op):
* inplace_pattern: a dictionary that maps the index of an output to the * inplace_pattern: a dictionary that maps the index of an output to the
index of an input so the output is calculated inplace using index of an input so the output is calculated inplace using
the input's storage. (Just like destroymap, but without the lists.) the input's storage. (Just like destroymap, but without the lists.)
* nfunc_spec: either None or a tuple of three elements, (nfunc_name, nin, nout) such
that getattr(numpy, nfunc_name) implements this operation, takes nin
inputs and nout **destination** outputs (nout == 0 if the numpy function
does not provide the option of providing a numpy array to store the
results in). Note that nin cannot always be inferred from the scalar op's
own nin field because that value is sometimes 0 (meaning a variable number
of inputs), whereas the numpy function may not have varargs.
""" """
self.name = name self.name = name
self.scalar_op = scalar_op self.scalar_op = scalar_op
...@@ -647,7 +644,14 @@ class Elemwise(Op): ...@@ -647,7 +644,14 @@ class Elemwise(Op):
ufunc_args = inputs # + output_storage ufunc_args = inputs # + output_storage
if self.nfunc and len(inputs) == self.nfunc_spec[1]: if self.nfunc and len(inputs) == self.nfunc_spec[1]:
ufunc = self.nfunc ufunc = self.nfunc
nout = 1 nout = self.nfunc_spec[2]
if nout == 0:
nout = 1
# Unfortunately, the else case does not allow us to
# directly feed the destination arguments to the nfunc
# since it sometimes requires resizing. Doing this
# optimization is probably not worth the effort, since we
# should normally run the C version of the Op.
else: else:
# the second calling form is used because in certain versions of numpy # the second calling form is used because in certain versions of numpy
# the first (faster) version leads to segfaults # the first (faster) version leads to segfaults
...@@ -661,7 +665,8 @@ class Elemwise(Op): ...@@ -661,7 +665,8 @@ class Elemwise(Op):
'for params of shape', [arg.shape for arg in ufunc_args] 'for params of shape', [arg.shape for arg in ufunc_args]
e.args = e.args + errormsg e.args = e.args + errormsg
raise raise
if nout == 1: variables = [variables] if nout == 1:
variables = [variables]
for variable, storage in zip(variables, output_storage): for variable, storage in zip(variables, output_storage):
if hasattr(variable,'shape') and storage[0].shape != variable.shape: if hasattr(variable,'shape') and storage[0].shape != variable.shape:
storage[0].resize(variable.shape) storage[0].resize(variable.shape)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论