简体   繁体   English

如何正确索引元组

[英]How to index tuple properly

I have to form a Jacobi matrix with the following code. 我必须用以下代码形成一个Jacobi矩阵。 The rows of Jacobi are values of function residual.residual. Jacobi的行是函数residual.residual的值。 and with each outer loop, the columns are added to Jacobi. 并通过每个外部循环将列添加到Jacobi。 Problem is I do not know how to use the index on variable 'Rop'. 问题是我不知道如何在变量“ Rop”上使用索引。

I used the given index in matlab and it works fine there. 我在matlab中使用了给定的索引,并且在那里工作良好。 but I guess I need to change it for python. 但我想我需要为python更改它。

    for i1 in range(1,nxt-1):
    for j1 in range(1,nyt-1):
    Rop=();Rsp=();Rsgp=()
    pit[i1,j1]=pit[i1,j1]+ep;
    sit[i1,j1]=sit[i1,j1]+es;
    sgit[i1,j1]=sgit[i1,j1]+es;
    for i in range(1,nxt-1):#inner loop to calculate perturbed residual
         for j in range(1,nyt-1):
            if (index[i,j]!=0):

             rop,rwp,rgp=residual.residual(pit,sw,sg,i,j,boold,bwold,bgold,
                                    swold,sgold,rsoold,rswold,index, 
                                    delx, dely, depth, phi, kx, ky, 
                                    ax, ay, h, delt, qo, nxt, nyt, welin, 
                                    Vb, pvtgas, pvtoil, pvtw, relpermgo, 
                                    relpermow, time, it,fmult)
               Rop[count:count+2,col]+=(rop/ep,rwp/ep,rgp/ep)

            rop,rwp,rgp=residual.residual(po,sit,sg,i,j,boold,bwold,bgold,
                                    swold,sgold,rsoold,rswold,index, 
                                    delx, dely, depth, phi, kx, ky, 
                                    ax, ay, h, delt, qo, nxt, nyt, welin, 
                                    Vb, pvtgas, pvtoil, pvtw, relpermgo, 
                                    relpermow, time, it,fmult)
               Rop[count:count+2,col+1]+=(rop/es,rwp/es,rgp/es)

             rop,rwp,rgp=residual.residual(po,sw,sgit,i,j,boold,bwold,bgold,
                                    swold,sgold,rsoold,rswold,index, 
                                    delx, dely, depth, phi, kx, ky, 
                                    ax, ay, h, delt, qo, nxt, nyt, welin, 
                                    Vb, pvtgas, pvtoil, pvtw, relpermgo, 
                                    relpermow, time, it,fmult)
               Rop[count:count+2,col+2]+=(rop/es,rwp/es,rgp/es)

MATLAB uses 1-based indexing, while Python uses 0-based indexing. MATLAB使用基于1的索引,而Python使用基于0的索引。

Try changing all your ranges from the form range(1,nxt-1) to range(nxt) , which will give you a sequence from 0 to nxt-1 , inclusive. 尝试将所有范围从range(1,nxt-1)更改为range(nxt) ,这将为您提供从0nxt-1 (包括0nxt-1的序列。 The long form is range(0, nxt) . 长格式为range(0, nxt)

It's hard to tell where you want to stop based on your code, but if you want to stop before nxt-1 just adjust the last argument of range accordingly. 很难根据代码说出要在哪里停止,但是如果要在nxt-1之前停止,只需相应地调整range的最后一个参数即可。

I'm not sure how to advise you on Rop[count:count+2,col] , since you haven't shown how count is defined. 我不确定如何在Rop[count:count+2,col]上为您提供建议,因为您尚未显示如何定义count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM