[SciPy-user] Newbie issues in moving to SciPy
Travis E. Oliphant
oliphant at ee.byu.edu
Tue Sep 23 12:04:39 EDT 2003
Gary Pajer wrote:
> Andy,
>
> I'm probably about two steps ahead of you. I think you had better learn at
> least a bit about "python properly". A fair bit of Numeric and scipy is
> structured into namespaces and objects, and while python itself works just
> fine with a basic set of commands, using scipy needs a little more. But not
> too terribly much more. Learn what you need as you go.
>
> There are a lot of differences w.r.t. Matlab. The one I find most irksome
> is the awkward way one adds an additional row or column to an array. Unless
> I'm missing something. The plotting facilities are still under
> construction: a few bugs and missing features, but quite servicable unless
> you want publication quality. (But there are solutions to that problem.)
> All things considered, scipy seems richer and *possibly* more featured than
> Matlab, but with a steeper learning curve.
>
I disagree with lack of publication quality. You can get publication
quality now with xplt. Others use add on tools to get quality output.
For example I've heard that grace has a nice interface with Python.
Regarding r_ and c_
These are tools for constructing arrays quickly. They essentially wrap
the concatentator.
Their primary use is to simplify construction of 1-d and 2-d arrays. r_
stands for row concatenation and c_ stands for column concatenation.
Of course in 1-d it doesn't matter which you use.
Examples:
>>> a = r_[1:10]; print a
[1 2 3 4 5 6 7 8 9]
>>> a = r_[0:10:0.5]; print a # sample spacing of 0.5
[ 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7.
7.5 8. 8.5 9. 9.5]
>>> a = r_[0:10:20j]; print a # 20j => generate 20 samples
[ 0. 0.5263 1.0526 1.5789 2.1053 2.6316 3.1579 3.6842
4.2105 4.7368 5.2632 5.7895 6.3158 6.8421 7.3684
7.8947
8.4211 8.9474 9.4737 10. ]
You can also combine lists
>>> a = r_[0:10, 1.0, 3.0, 5:10:10j]; print a
[ 0. 1. 2. 3. 4. 5. 6. 7.
8. 9. 1. 3. 5. 5.5556 6.1111
6.6667
7.2222 7.7778 8.3333 8.8889 9.4444 10. ]
When called with multiple "indexes" r_ concatentates the rows of the
arguments. For 1-d arrays as in this example it just creates a 1-d array.
For 2-d arrays you get the effect of stacking the arguments on top of
each other.
>>> C = r_[A,B]
produces the equivalent of MATLAB's C = [A; B] (note that A and B must
be 2-d for this to work as you want)
The column equivalent is c_[]
So,
>>> C = c_[A,B]
produces the equivalent of C = [A, B] in MATLAB
To construct a matrix from blocks
F = [ A B ]
[ C D ]
use
>>> F = c_[r_[A,C],r_[A,D]]
or
>>> F = r_[c_[A,B],c_[C,D]]
A = [[1,2]]
B = [[3,4,5]]
C = [[6,7],[8,9],[10,11]]
D = [[12,13,14],[15,16,17],[18,19,20]]
F = r_[c_[A,B],c_[C,D]]
print F
results in
[[ 1 2 3 4 5]
[ 6 7 12 13 14]
[ 8 9 15 16 17]
[10 11 18 19 20]]
I would not be opposed to adding a version that interprets a string
using MATLAB syntax so that
r_['A B; C D']
did the same thing.
-Travis O.
More information about the SciPy-User
mailing list