ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 08 Oct 2015 17:49:43 +0200scalar^[vector]?https://ask.sagemath.org/question/29814/scalarvector/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.Thu, 08 Oct 2015 03:00:40 +0200https://ask.sagemath.org/question/29814/scalarvector/Answer by vdelecroix for <p>I feel really stupid like this should be obvious, but I can't figure out how to do it. </p>
<pre><code>v = vector([1,2,3])
2^v
</code></pre>
<p>should give me</p>
<pre><code>[2, 4, 8]
</code></pre>
<p>I can't find any other way to do this without writing a function.</p>
https://ask.sagemath.org/question/29814/scalarvector/?answer=29815#post-id-29815Hello,
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)
VincentThu, 08 Oct 2015 04:03:52 +0200https://ask.sagemath.org/question/29814/scalarvector/?answer=29815#post-id-29815Comment by dr for <p>Hello,</p>
<p>In Python (and hence Sage) you have very useful list comprehension</p>
<pre><code>sage: my_list = [1, 2, 3, 4]
sage: [2^i for i in my_list]
[2, 4, 8, 16]
</code></pre>
<p>If you use vectors, you can alternatively do</p>
<pre><code>sage: v = vector([1, 2, 3, 4])
sage: v.apply_map(lambda x: 2^x)
(2, 4, 8, 16)
</code></pre>
<p>Vincent</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29824#post-id-29824That makes writing the function easier, but for readability in the middle of an equation it leaves a lot to be desired.Thu, 08 Oct 2015 11:22:26 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29824#post-id-29824Answer by fidbc for <p>I feel really stupid like this should be obvious, but I can't figure out how to do it. </p>
<pre><code>v = vector([1,2,3])
2^v
</code></pre>
<p>should give me</p>
<pre><code>[2, 4, 8]
</code></pre>
<p>I can't find any other way to do this without writing a function.</p>
https://ask.sagemath.org/question/29814/scalarvector/?answer=29832#post-id-29832vdelecroix'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](https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage) for an example.
Update: It is possible to use `2.^v` from within sage if v is a numpy array (see link above).Thu, 08 Oct 2015 16:04:24 +0200https://ask.sagemath.org/question/29814/scalarvector/?answer=29832#post-id-29832Comment by dr for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29833#post-id-298332.^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?Thu, 08 Oct 2015 16:18:52 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29833#post-id-29833Comment by fidbc for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29837#post-id-29837You 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.Thu, 08 Oct 2015 16:32:10 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29837#post-id-29837Comment by fidbc for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29838#post-id-29838In 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](http://doc.sagemath.org/html/en/tutorial/programming.html#loading-and-attaching-sage-files).Thu, 08 Oct 2015 16:40:37 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29838#post-id-29838Comment by dr for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29839#post-id-29839Right, 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?Thu, 08 Oct 2015 16:42:54 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29839#post-id-29839Comment by fidbc for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29841#post-id-29841It 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.Thu, 08 Oct 2015 16:49:27 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29841#post-id-29841Comment by dr for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29842#post-id-29842Can't upvote yet, so thanks!Thu, 08 Oct 2015 17:14:13 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29842#post-id-29842Comment by dr for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29843#post-id-29843Actually, it still doesn't work right.
from numpy import array as npvec
N = 100
angs = -i*2*pi/N*npvec(range(1,N+1))
e.^angs
gives an error. In fact, putting anything in place of `e` except an integer fails. Even a variable that has an integer value gives an error.Thu, 08 Oct 2015 17:32:08 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29843#post-id-29843Comment by fidbc for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29844#post-id-29844I guess this only works for integers. But then you can use `numpy.power`.Thu, 08 Oct 2015 17:45:57 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29844#post-id-29844Comment by fidbc for <p>vdelecroix's answer is perfectly fine. I just wanted to add that if you insist on being able to use <code>2**v</code> then maybe you will like numpy. See <a href="https://sagecell.sagemath.org/?z=eJxLK8rPVcgrzS2oVMjMLcgvKlFILCpKrFRILFbIK4gvS03m5aqwhbA0og11jHSMYzV5uQqKMvNKFIz04ioA6UQVbw==&lang=sage">here</a> for an example.</p>
<p>Update: It is possible to use <code>2.^v</code> from within sage if v is a numpy array (see link above).</p>
https://ask.sagemath.org/question/29814/scalarvector/?comment=29845#post-id-29845Also, I'm not sure if the behaviour of mixing sage's symbolic constants with numpy is unpredictable. Just saying. Numpy has `numpy.e`, `numpy.pi`, and `numpy.complex(0,1)` for i.Thu, 08 Oct 2015 17:49:43 +0200https://ask.sagemath.org/question/29814/scalarvector/?comment=29845#post-id-29845