Hi, I was wondering what an empty matrix is, and what it is useful for (by empty matrix, I mean something created by numpy.matrix([])) ? Using those crash some functions (see for example scipy ticket #381), and I am not sure how to fix this bug. David
David Cournapeau wrote:
Hi,
I was wondering what an empty matrix is, and what it is useful for (by empty matrix, I mean something created by numpy.matrix([])) ? Using those crash some functions (see for example scipy ticket #381), and I am not sure how to fix this bug.
David _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
empty(...) empty((d1,...,dn),dtype=float,order='C') Return a new array of shape (d1,...,dn) and given type with all its entries uninitialized. This can be faster than zeros. Nils
Nils Wagner wrote:
David Cournapeau wrote:
Hi,
I was wondering what an empty matrix is, and what it is useful for (by empty matrix, I mean something created by numpy.matrix([])) ? Using those crash some functions (see for example scipy ticket #381), and I am not sure how to fix this bug.
David _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
empty(...) empty((d1,...,dn),dtype=float,order='C')
Return a new array of shape (d1,...,dn) and given type with all its entries uninitialized. This can be faster than zeros.
I understand numpy.empty, but this is different: an empty matrix is not equivalent at all to empty. What I am talking about is what is the nature of something lie numpy.array((1, 0)) ? I am actually wondering whether it makes sense at all. a = numpy.matrix([]) b = a + 1 does not raise any error, but I don't see how this should be considered meaningful ? David
As for me, I used empty matrices in MATLAB as well as python rather often. Simple example: from numpy import array, hstack #... m = A.shape[1] a = array(()).reshape(0,m) for i in some_ind: a = hstack((a, A[i])) for i in some_ind2: a = hstack((a, Aeq[i])) return a other example: from numpy import append def myfunc(arr, arr2 = numpy.array(())) assert(arr.ndim==1 and arr2.ndim==1) return append(arr, arr2, 1) HTH, Dmitrey David Cournapeau wrote:
Hi,
I was wondering what an empty matrix is, and what it is useful for (by empty matrix, I mean something created by numpy.matrix([])) ? Using those crash some functions (see for example scipy ticket #381), and I am not sure how to fix this bug.
David _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
David Cournapeau wrote:
Hi,
I was wondering what an empty matrix is, and what it is useful for (by empty matrix, I mean something created by numpy.matrix([])) ? Using those crash some functions (see for example scipy ticket #381), and I am not sure how to fix this bug.
David
Empty input to an algorithm can often be handled naturally by empty matrices in the implementation. Quite often I find that if I've coded things right, the boundary empty input case is handled naturally in this way. I would prefer that no functions crashed when passed empty matrices. Just my 2 cents, John.
David Cournapeau wrote:
Hi,
I was wondering what an empty matrix is, and what it is useful for (by empty matrix, I mean something created by numpy.matrix([])) ? Using those crash some functions (see for example scipy ticket #381), and I am not sure how to fix this bug.
David
Empty input to an algorithm can often be handled naturally by empty matrices in the implementation. Quite often I find that if I've coded things right, the boundary empty input case is handled naturally in this way. I would prefer that no functions crashed when passed empty matrices. Well, I think nobody argues that scipy function should crash whatever input you give :). The problem is more how to treat them. For example, using numpy.linalg.pinv crashes numpy right now, det and fft do not work, and norm returns 0. This is seems inconcistent to me. If norm is 0, why det should not be ? Personally, I would say both should be errors, but I don't use empty arrays, so I don't have a good grasp of
John Reid wrote: their usefulness. David
Ok so crashing is always bad. What I should have said is that I think errors are bad in almost all cases as well. The norm returning zero seems sensible to me so perhaps inversions and such should raise exceptions. I would much prefer no errors were raised except where necessary. Like I said I tend to find that if I write code that handles the non-empty case naturally then I don't have to change it to do something sensible for the empty boundary case. John.
David Cournapeau schrieb:
Well, I think nobody argues that scipy function should crash whatever input you give :). The problem is more how to treat them. For example, using numpy.linalg.pinv crashes numpy right now, det and fft do not work, and norm returns 0. This is seems inconcistent to me. If norm is 0, why det should not be ? Personally, I would say both should be errors, but I don't use empty arrays, so I don't have a good grasp of their usefulness.
First of all I suggest to change terminology from "empty" (because of the confusion with numpy.empty()) to something like zero-length array. Such zero-length arrays are useful for generic code, so I agree with other posters that errors should normally not be raised just because of this. Then I would suggest that all functions that return arrays should return some conformable zero-length array. For example, IMHO e = np.linalg.inv(np.ones((0,0))) should return another (0,0)-array (it crashes right now). For things like sum() or det(), I guess the problem is that such reduce-like methods return scalars for other good reasons, and therefore they cannot be zero-length. I don't see a better solution for this inconsistency except to document this and tell people to watch out; but maybe the real experts know better!? just my 2€-ct, sven
participants (5)
-
David Cournapeau
-
dmitrey
-
John Reid
-
Nils Wagner
-
Sven Schreiber