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