예제 #1
0
MatlabEngine::DoubleMatrix
MatlabEngine::GetMatrix( const string& inExp )
{
    DoubleMatrix result;
    mxArray* ans = GetMxArray( inExp );
    if( ans )
    {
        int numDims = mxGetNumberOfDimensions( ans );
        const int* dims = mxGetDimensions( ans );
        if( numDims != 2 )
            bcierr << "Can only handle two dimensions" << endl;
        result.resize( dims[ 0 ], vector<double>( dims[ 1 ] ) );
        double* value = mxGetPr( ans );
        if( value )
        {
            int indices[] = { 0, 0 };
            for( size_t i = 0; i < result.size(); ++i )
            {
                indices[ 0 ] = i;
                for( size_t j = 0; j < result[ i ].size(); ++j )
                {
                    indices[ 1 ] = j;
                    result[ i ][ j ] = value[ mxCalcSingleSubscript( ans, 2, indices ) ];
                }
            }
        }
        mxDestroyArray( ans );
    }
    return result;
}
예제 #2
0
파일: stoprule.cpp 프로젝트: Cibiv/IQ-TREE
void StopRule::multiple (DoubleVector &vec1, DoubleVector &vec2, DoubleMatrix &proMat) {
	int row_, col_;
	proMat.resize(vec1.size());
	for (row_ = 0; row_ < vec1.size(); row_++)
		proMat[row_].resize(vec2.size());

	for (row_ = 0; row_ < vec1.size(); row_ ++)
		for (col_ = 0; col_ < vec2.size(); col_ ++)
			proMat[row_][col_] = vec1[row_] * vec2[col_];
}
예제 #3
0
double
MatlabEngine::GetScalar( const string& inExp )
{
    DoubleMatrix result = GetMatrix( inExp );
    if( result.empty() || result[ 0 ].empty() )
    {
        bcierr << "Could not get scalar value \"" << inExp << "\"" << endl;
        result.resize( 1, vector<double>( 1 ) );
    }
    return result[ 0 ][ 0 ];
}
예제 #4
0
파일: stoprule.cpp 프로젝트: Cibiv/IQ-TREE
void StopRule::readMat (char *fileName, DoubleMatrix &oriMat, int &size) {
	std::ifstream inFile_;
	inFile_.open(fileName);
	inFile_ >> size;
	oriMat.resize(size);
	for (int i = 0; i < size; i++) oriMat[i].resize(size);
	for (int row_ = 0; row_ < size; row_ ++)
		for (int col_ = 0; col_ < size; col_ ++)
			inFile_ >> oriMat[row_][col_];
	inFile_.close ();

}
예제 #5
0
void multiply(const DoubleMatrix &X, const DoubleMatrix &Y, DoubleMatrix& Z)
{
  assert(&X!=&Z);
  assert(&Y!=&Z);
  if( X.isEmpty() || Y.isEmpty() )
    return;

  //
  // Row Major Matrices
  //
  //    C = alpha * A * B + beta * C  --- (1)
  //
  // A : m by k         lda (stride) = k
  // B : k by n         ldb (stride) = n
  // C : m by n         ldc (stride) = n
  //
  // Column Major Matrices
  //
  //    Z = alpha * X * Y + beta * C  --- (2)
  //    Z = C^t 
  //      = alpha * B^t * A^t + beta * C^t  --- (3)
  //
  // X = B^t : n by k   ldx (stride) = n
  // Y = A^t : k by m   ldy (stride) = k
  // Z = C^t : n by m   ldz (stride) = n
  //
  int m = Y.nc();
  int k = X.nc();
  int n = X.nr();

  Z.resize( n, m );
  
  assert( X.nr() == n );
  assert( X.nc() == k );
  assert( Y.nr() == k );
  assert( Y.nc() == m );
  assert( Z.nr() == n );
  assert( Z.nc() == m );
  
  const double * pX = X.data();
  const double * pY = Y.data();
  double       * pZ = Z.data(); 
  
  int lda = n;
  int ldb = k;
  int ldc = n;

  cblas_dgemm( CblasColMajor, CblasNoTrans, CblasNoTrans, n, m, k, ALPHA, pX, lda, pY, ldb , BETA, pZ, ldc );

}
예제 #6
0
파일: stoprule.cpp 프로젝트: Cibiv/IQ-TREE
void StopRule::multiple (DoubleMatrix &mat1, DoubleMatrix &mat2, DoubleMatrix &proMat) {
	int row_, col_;
	//proMat.setLimit (mat1.getNRow (), mat2.getNCol ());
	proMat.resize(mat1.size());
	int nrow_ = proMat.size();
	int ncol_ = mat2[0].size();
	for (row_ = 0; row_ < proMat.size(); row_++)   proMat[row_].resize(ncol_);
	for (row_ = 0; row_ < nrow_; row_ ++)
		for (col_ = 0; col_ < ncol_; col_ ++) {
			proMat[row_][col_] = 0.0;
			for (int count_ = 0; count_ < mat1[0].size(); count_ ++) {
				proMat[row_][col_] += mat1[row_][count_] * mat2[count_][col_];
				//         std::cout << mat1[row_][count_] << " --> " << mat2[count_][col_] << endl;
			}
		}
}
예제 #7
0
long _stdcall getChartData(int bond_id, bool cluster, double* arr, unsigned char bonds[], int numbonds, long* maturities, int microWidth, int dateTolerance, int clusterLower, int clusterUpper, int history){
	SimpleRefData bbg(1);
	DoubleMatrix yieldData;
	std::vector <std::string> bond_vec;
	for (int b = 0; b < numbonds; b++){
		std::string prefix = "/isin/";
		std::string suffix;
		suffix.clear();
		for (int j = 0; j < ISIN_LENGTH; j++){
			char a = bonds[b + j*(numbonds + 1)];
			suffix += a;
		}
		bond_vec.push_back((prefix + suffix).c_str());
	}

	double** yieldArray;
	yieldArray = new double*[numbonds+1];
	for (int i = 0; i <= numbonds; i++){
		yieldArray[i] = new double[history];
	}

	int days_data = bbg.runHistData(yieldArray, bond_vec, history);

	yieldData.resize(numbonds);
	for (int i = 0; i <= numbonds; i++){
		yieldData[i].resize(days_data);
	}
	for (int i = 0; i <= numbonds; i++){
		for (int j = 0; j < days_data; j++){
			yieldData.at(i).at(j) = yieldArray[i][j];
		}
	}
	
	std::vector<double> dataLocal;
	if (cluster){
		chartData(dataLocal, cluster, bond_id, yieldData, maturities, 0, clusterLower, clusterUpper, days_data);
	}
	else{
		chartData(dataLocal, cluster, bond_id, yieldData, maturities, dateTolerance, microWidth, microWidth, days_data);
	}
	
	for (int i = 0; i < days_data; i++){
		arr[i] = dataLocal.at(i);
	}
	return days_data;
}
예제 #8
0
DoubleMatrix ParseMatrixFromText(const string& textMatrix)
{
	DoubleMatrix mat;

    //Parse the matrix
    vector<string> rows = splitString(textMatrix, "\n");
    for(int row = 0; row < rows.size(); row++)
    {
        vector<string> values = splitString(rows[row], " \t");
        for(int col = 0; col < values.size(); col++)
        {
        	if(!mat.size())
            {
                mat.resize(rows.size(), values.size());
            }

            mat(row, col) = toDouble(values[col]);
        }
    }
	return mat;
}
예제 #9
0
파일: stoprule.cpp 프로젝트: Cibiv/IQ-TREE
void StopRule::cmpLamdaMat (int k, DoubleMatrix &lamdaMat) {
	int i, j;
	lamdaMat.resize(k);
	for (i = 0; i < k; i ++)
		lamdaMat[i].resize(k);
	double muy_ = cmpMuy (k);
	for (i = 0; i < k; i ++)
		for (j = 0; j <= i; j ++) {
			/*
			lamdaMat[i][j] = (cmpGamma (2*muy_ + i + 1) * cmpGamma (muy_ + j + 1) ) /
			                 ( cmpGamma (muy_ + i + 1) * cmpGamma (j + 1) );*/

			// to fix divide by zero PROBLEM!
			lamdaMat[i][j] = cmpLnGamma (2*muy_ + i + 1) + cmpLnGamma (muy_ + j + 1) -
			                 cmpLnGamma (muy_ + i + 1) - cmpLnGamma (j + 1);

			//if (i == 98 && j == 97)
			 //     std::cout << i << "," << j << " -> " << lamdaMat[i][j] << endl;
			lamdaMat[i][j] = exp(lamdaMat[i][j]);
			lamdaMat[j][i] = lamdaMat[i][j];
		}
}
예제 #10
0
/*
 * b = [1]
 *     [1]
 */
void lambdaTest::setB(DoubleMatrix &b)
{
    b.resize(2,1);
    b.fill(rand()/(pow(2.0,32.0)) * 100.0);
}
예제 #11
0
extern void spk_non_par(
	size_t                             level       ,
	SpkModel< CppAD::AD<double> >     &admodel     ,
	SpkModel<double>                  &model       ,
	const DoubleMatrix                &N           ,
	const DoubleMatrix                &y           ,
	const DoubleMatrix                &max_itr     ,
	const DoubleMatrix                &epsilon     ,
	const DoubleMatrix                &blow        ,
	const DoubleMatrix                &bup         ,
	const DoubleMatrix                &Bin         ,
	DoubleMatrix                      &Bout        ,
	DoubleMatrix                      &lamout      ,
	DoubleMatrix                      &Pout  )
{
	// temporary indices
	size_t i, j, k;

	// temporary double pointer
	double       *ptr;
	const double *ptr_c;

	// number of discrete measure points
	size_t J = Bin.nc();

	// number of random effects
	size_t n = blow.nr();
	
	// ------------ Arguments to non_par::opt_measure --------------------
	assert( max_itr.nr()  == 2 );
	mat2cpp::matrix<size_t> maxitr(2, 1);
	maxitr(0, 0) = size_t( *(max_itr.data() + 0) );
	maxitr(1, 0) = size_t( *(max_itr.data() + 1) );

	assert( epsilon.nr()  == 5 );
	mat2cpp::matrix<double> eps(5, 1);
	eps(0, 0)    = *(epsilon.data() + 0);
	eps(1, 0)    = *(epsilon.data() + 1);
	eps(2, 0)    = *(epsilon.data() + 2);
	eps(3, 0)    = *(epsilon.data() + 3);
	eps(4, 0)    = *(epsilon.data() + 4);

	// input likelihood function
	Like like(admodel, model, N, y, n);

	// input number of individuals in the population
	size_t M = N.nr();

	// input lower limit for the random effects
	mat2cpp::matrix<double> xLow(1, n);
	ptr_c = blow.data();
	for(k = 0; k < n; k++)
		xLow(0, k) = ptr_c[k];

	// input upper limit for the random effects
	mat2cpp::matrix<double> xUp(1, n);
	ptr_c = bup.data();
	for(k = 0; k < n; k++)
		xUp(0, k) = ptr_c[k];

	// input and return discrete measure points
	mat2cpp::matrix<double> X(J, n);
	ptr_c = Bin.data();
	for(j = 0; j < J; j++)
	{	for(k = 0; k < n; k++)
			X(j, k) = ptr_c[k + j * n];
	}

	// return weight corresponding to each measure oint
	mat2cpp::matrix<double> lambda(J, 1);

	// return convergence information
	mat2cpp::matrix<double> info; 

	// return status message
	const char *msg;

	// -----------------------------------------------------------------
	msg = non_par::opt_measure( level, maxitr, eps, 
		&like, M, xLow, xUp, X, lambda, info
	);
	// -----------------------------------------------------------------

	if( strcmp(msg, "ok") != 0 )
	{	throw SpkException(
      			SpkError::SPK_NON_PAR_ERR,
			msg,
      			__LINE__,
      			__FILE__ 
		);
	}
	// determine number of discrete measure points
	assert( n == X.size2() );
	J = X.size1();

	// dimension the return matrices
	Bout.resize(n, J);
	lamout.resize(J, 1);
	Pout.resize(M, J);

	// retrun elements of Bout
	ptr = Bout.data();
	for(j = 0; j < J; j++)
	{	for(k = 0; k < n; k++)
			ptr[k + j * n] = X(j, k);
	}

	// return elements of lamout 
	ptr = lamout.data();
	for(j = 0; j < J; j++)
		ptr[j] = lambda(j, 0);

	// return elements of Pout
	mat2cpp::matrix<double> beta(1, n);
	mat2cpp::matrix<double> psi(M, 1);
	ptr = Pout.data();
	for(j = 0; j < J; j++)
	{	for(k = 0; k < n; k++)
			beta(0, k) = X(j, k);
		psi = like(beta);
		for(i = 0; i < M; i++)
			ptr[ i + j * M ] = psi(i, 0);
	}

	// Check for measure points that are constrained
	checkMeasurePoints( eps(0, 0), xLow, xUp, X );

	return;
}
예제 #12
0
/*Inputs: Destinaion Excel grid, array of ISINS, number of bonds, array of maturities, date tolerance for flies,  */
long _stdcall getFlyData(double* arr, unsigned char bonds[], int numbonds, long* maturities, int microWidth, int dateTolerance, int clusterLower, int clusterUpper, int history){
	SimpleRefData bbg(1);
	DoubleMatrix yieldData;
	DoubleMatrix micro;
	DoubleMatrix cluster;
	std::vector <std::string> bond_vec;
	std::wstring stemp2 = std::to_wstring(numbonds);
	LPCWSTR sw2 = stemp2.c_str();
	OutputDebugString(L"numbonds ");

	OutputDebugString(sw2);

	for (int b = 0; b <= numbonds; b++){
		std::string prefix = "/isin/";
		std::string suffix;
		suffix.clear();
		for (int j = 0; j < ISIN_LENGTH; j++){
			char a = bonds[b + j*(numbonds + 1)];
			suffix += a;
		}
		std::wstring stemp = std::wstring(suffix.begin(), suffix.end());
		LPCWSTR sw = stemp.c_str();
		OutputDebugString(sw);
		OutputDebugString(L"\n");

		bond_vec.push_back((prefix + suffix).c_str());
	}

	double** yieldArray;
	yieldArray = new double*[numbonds+1];
	for (int i = 0; i <= numbonds; i++){
		yieldArray[i] = new double[history];
	}

	int days_data = bbg.runHistData(yieldArray, bond_vec, history);
	std::wstring stemp = std::to_wstring(days_data);
	LPCWSTR sw = stemp.c_str();
	OutputDebugString(sw);

	yieldData.resize(numbonds+1);
	for (int i = 0; i <= numbonds; i++){
		yieldData[i].resize(days_data);
	}
	for (int i = 0; i <= numbonds; i++){
		for (int j = 0; j < days_data; j++){
			yieldData.at(i).at(j) = yieldArray[i][j];
		}
	}

	DoubleMatrix microMatrix;
	DoubleMatrix clusterMatrix;
	for (int i = 0; i < numbonds; i++){
		double microLocal[8];
		double clusterLocal[8];
		getFlyMetrics(microLocal, false, i, yieldData, maturities, dateTolerance, microWidth, microWidth, days_data);
		getFlyMetrics(clusterLocal, true, i, yieldData, maturities, 0, clusterLower, clusterUpper, days_data);
		double flyLocal[16];
		for (int j = 0; j < 8; j++){
			flyLocal[j] = microLocal[j];
			flyLocal[8 + j] = clusterLocal[j];
		}

		for (int k = 0; k < 16; k++){
			arr[i + k*(numbonds + 1)] = flyLocal[k];
		}
	}
	delete[] yieldArray;
	return 0;
}
예제 #13
0
void BackwardParabolicIBVP::gridMethod(DoubleMatrix &p, SweepMethodDirection direction) const
{
    typedef void (*t_algorithm)(const double*, const double*, const double*, const double*, double*, unsigned int);
    t_algorithm algorithm = &tomasAlgorithm;
    if (direction == ForwardSweep) algorithm = &tomasAlgorithmL2R;
    if (direction == BackwardSweep) algorithm = &tomasAlgorithmR2L;

    Dimension time = mtimeDimension;
    Dimension dim1 = mspaceDimension.at(0);

    double ht = time.step();
    unsigned int minM = time.min();
    unsigned int maxM = time.max();
    unsigned int M = maxM-minM;

    double hx = dim1.step();
    unsigned int minN = dim1.min();
    unsigned int maxN = dim1.max();
    unsigned int N = maxN-minN;

    double h = ht/(hx*hx);

    p.clear();
    p.resize(M+1, N+1);

    double *ka = (double*) malloc(sizeof(double)*(N-1));
    double *kb = (double*) malloc(sizeof(double)*(N-1));
    double *kc = (double*) malloc(sizeof(double)*(N-1));
    double *kd = (double*) malloc(sizeof(double)*(N-1));
    double *rx = (double*) malloc(sizeof(double)*(N-1));

    /* initial condition */
    SpaceNodePDE isn;
    for (unsigned int n=0; n<=N; n++)
    {
        isn.i = n+minN;
        isn.x = isn.i*hx;
        p[M][n] = initial(isn);
    }
    layerInfo(p, M);

    SpaceNodePDE lsn;
    lsn.i = minN;
    lsn.x = minN*hx;

    SpaceNodePDE rsn;
    rsn.i = maxN;
    rsn.x = maxN*hx;

    TimeNodePDE tn;
    for (unsigned int m=M-1; m!=UINT32_MAX; m--)
    {
        tn.i = m+minM;
        tn.t = tn.i*ht;

        for (unsigned int n=1; n<=N-1; n++)
        {
            isn.i = n+minN;
            isn.x = isn.i*hx;

            double alpha = -a(isn,tn)*h;
            double betta = 1.0 - 2.0*alpha;

            ka[n-1] = alpha;
            kb[n-1] = betta;
            kc[n-1] = alpha;
            kd[n-1] = p[m+1][n] - ht * f(isn, tn);
        }

        ka[0]   = 0.0;
        kc[N-2] = 0.0;

        /* border conditions */
        p[m][0] = boundary(lsn, tn);
        p[m][N] = boundary(rsn, tn);

        kd[0]   += a(lsn,tn) * h * p[m][0];
        kd[N-2] += a(rsn,tn) * h * p[m][N];

        (*algorithm)(ka, kb, kc, kd, rx, N-1);

        for (unsigned int n=1; n<=N-1; n++) p[m][n] = rx[n-1];

        layerInfo(p, m);
    }

    free(ka);
    free(kb);
    free(kc);
    free(kd);
    free(rx);
}
예제 #14
0
void ppedOpt( SpdModel&             model,
              Optimizer&            xOptInfo,
              const DoubleMatrix&   dvecXLow,
              const DoubleMatrix&   dvecXUp,
              const DoubleMatrix&   dvecXIn,
              DoubleMatrix*         pdvecXOut,
              const DoubleMatrix&   dvecXStep,
              Optimizer&            alpOptInfo,
              const DoubleMatrix&   dvecAlpLow,
              const DoubleMatrix&   dvecAlpUp,
              const DoubleMatrix&   dvecAlpIn,
              DoubleMatrix*         pdvecAlpOut,
              const DoubleMatrix&   dvecAlpStep,
              double*               pdPhiTildeOut,
              DoubleMatrix*         pdrowPhiTilde_xOut,
              DoubleMatrix*         pdmatPhiTilde_x_xOut )
{
  //------------------------------------------------------------
  // Preliminaries.
  //------------------------------------------------------------

  using namespace std;

  // If no evaluation is requested, return immediately.
  if ( !pdvecXOut && 
       !pdPhiTildeOut &&
       !pdrowPhiTilde_xOut &&
       !pdmatPhiTilde_x_xOut )
  {
    return;
  }

  // Get the number of design and fixed population parameters.
  int nX   = dvecXIn.nr();
  int nAlp = dvecAlpIn.nr();


  //------------------------------------------------------------
  // Validate the inputs (Debug mode).
  //------------------------------------------------------------

  // Check the length of the design parameter vector.
  assert( nX == model.getNDesPar() );


  //------------------------------------------------------------
  // Validate the inputs (All modes).
  //------------------------------------------------------------

  // [Revisit - Warm Start Disabled - Mitch]
  // In order to simplify the testing of this function,
  // warm starts have been disabled for now.
  if ( xOptInfo.getIsWarmStart() || alpOptInfo.getIsWarmStart() )
  {
    throw SpkException(
      SpkError::SPK_USER_INPUT_ERR,
      "One of the input Optimizer objects requested a warm start, which have been disabled for now.",
      __LINE__,
      __FILE__ );
  }


  //------------------------------------------------------------
  // Prepare the values required to calculate the output values.
  //------------------------------------------------------------

  // If this value is required to calculate the output values, 
  // initialize it so that it wil be calculated.  Otherwise, 
  // set its pointer to zero so that it won't be calculated.
  DoubleMatrix dvecXOutTemp;
  DoubleMatrix* pdvecXOutTemp = &dvecXOutTemp;
  if ( pdvecXOut )
  {
    dvecXOutTemp.resize( nX, 1 );
  }
  else
  {
    pdvecXOutTemp = 0;
  }

  // If this value is required to calculate the output values, 
  // initialize it so that it wil be calculated.  Otherwise, 
  // set its pointer to zero so that it won't be calculated.
  DoubleMatrix dvecAlpOutTemp;
  DoubleMatrix* pdvecAlpOutTemp = &dvecAlpOutTemp;
  if ( pdvecAlpOut )
  {
    dvecAlpOutTemp.resize( nAlp, 1 );
  }
  else
  {
    pdvecAlpOutTemp = 0;
  }

  // If this value is required to calculate the output values, 
  // initialize it so that it wil be calculated.  Otherwise, 
  // set its pointer to zero so that it won't be calculated.
  double dLTildeOut;
  double* pdLTildeOut = &dLTildeOut;
  if ( pdPhiTildeOut || pdrowPhiTilde_xOut || pdmatPhiTilde_x_xOut )
  {
    dLTildeOut = 0.0;
  }
  else
  {
    pdLTildeOut = 0;
  }

  // If this value is required to calculate the output values, 
  // initialize it so that it wil be calculated.  Otherwise, 
  // set its pointer to zero so that it won't be calculated.
  DoubleMatrix drowLTilde_xOut;
  DoubleMatrix* pdrowLTilde_xOut = &drowLTilde_xOut;
  if ( pdrowPhiTilde_xOut || pdmatPhiTilde_x_xOut )
  {
    drowLTilde_xOut.resize( 1, nX );
  }
  else
  {
    pdrowLTilde_xOut = 0;
  }
  
  // If this value is required to calculate the output values, 
  // initialize it so that it wil be calculated.  Otherwise, 
  // set its pointer to zero so that it won't be calculated.
  DoubleMatrix dmatLTilde_x_xOut;
  DoubleMatrix* pdmatLTilde_x_xOut = &dmatLTilde_x_xOut;
  if ( pdmatPhiTilde_x_xOut )
  {
    dmatLTilde_x_xOut.resize( nX, nX );
  }
  else
  {
    pdmatLTilde_x_xOut = 0;
  }
  

  //------------------------------------------------------------
  // Set arguments related to using ppkaOpt for optimal design.
  //------------------------------------------------------------

  // ppkaOpt solves a two-level optimization problem, where the second
  // level is made up of multiple optimization problems that are all 
  // solved for each iteration of the first level.  The length of the 
  // vector N specifies the number of optimation problems that must be 
  // solved at the second level.  In this use of ppkaOpt, the first 
  // level optimization is over the design parameter vector x, while the 
  // second level is over the fixed population vector alp.  Since there 
  // is only a single fixed population vector alp, N has length one.  Its
  // only value is one because lTilde requires that there be at least
  // one data value, but this criterion itself does not depend on the data.
  int nIndPped = 1;
  int nY_iPped = 1;
  DoubleMatrix dvecN( nIndPped, 1 );
  DoubleMatrix dvecY( nY_iPped, 1 );
  dvecN.fill( (double) nY_iPped );
  dvecY.fill( 0.0 );

  // Choose the modified Laplace approximation.
  enum Objective objective = MODIFIED_LAPLACE;

  // Construct a model that evaluates first order approximations for
  // the mean and covariance of the data, maps individual to population
  // parameters, and maps population to design parameters.
  FoMapsParSpkModel ppedOptModel( &model, dvecAlpStep.toValarray() );


  //------------------------------------------------------------
  // Optimize the modified Laplace approximation for the negative log of phi(x).
  //------------------------------------------------------------

  // The criterion phi(x) is optimized by replacing the definition of 
  // lambda(alp, b) and mapObj(b) by the negative natural logarithm 
  // of the integral with respect to alp and then calls the function 
  // ppkaOpt to optimize the Laplace approximation for the integral.
  try
  {
    ppkaOpt( ppedOptModel,
             objective,
             dvecN,
             dvecY,
             xOptInfo,
             dvecXLow,
             dvecXUp,
             dvecXIn,
             &dvecXOutTemp,
             dvecXStep,
             alpOptInfo,
             dvecAlpLow,
             dvecAlpUp,
             dvecAlpIn,
             &dvecAlpOutTemp,
             dvecAlpStep,
             &dLTildeOut,
             &drowLTilde_xOut,
             &dmatLTilde_x_xOut );
  }
  catch( SpkException& e )
  {
    throw e.push(
      SpkError::SPK_UNKNOWN_ERR,
      "Optimization of the population expected determinant criterion failed.",
      __LINE__,
      __FILE__ );
  }
  catch( const std::exception& stde )
  {
    throw SpkException(
      stde,
      "A standard exception occurred during the optimization of the population expected determinant criterion.",
      __LINE__,
      __FILE__ );
  }
  catch( ... )
  {
    throw SpkException(
      SpkError::SPK_UNKNOWN_ERR,
      "An unknown exception occurred during the optimization of the population expected determinant criterion.",
      __LINE__,
      __FILE__ );
  }


  //------------------------------------------------------------
  // Finish up.
  //------------------------------------------------------------

  // Don't set any of the values if the maximum number of
  // iterations was reached for either optimization level.
  if ( xOptInfo.getIsTooManyIter() || alpOptInfo.getIsTooManyIter() )
  {
    return;
  }

  // Set the final design parameter value, if necessary.
  if ( pdvecXOut )
  {
    *pdvecXOut = dvecXOutTemp;
  }

  // Set the final fixed population parameter value, if necessary.
  if ( pdvecAlpOut )
  {
    *pdvecAlpOut = dvecAlpOutTemp;
  }

  // Calculate and set the final population expected determinant
  // criterion value, if necessary.
  double dPhiTildeOutTemp;
  if ( pdPhiTildeOut || pdrowPhiTilde_xOut || pdmatPhiTilde_x_xOut )
  {
    // Calculate
    //
    //     phiTilde(x)  =  exp[ - LTilde(x) ] 
    //
    // at x = xOut.
    dPhiTildeOutTemp = exp( -dLTildeOut );

   if ( pdPhiTildeOut )
   {
     *pdPhiTildeOut = dPhiTildeOutTemp;
   }
  }

  // Calculate and set the first derivative of the population
  // expected determinant criterion at the final parameter value,
  // if necessary.
  DoubleMatrix drowPhiTilde_xOutTemp;
  if ( pdrowPhiTilde_xOut || pdmatPhiTilde_x_xOut )
  {
    // Calculate
    //
    //     phiTilde_x(x)  =  - exp[ - LTilde(x) ] * LTilde_x(x)
    //
    //                    =  - phiTilde(x) * LTilde_x(x)
    //
    // at x = xOut.
    drowPhiTilde_xOutTemp.resize( 1, nX );
    mulByScalar( drowLTilde_xOut, -dPhiTildeOutTemp, drowPhiTilde_xOutTemp );

    if ( pdrowPhiTilde_xOut )
    {
      *pdrowPhiTilde_xOut = drowPhiTilde_xOutTemp;
    }
  }

  // Calculate and set the second derivative of the population
  // expected determinant criterion at the final parameter value, if necessary.
  if ( pdmatPhiTilde_x_xOut )
  {
    // [Revisit - Hessian Mixes Analytic and Approximate Derivatives - Mitch]
    // Would it be better to approximate the Hessian, phiTilde_x_x(x),
    // by taking finite differences of the full derivative, phiTilde_x(x),
    // rather than approximating one term in the Hessian, LTilde_x_xOut(x),
    // by taking finite differences of LTilde_x(x)?
    //
    // Calculate
    //
    //     phiTilde_x_x(x)  =  exp[ - LTilde(x) ]  *
    //
    //                            -                                                -
    //                           |                                 T                |
    //                           |  - LTilde_x_x(x)  +  LTilde_x(x)  * LTilde_x(x)  |
    //                           |                                                  |
    //                            -                                                -
    //
    //                      =  phiTilde(x)  *
    //
    //                            -                                                -
    //                           |                                 T                |
    //                           |  - LTilde_x_x(x)  +  LTilde_x(x)  * LTilde_x(x)  |
    //                           |                                                  |
    //                            -                                                -
    //
    // at x = xOut.
    //
    DoubleMatrix dvecLTilde_xTrans;
    DoubleMatrix temp1;
    DoubleMatrix temp2;
    transpose( drowLTilde_xOut, dvecLTilde_xTrans );
    multiply( dvecLTilde_xTrans, drowLTilde_xOut, temp1 );
    subtract( temp1, dmatLTilde_x_xOut, temp2 );
    mulByScalar( temp2, dPhiTildeOutTemp, *pdmatPhiTilde_x_xOut );
  }

}
예제 #15
0
/*
 * y = [1]
 *     [1]
 */
void lambdaTest::setY(DoubleMatrix &y)
{
    y.resize(2,1);
    y.fill(rand()/(pow(2.0,32.0)) * 100.0 );
}
예제 #16
0
/*
 * alpha = [1]
 *         [1]
 */
void lambdaTest::setAlp(DoubleMatrix &alp)
{
    alp.resize(2,1);
    alp.fill(rand()/(pow(2.0,32.0)) * 100.0);
}
예제 #17
0
파일: stoprule.cpp 프로젝트: Cibiv/IQ-TREE
void StopRule::cmpInvMat (DoubleMatrix &oriMat, DoubleMatrix &invMat, int size) {
	//invMat.setLimit (size, size);
	double eps = 1.0e-20; /* ! */
	int i, j, k, l, maxi=0, idx, ix, jx;
	double sum, tmp, maxb, aw;

	invMat.resize(size);
	for (i = 0; i < size; i++) invMat[i].resize(size);

	IntVector index (size);
	double *wk;
	DoubleMatrix omtrx (size);
	for (i = 0; i < size; i++) omtrx[i].resize(size);



	/* copy oriMat to omtrx */
	for (i = 0; i < size; i++)
		for (j = 0; j < size; j++)
			omtrx[i][j] = oriMat[i][j];

	wk = (double *) calloc((size_t)size, sizeof(double));
	aw = 1.0;
	for (i = 0; i < size; i++) {
		maxb = 0.0;
		for (j = 0; j < size; j++) {
			if (fabs(omtrx[i][j]) > maxb)
				maxb = fabs(omtrx[i][j]);
		}
		if (maxb == 0.0) {
			/* Singular matrix */
			cout << "\n\n\nHALT: PLEASE REPORT ERROR D TO DEVELOPERS\n\n\n";
			//OutStream::write(oriMat, cout);
			exit(1);
		}
		wk[i] = 1.0 / maxb;
	}
	for (j = 0; j < size; j++) {
		for (i = 0; i < j; i++) {
			sum = omtrx[i][j];
			for (k = 0; k < i; k++)
				sum -= omtrx[i][k] * omtrx[k][j];
			omtrx[i][j] = sum;
		}
		maxb = 0.0;
		for (i = j; i < size; i++) {
			sum = omtrx[i][j];
			for (k = 0; k < j; k++)
				sum -= omtrx[i][k] * omtrx[k][j];
			omtrx[i][j] = sum;
			tmp = wk[i] * fabs(sum);
			if (tmp >= maxb) {
				maxb = tmp;
				maxi = i;
			}
		}
		if (j != maxi) {
			for (k = 0; k < size; k++) {
				tmp = omtrx[maxi][k];
				omtrx[maxi][k] = omtrx[j][k];
				omtrx[j][k] = tmp;
			}
			aw = -aw;
			wk[maxi] = wk[j];
		}
		index[j] = maxi;
		if (omtrx[j][j] == 0.0)
			omtrx[j][j] = eps;
		if (j != size - 1) {
			tmp = 1.0 / omtrx[j][j];
			for (i = j + 1; i < size; i++)
				omtrx[i][j] *= tmp;
		}
	}
	for (jx = 0; jx < size; jx++) {
		for (ix = 0; ix < size; ix++)
			wk[ix] = 0.0;
		wk[jx] = 1.0;
		l = -1;
		for (i = 0; i < size; i++) {
			idx = index[i];
			sum = wk[idx];
			wk[idx] = wk[i];
			if (l != -1) {
				for (j = l; j < i; j++)
					sum -= omtrx[i][j] * wk[j];
			} else if (sum != 0.0)
				l = i;
			wk[i] = sum;
		}
		for (i = size - 1; i >= 0; i--) {
			sum = wk[i];
			for (j = i + 1; j < size; j++)
				sum -= omtrx[i][j] * wk[j];
			wk[i] = sum / omtrx[i][i];
		}
		for (ix = 0; ix < size; ix++)
			invMat[ix][jx] = wk[ix];
	}
	free((char *)wk);
	wk = NULL;
} /* luinverse */
예제 #18
0
void ppdOpt( SpdModel&             model,
             Optimizer&            optInfo,
             const DoubleMatrix&   dvecXLow,
             const DoubleMatrix&   dvecXUp,
             const DoubleMatrix&   dvecXIn,
             DoubleMatrix*         pdvecXOut,
             const DoubleMatrix&   dvecXStep,
             const DoubleMatrix&   dvecAlp,
             const DoubleMatrix&   dvecAlpStep,
             double*               pdPhiTildeOut,
             DoubleMatrix*         pdrowPhiTilde_xOut,
             DoubleMatrix*         pdmatPhiTilde_x_xOut )
{
  //------------------------------------------------------------
  // Preliminaries.
  //------------------------------------------------------------

  using namespace std;

  // If no evaluation is requested, return immediately.
  if ( !pdvecXOut && 
       !pdPhiTildeOut &&
       !pdrowPhiTilde_xOut &&
       !pdmatPhiTilde_x_xOut )
  {
    return;
  }

  // Get the number of design parameters.
  int nX = dvecXIn.nr();


  //------------------------------------------------------------
  // Validate the inputs (Debug mode).
  //------------------------------------------------------------

  // Check the length of the design parameter vector.
  assert( nX == model.getNDesPar() );


  //------------------------------------------------------------
  // Validate the inputs (All modes).
  //------------------------------------------------------------

  // [Revisit - Warm Start Disabled - Mitch]
  // In order to simplify the testing of this function,
  // warm starts have been disabled for now.
  if ( optInfo.getIsWarmStart() )
  {
    throw SpkException(
      SpkError::SPK_USER_INPUT_ERR,
      "The input Optimizer object requested a warm start, which have been disabled for now.",
      __LINE__,
      __FILE__ );
  }


  //------------------------------------------------------------
  // Prepare the values required to calculate the output values.
  //------------------------------------------------------------

  // If this value is required to calculate the output values, 
  // initialize it so that it wil be calculated.  Otherwise, 
  // set its pointer to zero so that it won't be calculated.
  DoubleMatrix dvecXOutTemp;
  DoubleMatrix* pdvecXOutTemp = &dvecXOutTemp;
  if ( pdvecXOut || pdmatPhiTilde_x_xOut )
  {
    dvecXOutTemp.resize( nX, 1 );
  }
  else
  {
    pdvecXOutTemp = 0;
  }
  
  // If this value is required to calculate the output values, 
  // initialize it so that it wil be calculated.  Otherwise, 
  // set its pointer to zero so that it won't be calculated.
  double dNegLogOut;
  double* pdNegLogOut = &dNegLogOut;
  if ( pdPhiTildeOut || pdrowPhiTilde_xOut || pdmatPhiTilde_x_xOut )
  {
    dNegLogOut = 0.0;
  }
  else
  {
    pdNegLogOut = 0;
  }

  // If this value is required to calculate the output values, 
  // initialize it so that it wil be calculated.  Otherwise, 
  // set its pointer to zero so that it won't be calculated.
  DoubleMatrix drowNegLog_xOut;
  DoubleMatrix* pdrowNegLog_xOut = &drowNegLog_xOut;
  if ( pdrowPhiTilde_xOut || pdmatPhiTilde_x_xOut )
  {
    drowNegLog_xOut.resize( 1, nX );
  }
  else
  {
    pdrowNegLog_xOut = 0;
  }
  

  //------------------------------------------------------------
  // Prepare the objective function.
  //------------------------------------------------------------

  // Construct the objective function, which will evaluate the 
  // negative logarithm of the population determinant criterion.
  PpdOptObj ppdOptObj( nX, &model, &dvecAlp, &dvecAlpStep );


  //------------------------------------------------------------
  // Handle nonzero iterations for the optimization problem.
  //------------------------------------------------------------

  // If the number of iterations is not zero, then optimize the
  // objective function to determine the optimal parameter value.
  if ( optInfo.getNMaxIter() > 0 )
  {
    try
    {
      // The criterion phi is maximized by finding
      // the minumum of its negative logarithm.
      quasiNewtonAnyBox( ppdOptObj,
			 optInfo,
			 dvecXLow,
			 dvecXUp,
			 dvecXIn,
			 pdvecXOutTemp,
			 pdNegLogOut,
			 pdrowNegLog_xOut ); 
    }
    catch( SpkException& e )
    {
      throw e.push(
        SpkError::SPK_UNKNOWN_ERR,
        "Optimization of the population determinant criterion failed.",
        __LINE__,
        __FILE__ );
    }
    catch( const std::exception& stde )
    {
      throw SpkException(
        stde,
        "A standard exception occurred during the optimization of the population determinant criterion.",
        __LINE__,
        __FILE__ );
    }
    catch( ... )
    {
      throw SpkException(
        SpkError::SPK_UNKNOWN_ERR,
        "An unknown exception occurred during the optimization of the population determinant criterion.",
        __LINE__,
        __FILE__ );
    }
  }


  //------------------------------------------------------------
  // Handle zero iterations for the optimization problem.
  //------------------------------------------------------------

  // If the number of iterations is zero, then the final
  // value for the parameter is equal to its initial value.
  if ( optInfo.getNMaxIter() == 0 )
  {
    // Set the final value.
    dvecXOutTemp = dvecXIn;
    
    try
    {
      // Evaluate the negative logarithm of the criterion phi,
      // and/or its derivative, at the final value.
      if ( pdPhiTildeOut || pdrowPhiTilde_xOut )
      {
	// The objective function must be called first to set x.
	ppdOptObj.function( dvecXOutTemp, pdNegLogOut );

	// Calculate the derivative, if necessary.
	if ( pdrowPhiTilde_xOut )
        {
	  ppdOptObj.gradient( pdrowNegLog_xOut );
	}
      }
    }
    catch( SpkException& e )
    {
      throw e.push(
        SpkError::SPK_UNKNOWN_ERR,
        "Evaluation of the population determinant criterion failed.",
        __LINE__,
        __FILE__ );
    }
    catch( const std::exception& stde )
    {
      throw SpkException(
        stde,
        "A standard exception occurred during the evaluation of the population determinant criterion.",
        __LINE__,
        __FILE__ );
    }
    catch( ... )
    {
      throw SpkException(
        SpkError::SPK_UNKNOWN_ERR,
        "An unknown exception occurred during the evaluation of the population determinant criterion.",
        __LINE__,
        __FILE__ );
    }
  }
  

  //------------------------------------------------------------
  // Finish up.
  //------------------------------------------------------------

  // Don't set any of the values if the maximum number of
  // iterations was reached.
  if ( optInfo.getIsTooManyIter() )
  {
    return;
  }

  // Set the final parameter value, if necessary.
  if ( pdvecXOut )
  {
    *pdvecXOut = dvecXOutTemp;
  }

  // Calculate and set the final population determinant criterion
  // value, if necessary.
  double dPhiTildeOutTemp;
  if ( pdPhiTildeOut || pdrowPhiTilde_xOut || pdmatPhiTilde_x_xOut )
  {
    // Calculate
    //
    //     phiTilde(x)  =  exp[ - negLogVal(x) ] 
    //
    // at x = xOut.
    dPhiTildeOutTemp = exp( -dNegLogOut );

   if ( pdPhiTildeOut )
   {
     *pdPhiTildeOut = dPhiTildeOutTemp;
   }
  }

  // Calculate and set the first derivative of the population
  // determinant criterion at the final parameter value, if necessary.
  DoubleMatrix drowPhiTilde_xOutTemp;
  if ( pdrowPhiTilde_xOut || pdmatPhiTilde_x_xOut )
  {
    // Calculate
    //
    //     phiTilde_x(x)  =  - exp[ - negLogVal(x) ] * negLogVal_x(x)
    //
    //                    =  - phiTilde(x) * negLogVal_x(x)
    //
    // at x = xOut.
    drowPhiTilde_xOutTemp.resize( 1, nX );
    mulByScalar( drowNegLog_xOut, -dPhiTildeOutTemp, drowPhiTilde_xOutTemp );

    if ( pdrowPhiTilde_xOut )
    {
      *pdrowPhiTilde_xOut = drowPhiTilde_xOutTemp;
    }
  }

  // Calculate and set the second derivative of the population
  // determinant criterion at the final parameter value, if necessary.
  if ( pdmatPhiTilde_x_xOut )
  {
    // [Revisit - Hessian Mixes Analytic and Approximate Derivatives - Mitch]
    // Would it be better to approximate the Hessian, phiTilde_x_x(x),
    // by taking finite differences of the full derivative, phiTilde_x(x),
    // rather than approximating one term in the Hessian, negLog_x_xOut(x),
    // by taking finite differences of negLog_x(x)?
    //
    // Calculate
    //
    //     phiTilde_x_x(x)  =  exp[ - negLogVal(x) ]  *
    //
    //                            -                                                         -
    //                           |                                       T                   |
    //                           |  - negLogVal_x_x(x)  +  negLogVal_x(x)  * negLogVal_x(x)  |
    //                           |                                                           |
    //                            -                                                         -
    //
    //                      =  phiTilde(x)  *
    //
    //                            -                                                         -
    //                           |                                       T                   |
    //                           |  - negLogVal_x_x(x)  +  negLogVal_x(x)  * negLogVal_x(x)  |
    //                           |                                                           |
    //                            -                                                         -
    //
    // at x = xOut.
    //

    // Calculate the central difference of the analytic derivative
    // of the negative log of the criterion.
    DoubleMatrix dmatNegLog_x_xTilde;
    PpdOptObj_xFuncObj ppdOptObj_xFuncObj( &ppdOptObj );
    try
    {
      dmatNegLog_x_xTilde = centdiff<PpdOptObj_xFuncObj>( 
        ppdOptObj_xFuncObj,
        dvecXOutTemp,
        dvecXStep );
    }
    catch( SpkException& e )
    {
      throw e.push(
        SpkError::SPK_UNKNOWN_ERR,
        "Evaluation of the Hessian of the population determinant criterion failed.",
        __LINE__,
        __FILE__ );
    }
    catch( const std::exception& stde )
    {
      throw SpkException(
        stde,
        "A standard exception occurred during the evaluation of the Hessian of the population determinant criterion.",
        __LINE__,
        __FILE__ );
    }
    catch( ... )
    {
      throw SpkException(
        SpkError::SPK_UNKNOWN_ERR,
        "An unknown exception occurred during the evaluation of the Hessian of the population determinant criterion.",
        __LINE__,
        __FILE__ );
    }

    // Switch the order of differentiation so that the central 
    // difference comes before the analytic derivative.
    DoubleMatrix dmatNegLog_xTilde_x;
    dmatNegLog_xTilde_x = transposeDerivative( 
      drowNegLog_xOut,
      dmatNegLog_x_xTilde );

    DoubleMatrix dvecNegLogVal_xTrans;
    DoubleMatrix temp1;
    DoubleMatrix temp2;

    // Construct the second derivative of the population
    // determinant criterion.
    transpose( drowNegLog_xOut, dvecNegLogVal_xTrans );
    multiply( dvecNegLogVal_xTrans, drowNegLog_xOut, temp1 );
    subtract( temp1, dmatNegLog_xTilde_x, temp2 );
    mulByScalar( temp2, dPhiTildeOutTemp, *pdmatPhiTilde_x_xOut );
  }

}
예제 #19
0
void DiscreteHeat::calculateP(const DoubleVector &f, const DoubleVector &u, DoubleMatrix &psi, DoubleVector &g)
{
    C_UNUSED(f);
    C_UNUSED(g);

    double lamda = -(a*ht)/(hx*hx);
    double k = 1.0-2.0*lamda;

    psi.clear();
    psi.resize(M+1, N+1);

    DoubleVector a1(N-1);
    DoubleVector b1(N-1);
    DoubleVector c1(N-1);
    DoubleVector d1(N-1);
    DoubleVector x1(N-1);

    for (unsigned int m=0; m<=M; m++)
    {
        unsigned int j = M-m;

        if (j==M)
        {
            for (unsigned int i=1; i<=N-1; i++)
            {
                a1[i-1] = lamda;
                b1[i-1] = k;
                c1[i-1] = lamda;
                d1[i-1] = -2.0*hx*(u[i]-U[i]);
            }
            a1[0]   = 0.0;
            c1[N-2] = 0.0;
            tomasAlgorithm(a1.data(), b1.data(), c1.data(), d1.data(), x1.data(), x1.size());

            for (unsigned int i=1; i<=N-1; i++)
            {
                psi[j][i] = x1[i-1];
            }
            psi[j][0] = -lamda*psi[j][1]   -2.0*hx*0.5*(u[0]-U[0]);
            psi[j][N] = -lamda*psi[j][N-1] -2.0*hx*0.5*(u[N]-U[N]);
        }
        else if (j==0)
        {
            psi[j][0] = -lamda*psi[j][1];
            psi[j][N] = -lamda*psi[j][N-1];

            for (unsigned int i=1; i<=N-1; i++)
            {
                psi[j][i] = psi[j+1][i];
            }
        }
        else
        {
            for (unsigned int i=1; i<=N-1; i++)
            {
                a1[i-1] = lamda;
                b1[i-1] = k;
                c1[i-1] = lamda;
                d1[i-1] = +psi[j+1][i];
            }
            a1[0]   = 0.0;
            c1[N-2] = 0.0;
            tomasAlgorithm(a1.data(), b1.data(), c1.data(), d1.data(), x1.data(), x1.size());

            for (unsigned int i=1; i<=N-1; i++)
            {
                psi[j][i] = x1[i-1];
            }
            psi[j][0] = -lamda*psi[j][1];
            psi[j][N] = -lamda*psi[j][N-1];
        }
    }
    a1.clear();
    b1.clear();
    c1.clear();
    d1.clear();
    x1.clear();
}