[sage-support] Re: using existing functions with vectors / lists / arrays

2011-05-22 Thread Sebastian Ramacher
On 05/22/2011 07:52 AM, mankoff wrote:
 I've never yet figured out in Python when to use lists, arrays, or
 vectors.
 
 I have something like:
 y = x^2
 
 I can evaluate it at one point:
 y(2) # 4
 
 I'd like to evaluate it at a group of points:
 y( x ) # 2, 4, 9, 16, ...
 
 Can this just work like in IDL or MATLAB? How would one define x?
 What is the quickest syntax if it can't just be a variable? Perhaps:
 
for xx in x: y(xx)
 
 Something simpler? Clearer?

You can use something like that

ys = map(y, xs)

or

ys = [y(x) for x in xs]

where xs is a list of the x values.

Kind regards


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


Re: [sage-support] Re: using existing functions with vectors / lists / arrays

2011-05-22 Thread Berkin Malkoc
 I have something like:
  y = x^2
 
  I can evaluate it at one point:
  y(2) # 4
 
  I'd like to evaluate it at a group of points:
  y( x ) # 2, 4, 9, 16, ...
 
  Can this just work like in IDL or MATLAB? How would one define x?
  What is the quickest syntax if it can't just be a variable? Perhaps:


For purely numerical work a la Matlab, you may want to use numpy arrays (you
will be using Python and not any Sage functionality):

sage: import numpy
sage: x = numpy.array([1, 2, 3, 4])
sage: def y(x):
: return x*x
:
sage: y(x)
array([ 1,  4,  9, 16])

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


[sage-support] Easy operations on Cyclic groups...

2011-05-22 Thread Nathann Cohen
Hello everybody !

I want to enumerate the k different translations of [0,1,2] in Z/kZ. I
thought there should exist in Sage something like that (don't try it
it does not work) :

g = CyclicGroup(k)
S = g([1,2,3])
for i in g:
print S+i

Is that possible ? It looks like the CyclicGroup is Sage natively
contains permutations, and as I had no idea in which direction to look
I wrote a custom script :-)

Thanks for your help !!!

Nathann

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


[sage-support] imposing commutation relation

2011-05-22 Thread Rajeev
Hi,

I wish to simplify some calculation that appear in quantum mechanics. To 
begin we use non-commutative variables as -

sage: R.a,b = FreeAlgebra(QQ, 2)
sage: (a+b)^3 + a*(a+b)
a^2 + a*b + a^3 + a^2*b + a*b*a + a*b^2 + b*a^2 + b*a*b + b^2*a + b^3

I want to impose the commutation relation [a,b]=1 and bring the expression 
to normal form (i.e. in all terms b appears before a, e.g. a*b gets replaced 
by b*a + 1). Is it possible to do this? If not then can I get the expression 
such that a*b^2 appears as a*b*b? If the later can be done then I can use 
some string manipulation hack and get my job done.

Thanks in advance.

Regards,
Rajeev

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


Re: [sage-support] Re: using existing functions with vectors / lists / arrays

2011-05-22 Thread Rajeev Singh
On Sun, May 22, 2011 at 4:45 PM, Berkin Malkoc malk...@gmail.com wrote:




  I have something like:
  y = x^2
 
  I can evaluate it at one point:
  y(2) # 4
 
  I'd like to evaluate it at a group of points:
  y( x ) # 2, 4, 9, 16, ...
 
  Can this just work like in IDL or MATLAB? How would one define x?
  What is the quickest syntax if it can't just be a variable? Perhaps:


 For purely numerical work a la Matlab, you may want to use numpy arrays
 (you will be using Python and not any Sage functionality):

 sage: import numpy
 sage: x = numpy.array([1, 2, 3, 4])
 sage: def y(x):
 : return x*x
 :
 sage: y(x)
 array([ 1,  4,  9, 16])

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


if the function of the single variable is somewhat more complicated then one
can use vectorize from numpy -

sage: import numpy as np
sage: x = np.array([1,2,3,4])
sage: x
array([1, 2, 3, 4])
sage: def y(x):
: if x  3:
: return 2*x
: else:
: return x*x
:
sage: y_vec = np.vectorize(y)

this fails -
sage: y(x)
---
ValueErrorTraceback (most recent call last)



while this works -
sage: y_vec(x)
array([ 2,  4,  9, 16])

hope it helps.

Rajeev

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


[sage-support] Re: imposing commutation relation

2011-05-22 Thread Simon King
Hi Rajeev,

On 22 Mai, 20:25, Rajeev rajs2...@gmail.com wrote:
 I wish to simplify some calculation that appear in quantum mechanics. To
 begin we use non-commutative variables as -

 sage: R.a,b = FreeAlgebra(QQ, 2)
 sage: (a+b)^3 + a*(a+b)
 a^2 + a*b + a^3 + a^2*b + a*b*a + a*b^2 + b*a^2 + b*a*b + b^2*a + b^3

 I want to impose the commutation relation [a,b]=1 and bring the expression
 to normal form (i.e. in all terms b appears before a, e.g. a*b gets replaced
 by b*a + 1). Is it possible to do this?

Well, if you start with a free algebra generated by a and b, and you
mod out the commutator of a and b, then you obtain the polynomial ring
generated by a and b, up to isomorphism. Hence, you could create a
polynomial ring and convert your free algebra elements into a
polynomial like this:

sage: R.a,b = FreeAlgebra(QQ, 2)
sage: p = (a+b)^3 + a*(a+b); p
a^2 + a*b + a^3 + a^2*b + a*b*a + a*b^2 + b*a^2 + b*a*b + b^2*a + b^3
sage: P = QQ['a','b']
sage: P(p)
a^3 + 3*a^2*b + 3*a*b^2 + b^3 + a^2 + a*b

You say that you want to appear b before a, but that should merely by
a choice of order:

sage: P2 = QQ['b','a']
sage: P2(p)
b^3 + 3*b^2*a + 3*b*a^2 + a^3 + b*a + a^2

Both P1 and P2 have a degree lexicographic order. If you want pure
lexicographic, it can be done as well:

sage: P3 = PolynomialRing(QQ, ['b','a'], order='lex')
sage: P3(p)
b^3 + 3*b^2*a + 3*b*a^2 + b*a + a^3 + a^2
sage: P4 = PolynomialRing(QQ, ['a','b'], order='lex')
sage: P4(p)
a^3 + 3*a^2*b + a^2 + 3*a*b^2 + a*b + b^3

Cheers,
Simon

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


[sage-support] Re: imposing commutation relation

2011-05-22 Thread John H Palmieri


On Sunday, May 22, 2011 10:44:50 PM UTC-7, Simon King wrote:

 Hi Rajeev, 

 On 22 Mai, 20:25, Rajeev rajs...@gmail.com wrote: 
  I wish to simplify some calculation that appear in quantum mechanics. To 
  begin we use non-commutative variables as - 
  
  sage: R.a,b = FreeAlgebra(QQ, 2) 
  sage: (a+b)^3 + a*(a+b) 
  a^2 + a*b + a^3 + a^2*b + a*b*a + a*b^2 + b*a^2 + b*a*b + b^2*a + b^3 
  
  I want to impose the commutation relation [a,b]=1 and bring the 
 expression 
  to normal form (i.e. in all terms b appears before a, e.g. a*b gets 
 replaced 
  by b*a + 1). Is it possible to do this? 

 Well, if you start with a free algebra generated by a and b, and you 
 mod out the commutator of a and b, then you obtain the polynomial ring 
 generated by a and b, up to isomorphism. 

 
The poster wants to impose the relation [a,b]=1, not [a,b]=0.

-- 
John

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