[python-dtcwt] 243/497: utils: re-write reflect to be non-iterative
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Tue Jul 21 18:06:10 UTC 2015
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch debian/sid
in repository python-dtcwt.
commit 4ba866fb19ff25825d9c19927ca9cf5062a9da89
Author: Rich Wareham <rjw57 at cam.ac.uk>
Date: Mon Jan 27 14:46:29 2014 +0000
utils: re-write reflect to be non-iterative
dtcwt.utils.reflect uses an iterative method to clip the input within
the bounds specified. This in effect makes the runtime proportional to
the magnitude of the input which is an unexpected performance
characteristic.
Taking the implementation from OpenCL, rewrite reflect() to use a
non-iterative, if far more opaque, method.
---
dtcwt/utils.py | 29 +++++++++--------------------
1 file changed, 9 insertions(+), 20 deletions(-)
diff --git a/dtcwt/utils.py b/dtcwt/utils.py
index dcfcd78..c338eef 100644
--- a/dtcwt/utils.py
+++ b/dtcwt/utils.py
@@ -100,29 +100,18 @@ def reflect(x, minx, maxx):
converted into a waveform which ramps linearly up and down between *minx* and
*maxx*. If *x* contains integers and *minx* and *maxx* are (integers + 0.5), the
ramps will have repeated max and min samples.
-
+
.. codeauthor:: Rich Wareham <rjw57 at cantab.net>, Aug 2013
.. codeauthor:: Nick Kingsbury, Cambridge University, January 1999.
-
- """
-
- # Copy x to avoid in-place modification
- y = np.array(x, copy=True)
- # Reflect y in maxx.
- t = y > maxx
- y[t] = (2*maxx - y[t]).astype(y.dtype)
-
- while np.any(y < minx):
- # Reflect y in minx.
- t = y < minx
- y[t] = (2*minx - y[t]).astype(y.dtype)
-
- # Reflect y in maxx.
- t = y > maxx
- y[t] = (2*maxx - y[t]).astype(y.dtype)
-
- return y
+ """
+ x = np.asanyarray(x)
+ rng = maxx - minx
+ rng_by_2 = 2 * rng
+ mod = np.fmod(x - minx, rng_by_2)
+ normed_mod = np.where(mod < 0, mod + rng_by_2, mod)
+ out = np.where(normed_mod >= rng, rng_by_2 - normed_mod, normed_mod) + minx
+ return np.array(out, dtype=x.dtype)
# note that this decorator ignores **kwargs
# From https://wiki.python.org/moin/PythonDecoratorLibrary#Alternate_memoize_as_nested_functions
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-dtcwt.git
More information about the debian-science-commits
mailing list