Author: Antonio Cuni <[email protected]>
Branch:
Changeset: r61080:ae915d2b7d56
Date: 2013-02-11 11:20 +0100
http://bitbucket.org/pypy/pypy/changeset/ae915d2b7d56/
Log: implement numpypy.outer (by copying it from numpy source)
diff --git a/lib_pypy/numpypy/core/numeric.py b/lib_pypy/numpypy/core/numeric.py
--- a/lib_pypy/numpypy/core/numeric.py
+++ b/lib_pypy/numpypy/core/numeric.py
@@ -428,3 +428,76 @@
True_ = bool_(True)
e = math.e
pi = math.pi
+
+def outer(a,b):
+ """
+ Compute the outer product of two vectors.
+
+ Given two vectors, ``a = [a0, a1, ..., aM]`` and
+ ``b = [b0, b1, ..., bN]``,
+ the outer product [1]_ is::
+
+ [[a0*b0 a0*b1 ... a0*bN ]
+ [a1*b0 .
+ [ ... .
+ [aM*b0 aM*bN ]]
+
+ Parameters
+ ----------
+ a, b : array_like, shape (M,), (N,)
+ First and second input vectors. Inputs are flattened if they
+ are not already 1-dimensional.
+
+ Returns
+ -------
+ out : ndarray, shape (M, N)
+ ``out[i, j] = a[i] * b[j]``
+
+ See also
+ --------
+ inner, einsum
+
+ References
+ ----------
+ .. [1] : G. H. Golub and C. F. van Loan, *Matrix Computations*, 3rd
+ ed., Baltimore, MD, Johns Hopkins University Press, 1996,
+ pg. 8.
+
+ Examples
+ --------
+ Make a (*very* coarse) grid for computing a Mandelbrot set:
+
+ >>> rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5))
+ >>> rl
+ array([[-2., -1., 0., 1., 2.],
+ [-2., -1., 0., 1., 2.],
+ [-2., -1., 0., 1., 2.],
+ [-2., -1., 0., 1., 2.],
+ [-2., -1., 0., 1., 2.]])
+ >>> im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,)))
+ >>> im
+ array([[ 0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j],
+ [ 0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j],
+ [ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
+ [ 0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j],
+ [ 0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j]])
+ >>> grid = rl + im
+ >>> grid
+ array([[-2.+2.j, -1.+2.j, 0.+2.j, 1.+2.j, 2.+2.j],
+ [-2.+1.j, -1.+1.j, 0.+1.j, 1.+1.j, 2.+1.j],
+ [-2.+0.j, -1.+0.j, 0.+0.j, 1.+0.j, 2.+0.j],
+ [-2.-1.j, -1.-1.j, 0.-1.j, 1.-1.j, 2.-1.j],
+ [-2.-2.j, -1.-2.j, 0.-2.j, 1.-2.j, 2.-2.j]])
+
+ An example using a "vector" of letters:
+
+ >>> x = np.array(['a', 'b', 'c'], dtype=object)
+ >>> np.outer(x, [1, 2, 3])
+ array([[a, aa, aaa],
+ [b, bb, bbb],
+ [c, cc, ccc]], dtype=object)
+
+ """
+ a = asarray(a)
+ b = asarray(b)
+ return a.ravel()[:,newaxis]*b.ravel()[newaxis,:]
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
@@ -178,3 +178,18 @@
assert not array_equal(a, array(b))
assert not array_equal(array(a), b)
assert not array_equal(array(a), array(b))
+
+class AppTestNumeric(BaseNumpyAppTest):
+
+ def test_outer(self):
+ from _numpypy import array
+ from numpypy import outer
+ a = [1, 2, 3]
+ b = [4, 5, 6]
+ res = outer(a, b)
+ expected = array([[ 4, 5, 6],
+ [ 8, 10, 12],
+ [12, 15, 18]])
+ assert (res == expected).all()
+
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit