element wise product of vectors

386 views
Skip to first unread message

Peng Yu

unread,
Jul 5, 2012, 2:48:27 PM7/5/12
to sage-s...@googlegroups.com
I don't find a convenient way for doing element wise product of vectors. Is there an operator for it? (or less cumbersome way)

~/linux/test/sage/vector/*$ ./main.sage 
5
~/linux/test/sage/vector/*$ cat main.sage 
#!/usr/bin/env sage

x=vector([1,2])
print x*x

Regards,
Peng

John H Palmieri

unread,
Jul 5, 2012, 2:57:07 PM7/5/12
to sage-s...@googlegroups.com


On Thursday, July 5, 2012 11:48:27 AM UTC-7, Peng Yu wrote:
I don't find a convenient way for doing element wise product of vectors. Is there an operator for it? (or less cumbersome way)

sage: v = vector([1,2,3])
sage: v.pairwise_product(v)
(1, 4, 9)


Is that what you want?

--
John


Jorge Garcia

unread,
Jul 5, 2012, 4:27:11 PM7/5/12
to sage-s...@googlegroups.com

Construct row vector objects and apply the dot() method!

--
To post to this group, send email to sage-s...@googlegroups.com
To unsubscribe from this group, send email to sage-support...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Peng Yu

unread,
Jul 5, 2012, 6:42:54 PM7/5/12
to sage-s...@googlegroups.com
On Thu, Jul 5, 2012 at 3:27 PM, Jorge Garcia <calc...@gmail.com> wrote:
> Construct row vector objects and apply the dot() method!

I don't find the dot() method. Would you please let me know where it is?

I think that it is the case, but just to make sure. Is the object
constructed by vector() a row vector?

--
Regards,
Peng

John H Palmieri

unread,
Jul 5, 2012, 7:48:17 PM7/5/12
to sage-s...@googlegroups.com


On Thursday, July 5, 2012 3:42:54 PM UTC-7, Peng Yu wrote:
On Thu, Jul 5, 2012 at 3:27 PM, Jorge Garcia wrote:
> Construct row vector objects and apply the dot() method!

I don't find the dot() method. Would you please let me know where it is?

I think that it is the case, but just to make sure. Is the object
constructed by vector() a row vector?

Do this:

   sage: v = vector([1,2])

Now type "v." and hit the TAB key. One of the entries will be "dot_product". So do

   sage: v.dot_product(v)
   5

Using the TAB key is a very useful feature of Sage.

--
John

Jorge Garcia

unread,
Jul 5, 2012, 8:12:14 PM7/5/12
to sage-s...@googlegroups.com

Have a look here http://alpha.sagenb.org/home/pub/155/

Jason Grout

unread,
Jul 5, 2012, 9:59:26 PM7/5/12
to sage-s...@googlegroups.com
On 7/5/12 7:12 PM, Jorge Garcia wrote:
> Have a look here http://alpha.sagenb.org/home/pub/155/
>


This might be useful too:
http://sage.math.washington.edu/home/jason/sage-HLA2.pdf

Thanks,

Jason

Peng Yu

unread,
Jul 6, 2012, 12:53:14 AM7/6/12
to sage-s...@googlegroups.com
Is there also a convenient way for elementwise divide?

--
Regards,
Peng

Jason Grout

unread,
Jul 6, 2012, 1:14:09 AM7/6/12
to sage-s...@googlegroups.com
No, but you can do it and many other operations like this:

u = vector([1,2,3])
v = vector([6,5,4])
vector([i/j for i,j in zip(u,v)])

If you use numpy arrays, the operations are all element-wise:

from numpy import array
sage: u=array([1.0,2.0,3.0])
sage: v=array([5.0,4.0,3.0])
sage: u/v
array([ 0.2, 0.5, 1. ])


I wonder if you'd have a more natural time working with numpy arrays.

Thanks,

Jason

Reply all
Reply to author
Forward
0 new messages