Xavier Tapia Gonzalez:
>
> 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?
>


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.


-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to