问:
parallel sections with openmp

Hi,

I am trying to run this code in parrallel sections with openmp

I wanted to speed up its execution. These blocs can by calculated independently at every time step t.

Unfortunatly, instead, the code is slowed down by openmp. I have been looking for concurrency but did not see any problem.

Did I use the sections argument correctly?

Is there a better parallalisation framework that I should use?

All the best

```<code>#define Nx 300
#define Ny 100
int main()
{
int i, j, t=0, it=0;

double vort[Nx][Ny], psi[Nx][Ny], ux[Nx][Ny], uy[Nx][Ny];

double fpsi[Nx][Ny];
double a[Nx][Ny], b[Nx][Ny],c[Nx][Ny], d[Nx][Ny], e[Nx][Ny], ee[Nx][Ny];

//tridiagonal coefficient
//coefficient for the internal grid for vort. Indexed from 0 to Nx-3 or Ny-3 to calculate the interior grid point only. The idix are shiffted of (+1,+1) compare to vort or psi or ux or uy.
//double DDx[Nx-2], AAx[Nx-2], BBx[Nx-2], CCx[Nx-2],    DDy[Ny-2], AAy[Ny-2],BBy[Ny-2], CCy[Ny-2];
double DDx[Nx], AAx[Nx], BBx[Nx], CCx[Nx],    DDy[Ny], AAy[Ny],BBy[Ny], CCy[Ny];
double epsx[Nx][Ny], epsy[Nx][Ny]; //same dimension than ux and uy
//double vortx[Nx-2], vorty[Ny-2];
double vortx[Nx], vorty[Ny];
double cx[Nx][Ny], cy[Nx][Ny]; //same dimension than ux and uy

//coefficient for the ADI
double n[Nx][Ny];
double DDnx[Nx], AAnx[Nx], BBnx[Nx], CCnx[Nx],    DDny[Ny], AAny[Ny],BBny[Ny], CCny[Ny];
double nx[Nx], ny[Ny];

//coefficient for the ADI
double cO2[Nx][Ny];
double DDcx[Nx], AAcx[Nx], BBcx[Nx], CCcx[Nx],    DDcy[Ny], AAcy[Ny],BBcy[Ny], CCcy[Ny];
double cO2x[Nx], cO2y[Ny];

//coefficient for the ADI
double cls[Nx][Ny];
double DDclsx[Nx], AAclsx[Nx], BBclsx[Nx], CCclsx[Nx],    DDclsy[Ny], AAclsy[Ny],BBclsy[Ny], CCclsy[Ny];
double clsx[Nx], clsy[Ny];

//coefficient for the ADI
double pvd[Nx][Ny];
double DDpvdx[Nx], AApvdx[Nx], BBpvdx[Nx], CCpvdx[Nx],    DDpvdy[Ny], AApvdy[Ny],BBpvdy[Ny], CCpvdy[Ny];
double pvdx[Nx], pvdy[Ny];
</code>```

////gap in the code display...///

```<code>for (t=tIni; t<tmax; t++)
    //      for (t=1; t<4; t++)
{

    ///////////////////////////////////////////////////////////////////////////////////////
    //calcul coef upwind differentiation
    ///////////////////////////////////////////////////////////////////////////////////////

    for (j=0; j<Ny; j++)
    {

        for (i=0; i<Nx; i++)
        {
            cx[i][j]=ux[i][j]*DtDx;
            cy[i][j]=uy[i][j]*DtDx;

            if(ux[i][j]>=0)
            {
                epsx[i][j]=1;
            }
            else
            {
                epsx[i][j]=-1;
            }

            if(uy[i][j]>=0)
            {
                epsy[i][j]=1;
            }
            else
            {
                epsy[i][j]=-1;
            }

        }

    }

#pragma omp parallel sections
    {
        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la concentration de bacterie
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            ADI_N(n, cO2, AAnx, BBnx, CCnx, DDnx, AAny, BBny, CCny, DDny, cx, cy, epsx, epsy, nx, ny, DtDbDxs, Dxs2);
        }
        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la concentration d'oxygene
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            ADI_C(cO2, n, AAcx, BBcx, CCcx, DDcx, AAcy, BBcy, CCcy, DDcy, cx, cy, epsx, epsy, cO2x, cO2y, DtDO2Dxs, Dxs2, gamma);
        }
        /*for (i=0; i<Nx; i++)
         {
         cO2[i][Ny-1]=1.;
         }*/

        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la concentration de cellulose
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            ADI_Cls(cls, n, cO2tmp, AAclsx, BBclsx, CCclsx, DDclsx, AAclsy, BBclsy, CCclsy, DDclsy, cx, cy, epsx, epsy, clsx, clsy, DtDclsDxs, Dxs2, Dt*kpvd, seuilCls, seuilStopCls);

        }
        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la concentration de poyverdine
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            ADI_Pvd(pvd, n, cO2, AApvdx, BBpvdx, CCpvdx, DDpvdx, AApvdy, BBpvdy, CCpvdy, DDpvdy, cx, cy, epsx, epsy, pvdx, pvdy, DtDpvdDxs, Dxs2, Dt*kpvd, seuilCls, seuilStopCls);
        }
        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la vorticité
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {

            ADI(vort, psi, n, cls, AAx, BBx, CCx, DDx, AAy, BBy, CCy, DDy, cx, cy, epsx, epsy, vortx, vorty, DtDxsRe, Dxs, coefMass, coefMassCls);
        }

        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la stream function
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            // Preparation de la force qui agit sur la stream function (potentiel vecteur)
            for (i=0; i<Nx; i++)
            {
                for (j=0; j<Ny; j++)
                {

                    fpsi[i][j]= - vort[i][j]*Dxs; //multiplie par Dxs car les coefficient son multiplie par beta=Dxs/Dys. Donc Dxs est l'unite spatiale.

                    if(isnan(fpsi[i][j]))
                    {
                        cout<<"error psi nan "<<__FILE__<<" line "<<__LINE__<<endl;
                        exit(2);
                    }

                }
            }

            //SOR
            //sorPoissonChebyshev(psi, fpsi, a, b, c, d, e, ee, Nx-1, Ny-1, rjac, it, Nitmax, relax, tol);
            sorPoissonChebyshev(psi, fpsi, a, b, c, d, e, ee, Nx, Ny, rjac, it, relax, Nitmax, tol);

            //condition aux bords. Psi constant sur la frontiere. Mise a zero pour le SOR
            for (i=0; i<Nx; i++)
            {
                psi[i][0]=0;
                psi[i][Ny-1]=0;
            }

            for (j=0; j<Ny; j++)
            {
                psi[0][j]=0;
                psi[Nx-1][j]=0;
            }
        }
            ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la vitesse
           ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            for (i=1; i<Nx-1; i++)
            {
                for (j=1; j<Ny-1; j++)
                {
                    ux[i][j]=(psi[i][j+1]-psi[i][j-1])/Dx2;
                    uy[i][j]=-(psi[i+1][j]-psi[i-1][j])/Dx2;
                }
            }

            //Boudary conditions
            for (i=0; i<Nx; i++)
            {
                ux[i][Ny-1]=ux[i][Ny-2]; //free slip
                //ux[i][Ny-1]=0; //no slip
                uy[i][Ny-1]=0;

                ux[i][0]=0;
                uy[i][0]=0;
            }
            for (j=0; j<Ny; j++)
            {
                ux[Nx-1][j]=0;
                uy[Nx-1][j]=0;

                ux[0][j]=0;
                uy[0][j]=0;
            }
        }

    }

}
}
</code>```

---EDIT : - Remove the tmp - ADI function

```<code>void ADI_N(double n[][Ny], double cO2[][Ny],
       double AAx[], double BBx[], double CCx[], double DDx[],
       double AAy[], double BBy[], double CCy[], double DDy[],
       double cx[][Ny], double cy[][Ny], double epsx[][Ny], double epsy[][Ny],
       double nx[], double ny[], double DbDtDxs, double Dxs2)

{

int i=0, j=0;

for (i=0; i<Nx; i++) //interior points
{
    for (j=0; j<Ny; j++) //interior points
    {
        if (cO2[i][j]>seuilO2) // Seuil en dessous du quel les bacteries ne pousse pas.
        {
            n[i][j]= n[i][j] * ( 1 + Dt /  tempsDeDivBact * cO2[i][j] ); // n et cO2 sont normalizée
        }

    }
}

////////////calcul sur y////////////
//calcul coef ADI

for (i=0; i<Nx; i++) //interior points
{
    for (j=1; j<Ny-1; j++) //interior points
    {

        //non conservative forme
        if (i==0)
        {
            AAy[j] = -.5 * DbDtDxs;

            BBy[j] = 1 + DbDtDxs;

            CCy[j] = - .5 * DbDtDxs;

            DDy[j] = .5 * DbDtDxs * n[1][j]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * DbDtDxs * n[i+1][j];

        }
        else if (i==Nx-1)
        {
            AAy[j] = -.5 * DbDtDxs;

            BBy[j] = 1 + DbDtDxs;

            CCy[j] = - .5 * DbDtDxs;

            DDy[j] = .5 * DbDtDxs * n[i-1][j]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * DbDtDxs * n[Nx-2][j];

        }
        else //conservative forme
        {

             AAy[j] = -.5 * ( .5 * (1 + epsy[i][j]) * cy[i][j-1] + DbDtDxs);

             BBy[j] = 1 + DbDtDxs + .5 * epsy[i][j] * cy[i][j];

             CCy[j] = .5 * ( .5 * ( 1 - epsy[i][j] ) * cy[i][j+1] - DbDtDxs);

             DDy[j] = .5 * (.5 * ( 1 + epsx[i][j] ) * cx[i-1][j] + DbDtDxs ) * n[i-1][j]
             + ( 1 - DbDtDxs - .5 * epsx[i][j] * cx[i][j] ) * n[i][j]
             + .5 * (- .5 * ( 1 - epsx[i][j] ) * cx[i+1][j] + DbDtDxs ) * n[i+1][j];

            /*AAy[j] = -.5 * ( .5 * cy[i][j-1] + DbDtDxs);

            BBy[j] = 1 + DbDtDxs ;

            CCy[j] = .5 * ( .5 * cy[i][j+1] - DbDtDxs);

            DDy[j] = .5 * (.5 * cx[i-1][j] + DbDtDxs ) * n[i-1][j]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * (- .5 * cx[i+1][j] + DbDtDxs ) * n[i+1][j];
            */
        }

        ny[j] = n[i][j];
    }

    //boundary condition values no flux n[i][j-1]=n[i][j+1] and substraction of a[0]*n[i][j] for tridaig calculation.
    //non conservative forme
    if (i==0) {
        j=0; //DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j] - -( .5 * cy[i][j] + DbDtDxs/2 ) * n[i][j-1];
        DDy[j] = DbDtDxs/2 * n[1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j]
        + DbDtDxs/2 * n[i][1];//AAy
        AAy[j] = 0; // pas utilise soustrait à DDy
        BBy[j] = 1 + DbDtDxs ;
        CCy[j] = .5 * ( .5 * cy[i][j+1] - DbDtDxs);

        j=Ny-1;
        DDy[j] = .5 * ( DbDtDxs + cx[i][j]/2) * n[1][j] + (1-DbDtDxs) * n[i][j] + .5 * ( DbDtDxs - cx[i][j]/2) * n[i+1][j]
        + DbDtDxs/2 * n[i][Ny-2]; //CCy
        AAy[j] = -.5 * ( .5  * cy[i][j] + DbDtDxs);
        BBy[j] = 1 + DbDtDxs;
        CCy[j] = 0; // pas utilise soustrait à DDy
    }
    else if(i==Nx-1)
    { j=0; //DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j] - -( .5 * cy[i][j] + DbDtDxs/2 ) * n[i][j-1];
        DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[Nx-2][j]
        + DbDtDxs/2 * n[i][1];
        AAy[j] = 0; // pas utilise soustrait à DDy
        BBy[j] = 1 + DbDtDxs ;
        CCy[j] = .5 * ( .5 * cy[i][j+1] - DbDtDxs);

        j=Ny-1;
        DDy[j] = .5 * ( DbDtDxs + cx[i][j]/2) * n[i-1][j] + (1-DbDtDxs) * n[i][j] + .5 * ( DbDtDxs - cx[i][j]/2) * n[Nx-2][j]
        + DbDtDxs/2 * n[i][Ny-2];
        AAy[j] = -.5 * ( .5  * cy[i][j] + DbDtDxs);
        BBy[j] = 1 + DbDtDxs;
        CCy[j] = 0; // pas utilise soustrait à DDy
    }
    else
    {
        j=0; //DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j] - -( .5 * cy[i][j] + DbDtDxs/2 ) * n[i][j-1];
        DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j]
        + DbDtDxs/2 * n[i][1];
        AAy[j] = 0; // pas utilise soustrait à DDy
        BBy[j] = 1 + DbDtDxs ;
        CCy[j] = .5 * ( .5 * cy[i][j+1] - DbDtDxs);

        j=Ny-1;
        DDy[j] = .5 * ( DbDtDxs + cx[i][j]/2) * n[i-1][j] + (1-DbDtDxs) * n[i][j] + .5 * ( DbDtDxs - cx[i][j]/2) * n[i+1][j]
        + DbDtDxs/2 * n[i][Ny-2];
        AAy[j] = -.5 * ( .5  * cy[i][j] + DbDtDxs);
        BBy[j] = 1 + DbDtDxs;
        CCy[j] = 0; // pas utilise soustrait à DDy

    }

    tridiag_full(AAy, BBy, CCy, DDy, ny, Ny-1); //calcul les point en 0 et en Ny-1 inclus

    for (j=0; j<Ny; j++)
    {
        n[i][j]=ny[j];
    }

}

////////////calcul sur x //////////
//calcul coef ADI
for (j=0; j<Ny; j++)
{

    for (i=1; i<Nx-1; i++)

    {

        //non conservative forme
        if(j==0)
        {

            AAx[i] = -.5 * DbDtDxs;
            BBx[i] = 1 + DbDtDxs;
            CCx[i] = - .5 * DbDtDxs ;

            DDx[i]= .5 * DbDtDxs * n[i][1]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * DbDtDxs * n[i][j+1];
        }

        else if(j==Ny-1)
        {

            AAx[i] = -.5* ( .5 * cx[i][j] + DbDtDxs );
            BBx[i] = 1 + DbDtDxs;
            CCx[i] = .5 * ( .5 * cx[i][j] - DbDtDxs) ;

            DDx[i]= .5 * ( .5 * cy[i][j] + DbDtDxs ) * n[i][j-1]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * ( - .5 * cy[i][j] + DbDtDxs ) * n[i][Ny-2];
        }
        else //conservative forme
        {

             AAx[i] = -.5* ( .5 * ( 1 + epsx[i][j] ) * cx[i-1][j] + DbDtDxs );
             BBx[i] = 1 + DbDtDxs + .5 * epsx[i][j] * cx[i][j];
             CCx[i] = .5 * ( .5 * ( 1 - epsx[i][j] ) * cx[i+1][j] - DbDtDxs) ;

             DDx[i]= .5 * ( .5 * ( 1 + epsy[i][j] ) * cy[i][j-1] + DbDtDxs ) * n[i][j-1]
             + ( 1 - DbDtDxs - .5 * epsy[i][j] * cy[i][j] ) * n[i][j]
             + .5 * (-.5 * ( 1 - epsy[i][j] ) * cy[i][j+1] + DbDtDxs ) * n[i][j+1];

        }

        nx[i]=n[i][j];

    }

    if(j==0)
    {
        i=0;
        AAx[i] = -.5 * DbDtDxs;
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = - .5 * DbDtDxs ;

        DDx[i]= .5 *  DbDtDxs * n[i][1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][j+1]
        -AAx[i];

        i=Nx-1;
        AAx[i] = -.5 * DbDtDxs;
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = - .5 * DbDtDxs ;

        DDx[i]= .5 *  DbDtDxs * n[i][1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][j+1]
        -CCx[i];

    }
    else if(j==Ny-1)
    {
        i=0;
        AAx[i] = -.5 * ( .5 * cx[i][j] + DbDtDxs);
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = .5 * (.5 * cx[i][j] - DbDtDxs );

        DDx[i]= .5 *  DbDtDxs * n[i][j-1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][Ny-2]
        -AAx[i];

        i=Nx-1;
        AAx[i] = -.5 * ( .5 * cx[i][j] + DbDtDxs);
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = .5 * (.5 * cx[i][j] - DbDtDxs );

        DDx[i]= .5 *  DbDtDxs * n[i][j-1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][Ny-2]
        -CCx[i];

    }
    else
    {
        i=0;
        AAx[i] = -.5 * DbDtDxs;
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = - .5 * DbDtDxs ;

        DDx[i]= .5 *  DbDtDxs * n[i][j-1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][j+1]
        -AAx[i];

        i=Nx-1;
        AAx[i] = -.5 * DbDtDxs;
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = - .5 * DbDtDxs ;

        DDx[i]= .5 *  DbDtDxs * n[i][j-1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][j+1]
        -CCx[i];

    }

    //boundary condition values no flux n[i-1][j]=n[i+1][j] and substraction of a[0]*n[i][j] for tridaig calculation.
    i=0;
    DDx[i] = (1-DbDtDxs) * n[i][j] + 3*DbDtDxs/2 * n[i+1][j];
    AAx[i] = 0 ; //pas utlise soustrait à DDx
    BBx[i] = 1 + DbDtDxs ;
    CCx[i] = - .5 * DbDtDxs ;

    i=Nx-1;
    DDx[i] = 3*DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j];
    AAx[i] = - .5* DbDtDxs;
    BBx[i] = 1 + DbDtDxs ;
    CCx[i] = 0 ; //pas utlise soustrait à DDx

    tridiag_full(AAx, BBx, CCx, DDx, nx, Nx-1); //calcul les point en 0 et en Nx-1 inclus

    for (i=0; i<Nx; i++)
    {
        n[i][j]=nx[i];

    }

}
}
</code>```

答: The index variables i & j are common across some of your sections which will cause synchronization issues between the sections and mostly incorrect output. Change it to either making the variables private for each section #pragma omp section private(i,j) or initialize them in each loop for (int i=1; i<Nx-1; i++). I always prefer to initialize the variables in my loops, there by I don't accidentally reuse a stale value from previous iteration.

Title: parallel sections with openmp

Body:
Hi,

I am trying to run this code in parrallel sections with openmp

I wanted to speed up its execution. These blocs can by calculated independently at every time step t.

Unfortunatly, instead, the code is slowed down by openmp. I have been looking for concurrency but did not see any problem.

Did I use the sections argument correctly?

Is there a better parallalisation framework that I should use?

All the best

#define Nx 300
#define Ny 100
int main()
{
int i, j, t=0, it=0;

double vort[Nx][Ny], psi[Nx][Ny], ux[Nx][Ny], uy[Nx][Ny];

double fpsi[Nx][Ny];
double a[Nx][Ny], b[Nx][Ny],c[Nx][Ny], d[Nx][Ny], e[Nx][Ny], ee[Nx][Ny];

//tridiagonal coefficient
//coefficient for the internal grid for vort. Indexed from 0 to Nx-3 or Ny-3 to calculate the interior grid point only. The idix are shiffted of (+1,+1) compare to vort or psi or ux or uy.
//double DDx[Nx-2], AAx[Nx-2], BBx[Nx-2], CCx[Nx-2],    DDy[Ny-2], AAy[Ny-2],BBy[Ny-2], CCy[Ny-2];
double DDx[Nx], AAx[Nx], BBx[Nx], CCx[Nx],    DDy[Ny], AAy[Ny],BBy[Ny], CCy[Ny];
double epsx[Nx][Ny], epsy[Nx][Ny]; //same dimension than ux and uy
//double vortx[Nx-2], vorty[Ny-2];
double vortx[Nx], vorty[Ny];
double cx[Nx][Ny], cy[Nx][Ny]; //same dimension than ux and uy

//coefficient for the ADI
double n[Nx][Ny];
double DDnx[Nx], AAnx[Nx], BBnx[Nx], CCnx[Nx],    DDny[Ny], AAny[Ny],BBny[Ny], CCny[Ny];
double nx[Nx], ny[Ny];

//coefficient for the ADI
double cO2[Nx][Ny];
double DDcx[Nx], AAcx[Nx], BBcx[Nx], CCcx[Nx],    DDcy[Ny], AAcy[Ny],BBcy[Ny], CCcy[Ny];
double cO2x[Nx], cO2y[Ny];

//coefficient for the ADI
double cls[Nx][Ny];
double DDclsx[Nx], AAclsx[Nx], BBclsx[Nx], CCclsx[Nx],    DDclsy[Ny], AAclsy[Ny],BBclsy[Ny], CCclsy[Ny];
double clsx[Nx], clsy[Ny];

//coefficient for the ADI
double pvd[Nx][Ny];
double DDpvdx[Nx], AApvdx[Nx], BBpvdx[Nx], CCpvdx[Nx],    DDpvdy[Ny], AApvdy[Ny],BBpvdy[Ny], CCpvdy[Ny];
double pvdx[Nx], pvdy[Ny];

////gap in the code display...///

for (t=tIni; t<tmax; t++)
    //      for (t=1; t<4; t++)
{

    ///////////////////////////////////////////////////////////////////////////////////////
    //calcul coef upwind differentiation
    ///////////////////////////////////////////////////////////////////////////////////////

    for (j=0; j<Ny; j++)
    {

        for (i=0; i<Nx; i++)
        {
            cx[i][j]=ux[i][j]*DtDx;
            cy[i][j]=uy[i][j]*DtDx;

            if(ux[i][j]>=0)
            {
                epsx[i][j]=1;
            }
            else
            {
                epsx[i][j]=-1;
            }

            if(uy[i][j]>=0)
            {
                epsy[i][j]=1;
            }
            else
            {
                epsy[i][j]=-1;
            }

        }

    }

#pragma omp parallel sections
    {
        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la concentration de bacterie
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            ADI_N(n, cO2, AAnx, BBnx, CCnx, DDnx, AAny, BBny, CCny, DDny, cx, cy, epsx, epsy, nx, ny, DtDbDxs, Dxs2);
        }
        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la concentration d'oxygene
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            ADI_C(cO2, n, AAcx, BBcx, CCcx, DDcx, AAcy, BBcy, CCcy, DDcy, cx, cy, epsx, epsy, cO2x, cO2y, DtDO2Dxs, Dxs2, gamma);
        }
        /*for (i=0; i<Nx; i++)
         {
         cO2[i][Ny-1]=1.;
         }*/

        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la concentration de cellulose
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            ADI_Cls(cls, n, cO2tmp, AAclsx, BBclsx, CCclsx, DDclsx, AAclsy, BBclsy, CCclsy, DDclsy, cx, cy, epsx, epsy, clsx, clsy, DtDclsDxs, Dxs2, Dt*kpvd, seuilCls, seuilStopCls);

        }
        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la concentration de poyverdine
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            ADI_Pvd(pvd, n, cO2, AApvdx, BBpvdx, CCpvdx, DDpvdx, AApvdy, BBpvdy, CCpvdy, DDpvdy, cx, cy, epsx, epsy, pvdx, pvdy, DtDpvdDxs, Dxs2, Dt*kpvd, seuilCls, seuilStopCls);
        }
        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la vorticité
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {

            ADI(vort, psi, n, cls, AAx, BBx, CCx, DDx, AAy, BBy, CCy, DDy, cx, cy, epsx, epsy, vortx, vorty, DtDxsRe, Dxs, coefMass, coefMassCls);
        }

        ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la stream function
        ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            // Preparation de la force qui agit sur la stream function (potentiel vecteur)
            for (i=0; i<Nx; i++)
            {
                for (j=0; j<Ny; j++)
                {

                    fpsi[i][j]= - vort[i][j]*Dxs; //multiplie par Dxs car les coefficient son multiplie par beta=Dxs/Dys. Donc Dxs est l'unite spatiale.

                    if(isnan(fpsi[i][j]))
                    {
                        cout<<"error psi nan "<<__FILE__<<" line "<<__LINE__<<endl;
                        exit(2);
                    }

                }
            }

            //SOR
            //sorPoissonChebyshev(psi, fpsi, a, b, c, d, e, ee, Nx-1, Ny-1, rjac, it, Nitmax, relax, tol);
            sorPoissonChebyshev(psi, fpsi, a, b, c, d, e, ee, Nx, Ny, rjac, it, relax, Nitmax, tol);

            //condition aux bords. Psi constant sur la frontiere. Mise a zero pour le SOR
            for (i=0; i<Nx; i++)
            {
                psi[i][0]=0;
                psi[i][Ny-1]=0;
            }

            for (j=0; j<Ny; j++)
            {
                psi[0][j]=0;
                psi[Nx-1][j]=0;
            }
        }
            ///////////////////////////////////////////////////////////////////////////////////////
        //calcul de la vitesse
           ///////////////////////////////////////////////////////////////////////////////////////
#pragma omp section
        {
            for (i=1; i<Nx-1; i++)
            {
                for (j=1; j<Ny-1; j++)
                {
                    ux[i][j]=(psi[i][j+1]-psi[i][j-1])/Dx2;
                    uy[i][j]=-(psi[i+1][j]-psi[i-1][j])/Dx2;
                }
            }

            //Boudary conditions
            for (i=0; i<Nx; i++)
            {
                ux[i][Ny-1]=ux[i][Ny-2]; //free slip
                //ux[i][Ny-1]=0; //no slip
                uy[i][Ny-1]=0;

                ux[i][0]=0;
                uy[i][0]=0;
            }
            for (j=0; j<Ny; j++)
            {
                ux[Nx-1][j]=0;
                uy[Nx-1][j]=0;

                ux[0][j]=0;
                uy[0][j]=0;
            }
        }

    }

}
}

---EDIT : - Remove the tmp - ADI function

void ADI_N(double n[][Ny], double cO2[][Ny],
       double AAx[], double BBx[], double CCx[], double DDx[],
       double AAy[], double BBy[], double CCy[], double DDy[],
       double cx[][Ny], double cy[][Ny], double epsx[][Ny], double epsy[][Ny],
       double nx[], double ny[], double DbDtDxs, double Dxs2)

{

int i=0, j=0;

for (i=0; i<Nx; i++) //interior points
{
    for (j=0; j<Ny; j++) //interior points
    {
        if (cO2[i][j]>seuilO2) // Seuil en dessous du quel les bacteries ne pousse pas.
        {
            n[i][j]= n[i][j] * ( 1 + Dt /  tempsDeDivBact * cO2[i][j] ); // n et cO2 sont normalizée
        }

    }
}

////////////calcul sur y////////////
//calcul coef ADI

for (i=0; i<Nx; i++) //interior points
{
    for (j=1; j<Ny-1; j++) //interior points
    {

        //non conservative forme
        if (i==0)
        {
            AAy[j] = -.5 * DbDtDxs;

            BBy[j] = 1 + DbDtDxs;

            CCy[j] = - .5 * DbDtDxs;

            DDy[j] = .5 * DbDtDxs * n[1][j]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * DbDtDxs * n[i+1][j];

        }
        else if (i==Nx-1)
        {
            AAy[j] = -.5 * DbDtDxs;

            BBy[j] = 1 + DbDtDxs;

            CCy[j] = - .5 * DbDtDxs;

            DDy[j] = .5 * DbDtDxs * n[i-1][j]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * DbDtDxs * n[Nx-2][j];

        }
        else //conservative forme
        {

             AAy[j] = -.5 * ( .5 * (1 + epsy[i][j]) * cy[i][j-1] + DbDtDxs);

             BBy[j] = 1 + DbDtDxs + .5 * epsy[i][j] * cy[i][j];

             CCy[j] = .5 * ( .5 * ( 1 - epsy[i][j] ) * cy[i][j+1] - DbDtDxs);

             DDy[j] = .5 * (.5 * ( 1 + epsx[i][j] ) * cx[i-1][j] + DbDtDxs ) * n[i-1][j]
             + ( 1 - DbDtDxs - .5 * epsx[i][j] * cx[i][j] ) * n[i][j]
             + .5 * (- .5 * ( 1 - epsx[i][j] ) * cx[i+1][j] + DbDtDxs ) * n[i+1][j];

            /*AAy[j] = -.5 * ( .5 * cy[i][j-1] + DbDtDxs);

            BBy[j] = 1 + DbDtDxs ;

            CCy[j] = .5 * ( .5 * cy[i][j+1] - DbDtDxs);

            DDy[j] = .5 * (.5 * cx[i-1][j] + DbDtDxs ) * n[i-1][j]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * (- .5 * cx[i+1][j] + DbDtDxs ) * n[i+1][j];
            */
        }

        ny[j] = n[i][j];
    }

    //boundary condition values no flux n[i][j-1]=n[i][j+1] and substraction of a[0]*n[i][j] for tridaig calculation.
    //non conservative forme
    if (i==0) {
        j=0; //DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j] - -( .5 * cy[i][j] + DbDtDxs/2 ) * n[i][j-1];
        DDy[j] = DbDtDxs/2 * n[1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j]
        + DbDtDxs/2 * n[i][1];//AAy
        AAy[j] = 0; // pas utilise soustrait à DDy
        BBy[j] = 1 + DbDtDxs ;
        CCy[j] = .5 * ( .5 * cy[i][j+1] - DbDtDxs);

        j=Ny-1;
        DDy[j] = .5 * ( DbDtDxs + cx[i][j]/2) * n[1][j] + (1-DbDtDxs) * n[i][j] + .5 * ( DbDtDxs - cx[i][j]/2) * n[i+1][j]
        + DbDtDxs/2 * n[i][Ny-2]; //CCy
        AAy[j] = -.5 * ( .5  * cy[i][j] + DbDtDxs);
        BBy[j] = 1 + DbDtDxs;
        CCy[j] = 0; // pas utilise soustrait à DDy
    }
    else if(i==Nx-1)
    { j=0; //DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j] - -( .5 * cy[i][j] + DbDtDxs/2 ) * n[i][j-1];
        DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[Nx-2][j]
        + DbDtDxs/2 * n[i][1];
        AAy[j] = 0; // pas utilise soustrait à DDy
        BBy[j] = 1 + DbDtDxs ;
        CCy[j] = .5 * ( .5 * cy[i][j+1] - DbDtDxs);

        j=Ny-1;
        DDy[j] = .5 * ( DbDtDxs + cx[i][j]/2) * n[i-1][j] + (1-DbDtDxs) * n[i][j] + .5 * ( DbDtDxs - cx[i][j]/2) * n[Nx-2][j]
        + DbDtDxs/2 * n[i][Ny-2];
        AAy[j] = -.5 * ( .5  * cy[i][j] + DbDtDxs);
        BBy[j] = 1 + DbDtDxs;
        CCy[j] = 0; // pas utilise soustrait à DDy
    }
    else
    {
        j=0; //DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j] - -( .5 * cy[i][j] + DbDtDxs/2 ) * n[i][j-1];
        DDy[j] = DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j] + DbDtDxs/2 * n[i+1][j]
        + DbDtDxs/2 * n[i][1];
        AAy[j] = 0; // pas utilise soustrait à DDy
        BBy[j] = 1 + DbDtDxs ;
        CCy[j] = .5 * ( .5 * cy[i][j+1] - DbDtDxs);

        j=Ny-1;
        DDy[j] = .5 * ( DbDtDxs + cx[i][j]/2) * n[i-1][j] + (1-DbDtDxs) * n[i][j] + .5 * ( DbDtDxs - cx[i][j]/2) * n[i+1][j]
        + DbDtDxs/2 * n[i][Ny-2];
        AAy[j] = -.5 * ( .5  * cy[i][j] + DbDtDxs);
        BBy[j] = 1 + DbDtDxs;
        CCy[j] = 0; // pas utilise soustrait à DDy

    }

    tridiag_full(AAy, BBy, CCy, DDy, ny, Ny-1); //calcul les point en 0 et en Ny-1 inclus

    for (j=0; j<Ny; j++)
    {
        n[i][j]=ny[j];
    }

}

////////////calcul sur x //////////
//calcul coef ADI
for (j=0; j<Ny; j++)
{

    for (i=1; i<Nx-1; i++)

    {

        //non conservative forme
        if(j==0)
        {

            AAx[i] = -.5 * DbDtDxs;
            BBx[i] = 1 + DbDtDxs;
            CCx[i] = - .5 * DbDtDxs ;

            DDx[i]= .5 * DbDtDxs * n[i][1]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * DbDtDxs * n[i][j+1];
        }

        else if(j==Ny-1)
        {

            AAx[i] = -.5* ( .5 * cx[i][j] + DbDtDxs );
            BBx[i] = 1 + DbDtDxs;
            CCx[i] = .5 * ( .5 * cx[i][j] - DbDtDxs) ;

            DDx[i]= .5 * ( .5 * cy[i][j] + DbDtDxs ) * n[i][j-1]
            + ( 1 - DbDtDxs ) * n[i][j]
            + .5 * ( - .5 * cy[i][j] + DbDtDxs ) * n[i][Ny-2];
        }
        else //conservative forme
        {

             AAx[i] = -.5* ( .5 * ( 1 + epsx[i][j] ) * cx[i-1][j] + DbDtDxs );
             BBx[i] = 1 + DbDtDxs + .5 * epsx[i][j] * cx[i][j];
             CCx[i] = .5 * ( .5 * ( 1 - epsx[i][j] ) * cx[i+1][j] - DbDtDxs) ;

             DDx[i]= .5 * ( .5 * ( 1 + epsy[i][j] ) * cy[i][j-1] + DbDtDxs ) * n[i][j-1]
             + ( 1 - DbDtDxs - .5 * epsy[i][j] * cy[i][j] ) * n[i][j]
             + .5 * (-.5 * ( 1 - epsy[i][j] ) * cy[i][j+1] + DbDtDxs ) * n[i][j+1];

        }

        nx[i]=n[i][j];

    }

    if(j==0)
    {
        i=0;
        AAx[i] = -.5 * DbDtDxs;
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = - .5 * DbDtDxs ;

        DDx[i]= .5 *  DbDtDxs * n[i][1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][j+1]
        -AAx[i];

        i=Nx-1;
        AAx[i] = -.5 * DbDtDxs;
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = - .5 * DbDtDxs ;

        DDx[i]= .5 *  DbDtDxs * n[i][1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][j+1]
        -CCx[i];

    }
    else if(j==Ny-1)
    {
        i=0;
        AAx[i] = -.5 * ( .5 * cx[i][j] + DbDtDxs);
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = .5 * (.5 * cx[i][j] - DbDtDxs );

        DDx[i]= .5 *  DbDtDxs * n[i][j-1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][Ny-2]
        -AAx[i];

        i=Nx-1;
        AAx[i] = -.5 * ( .5 * cx[i][j] + DbDtDxs);
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = .5 * (.5 * cx[i][j] - DbDtDxs );

        DDx[i]= .5 *  DbDtDxs * n[i][j-1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][Ny-2]
        -CCx[i];

    }
    else
    {
        i=0;
        AAx[i] = -.5 * DbDtDxs;
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = - .5 * DbDtDxs ;

        DDx[i]= .5 *  DbDtDxs * n[i][j-1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][j+1]
        -AAx[i];

        i=Nx-1;
        AAx[i] = -.5 * DbDtDxs;
        BBx[i] = 1 + DbDtDxs;
        CCx[i] = - .5 * DbDtDxs ;

        DDx[i]= .5 *  DbDtDxs * n[i][j-1] + ( 1 - DbDtDxs ) * n[i][j] + .5 * DbDtDxs * n[i][j+1]
        -CCx[i];

    }

    //boundary condition values no flux n[i-1][j]=n[i+1][j] and substraction of a[0]*n[i][j] for tridaig calculation.
    i=0;
    DDx[i] = (1-DbDtDxs) * n[i][j] + 3*DbDtDxs/2 * n[i+1][j];
    AAx[i] = 0 ; //pas utlise soustrait à DDx
    BBx[i] = 1 + DbDtDxs ;
    CCx[i] = - .5 * DbDtDxs ;

    i=Nx-1;
    DDx[i] = 3*DbDtDxs/2 * n[i-1][j] + (1-DbDtDxs) * n[i][j];
    AAx[i] = - .5* DbDtDxs;
    BBx[i] = 1 + DbDtDxs ;
    CCx[i] = 0 ; //pas utlise soustrait à DDx

    tridiag_full(AAx, BBx, CCx, DDx, nx, Nx-1); //calcul les point en 0 et en Nx-1 inclus

    for (i=0; i<Nx; i++)
    {
        n[i][j]=nx[i];

    }

}
}


来源: stackexchange

问:
OpenCV HoughCircles not detecting circles
I'm implementing a function in order to detect circles in an image. I am using OpenCV for Java to identify the circles. The gray scale images do show a circle.

Here's my code:

```<code>Mat gray = new Mat();
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.blur(gray, gray, new Size(3, 3));

Mat edges = new Mat();
int lowThreshold = 100;
int ratio = 3;
Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio);

Mat circles = new Mat();
Vector<Mat> circlesList = new Vector<Mat>();

Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 60, 200, 20, 30, 0);

Imshow grayIM = new Imshow("grayscale");
grayIM.showImage(edges);
</code>```

Any idea why this may be the case?

答:
First, as Miki pointed out HughCircles should be applied directly on the greyscale, it has an internal Canny Edge detector of its own.

Second the parameters of HughCircles should be tuned to your specific type of images. There isn't a one setting fits all formula.

Based on your code this worked for me on some generated circles:

```<code>public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat img = Highgui.imread("circle-in.jpg", Highgui.CV_LOAD_IMAGE_ANYCOLOR);

    Mat gray = new Mat();
    Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.blur(gray, gray, new Size(3, 3));

    Mat circles = new Mat();
    double minDist = 60;
    // higher threshold of Canny Edge detector, lower threshold is twice smaller
    double p1UpperThreshold = 200;
    // the smaller it is, the more false circles may be detected
    double p2AccumulatorThreshold = 20;
    int minRadius = 30;
    int maxRadius = 0;
    // use gray image, not edge detected
    Imgproc.HoughCircles(gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, minDist, p1UpperThreshold, p2AccumulatorThreshold, minRadius, maxRadius);

    // draw the detected circles
    Mat detected = img.clone();
    for (int x = 0; x < circles.cols(); x++) {
        double[] c1xyr = circles.get(0, x);
        Point xy = new Point(Math.round(c1xyr[0]), Math.round(c1xyr[1]));
        int radius = (int) Math.round(c1xyr[2]);
        Core.circle(detected, xy, radius, new Scalar(0, 0, 255), 3);
    }

    Highgui.imwrite("circle-out.jpg", detected);
}
</code>```

Input image with circles:

Detected circles are colored red:

Note how in the output image the white circle on the left was not detected being very close to white. It will be if you set p1UpperThreshold=20.

ContentLicense: CC BY-SA 3.0

Body:
I'm implementing a function in order to detect circles in an image. I am using OpenCV for Java to identify the circles. The gray scale images do show a circle.

Here's my code:

Mat gray = new Mat();
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.blur(gray, gray, new Size(3, 3));

Mat edges = new Mat();
int lowThreshold = 100;
int ratio = 3;
Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio);

Mat circles = new Mat();
Vector<Mat> circlesList = new Vector<Mat>();

Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 60, 200, 20, 30, 0);

Imshow grayIM = new Imshow("grayscale");
grayIM.showImage(edges);

Any idea why this may be the case?

Body:
First, as Miki pointed out HughCircles should be applied directly on the greyscale, it has an internal Canny Edge detector of its own.

Second the parameters of HughCircles should be tuned to your specific type of images. There isn't a one setting fits all formula.

Based on your code this worked for me on some generated circles:

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat img = Highgui.imread("circle-in.jpg", Highgui.CV_LOAD_IMAGE_ANYCOLOR);

    Mat gray = new Mat();
    Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.blur(gray, gray, new Size(3, 3));

    Mat circles = new Mat();
    double minDist = 60;
    // higher threshold of Canny Edge detector, lower threshold is twice smaller
    double p1UpperThreshold = 200;
    // the smaller it is, the more false circles may be detected
    double p2AccumulatorThreshold = 20;
    int minRadius = 30;
    int maxRadius = 0;
    // use gray image, not edge detected
    Imgproc.HoughCircles(gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, minDist, p1UpperThreshold, p2AccumulatorThreshold, minRadius, maxRadius);

    // draw the detected circles
    Mat detected = img.clone();
    for (int x = 0; x < circles.cols(); x++) {
        double[] c1xyr = circles.get(0, x);
        Point xy = new Point(Math.round(c1xyr[0]), Math.round(c1xyr[1]));
        int radius = (int) Math.round(c1xyr[2]);
        Core.circle(detected, xy, radius, new Scalar(0, 0, 255), 3);
    }

    Highgui.imwrite("circle-out.jpg", detected);
}

Input image with circles:

Detected circles are colored red:

Note how in the output image the white circle on the left was not detected being very close to white. It will be if you set p1UpperThreshold=20.


问:
Finding correlation coefficient from 2 lists
I am working on a project which has many functions when given a couple lists of data. I've already seperated the lists and I have defined some functions which I know for certain work correctly, that being a mean function and standard deviation function. My issue is when testing my lists I get a correct mean, correct standard deviation, but incorrect correlation coefficient. Could my math be off here? I need to find the correlation coefficient with only Python's standard library.

MY CODE:

```<code>def correlCo(someList1, someList2):

    # First establish the means and standard deviations for both lists.
    xMean = mean(someList1)
    yMean = mean(someList2)
    xStandDev = standDev(someList1)
    yStandDev = standDev(someList2)
    zList1 = []
    zList2 = []

    # Create 2 new lists taking (a[i]-a's Mean)/standard deviation of a
    for x in someList1:
        z1 = ((float(x)-xMean)/xStandDev)
        zList1.append(z1)

    for y in someList2:
        z2 = ((float(y)-yMean)/yStandDev)
        zList2.append(z2)

    # Mapping out the lists to be float values instead of string
    zList1 = list(map(float,zList1))
    zList2 = list(map(float,zList2))
    # Multiplying each value from the lists
    zFinal = [a*b for a,b in zip(zList1,zList2)]
    totalZ = 0
    # Taking the sum of all the products
    for a in zFinal:
        totalZ += a
    # Finally calculating correlation coefficient
    r = (1/(len(someList1) - 1)) * totalZ

    return r
</code>```

SAMPLE RUN:

I have a list of [1,2,3,4,4,8] and [3,3,4,5,8,9]

I expect the correct answer of r = 0.8848, but get r = .203727

EDIT: To include the mean and standard deviation functions I have made.

```<code>def mean(someList):
    total = 0
    for a in someList:
        total += float(a)
    mean = total/len(someList)
    return mean

def standDev(someList):
    newList = []
    sdTotal = 0
    listMean = mean(someList)
    for a in someList:
        newNum = (float(a) - listMean)**2
        newList.append(newNum)

    for z in newList:
        sdTotal += float(z)

    standardDeviation = sdTotal/(len(newList))

    return standardDeviation
</code>```

答:
Pearson Correlation Coefficient

Code (modified)

```<code>def mean(someList):
    total = 0
    for a in someList:
        total += float(a)
    mean = total/len(someList)
    return mean
def standDev(someList):
    listMean = mean(someList)
    dev = 0.0
    for i in range(len(someList)):
        dev += (someList[i]-listMean)**2
    dev = dev**(1/2.0)
    return dev
def correlCo(someList1, someList2):

    # First establish the means and standard deviations for both lists.
    xMean = mean(someList1)
    yMean = mean(someList2)
    xStandDev = standDev(someList1)
    yStandDev = standDev(someList2)
    # r numerator
    rNum = 0.0
    for i in range(len(someList1)):
        rNum += (someList1[i]-xMean)*(someList2[i]-yMean)

    # r denominator
    rDen = xStandDev * yStandDev

    r =  rNum/rDen
    return r

print(correlCo([1,2,3,4,4,8], [3,3,4,5,8,9]))
</code>```

Output

```<code>0.884782972876
</code>```

Body:
I am working on a project which has many functions when given a couple lists of data. I've already seperated the lists and I have defined some functions which I know for certain work correctly, that being a mean function and standard deviation function. My issue is when testing my lists I get a correct mean, correct standard deviation, but incorrect correlation coefficient. Could my math be off here? I need to find the correlation coefficient with only Python's standard library.

MY CODE:

def correlCo(someList1, someList2):

    # First establish the means and standard deviations for both lists.
    xMean = mean(someList1)
    yMean = mean(someList2)
    xStandDev = standDev(someList1)
    yStandDev = standDev(someList2)
    zList1 = []
    zList2 = []

    # Create 2 new lists taking (a[i]-a's Mean)/standard deviation of a
    for x in someList1:
        z1 = ((float(x)-xMean)/xStandDev)
        zList1.append(z1)

    for y in someList2:
        z2 = ((float(y)-yMean)/yStandDev)
        zList2.append(z2)

    # Mapping out the lists to be float values instead of string
    zList1 = list(map(float,zList1))
    zList2 = list(map(float,zList2))
    # Multiplying each value from the lists
    zFinal = [a*b for a,b in zip(zList1,zList2)]
    totalZ = 0
    # Taking the sum of all the products
    for a in zFinal:
        totalZ += a
    # Finally calculating correlation coefficient
    r = (1/(len(someList1) - 1)) * totalZ

    return r

SAMPLE RUN:

I have a list of [1,2,3,4,4,8] and [3,3,4,5,8,9]

I expect the correct answer of r = 0.8848, but get r = .203727

EDIT: To include the mean and standard deviation functions I have made.

def mean(someList):
    total = 0
    for a in someList:
        total += float(a)
    mean = total/len(someList)
    return mean

def standDev(someList):
    newList = []
    sdTotal = 0
    listMean = mean(someList)
    for a in someList:
        newNum = (float(a) - listMean)**2
        newList.append(newNum)

    for z in newList:
        sdTotal += float(z)

    standardDeviation = sdTotal/(len(newList))

    return standardDeviation

Body:
Pearson Correlation Coefficient

Code (modified)

def mean(someList):
    total = 0
    for a in someList:
        total += float(a)
    mean = total/len(someList)
    return mean
def standDev(someList):
    listMean = mean(someList)
    dev = 0.0
    for i in range(len(someList)):
        dev += (someList[i]-listMean)**2
    dev = dev**(1/2.0)
    return dev
def correlCo(someList1, someList2):

    # First establish the means and standard deviations for both lists.
    xMean = mean(someList1)
    yMean = mean(someList2)
    xStandDev = standDev(someList1)
    yStandDev = standDev(someList2)
    # r numerator
    rNum = 0.0
    for i in range(len(someList1)):
        rNum += (someList1[i]-xMean)*(someList2[i]-yMean)

    # r denominator
    rDen = xStandDev * yStandDev

    r =  rNum/rDen
    return r

print(correlCo([1,2,3,4,4,8], [3,3,4,5,8,9]))

Output

0.884782972876


问:
Textbox omits the string after the double quote sign in Asp.net 3.5
I am writing value to a textbox from handler(ashx) file. But the problem is that when the value contains double quote or single quote the textbox only shows the value before that quote and omits the the string after that quote.
For eg Value is: Hi "there
The textbox shows: Hi
Please help me out.</p>

答:
Use a escape sequence '\' before using ". So, like this - </p>

```<code>Textbox.Text = "Hi \"there";
</code>```

ContentLicense: CC BY-SA 2.5

Body:
Use a escape sequence '\' before using ". So, like this -

Textbox.Text = "Hi \"there";


问:
Figuring out if variable is instanceof a prototype of a function
Say I have

```<code>function Foo() {
    // stuff
}

Foo.prototype.bar = function() {
    return new Bar();
}

function Bar() {};

module.exports = Foo;
</code>```

Now say I have

```<code>const foo = new Foo();
console.log(foo instanceof Foo); // true

const bar = new Foo().bar();
console.log(bar instanceof Foo); // false
console.log(bar instanceof Foo.prototype.bar); // false
</code>```

Here Bar is never exported. So how can I test if bar is an instance of ... something? Can I somehow have bar instanceof Foo or a subset of Foo or something?

答:
By default, the function Bar is available as the constructor on Bar.prototype, which is in new Bar()'s prototype chain. (This is basically the only thing that constructor is ever useful for in practice.)

```<code>bar instanceof new Foo().bar().constructor
</code>```

Conversely, if you don't want to leak the Bar constructor function in this fashion, you can clobber Bar.prototype.constructor with a new value (or just delete it) before your export Foo. If constructor has been cleared in this way, you can still check if an object's prototype chain includes the same prototype as a newly-created Bar instance:

```<code>var barProto = Object.getPrototypeOf(new Foo().bar());
var isBar = barProto.isPrototypeOf(bar);
</code>```

Body:
Say I have

function Foo() {
    // stuff
}

Foo.prototype.bar = function() {
    return new Bar();
}

function Bar() {};

module.exports = Foo;

Now say I have

const foo = new Foo();
console.log(foo instanceof Foo); // true

const bar = new Foo().bar();
console.log(bar instanceof Foo); // false
console.log(bar instanceof Foo.prototype.bar); // false

Here Bar is never exported. So how can I test if bar is an instance of ... something? Can I somehow have bar instanceof Foo or a subset of Foo or something?

Body:
By default, the function Bar is available as the constructor on Bar.prototype, which is in new Bar()'s prototype chain. (This is basically the only thing that constructor is ever useful for in practice.)

bar instanceof new Foo().bar().constructor

Conversely, if you don't want to leak the Bar constructor function in this fashion, you can clobber Bar.prototype.constructor with a new value (or just delete it) before your export Foo. If constructor has been cleared in this way, you can still check if an object's prototype chain includes the same prototype as a newly-created Bar instance:

var barProto = Object.getPrototypeOf(new Foo().bar());
var isBar = barProto.isPrototypeOf(bar);


问:
Php to replace @username with link to twitter account
I assume this is some to do with regex, but it's an art all it's own - and I need some help.

When we display a story, we store all the text in a varilable - let's say $story.

What i'd like is to do a str_replace (I think that's what I need) that says something like this "If the text contains @something then turn it into a link like @something- but only do this if there's nothing before the @ symbol" (to exclude email addresses)

Also, we'd need to stop the 'something' if there's a space or punctuation after it. so that @something. doesn't turn into @something.

Any suggestions on how to make this work?

答:
A positive lookbehind could do the trick:

```<code>preg_replace('/(?<=\s)@(.*?)/', '<a href="....com/$1">@$1</a>')
</code>```

going off the top of my head. "If there's a @ which is preceded by something which is whitespace, then take whatever follows after the @ and do the html tag wrapping".

Body:
A positive lookbehind could do the trick:

preg_replace('/(?<=\s)@(.*?)/', '<a href="....com/$1">@$1</a>')

going off the top of my head. "If there's a @ which is preceded by something which is whitespace, then take whatever follows after the @ and do the html tag wrapping".


问:
Is there a way to start windows installer and avoid the UAC popup
I need to run an *.msi file from within my c# application.  The install runs without a UI and must run unattended.  Is there a way to avoid the UAC popup window?</p>

答: There must be a an elevation prompt somewhere, but you could defer the UAC prompt to your C# launcher. It starts, gets UAC elevation, and if you then start the MSI install as a CreateProcess() initiation (or just call MsiInstallProduct) the MSI will also be elevated. </p>


问:
Longitude and Latitude to location on sphere in Unity

Hi all,

I'm trying to transform locations based upon longitude and latitude to a vector3 location, which will be placed on a sphere in Unity. However, the location seems to be constantly off (compared to the actual location).

I use the following code at the moment:

```<code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testPosLatLong : MonoBehaviour {

    public float longi;
    public float lati;
    public float radius;
    public Transform marker;

    // Use this for initialization
    void Start () {

        // Transfer to Radians from Degrees
        float templongi = longi * Mathf.PI / 180;
        float templati = lati * Mathf.PI / 180;

        float Xpos = radius * Mathf.Cos(templati) * Mathf.Cos(templongi);
        float Ypos = radius * Mathf.Cos(templati) * Mathf.Sin(templongi);
        float Zpos = radius * Mathf.Sin(templati);

        Debug.Log ("X, Y, Z" + Xpos + " " + Ypos + " " + Zpos);

        // Set the X,Y,Z pos from the long and lat
        Instantiate(marker);
        marker.position = new Vector3 (Xpos, Zpos, Ypos);

    }

    // Update is called once per frame
    void Update () {

    }
}
</code>```

I've tried to set the longitude and latitude to zero, which looks like it got the right position on the sphere:

But when I try the longitude and latitude of Amsterdam for example it gives the following results (left side), while it should be the result on the right side.

Am I missing something or what is going wrong here? I tried googling a lot, but couldn't find anything that might explain my current problem. The project itself can be found here: https://wetransfer.com/downloads/31303bd353fd9fde874e92338e68573120171205170107/1208ac%3E

Hope somebody can help.

答:
I think your globe is what's wrong. It's tough to see from your images, but to me the equator looks like it's in slightly the wrong spot, and the North Pole looks to be too crowded with Greenland. I suspect is has to do with the projection you're using to paste the globe image onto the sphere.

Typically there are very complex ways of projecting 2D maps/images onto 3D surfaces. There's a whole field of geospatial analysis on how to do this accurately. It's a tough problem.

Also, The actual earth isn't exactly a sphere, so this could give you errors as well, but I would guess these would result in much smaller errors that what you describe. Accurate maps reperesent the 3D earth as a "geoid". to get locally accurate maps, people typically project data differently in small areas to maximize accuracy at a local scale. Otherwise you see large distortions. So as you zoom into a global map, the projections actually change significantly.

One possible way to test where the issue is coming from could be to set up control points of known coordinates both on your map image and on the globe, and then test to see if they line up. Like put a spike sticking out of the world at where Amsterdam SHOULD be on the globe, and where it is on the actual image. If these two don't line up then you can be pretty sure where your problem lies. This is called "Georeferencing".

I think for 3D projections you'd likely need at least 4 such "control points". You might take a look at the geospatial community on stackexchange for more detailed info on projecting maps and swapping between coordinate systems.

Body:
Hi all,

I'm trying to transform locations based upon longitude and latitude to a vector3 location, which will be placed on a sphere in Unity. However, the location seems to be constantly off (compared to the actual location).

I use the following code at the moment:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testPosLatLong : MonoBehaviour {

    public float longi;
    public float lati;
    public float radius;
    public Transform marker;

    // Use this for initialization
    void Start () {

        // Transfer to Radians from Degrees
        float templongi = longi * Mathf.PI / 180;
        float templati = lati * Mathf.PI / 180;

        float Xpos = radius * Mathf.Cos(templati) * Mathf.Cos(templongi);
        float Ypos = radius * Mathf.Cos(templati) * Mathf.Sin(templongi);
        float Zpos = radius * Mathf.Sin(templati);

        Debug.Log ("X, Y, Z" + Xpos + " " + Ypos + " " + Zpos);

        // Set the X,Y,Z pos from the long and lat
        Instantiate(marker);
        marker.position = new Vector3 (Xpos, Zpos, Ypos);

    }

    // Update is called once per frame
    void Update () {

    }
}

I've tried to set the longitude and latitude to zero, which looks like it got the right position on the sphere:

But when I try the longitude and latitude of Amsterdam for example it gives the following results (left side), while it should be the result on the right side.

Am I missing something or what is going wrong here? I tried googling a lot, but couldn't find anything that might explain my current problem. The project itself can be found here: https://wetransfer.com/downloads/31303bd353fd9fde874e92338e68573120171205170107/1208ac%3E

Hope somebody can help.


问:
If statement is not branching correctly
I am a novice at python.  The program was to pass any list into a function and print a string with the words separated with a ',' with the last word separated by a ', and'.  But the If statement does not branch for the last word.  I don't know if putting it in a for loop is breaking it.

```<code>spam = ['apples', 'oranges', 'pears', 'strawberries', 'watermelon']

def comma(x):
    newSpam = x[0]
    del x[0]
    for i in range(len(x)):
        print(type(len(x)))
        if i == (len(x)):
            newSpam = newSpam + ', and ' + [x(i)]
        else:
            newSpam = newSpam + ', '+ x[i]
    print(newSpam)
comma(spam)
</code>```

The output I get is:

```<code>apples, oranges, pears, strawberries, watermelon
</code>```

答:
Replace if i == (len(x)): with

```<code>if i == (len(x) - 1):
</code>```

Title: If statement is not branching correctly

Body:
I am a novice at python.  The program was to pass any list into a function and print a string with the words separated with a ',' with the last word separated by a ', and'.  But the If statement does not branch for the last word.  I don't know if putting it in a for loop is breaking it.

spam = ['apples', 'oranges', 'pears', 'strawberries', 'watermelon']

def comma(x):
    newSpam = x[0]
    del x[0]
    for i in range(len(x)):
        print(type(len(x)))
        if i == (len(x)):
            newSpam = newSpam + ', and ' + [x(i)]
        else:
            newSpam = newSpam + ', '+ x[i]
    print(newSpam)
comma(spam)

The output I get is:

apples, oranges, pears, strawberries, watermelon

Body:
Replace if i == (len(x)): with

if i == (len(x) - 1):


问:
Python subprocess.Popen stdout adds spaces and corrupts term
Okay, so. I'm writing a very basic Python script that just takes the output from a df -h and shows it on the screen. I'm going to implement some other functionality but this part is leaving me stumped.

I'm pretty sure my code is correct, I've even put in time.sleep() statements to see if maybe the output was going too quickly. But when I iterate through stdout, even with rstrip(), the terminal output is spaced oddly every time a new line is created and it corrupts the terminal.

Anythoughts?

Here's my code:

```<code>    #!/usr/bin/python

import sys
import os
import re
import subprocess
import time

np1=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
np2=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p1=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p2=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p3=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p4=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)

for x in np1.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid NP01 is at ", str(y)
time.sleep(1)
for x in np2.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid NP02 is at ", str(y)
time.sleep(1)
for x in p1.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid P01 is at ", str(y)
time.sleep(1)
for x in p2.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid P02 is at ", str(y)
time.sleep(1)
for x in p3.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid P03 is at ", str(y)
time.sleep(1)
for x in p4.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid P04 is at ", str(y)
</code>```

Here's the output

```<code>SAS Grid NP01 is at  33%
                    SAS Grid NP02 is at  36%
                                            SAS Grid P01 is at   3%
                                                                   SAS Grid P02 is at  23%
                                                                                SAS Grid P03 is at  41%
                                                                                SAS Grid P04 is at  24%
                                                                                [<service account>@werindgatep01 ~]$
                                                                                [<service account>@werindgatep01 ~]$
</code>```

What I have to do at this point is CTRL-C and CTRL-D until it drops me out of the service account and back to my standard user account. From there I can sudo su - service account

I'm lost . . .quite lost

答:
I had a similar issue, but fixed it by adding the "-T" option to the SSH command.

-T      Disable pseudo-tty allocation.

Body:
Okay, so. I'm writing a very basic Python script that just takes the output from a df -h and shows it on the screen. I'm going to implement some other functionality but this part is leaving me stumped.

I'm pretty sure my code is correct, I've even put in time.sleep() statements to see if maybe the output was going too quickly. But when I iterate through stdout, even with rstrip(), the terminal output is spaced oddly every time a new line is created and it corrupts the terminal.

Anythoughts?

Here's my code:

    #!/usr/bin/python

import sys
import os
import re
import subprocess
import time

np1=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
np2=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p1=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p2=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p3=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p4=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)

for x in np1.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid NP01 is at ", str(y)
time.sleep(1)
for x in np2.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid NP02 is at ", str(y)
time.sleep(1)
for x in p1.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid P01 is at ", str(y)
time.sleep(1)
for x in p2.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid P02 is at ", str(y)
time.sleep(1)
for x in p3.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid P03 is at ", str(y)
time.sleep(1)
for x in p4.stdout:
    x=x.rstrip()
    x=re.findall('.\d%', x)
    for y in x:
        print "SAS Grid P04 is at ", str(y)

Here's the output

SAS Grid NP01 is at  33%
                    SAS Grid NP02 is at  36%
                                            SAS Grid P01 is at   3%
                                                                   SAS Grid P02 is at  23%
                                                                                SAS Grid P03 is at  41%
                                                                                SAS Grid P04 is at  24%
                                                                                [<service account>@werindgatep01 ~]$
                                                                                [<service account>@werindgatep01 ~]$

What I have to do at this point is CTRL-C and CTRL-D until it drops me out of the service account and back to my standard user account. From there I can sudo su - service account

I'm lost . . .quite lost


问:
Odata V4 how to allow special character like lastname with single quote O'Keefe
We have OData post to insert client first name and last name, but we have a problem with the last name with the single quotes like O'Keefe or O'Brien. whenever we have the last name with the single quote it will reject the data.just I want to ask how to enable OData to accept special character in web.config or Web API configuration?</p>

答:
We found this GitHub issue  and it includes workaround
[Breaking Change] Exception deserializing open type properties that contain special characters like open bracket '[' or single quote ' in ODataV4-7.x </p>


问:
Software Foundations: proving leb_complete and leb_correct
I've been working through Volume 1 of Benjamin Pierce, et al., Software Foundations, and I've gotten stuck on a couple of problems in the chapter IndProp. Unfortunately, I don't know of a better place to ask: anyone have any hints?

```<code> Theorem leb_complete : forall n m,
   leb n m = true -> n <= m.
 Proof.
 (* FILL IN HERE *) Admitted.

Theorem leb_correct : forall n m,
   n <= m ->
   leb n m = true.
Proof.
(* FILL IN HERE *) Admitted.
</code>```

These are exercises in an online textbook; please don't present the solution. But it'd be helpful to have a place to start.

答:
ejgallego has it! generalize dependent is your friend.

So this is a case where you need to prove a property for all natural numbers [and it doesn't follow from your combinatorial theory as you are just defining the objects], thus indeed induction is usually the right tool. However note that you have two numbers, and you will want your induction hypothesis, let's say for n to be general enough to cover all m! This is an important step, which the induction tactic of Coq actually doesn't cover well. – ejgallego Dec 2 at 1:32

Body:
I've been working through Volume 1 of Benjamin Pierce, et al., Software Foundations, and I've gotten stuck on a couple of problems in the chapter IndProp. Unfortunately, I don't know of a better place to ask: anyone have any hints?

 Theorem leb_complete : forall n m,
   leb n m = true -> n <= m.
 Proof.
 (* FILL IN HERE *) Admitted.

Theorem leb_correct : forall n m,
   n <= m ->
   leb n m = true.
Proof.
(* FILL IN HERE *) Admitted.

These are exercises in an online textbook; please don't present the solution. But it'd be helpful to have a place to start.


问:
How to remove the underline for anchors(links)?
Is there anyway (in CSS) to avoid the underline for the text and links introduced in the page .. ?</p>

答:
When you want to use an anchor tag simply as a link without the added styling (such as the underline on hover or blue color) add  class="no-style" to the anchor tag. Then in your global stylesheet create the class "no-style".

.no-style {
    text-decoration: none !important;
}
```

This has two advantages.

It will not affect all anchor tags, just the ones with the "no-style" class added to them.

It will override any other styling that may otherwise prevent setting text-decoration to none.

Body:
When you want to use an anchor tag simply as a link without the added styling (such as the underline on hover or blue color) add  class="no-style" to the anchor tag. Then in your global stylesheet create the class "no-style".

.no-style {
    text-decoration: none !important;
}

This has two advantages.

It will not affect all anchor tags, just the ones with the "no-style" class added to them.

It will override any other styling that may otherwise prevent setting text-decoration to none.

ContentLicense: CC BY-SA 4.0


问:
How can i use SaveChanges() in order to save existing data?
I just need to change some data already saved in my db in SQL.

Here is my code:

```<code>var userData = new MinciUsers
{
    ApplicationUserId = stringUserId,
    Name = userUnity.firstName,
    LastName = userUnity.lastName,
    DateOfBirth = date,
    Address = userUnity.address,
    CityId = null,
    CountryId = null,
    LanguageId = null,
};

dbContext.MinciUsers.Add(userData);
dbContext.SaveData();
</code>```

When I change some of the data it throws an error because the primary key already exists.

How can I use SaveChanges() when I change data?

答:
One approach is to first fetch the corresponding object from the database and then change it's state by specifying the new values. Last you call SaveChanges on the corresponding context class to persist the changes.

For instance, if the primary key is denoted in your application as Id:

```<code>var minciUser = dbContext.MinciUsers.SingleOrDefault(m => m.Id == 1);
if(minciUser != null)
{
    // The record with Id 1 has been found. So you can change it's state.

    minciUser.ApplicationUserId = stringUserId;

    // here you will add the rest of the fields you want to update.

    dbContext.SaveChagnes();
}
</code>```

Update

Another approach ii would be to make use of AddOrUpdate:

```<code>var userData = new MinciUsers
{
    ApplicationUserId = stringUserId,
    Name = userUnity.firstName,
    LastName = userUnity.lastName,
    DateOfBirth = date,
    Address = userUnity.address,
    CityId = null,
    CountryId = null,
    LanguageId = null,
};

dbContext.MinciUsers.AddOrUpdate(userData);
dbContext.SaveChanges();
</code>```

Body:
I just need to change some data already saved in my db in SQL.

Here is my code:

var userData = new MinciUsers
{
    ApplicationUserId = stringUserId,
    Name = userUnity.firstName,
    LastName = userUnity.lastName,
    DateOfBirth = date,
    Address = userUnity.address,
    CityId = null,
    CountryId = null,
    LanguageId = null,
};

dbContext.MinciUsers.Add(userData);
dbContext.SaveData();

When I change some of the data it throws an error because the primary key already exists.

How can I use SaveChanges() when I change data?

Body:
One approach is to first fetch the corresponding object from the database and then change it's state by specifying the new values. Last you call SaveChanges on the corresponding context class to persist the changes.

For instance, if the primary key is denoted in your application as Id:

var minciUser = dbContext.MinciUsers.SingleOrDefault(m => m.Id == 1);
if(minciUser != null)
{
    // The record with Id 1 has been found. So you can change it's state.

    minciUser.ApplicationUserId = stringUserId;

    // here you will add the rest of the fields you want to update.

    dbContext.SaveChagnes();
}

Update

Another approach ii would be to make use of AddOrUpdate:

var userData = new MinciUsers
{
    ApplicationUserId = stringUserId,
    Name = userUnity.firstName,
    LastName = userUnity.lastName,
    DateOfBirth = date,
    Address = userUnity.address,
    CityId = null,
    CountryId = null,
    LanguageId = null,
};

dbContext.MinciUsers.AddOrUpdate(userData);
dbContext.SaveChanges();


问:
Merging dataframes iteratively with pandas

I'm trying to merge two dataframes in pandas, using read_csv. But one of my dataframes (in this example d1) is too big for my computer to handle, so I'm using the iterator argument in read_csv.

Let's say I have two dataframes

```<code>d1 = pd.DataFrame({
    "col1":[1,2,3,4,5,6,7,8,9],
    "col2": [5,4,3,2,5,43,2,5,6],
    "col3": [10,10,10,10,10,4,10,10,10]},
    index=["paul", "peter", "lauren", "dave", "bill", "steve", "old-man", "bob", "tim"])

d2 = pd.DataFrame({
    "yes/no": [1,0,1,0,1,1,1,0,0]},
    index=["paul", "peter", "lauren", "dave", "bill", "steve", "old-man", "bob", "tim"])
</code>```

I need to merge them so that each row captures all data for each person, so the equivalent of doing:

```<code>pd.concat((d1,d2), axis=1,join="outer")
</code>```

but since I can't fit d1 into memory, I've been using read_csv (I'm using  read_csv because I already processed a huge file and saved it into .csv format, so imagine my dataframe d1 is contained in the file test.csv).

```<code>itera = pd.read_csv("test.csv",index_col="index",iterator=True,chunksize=2)
</code>```

But when I do

```<code>for i in itera:
    d2 = pd.concat((d2,i), axis=1,join="outer")
</code>```

my output is the first dataframe appended by the second dataframe.

My output looks like this:

```<code>        col1  col2  col3   yes/no
one     NaN   NaN   NaN     1.0
two     NaN   NaN   NaN     0.0
three   NaN   NaN   NaN     1.0
four    NaN   NaN   NaN     0.0
five    NaN   NaN   NaN     1.0
six     NaN   NaN   NaN     1.0
seven   NaN   NaN   NaN     1.0
eight   NaN   NaN   NaN     0.0
nine    NaN   NaN   NaN     0.0
one     1.0   5.0  10.0     NaN
two     2.0   4.0  10.0     NaN
three   3.0   3.0  10.0     NaN
four    4.0   2.0  10.0     NaN
five    5.0   5.0  10.0     NaN
six     6.0  43.0   4.0     NaN
seven   7.0   2.0  10.0     NaN
eight   8.0   5.0  10.0     NaN
nine    9.0   6.0  10.0     NaN
</code>```

Hope my question makes sense :)

答:
I think you are looking for combine first method.  It basically updates df1 with values from the each chunk in the read_csv iterator.

```<code>import pandas as pd
from StringIO import StringIO

d1 = pd.DataFrame({
    "col1":[1,2,3,4,5,6,7,8,9],
    "col2": [5,4,3,2,5,43,2,5,6],
    "col3": [10,10,10,10,10,4,10,10,10]},
    index=["paul", "peter", "lauren", "dave", "bill", "steve", "old-man", "bob", "tim"])

#d2 converted to string tho use with pd.read_csv
d2 =  StringIO("""y/n col5
paul 1
peter 0
lauren 1
dave 0
bill 1
steve 1
old-man 1
bob 0
tim 0
""")

#For each chunk update d1 with data
for chunk in pd.read_csv(d2, sep = ' ',iterator=True,chunksize=1):
    d1 = d1.combine_first(chunk[['y/n']])
#Number formatting
d1['y/n'] = d1['y/n'].astype(int)
</code>```

Which returns d1 looking like:

```<code>         col1  col2  col3  y/n
bill        5     5    10    1
bob         8     5    10    0
dave        4     2    10    0
lauren      3     3    10    1
old-man     7     2    10    1
paul        1     5    10    1
peter       2     4    10    0
steve       6    43     4    1
tim         9     6    10    0
</code>```

Body:
I'm trying to merge two dataframes in pandas, using read_csv. But one of my dataframes (in this example d1) is too big for my computer to handle, so I'm using the iterator argument in read_csv.

Let's say I have two dataframes

d1 = pd.DataFrame({
    "col1":[1,2,3,4,5,6,7,8,9],
    "col2": [5,4,3,2,5,43,2,5,6],
    "col3": [10,10,10,10,10,4,10,10,10]},
    index=["paul", "peter", "lauren", "dave", "bill", "steve", "old-man", "bob", "tim"])

d2 = pd.DataFrame({
    "yes/no": [1,0,1,0,1,1,1,0,0]},
    index=["paul", "peter", "lauren", "dave", "bill", "steve", "old-man", "bob", "tim"])

I need to merge them so that each row captures all data for each person, so the equivalent of doing:

pd.concat((d1,d2), axis=1,join="outer")

but since I can't fit d1 into memory, I've been using read_csv (I'm using  read_csv because I already processed a huge file and saved it into .csv format, so imagine my dataframe d1 is contained in the file test.csv).

itera = pd.read_csv("test.csv",index_col="index",iterator=True,chunksize=2)

But when I do

for i in itera:
    d2 = pd.concat((d2,i), axis=1,join="outer")

my output is the first dataframe appended by the second dataframe.

My output looks like this:

        col1  col2  col3   yes/no
one     NaN   NaN   NaN     1.0
two     NaN   NaN   NaN     0.0
three   NaN   NaN   NaN     1.0
four    NaN   NaN   NaN     0.0
five    NaN   NaN   NaN     1.0
six     NaN   NaN   NaN     1.0
seven   NaN   NaN   NaN     1.0
eight   NaN   NaN   NaN     0.0
nine    NaN   NaN   NaN     0.0
one     1.0   5.0  10.0     NaN
two     2.0   4.0  10.0     NaN
three   3.0   3.0  10.0     NaN
four    4.0   2.0  10.0     NaN
five    5.0   5.0  10.0     NaN
six     6.0  43.0   4.0     NaN
seven   7.0   2.0  10.0     NaN
eight   8.0   5.0  10.0     NaN
nine    9.0   6.0  10.0     NaN

Hope my question makes sense :)

Body:
I think you are looking for combine first method.  It basically updates df1 with values from the each chunk in the read_csv iterator.

import pandas as pd
from StringIO import StringIO

d1 = pd.DataFrame({
    "col1":[1,2,3,4,5,6,7,8,9],
    "col2": [5,4,3,2,5,43,2,5,6],
    "col3": [10,10,10,10,10,4,10,10,10]},
    index=["paul", "peter", "lauren", "dave", "bill", "steve", "old-man", "bob", "tim"])

#d2 converted to string tho use with pd.read_csv
d2 =  StringIO("""y/n col5
paul 1
peter 0
lauren 1
dave 0
bill 1
steve 1
old-man 1
bob 0
tim 0
""")

#For each chunk update d1 with data
for chunk in pd.read_csv(d2, sep = ' ',iterator=True,chunksize=1):
    d1 = d1.combine_first(chunk[['y/n']])
#Number formatting
d1['y/n'] = d1['y/n'].astype(int)

Which returns d1 looking like:

         col1  col2  col3  y/n
bill        5     5    10    1
bob         8     5    10    0
dave        4     2    10    0
lauren      3     3    10    1
old-man     7     2    10    1
paul        1     5    10    1
peter       2     4    10    0
steve       6    43     4    1
tim         9     6    10    0


问:
Subselect in the ON clause
Please dont bash me if there are already answers for this question, but I found none.

Basically I want to make a subselect in the ON clause of a Left join to get the newest entry in a timeframe.

(start and endtime are timestamps, hardcoded, in local variables or host variables in a Cobol program) to simplify I used integers in that question.

```<code>Select * from table1 as t1
left join table2 as t2 on
t1.primary = t2.secondary
and t2.timestamp = (
    select max(t2a.timestamp) from table2 as t2a
        where t2.primary = t2a.primary
            and t2a.timestamp > starttime
            and t2a.timestamp < endtime
    )
</code>```

Now this does not work, I get the following error:

AN ON CLAUSE IS INVALID. SQLCODE=-338

Because (see the docs)

The ON clause cannot contain a subquery.

Now what we can do to surround that is instead of joining table2 to join a already delimited subquery. But that surrounds the query optimizer what literally kills the performance:

```<code>Select * from table1 as t1
left join (
    select t2a.secondary from table2 as t2a
        where t2a.timestamp = (
            select max(t2b.timestamp)
            from table2 as t2b
            where t2a.primary = t2b.primary
            and t2b.timestamp > starttime
            and t2b.timestamp < endtime
        )
    )as t2
on t1.primary = t2.secondary
</code>```

Any idea how to slove this?

Example data table1:

```<code>t1.primary
         1
         2
         3
</code>```

Example data table2:

```<code>t2.primary t2.secondary t2.timestamp
         1            1           4
         2            1           5
         3            1          10
         4            2           4
         5            2           5
</code>```

Variables:

```<code>starttime = 3
endtime   = 6
</code>```

Expected result:

```<code>t1.primary t2.primary t2.secondary t2.timestamp
         1          2            1           5  --Leftjoined the newest entry in range
         2          5            2           5
         3       NULL         NULL        NULL
</code>```

答:
This should work

```<code>select *
from table1 t1
left join (
   select t2.primary, t2.secondary, t2.timestamp,
          row_number() over (partition by t2.secondary order by t2.timestamp desc) rn
   from table2 t2
   where t2.timestamp between starttime and endtime
) t on t1.primary = t.secondary and t.rn = 1
</code>```

If you have an index table2(timestamp, secondary, primary) or at least table2(timestamp, secondary) then it should run really fast. Without the indexes, it still works with quite good performance, since it leads to one sequential scan of the tables.

Title: Subselect in the ON clause

Body:
Please dont bash me if there are already answers for this question, but I found none.

Basically I want to make a subselect in the ON clause of a Left join to get the newest entry in a timeframe.

(start and endtime are timestamps, hardcoded, in local variables or host variables in a Cobol program) to simplify I used integers in that question.

Select * from table1 as t1
left join table2 as t2 on
t1.primary = t2.secondary
and t2.timestamp = (
    select max(t2a.timestamp) from table2 as t2a
        where t2.primary = t2a.primary
            and t2a.timestamp > starttime
            and t2a.timestamp < endtime
    )

Now this does not work, I get the following error:

AN ON CLAUSE IS INVALID. SQLCODE=-338

Because (see the docs)

The ON clause cannot contain a subquery.

Now what we can do to surround that is instead of joining table2 to join a already delimited subquery. But that surrounds the query optimizer what literally kills the performance:

Select * from table1 as t1
left join (
    select t2a.secondary from table2 as t2a
        where t2a.timestamp = (
            select max(t2b.timestamp)
            from table2 as t2b
            where t2a.primary = t2b.primary
            and t2b.timestamp > starttime
            and t2b.timestamp < endtime
        )
    )as t2
on t1.primary = t2.secondary

Any idea how to slove this?

Example data table1:

t1.primary
         1
         2
         3

Example data table2:

t2.primary t2.secondary t2.timestamp
         1            1           4
         2            1           5
         3            1          10
         4            2           4
         5            2           5

Variables:

starttime = 3
endtime   = 6

Expected result:

t1.primary t2.primary t2.secondary t2.timestamp
         1          2            1           5  --Leftjoined the newest entry in range
         2          5            2           5
         3       NULL         NULL        NULL

Body:
This should work

select *
from table1 t1
left join (
   select t2.primary, t2.secondary, t2.timestamp,
          row_number() over (partition by t2.secondary order by t2.timestamp desc) rn
   from table2 t2
   where t2.timestamp between starttime and endtime
) t on t1.primary = t.secondary and t.rn = 1

If you have an index table2(timestamp, secondary, primary) or at least table2(timestamp, secondary) then it should run really fast. Without the indexes, it still works with quite good performance, since it leads to one sequential scan of the tables.


问:
Find consecutive Nans in pandas dataframe
I would like to find consecutive nans in my dataframe columns, something like

```<code>>>> df = pd.DataFrame([[np.nan, 2, np.nan],
...                    [3, 4, np.nan],
...                    [np.nan, np.nan, np.nan],
...                    [np.nan, 3, np.nan]],
...                    columns=list('ABC'))
>>> df
     A    B   C
0  NaN  2.0 NaN
1  3.0  4.0 NaN
2  NaN  NaN NaN
3  NaN  3.0 NaN
</code>```

would give

```<code>>>> df
     A    B   C
0  1.0  NaN 4.0
1  NaN  NaN 4.0
2  2.0  1.0 4.0
3  2.0  NaN 4.0
</code>```

答:
IIUC...groupby + mask + isnull

```<code>df.apply(lambda x :x.groupby(x.isnull().diff().ne(0).cumsum()).transform(len).mask(~x.isnull()))
Out[751]:
     A    B    C
0  1.0  NaN  4.0
1  NaN  NaN  4.0
2  2.0  1.0  4.0
3  2.0  NaN  4.0
</code>```

For one column

```<code>df.A.groupby(df.A.isnull().diff().ne(0).cumsum()).transform(len).mask(~df.A.isnull())
Out[756]:
0    1.0
1    NaN
2    2.0
3    2.0
Name: A, dtype: float64
</code>```

Body:
I would like to find consecutive nans in my dataframe columns, something like

>>> df = pd.DataFrame([[np.nan, 2, np.nan],
...                    [3, 4, np.nan],
...                    [np.nan, np.nan, np.nan],
...                    [np.nan, 3, np.nan]],
...                    columns=list('ABC'))
>>> df
     A    B   C
0  NaN  2.0 NaN
1  3.0  4.0 NaN
2  NaN  NaN NaN
3  NaN  3.0 NaN

would give

>>> df
     A    B   C
0  1.0  NaN 4.0
1  NaN  NaN 4.0
2  2.0  1.0 4.0
3  2.0  NaN 4.0

Body:
IIUC...groupby + mask + isnull

df.apply(lambda x :x.groupby(x.isnull().diff().ne(0).cumsum()).transform(len).mask(~x.isnull()))
Out[751]:
     A    B    C
0  1.0  NaN  4.0
1  NaN  NaN  4.0
2  2.0  1.0  4.0
3  2.0  NaN  4.0

For one column

df.A.groupby(df.A.isnull().diff().ne(0).cumsum()).transform(len).mask(~df.A.isnull())
Out[756]:
0    1.0
1    NaN
2    2.0
3    2.0
Name: A, dtype: float64


问:
Check if an input string is a positive int/float number
On my webpage there is an input box that should only allow user to enter a positive int/float number (i.e. 1234, 123.4, 12.34, 1.234 should all be allowed).

To make sure the value is valid, I have a function to validate the value before sending it to the server:

```<code>function isPositiveFloat(s){
    return String(Number(s)) === s && Math.floor(Number(s)) > 0;
}
</code>```

The function works great expect for one scenario:  isPositiveFloat(1.0) will return false, as it turns out Number(1.0) will convert it to 1, therefore made the validation failed.

Any suggestions on how should I resolve this issue? Is using regex the only way to go?

Thanks in advance for any help!

答:
You just need to use !isNaN(s)along with Number(s)>0:

```<code>function isPositiveFloat(s) {
  return !isNaN(s) && Number(s) > 0;
}
</code>```

Demo:

function isPositiveFloat(s) {
  return !isNaN(s) && Number(s) > 0;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><input type="text" onchange="console.log(isPositiveFloat(this.value))" /></code>```

You can check on the MDN reference of isNaN() examples that:

isNaN('37.37');   // false: "37.37" is converted to the number 37.37 which is not NaN

So as you can see it will be working in your case with float numbers.

Body:
On my webpage there is an input box that should only allow user to enter a positive int/float number (i.e. 1234, 123.4, 12.34, 1.234 should all be allowed).

To make sure the value is valid, I have a function to validate the value before sending it to the server:

function isPositiveFloat(s){
    return String(Number(s)) === s && Math.floor(Number(s)) > 0;
}

The function works great expect for one scenario:  isPositiveFloat(1.0) will return false, as it turns out Number(1.0) will convert it to 1, therefore made the validation failed.

Any suggestions on how should I resolve this issue? Is using regex the only way to go?

Thanks in advance for any help!

Body:
You just need to use !isNaN(s)along with Number(s)>0:

function isPositiveFloat(s) {
  return !isNaN(s) && Number(s) > 0;
}

Demo:

function isPositiveFloat(s) {
  return !isNaN(s) && Number(s) > 0;
}

<input type="text" onchange="console.log(isPositiveFloat(this.value))" />

You can check on the MDN reference of isNaN() examples that:

isNaN('37.37');   // false: "37.37" is converted to the number 37.37 which is not NaN

So as you can see it will be working in your case with float numbers.


问:
I don't know why i can't use "datagen.flow_from_directory()"
full image

I don't know how to fix it.

答: If you are dealing with a problem which has multiple classes, try changing 'binary' to 'categorical' and see if it works. I think there are more than two directories in your train and test folders. When using flow_from_directory() each directory inside the parent directory is considered as a different class. Can you post the directory structure? It will become clearer.

Body:
full image

I don't know how to fix it.


问:
What is the difference between common and server models in loopback?
When I'm creating a model in strongloop with the following command:

```<code>slc loopback:model
</code>```

loopback asks me for choose between common model or server.

Common model or server only?

I really don't know what criteria to take into account to choose between one option or the other. I'll be thankful if any of you can help me to understand or give me any insights to take a wise decision.

答:
Loopback can be run on both the client and the server using isomorphic LoopBack.  The same app running in NodeJs can also run in the client browser so instead of coding HTTP requests and responses on the client to CRUD your LoopBack model instances, you can simply call the appropriate loopback method.

In other words, instead of calling a POST to an API end-point to create a new object (POST /api/MyObject) in your database, you can simply call MyObject.create(data) on the client and the LoopBack app in the client will make the call for you.  And instead of returning an HTTP response, it will return the created object.

Now, this brings us to the difference between /common and /server models:  if you want the model used in the browser to be the same as the model used on the server, create your model in /common.  If you need the model to be different, create the model in the /server/models folder (not sure where to create the model for the client)

Body:
When I'm creating a model in strongloop with the following command:

slc loopback:model

loopback asks me for choose between common model or server.

Common model or server only?

I really don't know what criteria to take into account to choose between one option or the other. I'll be thankful if any of you can help me to understand or give me any insights to take a wise decision.


问:
PHP: DATETIME in array as object. How to echo
Hope that title isn't too cryptic. I have an array with a DATETIME object in it and I'm just trying to figure out how to echo this to a page.

```<code> ["created"]=> object(DateTime)#3 (3) { ["date"]=> string(19) "2010-10-22 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London"
</code>```

Can someone help me out?

tried date() but get:

```<code>Warning: date() expects parameter 2 to be long, object given in C:\
</code>```

any help most appreciated,

Jonesy

答:
I add this answer even if I am not sure it answers specifically the question (and best answer is already there though),
but I couldn't find much other places where the above format (date/timezone_type/timezone) is mentioned.

If you have the date translated from object to array

via var_export

DateTime::__set_state(array(
   'date' => '2017-12-05 11:58:25.428595',
   'timezone_type' => 3,
   'timezone' => 'US/Pacific',
))

or json_encode

{"date":"2017-12-05 11:57:07.938671","timezone_type":3,"timezone":"US\/Pacific"}

you can use again the DateTime::__set_state magic method mentioned above to convert it again to a DateTime object.

Not sure how here __set_state could bidirectional, but it does the magic.
I couldn't find documentation.

But you can test it here:
http://sandbox.onlinephpfunctions.com/code/0a18e6937e7d4373beb91713f2e6e5f75f9af3e2

Body:
Hope that title isn't too cryptic. I have an array with a DATETIME object in it and I'm just trying to figure out how to echo this to a page.

 ["created"]=> object(DateTime)#3 (3) { ["date"]=> string(19) "2010-10-22 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London"

Can someone help me out?

tried date() but get:

Warning: date() expects parameter 2 to be long, object given in C:\

any help most appreciated,

Jonesy


问:
Define operation for odata routing
The goal is to route the service call to internal OData endpoints, but finding a bit tough to get it work, e.g. one operation is to route /odata/customer, if I set the URL template to /odata/customer*, UI complains error 'One or more fields contain incorrect values:' 'URL template is invalid', this could work if URL template is set to /odata/customer/*, however that would require the client call to include the extra / in the request. How would this work out? Thanks!!

答: We'll look into that, but I think for the time being you'll have to define two operations for that: "/odata/Customer", and "/odata/Customer/*".</p>

Title: Define operation for odata routing