Exemplo n.º 1
0
/**
 * Copy the parameter values to a GSLVector.
 * @param params :: A vector to copy the parameters to
 */
void CostFuncLeastSquares::getParameters(GSLVector &params) const {
  if (params.size() != nParams()) {
    params.resize(nParams());
  }
  for (size_t i = 0; i < nParams(); ++i) {
    params.set(i, getParameter(i));
  }
}
Exemplo n.º 2
0
/**
 * Improve the solution by using obtained functions as a basis for diagonalization.
 *
 * @param hamiltonian :: The Hamiltonian.
 * @param basis :: The basis functions.
 * @param n :: The size of the basis to use.
 * @param eigv :: Output eigenvalues.
 * @param eigf :: Output eigenvectors (functions). This method creates a new ChebfunVector instance and returns it.
 */
void Schrodinger1D::improve(ChebOperator *hamiltonian, ChebfunVector *basis, GSLVector &eigv, ChebfunVector **eigf) const
{
    size_t n = basis->size();

    GSLMatrix H(n,n);

    for(size_t i = 0; i < n; ++i)
    {
        const chebfun &f1 = basis->cfun(i).cfun(0).getUnscaled();
        chebfun d1(f1);
        hamiltonian->apply(f1, d1);
        chebfun dd = d1;
        dd *= f1;
        double me = dd.integr();
        H.set( i, i, me );
        //std::cerr << "e=" << me << std::endl;
        for(size_t j = i+1; j < n; ++j)
        {
            const chebfun &f2 = basis->cfun(j).cfun(0).getUnscaled();
            chebfun d2(f2);
            hamiltonian->apply(f2, d2);
            d2 *= f1;
            double me = d2.integr();
            //std::cerr << "o=" << me << std::endl;
            H.set( i, j, me );
            H.set( j, i, me );
        }
    }
    //std::cerr << H << std::endl;
    GSLVector en;
    GSLMatrix v;
    H.diag(en,v);
    std::vector<size_t> indx;
    getSortedIndex( en, indx );

    if ( indx.empty() )
    {
        throw std::runtime_error("Schrodinger1D failed to find solution.");
    }

    eigv.resize( indx.size() );
    *eigf = new ChebfunVector;
    ChebfunVector &funs = **eigf;
    funs.fromLinearCombinations( *basis, v );
    funs.sort( indx );
    for(size_t i = 0; i < indx.size(); ++i)
    {
        std::cerr << i << ' ' << en[indx[i]] << std::endl;
        eigv.set( i, en[indx[i]] );
    }
}