Ask Your Question
1

scalar^[vector]?

asked 2015-10-08 03:00:40 +0100

dr gravatar image

updated 2015-10-08 03:00:55 +0100

I feel really stupid like this should be obvious, but I can't figure out how to do it.

v = vector([1,2,3])
2^v

should give me

[2, 4, 8]

I can't find any other way to do this without writing a function.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2015-10-08 04:03:52 +0100

vdelecroix gravatar image

Hello,

In Python (and hence Sage) you have very useful list comprehension

sage: my_list = [1, 2, 3, 4]
sage: [2^i for i in my_list] 
[2, 4, 8, 16]

If you use vectors, you can alternatively do

sage: v = vector([1, 2, 3, 4])
sage: v.apply_map(lambda x: 2^x)
(2, 4, 8, 16)

Vincent

edit flag offensive delete link more

Comments

That makes writing the function easier, but for readability in the middle of an equation it leaves a lot to be desired.

dr gravatar imagedr ( 2015-10-08 11:22:26 +0100 )edit
1

answered 2015-10-08 16:04:24 +0100

fidbc gravatar image

updated 2015-10-08 16:34:55 +0100

vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use 2**v then maybe you will like numpy. See here for an example.

Update: It is possible to use 2.^v from within sage if v is a numpy array (see link above).

edit flag offensive delete link more

Comments

2.^x

works.

I guess I don't understand the relationship between sage and numpy or scipy or really any of the packages. Why is numpy not the default. What are the implications of doing the import? Are numpy vectors going to interact with the plotting correctly? Etc.

Ugh, I didn't even notice the difference between "through sage" and "with python". .^ works in one and errors in the other, while ^ is the reverse. I don't understand. What document gives a map for how all these things interact?

dr gravatar imagedr ( 2015-10-08 16:18:52 +0100 )edit

You are right, that works. I'll update the answer, thanks!

Numpy is a Python package intended for scientific computing and you have the option of using it through Sagemath. Doing the import will load the array function from numpy (in this case). I can't think of any errors you can run into if you try to plot from numpy arrays.

fidbc gravatar imagefidbc ( 2015-10-08 16:32:10 +0100 )edit

In Python ^ stands for bitwise xor. Sagemath was designed to override the meaning of ^ to be exponentiation. If you want to keep on the safe side you can just use ** which means exponentiation in both Python and Sagemath. A bit of this is explaned here.

fidbc gravatar imagefidbc ( 2015-10-08 16:40:37 +0100 )edit

Right, I know what numpy is. I just assumed the default python-like interaction I'm doing in a sagecell WAS numpy. Instead it's another language? Or something?

dr gravatar imagedr ( 2015-10-08 16:42:54 +0100 )edit

It is "the same language", it is just that Sagemath has its own way of defining/handling vectors, and Numpy has also its own way of handling them, they are just meant for different purposes. Which one to use? That depends on what you want to do.

fidbc gravatar imagefidbc ( 2015-10-08 16:49:27 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-10-08 03:00:40 +0100

Seen: 581 times

Last updated: Oct 08 '15