NumPy Array with generator

670 views
Skip to first unread message

LFS

unread,
Dec 30, 2012, 12:03:21 PM12/30/12
to sage-s...@googlegroups.com
Hiya all - Firstly Happy Holidays and a Good New Year in 2013 (no cool dates anymore though like 20:12 20-12-2012)
Anyway - I have looked everywhere for days and days.
How do you generate a numpy array like X[j] where e.g. X[j]=cos(j) for j in [x0,xend,step=h]. I have a count number n.
I can't imagine this is hard to do, but I cannot find it.
I don't want a list; I don't want to use "range" and I don't want to use "fromiter" as that appears to mix lists and arrays.
(It is for my students so I want it as simple and intuitive as possible :)
Thanks!

LFS

unread,
Dec 31, 2012, 3:10:19 AM12/31/12
to sage-s...@googlegroups.com
P.S. Sorry there is an error in the above post. Of course the counter should be integer.
This is the best I could do.
x0=0.;xend=2.*pi;n=8.;h=(xend-x0)/n;
y=numpy.ones(n+1)
y=[y[j]*cos(x0+j*h) for j in [0..n]]
y
If anyone knows of an easier or more intuitive way OR how to this array to print only decimals - many thanks!
Linda

Javier López Peña

unread,
Dec 31, 2012, 6:23:32 AM12/31/12
to sage-s...@googlegroups.com
You can create a numpy array from a list with

numpy.array([cos(x) for x in range(x0, xend, h)])

or if you want to avoid using python lists at al costs, you can do

numpy.cos(arrange(x0, xend, h))

"arange" is the numpy equivalent to range, creating an array instead of a list, 
and then you can use numpy "cos" function, which is vectorized.

Hope this helps!

Cheers,
J

LFS

unread,
Dec 31, 2012, 10:52:37 AM12/31/12
to sage-s...@googlegroups.com
Thanks indeed Juan, it did help immensely! I couldn't get array to work and your example finally clicked for me.
In the end, I used: w=numpy.array([cos(x0+j*h) for j in range(n+1)], float)
THANKS. Linda

Harald Schilly

unread,
Dec 31, 2012, 11:04:46 AM12/31/12
to sage-s...@googlegroups.com

Hi, you line will work, that's fine, but it has a few flaws. e.g. it creates the whole list as a python array, you don't need to do this. Second, this "x0 + j * h" is evaluated "in the world of Python" which is doomed to be slow. It would be much better, if you use numpy's "arange" if you know the start point and the step width, and "linspace" if you know start and end point and the number of steps method.

import numpy as np

In [9]: np.arange(1.1, 4.1, step = 0.33)
Out[9]: array([ 1.1 ,  1.43,  1.76,  2.09,  2.42,  2.75,  3.08,  3.41,  3.74,  4.07])

In [13]: np.linspace(1.1, 4.1, num = 5)
Out[13]: array([ 1.1 ,  1.85,  2.6 ,  3.35,  4.1 ])

Harald

LFS

unread,
Dec 31, 2012, 1:44:23 PM12/31/12
to sage-s...@googlegroups.com
Thank you Harald. Actually, I think I do need the whole list/array with the function evaluated - either as a list or an array. (I am working the TriDiagonal Algorithm.). I am an "in-between" person. I have found that the students make no connection between their engineering classes that often use extremely sophisticated commands from MatLab or just recipes and their math classes with analytic solutions. So I am trying to teach them to do mathematics (or at least check their analytic solutions) with technology but in a way that the understand the steps. I need the simplest most intuitive format possible with the least number of commands. I am still deciding whether to go with Sage lists or NumPy arrays. I had already done the TDA with lists when I thought I should check numpy arrays (and got stuck on the function array). As I understand, arrays are faster as their size is pre-allocated and all elements must be of the same data type.
Thanks again, Linda
P.S. I see the way to "see" whether it is a list or an array in sage is to use print. If you get commas, it is a list; if you don't it is an array.

Harald Schilly

unread,
Dec 31, 2012, 2:10:10 PM12/31/12
to sage-s...@googlegroups.com
On Mon, Dec 31, 2012 at 7:44 PM, LFS <lfah...@gmail.com> wrote:
> Thank you Harald.

np, i'm just heading to a party, so, just a short answer:

> I need the simplest most intuitive
> format possible with the least number of commands.

ok, then i suggest you to use python lists, nothing else. and tell
them that there are more sophisticated solutions available. (e.g. you
cannot do calculations with vectors, but in your example this didn't
happen anyways since you were just using the property that it is a
list)

> I am still deciding
> whether to go with Sage lists or NumPy arrays.

there are 3: python lists, sage matrices, and numpy nd-arrays. don't
get confused!
numpy and sage have python lists, they are the ones created via plain
[ ... , ... ]

> P.S. I see the way to "see" whether it is a list or an array in sage is to
> use print. If you get commas, it is a list; if you don't it is an array.

no, don't rely on the string representation. that's doomed to fail in
general. what you should do, and teach them, is to use the builtin
"type" comman. i.e.

l1 = [ 1, 2, 3 ]
print type(l1)
...
l2 = np.array(l1)
print type(l2)
...

and probably most interesting for you, the online python tutor. there,
you can write simple python-only programs (for loops, ifs, lists,
tuples, functions and dictionaries) and let it execute step by step.
this is extremely good for teaching!

http://www.pythontutor.com/ (there are some examples online, but you
can insert your own)

Harald

LFS

unread,
Jan 1, 2013, 1:25:03 AM1/1/13
to sage-s...@googlegroups.com
Short letter indeed! Full of extremely useful information on so many levels. Thank-you SO much. Linda
Reply all
Reply to author
Forward
0 new messages