What is wrong with this SAGE function?

84 views
Skip to first unread message

Phoenix

unread,
Jun 4, 2015, 5:44:24 PM6/4/15
to sage-s...@googlegroups.com

I am trying to define a function "elem" which will create me a list of length n which has 1 at the i^th position.

def elem (i,n):
  A = []
  for k in range [n-1]:
      if k != i:
          A.append([0])
      if k == i:
          A.append([1])

  return A

elem (1,5)

Why does the above not work?


Christophe Bal

unread,
Jun 4, 2015, 5:48:01 PM6/4/15
to sage-s...@googlegroups.com
Use range(n-1) instead of range [n-1].


Christophe BAL
Enseignant de mathématiques en Lycée et développeur Python amateur
---
French math teacher in a "Lycée" and Python amateur developer

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Phoenix

unread,
Jun 4, 2015, 5:53:50 PM6/4/15
to sage-s...@googlegroups.com

Thanks!

Is there otherwise any standard operation in SAGE to create such vectors?

If you have time can you also look into another question I had about vectors in SAGE:


Given a vector $v$ and a matrix $A$ of dimension $n$, one would say that $v$ is a cyclic vector of $A$ if the following set is linearly independent $\{ v,Av,A^2v,..,A^{n-1}v \}$.

Is there a way to test this property on SAGE given a $v$ and a $A$? 


Michael Orlitzky

unread,
Jun 4, 2015, 6:11:59 PM6/4/15
to sage-s...@googlegroups.com
On 06/04/2015 05:53 PM, Phoenix wrote:
>
> Thanks!
>
> Is there otherwise any standard operation in SAGE to create such vectors?
>

If the construction isn't too complicated, a "list comprehension"
usually suffices. This will do what you want, I think:

def elem(i,n):
return [ ZZ(i == j) for j in range(0,n) ]

A list comprehension is written much like the usual math notation for
set construction, so nothing to be afraid of.


> Given a vector $v$ and a matrix $A$ of dimension $n$, one would say that
> $v$ is a cyclic vector of $A$ if the following set is linearly independent
> $\{ v,Av,A^2v,..,A^{n-1}v \}$.
>
> Is there a way to test this property on SAGE given a $v$ and a $A$?
>

Sure, using list comprehensions again. First we construct the list of
A(v), A^2(v), etc. Then we stick those vectors in a big matrix, and ask
for its rank. If the matrix has full rank, it's columns/rows are
independent.


def f(A,v):
M = matrix([ (A^j)*v for j in range(0,len(v)) ])
return M.rank() == len(v)

Note that you will need to pass that function a vector (that you get
from calling vector() on a list), not a list. For example,

sage: A = matrix([[1,2],[3,4]])
sage: v = vector([1,2])
sage: f(A,v)
True

Vincent Delecroix

unread,
Jun 5, 2015, 2:10:25 AM6/5/15
to sage-s...@googlegroups.com
[0] * i + [1] + [0] * (n-i-1)

Lee Worden

unread,
Jun 5, 2015, 1:32:57 PM6/5/15
to sage-s...@googlegroups.com

On Thursday, June 4, 2015 at 2:53:50 PM UTC-7, Phoenix wrote:

Thanks!

Is there otherwise any standard operation in SAGE to create such vectors?

 
sage: def elem(i,n):
....:     return VectorSpace( QQ, n ).basis()[i]
....:
sage: elem( 1, 5 )
(0, 1, 0, 0, 0)

or if you need it to return a list rather than a tuple:

sage: def elem(i,n):
    return list( VectorSpace( QQ, n ).basis()[i] )
....:
sage: elem( 1, 5 )
[0, 1, 0, 0, 0]

Lee Worden

unread,
Jun 5, 2015, 2:20:08 PM6/5/15
to sage-s...@googlegroups.com


On Thursday, June 4, 2015 at 3:11:59 PM UTC-7, Michael Orlitzky wrote:
> Given a vector $v$ and a matrix $A$ of dimension $n$, one would say that
> $v$ is a cyclic vector of $A$ if the following set is linearly independent
> $\{ v,Av,A^2v,..,A^{n-1}v \}$.
>
> Is there a way to test this property on SAGE given a $v$ and a $A$?  
>

Sure, using list comprehensions again. First we construct the list of
A(v), A^2(v), etc. Then we stick those vectors in a big matrix, and ask
for its rank. If the matrix has full rank, it's columns/rows are
independent.


  def f(A,v):
      M = matrix([ (A^j)*v for j in range(0,len(v)) ])
      return M.rank() == len(v)

Note that you will need to pass that function a vector (that you get
from calling vector() on a list), not a list. For example,

  sage: A = matrix([[1,2],[3,4]])
  sage: v = vector([1,2])
  sage: f(A,v)
  True

In some cases, Sage's built-in linear algebra functions might be more efficient:


sage: A = matrix( [[1,2],[3,4]] )
sage: v = vector([1,2])
sage: def is_cyclic_vector( v, A ):
        return not A.iterates( v, A.ncols() ).is_singular()
....:
sage: is_cyclic_vector( v, A )
True

 

Anirbit

unread,
Jun 9, 2015, 1:05:26 PM6/9/15
to sage-s...@googlegroups.com
@Lee Worden

Your code is deciding the linear independence of the set {v,v*A,v*A^2,...,v*A^{dim(A)}}

But I want to decide the linear independence of the set {v, A*v,..,,A^{dim(A)-1}*v}



--
You received this message because you are subscribed to a topic in the Google Groups "sage-support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-support/HjjmEeJAPgs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-support...@googlegroups.com.

Nils Bruin

unread,
Jun 9, 2015, 2:44:46 PM6/9/15
to sage-s...@googlegroups.com
On Tuesday, June 9, 2015 at 10:05:26 AM UTC-7, Phoenix wrote:
@Lee Worden

Your code is deciding the linear independence of the set {v,v*A,v*A^2,...,v*A^{dim(A)}}

That would always be linear dependent and not what the proposed code does
 
But I want to decide the linear independence of the set {v, A*v,..,,A^{dim(A)-1}*v}

Reading the documentation of A.iterates or the documentation of A.transpose will allow you to construct a solution to that problem quite easily.

Lee Worden

unread,
Jun 10, 2015, 1:37:09 PM6/10/15
to sage-s...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages