method to get the left-lower and right-upper triangular parts of a matrix

368 views
Skip to first unread message

Huayi Wei

unread,
May 20, 2015, 2:21:10 AM5/20/15
to sage-s...@googlegroups.com
Hi, everyone,

I have a sage matrix, and I want to get its left-lower and right-upper
triangular parts. Does there exist any method to get them directly?

In matlab, there are two functions `triu` and `tril` to do such things.
Does sage have the similar functions?

Best

Huayi

Vincent Delecroix

unread,
May 20, 2015, 2:26:28 AM5/20/15
to sage-s...@googlegroups.com
Hi Huayi,
It does not seem to exists. Sage only has a `diagonal` method to return
the list of entries on the diagonal.

Do you know how to implement it in Sage? Otherwise the small function
below does the job

def triu(m):
t = matrix(m.base_ring(), m.nrows())
for i in range(m.nrows()):
t[i,i:] = m[i,i:]
return t

Though, if your matrices are sparse, there are more efficient ways for sure.

Best,
Vincent

Huayi Wei

unread,
May 20, 2015, 2:55:19 AM5/20/15
to sage-s...@googlegroups.com
Hi, Vincent,

Yes, my matrix is sparse. I just move here from matlab, so I am a
newbie for sage and python.

The function you have offered can do the job. But I am worried about its
efficiency, because my sparse matrix
maybe very large which comes from Finite Element method.

So about the efficient ways for sparse matrix, can you give me some
suggestion? Thanks very much.

Best

Huayi

Vincent Delecroix

unread,
May 20, 2015, 3:55:18 AM5/20/15
to sage-s...@googlegroups.com
Hi,

On 20/05/15 08:54, Huayi Wei wrote:
> Hi, Vincent,
>
> Yes, my matrix is sparse. I just move here from matlab, so I am a
> newbie for sage and python.
>
> The function you have offered can do the job. But I am worried about its
> efficiency, because my sparse matrix
> maybe very large which comes from Finite Element method.

The function I gave is not so bad, because if you have a sparse matrix
accessing to a row (or even a slice of a row) gives you a sparse vector:

sage: m = matrix(100, sparse=True)
sage: v = m[5,5:]
sage: v.is_sparse()
True

Thouh I am not sure about the efficiency of

sage: m[5,5:] = v

when m is a sparse matrix and v a sparse vector. I quickly look at the
code and it seems to run through all entries of v which is very stupid.

> So about the efficient ways for sparse matrix, can you give me some
> suggestion? Thanks very much.

If your matrix is sparse I would do

def triu(m):
t = matrix(m.base_ring(), m.nrows(), sparse=True)
for (i,j) in m.nonzero_positions():
if i <= j:
t[i,j] = m[i,j]
return t

Depending whether you want the diagonal or not you have to adjust the
inequality for being strict or not.

Best,
Vincent

魏华祎

unread,
May 20, 2015, 8:50:52 AM5/20/15
to sage-support
Hi, Vincent,

Thanks for your reply. I will try it following your suggestion.

Best

Huayi
 
 
------------------ Original ------------------
From:  "Vincent Delecroix"<20100.d...@gmail.com>;
Date:  Wed, May 20, 2015 03:55 PM
To:  "sage-support"<sage-s...@googlegroups.com>;
Subject:  Re: [sage-support] method to get the left-lower and right-uppertriangular parts of a matrix
--
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.
Reply all
Reply to author
Forward
0 new messages