Regarding the question about broadcating. Every numpy array has a
shape parameter. By manipulating this you can control how an array of
data is viewed and . A simple example is

sage: import numpy
sage: a=numpy.array(range(8))
sage: a
array([ 0,  1,  2,  3,  4,  5,  6,  7])
sage: a.shape=(2,4)
sage: a
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
sage: a.shape=(2,2,2)
sage: a
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

Note that no copying or new object creation occurs.
This is perhaps the multiple views into the same data that was
referenced.

Another interesting facility is that  the following works

sage: w=numpy.array([[1,2],[3,4]])
sage: v=numpy.array([1,1])
sage: w+v

array([[2, 3],
       [4, 5]], dtype=object)

Numpy broadcasts v to a 2x2 array by copying it in the remaining
dimensions. In fact  v can be added to any 2xnxnxn... array and  and
numpy will automatically copy it in the remaining dimensions. you can
also do the following

sage: w=numpy.array([1,2])
sage: w.shape=(2,1)
sage: v=numpy.array([3,4])
sage: v.shape=(1,2)
sage: v
array([[3, 4]]
sage: w
array([[1],
       [2]])
sage: v+w
array([[4, 5],
       [5, 6]], dtype=object)

Here both v and w were broadcast to 2x2 arays by being copied in the
remaining dimensions.


This broadcasting can be very useful. If v is an array 3.2+v will add
3.2 to every element of v (3.2 is broadcast to an array). If you want
to add a vector to every row in a matrix as above. This lets you do
that without writing a for loop and it happens in C. On the other
hand, this completely violates sage's coercion model since numpy takes
the perspective that everything is an array of data, so an array of 8
elements is a 1 dimensional array, and a 2x4 and a 4x2 and a 2x2x2
array depending on what you want to do at the given moment.

 
Josh


On Jul 26, 9:54 pm, "William Stein" <[EMAIL PROTECTED]> wrote:
> On 7/26/07, Dan Christensen <[EMAIL PROTECTED]> wrote:
>
> > > There is no SAGE equivalent yet.
>
> > I really miss the numpy syntax, including broadcasting.  For example,
>
> Naive question: what exactly is broadcasting, and how might it be
> useful in the context of SAGE?  Probably it's something to add
> to the todo list.
>
> >   a[1]      is the second row
>
> SAGE does that, though it returns a new vector rather than a reference.
>
> Numpy has a cool
>
> r_[...] notation for making matrices -- any thoughts about that?  Should
> SAGE implement it too?
>
> >   a[1:3]    is the subarray consisting of the second and third rows
> >   a[:, 1]   is the second column
> >   a[:, 1:3] is the subarray consisting of the second and third columns
> >   a[:, 1:3] += 2  adds 2 to each element in that subarray
>
> I can't see any real reason not to add this sort of functionality to SAGE.
> Maybe we could even consider making the generic dense matrices  in
> SAGE have an underlying numpy representation (i.e., where we don't
> use our own underlying C representation of the data).
>
> > But there's a lot more you do, concisely and with the loops done in C.
>
> > (Implicitly, I also like that numpy allows multiple views into the
> > same data.)
>
> Cool.
>
> William


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to