I added some content to the "NumPy/SciPy for Matlab users" page on the scipy wiki. But my knowledge of NumPy/SciPy isn't sufficient to fill in the whole chart of equivalents that I laid out. If folks who know both could browse by and maybe fill in a blank or two, that would be great. I think this will be a helpful "getting started" page for newbies to NumPy coming from matlab, like me. One of the most frustrating things is when you sit down and can't figure out how to do the most basic things that do in your sleep in another environment (like making a column vector). So hopefully this page will help. The URL is : http://www.scipy.org/Wiki/NumPy_for_Matlab_Addicts Thanks, Bill Baxter
Hi Bill, On 2/9/06, Bill Baxter <wbaxter@gmail.com> wrote:
I added some content to the "NumPy/SciPy for Matlab users" page on the scipy wiki.
Thanks a lot for doing this. Did you see this excellent reference? Maybe it would be useful to combine effort in some way? http://www.37mm.no/matlab-python-xref.html Best, Matthew
Vidar's documentation is under a GNU Free Documentation License. This is probably a problem with incorporating it directly into the scipy site, although Vidar was at one point happy to incorporate the MATLAB parts into Perry Greenfield and Robert Jedrzejewski's interactive data analysis tutorial. This tutorial used to be on the numarray page but has now disappeared and hasn't quite found it's way onto the scipy site, although it may just be due to a broken link. I've added a link to Vidar's site to the wiki. Gary R. Matthew Brett wrote:
Hi Bill,
On 2/9/06, Bill Baxter <wbaxter@gmail.com> wrote:
I added some content to the "NumPy/SciPy for Matlab users" page on the scipy wiki.
Thanks a lot for doing this. Did you see this excellent reference? Maybe it would be useful to combine effort in some way?
http://www.37mm.no/matlab-python-xref.html
Best,
Matthew
===== Original message from Gary Ruben | 9 Feb 2006:
Vidar's documentation is under a GNU Free Documentation License. This is probably a problem with incorporating it directly into the scipy site, although Vidar was at one point happy to incorporate the MATLAB parts
this was not the intention when i picked an open license for it. i'm not that familiar with the legal stuff, and i guess when first used a GPL/GFDL it always has to be? i also considered CC, but didn't want to spend a lot of time digging into legal stuff: i wanted to make the reference available and reusable to anyone. i don't mind, i wanted to achieve openness and encourage contributions and derivations, and be able to use these to improve and update the original reference. i need to update it with the new NumPy package, but i haven't taken the time to buy the manual and start looking into it yet. will including NumPy commands be a problem related to licensing on the NumPy documentation? also, i'd prefer to publish it on a more appropriate site (like scipy.org, sourceforge.net, or wherever useful) when i feel the documents are more complete. but note that this is not really a Numerical Python and Matlab thing, but a framework to get from math environment a to b. it could also (when i include NumPy) help transition between Numeric/numarray/NumPy: this can easily be generated as a separate reference (i use XSL and LaTeX). (although i did this to support my own transition from Matlab to non-commercial alternatives, e.g. Python and R/RPy, Gnuplot, etc for plotting.) thanks for cross-posting this to me, Gary. i'm jumping right into this list, so please be indulgent if i seem uninformed on late talks here. A brief observation on "NumPy for Matlab Addicts": The section "Some Key Differences" says nothing about the amount of routines found in Matlab toolboxes for Optimization, Control engineering, Wavelets, etc. for these there are no real alternatives. kind regards, Vidar Bronken Gundersen
On Fri, 10 Feb 2006, Vidar Gundersen apparently wrote:
i guess when first used a GPL/GFDL it always has to be?
If you own the copyright, you can license it anyway you want at any time. You have already licensed it under the GFDL, but you can license it other ways as well.
i also considered CC, but didn't want to spend a lot of time digging into legal stuff: i wanted to make the reference available and reusable to anyone.
If that is really the goal, then just include a statement placing it in the public domain. E.g., Copyright: This document has been placed in the public domain. If you want attribution, use an attribution license: http://creativecommons.org/licenses/by/2.5/ (Be sure to say what you want as attribution.) Cheers, Alan Isaac PS IANAL!
On Thursday 09 February 2006 3:21 am, Bill Baxter wrote:
I added some content to the "NumPy/SciPy for Matlab users" page on the scipy wiki.
But my knowledge of NumPy/SciPy isn't sufficient to fill in the whole chart of equivalents that I laid out. If folks who know both could browse by and maybe fill in a blank or two, that would be great. I think this will be a helpful "getting started" page for newbies to NumPy coming from matlab, like me. One of the most frustrating things is when you sit down and can't figure out how to do the most basic things that do in your sleep in another environment (like making a column vector). So hopefully this page will help.
The URL is : http://www.scipy.org/Wiki/NumPy_for_Matlab_Addicts
I filled in a couple of places, where I could. I have a question about upcasting related to this example: a with elements less than 0.5 zeroed out: Matlab: a .* (a>0.5) NumPy: where(a<0.5, 0, a) I think numpy should be able to do a*a>0.5 as well, but instead one must do: a*(a>0.5).astype('i') Is it desireable to upcast bools in this case? I think so, one could always recover the current behavior by doing: (a*(a>0.5)).astype('?') Darren
On Thursday 09 February 2006 7:03 am, Darren Dale wrote:
I have a question about upcasting related to this example:
a with elements less than 0.5 zeroed out: Matlab: a .* (a>0.5) NumPy: where(a<0.5, 0, a)
I think numpy should be able to do a*a>0.5 as well, but instead one must do: a*(a>0.5).astype('i')
Is it desireable to upcast bools in this case? I think so, one could always recover the current behavior by doing: (a*(a>0.5)).astype('?')
oops: I should have been doing a*(a>0.5), the order of operations is important. My mistake.
Hi, The example of ndim to give the rank is not the same as the Matlab function rank(a). See http://en.wikipedia.org/wiki/Rank_of_a_matrix for definition of rank that I would think that most people would use if they use Matlab and is provided by rank(a). I have not used the latest numpy but the equivalent function is not present in numarray/Numeric (to my knowledge) so you have to find some other way like using svd. Regards Bruce On 2/9/06, Bill Baxter <wbaxter@gmail.com> wrote:
I added some content to the "NumPy/SciPy for Matlab users" page on the scipy wiki.
But my knowledge of NumPy/SciPy isn't sufficient to fill in the whole chart of equivalents that I laid out. If folks who know both could browse by and maybe fill in a blank or two, that would be great. I think this will be a helpful "getting started" page for newbies to NumPy coming from matlab, like me. One of the most frustrating things is when you sit down and can't figure out how to do the most basic things that do in your sleep in another environment (like making a column vector). So hopefully this page will help.
The URL is : http://www.scipy.org/Wiki/NumPy_for_Matlab_Addicts
Thanks, Bill Baxter
On Thu, 9 Feb 2006, Bruce Southey apparently wrote:
The example of ndim to give the rank is not the same as the Matlab function rank(a). See http://en.wikipedia.org/wiki/Rank_of_a_matrix for definition of rank that I would think that most people would use if they use Matlab
Coming from GAUSS and having never studies tensors, I was also surprised by the 'rank' terminology. I believe this is why Travis changed to ndim, which is less likely to confuse users coming from a linear algebra perspective. Unfortunately the SciPy book currently uses the term 'rank' in the two conflicting ways. (It uses 'rank' in the linear algebra sense only in the discussion of lstsq on p.145.) It might be helpful for the tensor sense to always be qualified as 'tensor rank'? Cheers, Alan Isaac
On 2/9/06, Alan G Isaac <aisaac@american.edu> wrote:
Unfortunately the SciPy book currently uses the term 'rank' in the two conflicting ways. (It uses 'rank' in the linear algebra sense only in the discussion of lstsq on p.145.) It might be helpful for the tensor sense to always be qualified as 'tensor rank'?
Another alternative would be "number of axes." I also find a glossary used by the J language (an APL descendant) useful in array discussions. See <http://www.jsoftware.com/books/help/jforc/glossary.htm>. Here is how J documentation explains the difference in their terminology and that of the C language: "What C calls an n-dimensional array of rank i×j×…×k is in J an array of rank n with axes of length i,j,…,k." <http://www.jsoftware.com/books/help/jforc/declarations.htm>
I find 'number of axes' to be even more confusing than 'dimension'. Both sound to me like they're talking about the number of components in a vector (e.g. 3-dimensional space vs 2-dimensional space), but axes moreso. The word dimension has a lot of uses, and in most programming languages arrays are described as being one, two or three dimensional etc. So that makes sense. But I can't think of any common usages of axis that aren't related to vectors in a vector space. But that's just me. Seems like this debate probably came and went a long time ago. What is right probably depends mostly on what sort of math you spend your time doing. --bb On 2/10/06, Sasha <ndarray@mac.com> wrote:
On 2/9/06, Alan G Isaac <aisaac@american.edu> wrote:
Unfortunately the SciPy book currently uses the term 'rank' in the two conflicting ways. (It uses 'rank' in the linear algebra sense only in the discussion of lstsq on p.145.) It might be helpful for the tensor sense to always be qualified as 'tensor rank'?
Another alternative would be "number of axes." I also find a glossary used by the J language (an APL descendant) useful in array discussions. See <http://www.jsoftware.com/books/help/jforc/glossary.htm >.
Here is how J documentation explains the difference in their terminology and that of the C language: "What C calls an n-dimensional array of rank i×j×…×k is in J an array of rank n with axes of length i,j,…,k." <http://www.jsoftware.com/books/help/jforc/declarations.htm>
[ regarding the way of describing arrays vs. matlab's matrices, and the use of 'dimension', 'rank', 'number of axes', etc.] Let's not introduce new terms where none are needed. These concepts have had well-established names (tensor rank and matrix rank) for a long time. It may be a good idea to add a local glossary page reminding anyone of what the definitions are, but for as long as I remember reading literature on these topics, the two terms have been fully unambiguous. A numpy array with length(array.shape)==d is closest to a rank d tensor (minus the geometric co/contravariance information). A d=2 array can be used to represent a matrix, and linear algebra operations can be performed on it; if a Matrix object is built out of it, a number of things (notably the * operator) are then performed in the linear algebra sense (and not element-wise). The rank of a matrix has nothing to do with the shape attribute of the underlying array, but with the number of non-zero singular values (and for floating-point matrices, is best defined up to a given tolerance). Since numpy is a n-dimensional array package, it may be convenient to introduce a matrix_rank() routine which does what matlab's rank() for 2-d arrays and matrices, while raising an error for any other shape. This would also make it explicit that this operation is only well-defined for 2-d objects. My 1e-2, f
For what it's worth, matlab's rank function just calls svd, and returns the number singular values greater than a tolerance. The implementation is a whopping 5 lines long. On 2/10/06, Fernando Perez <Fernando.Perez@colorado.edu> wrote:
Since numpy is a n-dimensional array package, it may be convenient to introduce a matrix_rank() routine which does what matlab's rank() for 2-d arrays and matrices, while raising an error for any other shape. This would also make it explicit that this operation is only well-defined for 2-d objects.
Or put it in numpy.linalg, which also makes it pretty clear what the scope is. --bb
Anyone know what the terms are for redistribution of applications built with Matlab? I searched around their site a bit but couldn't find anything conclusive. One page seemed to be saying there was a per-application fee for distributing a matlab-based application, but other pages made it sound more like it was no extra charge. If the former, then that's another point that should go in the 'key differences' section. --bb
Bill Baxter wrote:
For what it's worth, matlab's rank function just calls svd, and returns the number singular values greater than a tolerance. The implementation is a whopping 5 lines long.
Yup, and it would be pretty much the same 5 lines in numpy, with the same semantics. Here's a quick and dirty implementation for old-scipy (I don't have new-scipy on this box): def matrix_rank(arr,tol=1e-8): """Return the matrix rank of an input array.""" arr = scipy.asarray(arr) if len(arr.shape) != 2: raise ValueError('Input must be a 2-d array or Matrix object') svdvals = scipy.linalg.svdvals(arr) return sum(scipy.where(svdvals>tol,1,0)) If you really hate readability and error-checking, it's a one-liner :) matrix_rank = lambda arr,tol=1e-8: sum(scipy.where(scipy.linalg.svdvals(arr)>tol,1,0)) Looks OK (RA is RandomArray from Numeric): In [21]: matrix_rank([[1,0],[0,0]]) Out[21]: 1 In [22]: matrix_rank(RA.random((3,3))) Out[22]: 3 In [23]: matrix_rank([[1,0],[0,0]]) Out[23]: 1 In [24]: matrix_rank([[1,0],[1,0]]) Out[24]: 1 In [25]: matrix_rank([[1,0],[0,1]]) Out[25]: 2 In [26]: matrix_rank(RA.random((3,3)),1e-1) Out[26]: 2 In [48]: matrix_rank([[1,0],[1,1e-8]]) Out[48]: 1 In [49]: matrix_rank([[1,0],[1,1e-4]]) Out[49]: 2 In [50]: matrix_rank([[1,0],[1,1e-8]],1e-9) Out[50]: 2 Cheers, f
Fernando Perez schrieb:
Bill Baxter wrote:
For what it's worth, matlab's rank function just calls svd, and returns the number singular values greater than a tolerance. The implementation is a whopping 5 lines long.
Yup, and it would be pretty much the same 5 lines in numpy, with the same semantics.
Here's a quick and dirty implementation for old-scipy (I don't have new-scipy on this box):
Is there any reason not to use the algorithm implicit in lstsq, as in: rk = linalg.lstsq(M, ones(p))[2] (where M is the matrix to check, and p==M.shape[0]) thanks, sven
Sven Schreiber wrote:
Fernando Perez schrieb:
Bill Baxter wrote:
For what it's worth, matlab's rank function just calls svd, and returns the number singular values greater than a tolerance. The implementation is a whopping 5 lines long.
Yup, and it would be pretty much the same 5 lines in numpy, with the same semantics.
Here's a quick and dirty implementation for old-scipy (I don't have new-scipy on this box):
Is there any reason not to use the algorithm implicit in lstsq, as in: rk = linalg.lstsq(M, ones(p))[2]
Simplicity? lstsq goes through a lot of contortions (needed for other reasons), and uses lapack's *gelss. If you read its man page: PURPOSE DGELSS computes the minimum norm solution to a real linear least squares problem: Minimize 2-norm(| b - A*x |). using the singular value decomposition (SVD) of A. A is an M-by-N matrix which may be rank-deficient. Several right hand side vectors b and solution vectors x can be handled in a single call; they are stored as the columns of the M-by-NRHS right hand side matrix B and the N-by-NRHS solution matrix X. The effective rank of A is determined by treating as zero those singu- lar values which are less than RCOND times the largest singular value. So you've gone through all that extra complexity, to get back what a direct call to svd would give you (caveat: the quick version I posted used absolute tolerance, while this one is relative; that can be trivially fixed). Given that a direct SVD call fits the definition of what we are computing (a numerical estimation of a matrix rank), I completely fail to see the point of going through several additional layers of unnecessary complexity, which both add cost and obscure the intent of the calculation. But perhaps I'm missing something... Cheers, f
Oh, yeh. I can see the problem with the phrase "rank of a matrix". It does sound like it means the linear algebra rank of rank/nullity fame. I changed the description on the page a bit. Thanks for catching that. --bb On 2/9/06, Bruce Southey <bsouthey@gmail.com> wrote:
Hi, The example of ndim to give the rank is not the same as the Matlab function rank(a). See http://en.wikipedia.org/wiki/Rank_of_a_matrix for definition of rank that I would think that most people would use if they use Matlab and is provided by rank(a).
I have not used the latest numpy but the equivalent function is not present in numarray/Numeric (to my knowledge) so you have to find some other way like using svd.
Regards Bruce
I added some content to the "NumPy/SciPy for Matlab users" page on the scipy wiki.
But my knowledge of NumPy/SciPy isn't sufficient to fill in the whole chart of equivalents that I laid out. If folks who know both could browse by and maybe fill in a blank or two,
On 2/9/06, Bill Baxter <wbaxter@gmail.com> wrote: that would be great. I think this will be a helpful "getting started" page for newbies to NumPy coming from matlab, like me. One of the most frustrating things is when you sit down and can't figure out how to do the most basic things that do in your sleep in another environment (like making a column vector). So hopefully this page will help.
The URL is : http://www.scipy.org/Wiki/NumPy_for_Matlab_Addicts
Thanks, Bill Baxter
Some kind soul added 'svd' and 'inv' to the NumPy/SciPi columns, but those don't seem to be defined, at least for the versions of NumPy/SciPy that I have. Are they new? Or are they perhaps defined by a 3rd package in your environment? By the way, is there any python way to tell which package a symbol is coming from? --bb
Bill Baxter <wbaxter@gmail.com> writes:
Some kind soul added 'svd' and 'inv' to the NumPy/SciPi columns, but those don't seem to be defined, at least for the versions of NumPy/SciPy that I have. Are they new? Or are they perhaps defined by a 3rd package in your environment?
They're in numpy.linalg.
By the way, is there any python way to tell which package a symbol is coming from?
Check it's __module__ attribute. -- |>|\/|< /--------------------------------------------------------------------------\ |David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/ |cookedm@physics.mcmaster.ca
Some kind soul added 'svd' and 'inv' to the NumPy/SciPi columns, but those
don't seem to be defined, at least for the versions of NumPy/SciPy that I have. Are they new? Or are they perhaps defined by a 3rd package in your environment?
They're in numpy.linalg.
Ooooh! Lots of goodies there!
By the way, is there any python way to tell which package a symbol is coming
from?
Check it's __module__ attribute.
Ah, perfect. I see it's also mentioned in help(thing) for thing. Thanks. --bb
David M. Cooke wrote:
Bill Baxter <wbaxter@gmail.com> writes:
Some kind soul added 'svd' and 'inv' to the NumPy/SciPi columns, but those don't seem to be defined, at least for the versions of NumPy/SciPy that I have. Are they new? Or are they perhaps defined by a 3rd package in your environment?
They're in numpy.linalg.
By the way, is there any python way to tell which package a symbol is coming from?
Check it's __module__ attribute.
Yes, but not all objects have this attribute and some do not yet have a docstring. Colin W.
On Fri, 10 Feb 2006, Bill Baxter apparently wrote:
Some kind soul added 'svd' and 'inv' to the NumPy/SciPi columns, but those don't seem to be defined, at least for the versions of NumPy/SciPy that I have.
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
from numpy.linalg import svd, inv
hth, Alan Isaac
Bill Baxter wrote:
By the way, is there any python way to tell which package a symbol is coming from?
Yes. Don't use "import *". There is a long tradition of using NumPy this way: from numpy import * But now I always use it this way: import numpy as N (or nx, or whatever short name you want). I like it, because it's always clear where stuff is coming from. numpy's addition of a number of methods for what used to be functions helps make this more convenient too. "Namespaces are one honking great idea -- let's do more of those!" from: http://www.python.org/doc/Humor.html#zen -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
On Thu, Feb 09, 2006 at 05:21:10PM +0900, Bill Baxter wrote:
I added some content to the "NumPy/SciPy for Matlab users" page on the scipy wiki.
But my knowledge of NumPy/SciPy isn't sufficient to fill in the whole chart of equivalents that I laid out.
One of my colleagues also asked about the shortest way to do array concatenation. In Octave that would be [1, 0, 1:4, 0, 1] Using numpy we currently do concatenate([[1, 0], arange(1,5), [0, 1]]) or vstack(...) The "+" operator now means something else, so you can't do [1,0] + arange(1,5) + [0,1], while [1, 0, arange(1,5), 0, 1] produces [1, 0, array([1, 2, 3, 4]), 0, 1] which can't be converted to an array by simply doing array([[1, 0, array([1, 2, 3, 4]), 0, 1]]) I'll add it to the wiki once I know what the best method is. Regards Stéfan
On Fri, 10 Feb 2006, Stefan van der Walt apparently wrote:
In Octave that would be [1, 0, 1:4, 0, 1] Using numpy we currently do concatenate([[1, 0], arange(1,5), [0, 1]]) or vstack(...)
numpy.r_[1,0,range(1,5),0,1] fwiw, Alan Isaac
Alan G Isaac wrote:
On Fri, 10 Feb 2006, Stefan van der Walt apparently wrote:
In Octave that would be [1, 0, 1:4, 0, 1] Using numpy we currently do concatenate([[1, 0], arange(1,5), [0, 1]]) or vstack(...)
numpy.r_[1,0,range(1,5),0,1]
fwiw, Alan Isaac
This seems to be a neat idea but not in the usual Python style.
help(numpy.r_) Help on concatenator in module numpy.lib.index_tricks object:
class concatenator(__builtin__.object) | Translates slice objects to concatenation along an axis. | | Methods defined here: | | __getitem__(self, key) | | __getslice__(self, i, j) | | __init__(self, axis=0, matrix=False) | | __len__(self) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __dict__ = <dictproxy object> | dictionary for instance variables (if defined) | | __weakref__ = <attribute '__weakref__' of 'concatenator' objects> | list of weak references to the object (if defined) The help refers to concatenator, presumably r_ is a synonym, but that name is not available to the user:
numpy.concatenator Traceback (most recent call last): File "<interactive input>", line 1, in ? AttributeError: 'module' object has no attribute 'concatenator'
If r_ is a class, couldn't it have have a more mnemonic name and, in the usual Python style, start with an upper case letter? help(numpy.r_.__init__) Help on method __init__ in module numpy.lib.index_tricks: __init__(self, axis=0, matrix=False) unbound numpy.lib.index_tricks.concatenator method
Colin W.
Colin J. Williams wrote:
numpy.r_[1,0,range(1,5),0,1]
This seems to be a neat idea but not in the usual Python style.
Exactly. couldn't it at least get a meaningful, but short, name? And is there a way to use it to concatenate along other axis? I couldn't see a way. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
Christopher Barker wrote:
Colin J. Williams wrote:
numpy.r_[1,0,range(1,5),0,1]
This seems to be a neat idea but not in the usual Python style.
Exactly. couldn't it at least get a meaningful, but short, name?
It is meaningful :-) r_ means row concatenation... (but, it has taken on more functionality than that). What name do you suggest?
And is there a way to use it to concatenate along other axis? I couldn't see a way.
Yes, add a string at the end with the number of the axis you want to concatenate along. But, you have to have that axis to start with or the result is no different. The default is to concatenate along the last axis. Thus (the ndmin keyword forces the array to have a minimum number of dimensions --- prepended). a = array([1,2,3],ndmin=2) b = array([1,2,3],ndmin=2) c = r_[a,b,'0'] print c [[1 2 3] [1 2 3]] print r_[a,b,'1'] [[1 2 3 1 2 3]] -Travis
Travis Oliphant wrote:
Christopher Barker wrote:
Colin J. Williams wrote:
numpy.r_[1,0,range(1,5),0,1]
This seems to be a neat idea but not in the usual Python style.
Exactly. couldn't it at least get a meaningful, but short, name?
It is meaningful :-) r_ means row concatenation... (but, it has taken on more functionality than that). What name do you suggest?
"cat"? "rcat"? "catr"? "catter"? Eric
I would be against any meaningful name because it will look too much like a function and people will be trying to use (...) instead of [...] with it. A single-letter identifier will look more like syntax and the concatenator is really just a clever way to take advantage of Python syntax that recognizes slices inside []. Novices may just think that something like c[1:3,9:20] is an array literal like r"xyz" for raw strings (another argument against "r"!). On 2/10/06, Eric Firing <efiring@hawaii.edu> wrote:
Travis Oliphant wrote:
Christopher Barker wrote:
Colin J. Williams wrote:
numpy.r_[1,0,range(1,5),0,1]
This seems to be a neat idea but not in the usual Python style.
Exactly. couldn't it at least get a meaningful, but short, name?
It is meaningful :-) r_ means row concatenation... (but, it has taken on more functionality than that). What name do you suggest?
"cat"? "rcat"? "catr"? "catter"?
Eric
------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
Alan G Isaac wrote:
On Fri, 10 Feb 2006, Stefan van der Walt apparently wrote:
In Octave that would be [1, 0, 1:4, 0, 1] Using numpy we currently do concatenate([[1, 0], arange(1,5), [0, 1]]) or vstack(...)
numpy.r_[1,0,range(1,5),0,1]
or even faster numpy.r_[1,0,1:5,0,1] The whole point of r_ is to allow you to use slice notation to build ranges easily. I wrote it precisely to make it easier to construct arrays in a simliar style that Matlab allows. -Travis
On 2/10/06, Travis Oliphant <oliphant@ee.byu.edu> wrote:
The whole point of r_ is to allow you to use slice notation to build ranges easily. I wrote it precisely to make it easier to construct arrays in a simliar style that Matlab allows.
Maybe it is just me, but r_ is rather unintuitive. I would expect something like this to be called "c" for "combine" or "concatenate." This is the name used by S+ and R.
From R manual: """ c package:base R Documentation Combine Values into a Vector or List ... Examples: c(1,7:9) ... """
Sasha wrote:
On 2/10/06, Travis Oliphant <oliphant@ee.byu.edu> wrote:
The whole point of r_ is to allow you to use slice notation to build ranges easily. I wrote it precisely to make it easier to construct arrays in a simliar style that Matlab allows.
Maybe it is just me, but r_ is rather unintuitive. I would expect something like this to be called "c" for "combine" or "concatenate." This is the name used by S+ and R.
I agree that c or c_ (don't care which) is more intuitive but I can understand why it's ended up as it has. Even v or v_ for 'vector' or a or a_ for 'array' would also make sense to me. I must say that Travis's example numpy.r_[1,0,1:5,0,1] highlights my pet hate with python - that the upper limit on an integer range is non-inclusive. I'm sure the BDFL has some excuse for this silliness. Gary R
On 2/10/06, Gary Ruben <gruben@bigpond.net.au> wrote:
... I must say that Travis's example numpy.r_[1,0,1:5,0,1] highlights my pet hate with python - that the upper limit on an integer range is non-inclusive.
In this case you must hate that an integer range starts at 0 (I don't think you would want len(range(10)) to be 11). If this is the case, I don't blame you: it is silly to start counting at 0, but algorithmically it is quite natural. Semi-closed integer ranges have many algorithmic advantages as well such as length = (stop - start)/step, empty range can be recognized by start=stop test regardless of step, adjacent ranges - start2=stop1 (again no need to know step) etc.
I'm sure the BDFL has some excuse for this silliness.
Maybe he does not like Fortran :-) PS: What's your second favorite language (I assume that python is the first :-)?
Sasha wrote:
On 2/10/06, Gary Ruben <gruben@bigpond.net.au> wrote:
... I must say that Travis's example numpy.r_[1,0,1:5,0,1] highlights my pet hate with python - that the upper limit on an integer range is non-inclusive.
In this case you must hate that an integer range starts at 0 (I don't think you would want len(range(10)) to be 11).
Actually, that wouldn't bother me and I'm not really fussed by whether a language chooses 0 or 1 based integer ranges, as long as you can override the default, but 0 seems more natural for any programming language.
If this is the case, I don't blame you: it is silly to start counting at 0, but algorithmically it is quite natural. Semi-closed integer ranges have many algorithmic advantages as well such as length = (stop - start)/step, empty range can be recognized by start=stop test regardless of step, adjacent ranges - start2=stop1 (again no need to know step) etc.
Thanks for the explanation Sasha. It does make some sense in terms of your examples, but I'll remain unconvinced.
I'm sure the BDFL has some excuse for this silliness.
Maybe he does not like Fortran :-)
PS: What's your second favorite language (I assume that python is the first :-)?
It's not Fortran-77! If I say it's Object Pascal (i.e. Delphi) you may begin to see where my range specifier preference comes from. Pascal lets you define things like enumeration type ranges like Monday..Friday. It would seem nonsensical to define the range of working weekdays as Monday..Saturday. I'm pretty competent with C, less-so with C++ and I've totally missed out on Java. One day I might have a play with Haskell and Ruby. Actually I see that Ruby sidesteps my pet hate by providing both types of range specifiers. I can't see myself defecting to the enemy just because of this though, Gary R.
Hi, I noted on http://www.scipy.org/Wiki/NumPy_for_Matlab_Users matlab a(1:5,:) numpy a[0:4] or a[0:4,:] a[0:4] or a[0:4,:] the first five rows of a I think this is wrong! in numpy it would be: a[:5] (or a[0:5] ) for the first five elements To the best of my knowledge (I have never used Matlab myself!) this is one of the biggest points of confusion for Matlab users !! WHY DOES a[4:6] NOT INCLUDE THE ELEMENTS 4,5 *AND* 6 ??? The only explanation I have is that its a) like a C/C++-for-loop (for(i=4;i<6;i++) --> note the '<' (not '<=') b) it then also always is true that: "last" minus first is equal to number-of-elements (example: 6-4 = 2) c) it turns out from experience that this convention WILL save you lots of '-1' and '+1' in your code (actually almost all of them) [if I see a "+1" in a Matlab person's numpy code I can almost always guess that he/she made a mistake !] Maybe this paragraph could be added to the wiki ... Thanks for this wiki page - I think I looks great - Sebastian Haase
Ah, right you are. Fixed. Thanks. I guess the python version could also be written a[:5]. --bb On 3/3/06, Sebastian Haase <haase@msg.ucsf.edu> wrote:
Hi, I noted on http://www.scipy.org/Wiki/NumPy_for_Matlab_Users
matlab a(1:5,:) numpy a[0:4] or a[0:4,:] a[0:4] or a[0:4,:] the first five rows of a
I think this is wrong!
in numpy it would be: a[:5] (or a[0:5] ) for the first five elements
To the best of my knowledge (I have never used Matlab myself!) this is one of the biggest points of confusion for Matlab users !! WHY DOES a[4:6] NOT INCLUDE THE ELEMENTS 4,5 *AND* 6 ???
The only explanation I have is that its a) like a C/C++-for-loop (for(i=4;i<6;i++) --> note the '<' (not '<=') b) it then also always is true that: "last" minus first is equal to number-of-elements (example: 6-4 = 2) c) it turns out from experience that this convention WILL save you lots of '-1' and '+1' in your code (actually almost all of them) [if I see a "+1" in a Matlab person's numpy code I can almost always guess that he/she made a mistake !]
Maybe this paragraph could be added to the wiki ...
Thanks for this wiki page - I think I looks great
- Sebastian Haase
------------------------------------------------------- This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory! http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
-- William V. Baxter III OLM Digital Kono Dens Building Rm 302 1-8-8 Wakabayashi Setagaya-ku Tokyo, Japan 154-0023 +81 (3) 3422-3380
Bill Baxter wrote:
Ah, right you are. Fixed. Thanks. I guess the python version could also be written a[:5].
I've just added that -- it's a very useful idiom. by the way, to prevent [:5] from being interpreted as a link by the Wiki software, I needed to wrap it in ``, to make it look like code. I think that's a good idea for all the code in that table. Maybe we can go in a bit at a time and add the ``. I did a few lines near the one I was working on anyway. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
===== Original message from Sebastian Haase | 3 Mar 2006:
there is one other difference, which i think is not so easily seen, and not noted on the above mentioned page: a(:) in Matlab, will flatten the array by following each column, whereas a.flatten() in NumPy will walk through the matrix like we read comics, putting each row after each other: In [1]: a = arange(9).reshape(3,-1) In [2]: a Out[2]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [3]: a.flatten() Out[3]: array([0, 1, 2, 3, 4, 5, 6, 7, 8]) In [4]: a.flatten(1) # same output as a(:) in Matlab Out[4]: array([0, 3, 6, 1, 4, 7, 2, 5, 8]) the user have to be aware of this when reshaping matrices also. I've updated my similar Matlab-Python reference to cover NumPy: http://37mm.no/mpy/matlab-numpy.html (example plot graphics is not yet included in the HTML version.) See other command equivalents, like IDL or R commands in NumPy, here: http://37mm.no/matlab-python-xref.html Vidar___
participants (17)
-
Alan G Isaac
-
Bill Baxter
-
Bruce Southey
-
Christopher Barker
-
Colin J. Williams
-
cookedm@physics.mcmaster.ca
-
Darren Dale
-
Eric Firing
-
Fernando Perez
-
Gary Ruben
-
Matthew Brett
-
Sasha
-
Sebastian Haase
-
Stefan van der Walt
-
Sven Schreiber
-
Travis Oliphant
-
Vidar Gundersen