"infinity" matrix

21 views
Skip to first unread message

Xavier Tapia Gonzalez

unread,
Apr 6, 2015, 1:56:22 PM4/6/15
to sage-s...@googlegroups.com
Hi, I want to create a matrix with this property:

c_{ij}=a_j*b_i-a_i*b_j    if   0<=i,j<=n

and c_{ij}=0   otherwise.


I don't know how to do it, because on my program will appears terms like  c[-1][0]   and I want this 0 and not c[n-1][0]

Could you help me?

slelievre

unread,
Apr 7, 2015, 6:34:31 AM4/7/15
to sage-s...@googlegroups.com


Xavier Tapia Gonzalez:


Why not use a function instead of a matrix?

def c(i,j):
    if 0 <= i <= n and 0 <= j <= n:
        return a[j] * b[i] - a[i] * b[j]
    return 0

Then use c(i,j) instead of c[i,j].

Or you could first build the finite matrix,
calling it say cmatrix, and then

def c(i,j):
    if 0 <= i <= n and 0 <= j <= n:
        return cmatrix[i,j]
    return 0

This would avoid save computation by
computing the cmatrix[i,j] only once,
and then just accessing matrix elements
when you call the function.

By the way, to access element (i,j) of a matrix,
it's faster to use c[i,j] than c[i][j]. Indeed, when
you do c[i][j], you are first extracting the whole
line i of the matrix, c[i], as a vector, and then
taking element j of that vector. You are wasting
time constructing a vector you won't need.
Also, if you want to change element (i,j) of the
matrix, a command such as

    c[i,j] = 1

will work, but the version with c[i][j] won't work.


Reply all
Reply to author
Forward
0 new messages