コード例 #1
0
ファイル: polyfit.hpp プロジェクト: rappoccio/PHY410
void polyfit( polyfit_vector x, polyfit_vector y, polyfit_vector sigma, int M,
              polyfit_vector& a_fit, polyfit_vector& sig_a, polyfit_vector& yy, double& chisqr) {
// Function to fit a polynomial to data
// Inputs
//   x       Independent variable
//   y       Dependent variable
//   sigma   Estimate error in y
//   M       Number of parameters used to fit data
// Outputs
//   a_fit   Fit parameters; a(0) is intercept, a(1) is slope
//   sig_a   Estimated error in the parameters a()
//   yy      Curve fit to the data
//   chisqr  Chi squared statistic

    //* Form the vector b and design matrix A
    int i, j, k, N = x.dim1();
    polyfit_vector b(N);
    polyfit_matrix A(N,M);
    for( i=0; i<N; i++ ) {
        b(i) = y(i)/sigma(i);
        for( j=0; j<M; j++ )
            if ( sigma(i) > 0.0 )
                A(i,j) = pow(x(i),(double)(j))/sigma(i);
            else
                A(i,j) = 0.0;
    }

    //* Compute the correlation matrix C
    polyfit_matrix C(M,M), Cinv(M,M);
    for( i=0; i<M; i++ ) {   // (C inverse) = (A transpose) * A
        for( j=0; j<M; j++ ) {
            Cinv(i,j) = 0.0;
            for( k=0; k<N; k++ )
                Cinv(i,j) += A(k,i)*A(k,j);
        }
    }

    inverse( Cinv, C );  // C = ( (C inverse) inverse)

    //* Compute the least squares polynomial coefficients a_fit
    for( k=0; k<M; k++ ) {
        a_fit(k) = 0.0;
        for( j=0; j<M; j++ )
            for( i=0; i<N; i++ ) {
                a_fit(k) += C(k,j) * A(i,j) * b(i);
            }
    }
    //* Compute the estimated error bars for the coefficients
    for( j=0; j<M; j++ )
        sig_a(j) = sqrt(C(j,j));
    //* Evaluate curve fit at each data point and compute Chi^2
    chisqr = 0.0;
    for( i=0; i<N; i++ ) {
        yy(i) = 0.0;      // yy is the curve fit
        for( j=0; j<M; j++ )
            yy(i) += a_fit(j) * pow( x(i), (double)(j) );
        double delta = (y(i)-yy(i))/sigma(i);
        chisqr += delta*delta;  // Chi square
    }
}
コード例 #2
0
ファイル: FSDEMatViscoT.cpp プロジェクト: samanseifi/Tahoe
  // material electric tangent modulus
  const dMatrixT& FSDEMatViscoT::B_IJ()
  {
    const dMatrixT& C = RightCauchyGreenDeformation();
	double J = C.Det();
	J = sqrt(J);

	dMatrixT Cinv(3);
	Cinv.Inverse(C);
	fTangentElectrical = Cinv;
	fTangentElectrical *= fElectricPermittivity;
	fTangentElectrical *= J;
    return fTangentElectrical;
  }
コード例 #3
0
int NeoHookeanCompressible3D::ComputeTrials()

{   

   tensor tensorI2("I", 2, def_dim_2);

   tensor tsr1;

   tensor tsr2;



   // Cinv:

   Cinv = C.inverse();

   Cinv.symmetrize11();



   // J:

   J = sqrt(C.determinant());



   // lame constants:

   double lambda = K - 2.0*G/3.0;

   double mu = G - lambda*log(J);



   // Pk2Stress:

   thisPK2Stress = (tensorI2-Cinv)*G + Cinv*lambda*log(J);

   

   // Green Strain:

   thisGreenStrain = (C - tensorI2) * 0.5; 

   

   // Langrangian Tangent Stiffness:

   tsr1 = Cinv("ij")*Cinv("kl");

     tsr1.null_indices();

   tsr2 = tsr1.transpose0110() + tsr1.transpose0111();

   Stiffness = tsr1*lambda + tsr2*mu;



   return 0;

}
コード例 #4
0
ファイル: wald_method.cpp プロジェクト: mfranberg/besiq
double
wald_method::run(const snp_row &row1, const snp_row &row2, float *output)
{
    arma::mat n0 = arma::zeros<arma::mat>( 3, 3 );
    arma::mat n1 = arma::zeros<arma::mat>( 3, 3 );
    for(int i = 0; i < row1.size( ); i++)
    {
        unsigned char snp1 = row1[ i ];
        unsigned char snp2 = row2[ i ];
        if( snp1 == 3 || snp2 == 3 || m_missing[ i ] == 1 )
        {
            continue;
        }

        unsigned int pheno = m_pheno[ i ];
        if( pheno == 0 )
        {
            n0( snp1, snp2 ) += 1;
        }
        else if( pheno == 1 )
        {
            n1( snp1, snp2 ) += 1;
        }
    }

    arma::mat eta( 3, 3 );
    double num_samples = 0.0;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if( n0( i, j ) < METHOD_SMALLEST_CELL_SIZE_BINOMIAL || n1( i, j ) < METHOD_SMALLEST_CELL_SIZE_BINOMIAL )
            {
                continue;
            }

            eta( i, j ) = log( n1( i, j ) / n0( i, j ) );
            num_samples += n1( i, j ) + n0( i, j );
        }
    }

    /* Find valid parameters and estimate beta */
    int num_valid = 0;
    arma::uvec valid( 4 );
    m_beta = arma::zeros<arma::vec>( 4 );
    int i_map[] = { 1, 1, 2, 2 };
    int j_map[] = { 1, 2, 1, 2 };
    for(int i = 0; i < 4; i++)
    {
        int c_i = i_map[ i ];
        int c_j = j_map[ i ];
        if( n0( 0, 0 ) >= METHOD_SMALLEST_CELL_SIZE_NORMAL &&
            n0( 0, c_j ) >= METHOD_SMALLEST_CELL_SIZE_NORMAL &&
            n0( c_i, 0 ) >= METHOD_SMALLEST_CELL_SIZE_NORMAL &&
            n0( c_i, c_j) >= METHOD_SMALLEST_CELL_SIZE_NORMAL &&
            n1( 0, 0 ) >= METHOD_SMALLEST_CELL_SIZE_NORMAL &&
            n1( 0, c_j ) >= METHOD_SMALLEST_CELL_SIZE_NORMAL &&
            n1( c_i, 0 ) >= METHOD_SMALLEST_CELL_SIZE_NORMAL &&
            n1( c_i, c_j) >= METHOD_SMALLEST_CELL_SIZE_NORMAL )
        {
            valid[ num_valid ] = i;
            m_beta[ num_valid ] = eta( 0, 0 ) - eta( 0, c_j ) - eta( c_i, 0 ) + eta( c_i, c_j );
            num_valid++;
        }
    }
    set_num_ok_samples( (size_t)num_samples );
    if( num_valid <= 0 )
    {
        return -9;
    }
    
    valid.resize( num_valid );
    m_beta.resize( num_valid );

    /* Construct covariance matrix */
    m_C = arma::zeros<arma::mat>( num_valid, num_valid );
    for(int iv = 0; iv < num_valid; iv++)
    {
        int i = valid[ iv ];
        int c_i = i_map[ i ];
        int c_j = j_map[ i ];

        for(int jv = 0; jv < num_valid; jv++)
        {
            int j = valid[ jv ];
            int o_i = i_map[ j ];
            int o_j = j_map[ j ];

            int same_row = c_i == o_i;
            int same_col = c_j == o_j;
            int in_cell = i == j;

            m_C( iv, jv ) = 1.0 / n0( 0, 0 ) + same_col / n0( 0, c_j ) + same_row / n0( c_i, 0 ) + in_cell / n0( c_i, c_j );
            m_C( iv, jv ) += 1.0 / n1( 0, 0 ) + same_col / n1( 0, c_j ) + same_row / n1( c_i, 0 ) + in_cell / n1( c_i, c_j );
        }
    }

    arma::mat Cinv( num_valid, num_valid );
    if( !inv( Cinv, m_C ) )
    {
        return -9;
    }
    
    /* Test if b != 0 with Wald test */
    double chi = dot( m_beta, Cinv * m_beta );
    output[ 0 ] = chi;
    output[ 1 ] = 1.0 - chi_square_cdf( chi, num_valid );
    output[ 2 ] = valid.n_elem;

    return output[ 1 ];
}