Re: [sage-support] Circulant matrix

281 views
Skip to first unread message

David Joyner

unread,
Mar 14, 2013, 9:15:35 PM3/14/13
to sage-s...@googlegroups.com
On Thu, Mar 14, 2013 at 9:10 PM, pascal <pascal...@gmail.com> wrote:
> Does Sage provide support for building the circulant matrix associated to a
> given list ?
>

Is this what you want?
http://www.sagemath.org/doc/reference/sage/combinat/matrices/latin.html


> --
> 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?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

pascal

unread,
Mar 15, 2013, 3:07:46 AM3/15/13
to sage-s...@googlegroups.com


Le vendredi 15 mars 2013 02:15:35 UTC+1, David Joyner a écrit :

Is this what you want?
http://www.sagemath.org/doc/reference/sage/combinat/matrices/latin.html



Not exactly : a call back_circulant(n) returns the circulant matrix associated to the list range(n). I need a more general function. Maple achieves this by passing an option to the Matrix constructor, for instance Matrix(3, shape=Circulant[[42, 20, 13]]) returns the circulant matrix with first line  [42, 20, 13]. Circulant matrix is so "classic" that I'm surprised not to find it in Sage.

Charles Bouillaguet

unread,
Mar 15, 2013, 3:44:21 AM3/15/13
to sage-s...@googlegroups.com
On Mar 15, 2013, at 8:07 AM, pascal wrote:

> Not exactly : a call back_circulant(n) returns the circulant matrix associated to the list range(n). I need a more general function. Maple achieves this by passing an option to the Matrix constructor, for instance Matrix(3, shape=Circulant[[42, 20, 13]]) returns the circulant matrix with first line [42, 20, 13]. Circulant matrix is so "classic" that I'm surprised not to find it in Sage.

So am I. However, it's in the pipeline (http://trac.sagemath.org/sage_trac/ticket/13703)

---
Charles Bouillaguet
http://www.lifl.fr/~bouillaguet/


pascal

unread,
Mar 15, 2013, 7:27:55 AM3/15/13
to sage-s...@googlegroups.com


Le vendredi 15 mars 2013 08:44:21 UTC+1, Charles Bouillaguet a écrit :


So am I. However, it's in the pipeline (http://trac.sagemath.org/sage_trac/ticket/13703)


Thanks for the link.  The code for circulant matrix generation :


def hankel(R,c,r): entries=c+r[1:]; return matrix(R, len(c), len(r), lambda i,j: entries[i+j])
def circulant(R,E): return hankel(R, E, E[-1:]+E[:-1])


doesn't return the _usual_ circulant matrix (as a result of successive right shifts) but rather a skew-circulant matrix :


# -------------------------------------------------------------------------------------------------------
def hankel(R,c,r): entries=c+r[1:]; return matrix(R, len(c), len(r), lambda i,j: entries[i+j])
def circulant(R,E): return hankel(R, E, E[-1:]+E[:-1])

R=ZZ
E=[42, 20, 13, 55]
print
print circulant(R, E)
# -------------------------------------------------------------------------------------------------------


outputing :

[42 20 13 55]
[20 13 55 42]
[13 55 42 20]
[55 42 20 13]

In fact, a circulant matrix is a special case of a Toeplitz matrix (not a Hankel Matrix). The correct code should look like this :


# -------------------------------------------------------------------------------------------------------
def toeplitz(R, c,r): return matrix(R,len(c), len(r), lambda i,j: c[i-j] if i>=j else r[j-i])
def circulant(R, E): return toeplitz(R, E[0:1]+E[-1:0:-1], E)

def hankel(R, c,r): entries=c+r[1:]; return matrix(R,len(c), len(r), lambda i,j: entries[i+j])
def skew_circulant(R,E): return hankel(R, E, E[-1:]+E[:-1])

R=ZZ
E=[42, 20, 13, 55]
print
print circulant(R, E)
print
print skew_circulant(R, E)
# -------------------------------------------------------------------------------------------------------

# -------------------------------------output ----------------------------------
[42 20 13 55]
[55 42 20 13]
[13 55 42 20]
[20 13 55 42]

[42 20 13 55]
[20 13 55 42]
[13 55 42 20]
[55 42 20 13]
# -------------------------------------------------------------------------------------------------------


Jason Grout

unread,
Mar 15, 2013, 9:20:52 AM3/15/13
to sage-s...@googlegroups.com
On 3/14/13 8:10 PM, pascal wrote:
> Does Sage provide support for building the circulant matrix associated
> to a given list ?
>

Here is a simple piece of code to do it directly in Sage:

def hankel(R,c,r):
entries=c+r[1:]
return matrix(R, len(c), len(r), lambda i,j: entries[i+j])
def circulant(R,E):
return hankel(R, E, E[-1:]+E[:-1])
circulant(QQ, [1,2,3])

Or you can directly use scipy to do it:

from scipy.linalg import circulant
matrix(circulant([1,2,3]))

(see
http://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.circulant.html#scipy.linalg.circulant)

FYI, you might find our chapter in the Handbook of Linear Algebra to be
a good resource: http://sage.math.washington.edu/home/jason/sage-HLA2.pdf

The code above was in an early draft of the chapter, which may have been
commented out recently to edit the chapter down. We'll put it back in
when we include the chapter in Sage documentation. Even better, as you
implicitly suggest, we should include these and many other matrix
constructors directly into Sage.

Thanks,

Jason


Jason Grout

unread,
Mar 15, 2013, 9:31:09 AM3/15/13
to sage-s...@googlegroups.com
On 3/15/13 8:20 AM, Jason Grout wrote:
> On 3/14/13 8:10 PM, pascal wrote:
>> Does Sage provide support for building the circulant matrix associated
>> to a given list ?
>>
>
> Here is a simple piece of code to do it directly in Sage:
>

Ignore this code. I see you are already discussing it on another thread
in these replies. I'll respond there.

Thanks,

Jason



Jason Grout

unread,
Mar 15, 2013, 9:38:25 AM3/15/13
to sage-s...@googlegroups.com
On 3/15/13 6:27 AM, pascal wrote:
>
>
> Le vendredi 15 mars 2013 08:44:21 UTC+1, Charles Bouillaguet a �crit :
>
>
>
> So am I. However, it's in the pipeline
> (http://trac.sagemath.org/sage_trac/ticket/13703
> <http://trac.sagemath.org/sage_trac/ticket/13703>)
>
>
> Thanks for the link. The code for circulant matrix generation :
>
>
> def hankel(R,c,r): entries=c+r[1:]; return matrix(R, len(c), len(r),
> lambda i,j: entries[i+j])
> def circulant(R,E): return hankel(R, E, E[-1:]+E[:-1])
>
>
> doesn't return the _usual_ circulant matrix (as a result of successive
> right shifts) but rather a skew-circulant matrix :

I should have read the rest of the messages more carefully before
sending my original response to this thread. I see we're already
discussing the code I proposed.

Thanks for catching the error! I've changed the trac ticket to reflect
your modifications.

Thanks,

Jason


pascal

unread,
Mar 15, 2013, 11:06:23 AM3/15/13
to sage-s...@googlegroups.com

Or you can directly use scipy to do it:

from scipy.linalg import circulant
matrix(circulant([1,2,3]))


Why I didn't think of it! 

 


FYI, you might find our chapter in the Handbook of Linear Algebra to be
a good resource: http://sage.math.washington.edu/home/jason/sage-HLA2.pdf


 Very good doc, thanks, i was looking for a synthetic view of linear algebra in sage, I got it. Two questions :
a) What is the publication date ?
b) The doc refers to 

"An updated and expanded version of this chapter is available in the Sage documentation as
a thematic tutorial on linear algebra."

Do you have the link to the tutorial ?

pascal

unread,
Mar 15, 2013, 11:07:15 AM3/15/13
to sage-s...@googlegroups.com


Le vendredi 15 mars 2013 14:38:25 UTC+1, Jason Grout a écrit :


Thanks for catching the error!   I've changed the trac ticket to reflect
your modifications.

Thanks,



You are more than welcome! 

Jason Grout

unread,
Mar 15, 2013, 11:31:56 AM3/15/13
to sage-s...@googlegroups.com
On 3/15/13 10:06 AM, pascal wrote:

> FYI, you might find our chapter in the Handbook of Linear Algebra to be
> a good resource:
> http://sage.math.washington.edu/home/jason/sage-HLA2.pdf
> <http://sage.math.washington.edu/home/jason/sage-HLA2.pdf>
>
>
>
> Very good doc, thanks, i was looking for a synthetic view of linear
> algebra in sage, I got it. Two questions :
> a) What is the publication date ?

I'm not sure. Hopefully sometime this year.

> b) The doc refers to
>
> "An updated and expanded version of this chapter is available in the
> Sage documentation as
> a thematic tutorial on linear algebra."
>
> Do you have the link to the tutorial ?
>

We've opened a trac ticket to include it:
http://trac.sagemath.org/sage_trac/ticket/13017

We haven't cleaned up the source for inclusion yet. Right now, the RST
source gives exactly the PDF I linked. The "expanded version" would
have things like the code I posted (now corrected; thanks again), which
right now are comments in the source file.

Thanks,

Jason


Reply all
Reply to author
Forward
0 new messages