예제 #1
0
  void deriveEquiFreqAndGtrParamsForReversibleRM(matrix_t const & Q, vector_t & equiFreq, vector_t & gtrParams)
  {
    equiFreq = deriveEquiFreqForReversibleRM(Q);
    unsigned n = equiFreq.size();
   
    // check
    for (unsigned i = 0; i < n; i++)
      assert(equiFreq[i] > 0);
    
    matrix_t pm = Q;  // parameter matrix
    for (unsigned i = 0; i < n; i++)
      for (unsigned j = 0; j < n; j++)
	pm(i, j) = Q(i, j) / equiFreq[j];

    // check grtParams size
    unsigned paramCount = (n * (n - 1) / 2);
    if (gtrParams.size() != paramCount)
      gtrParams.resize(paramCount);

    // reverse of gtrRateParametersToMatrix
    unsigned idx = 0;
    for (unsigned i = 0; i < n; i++)
      for (unsigned j = i + 1; j < n; j++) {
	gtrParams[idx] = pm(i, j);
	idx++;
      }
  }
예제 #2
0
void lfit(vector_t &x, vector_t &y, vector_t &sig, vector_t &a,
 	  vector<bool> &ia, matrix_t &covar, double &chisq, matrix_t & X)
{
  int i,j,k,l,m,mfit=0;
  double ym,wt,sum,sig2i;
  
  int ndat=x.size();
  int ma=a.size();
  vector_t afunc(ma);
  matrix_t beta;
  sizeMatrix(beta,ma,1);
  for (j=0;j<ma;j++)
    if (ia[j]) mfit++;
  if (mfit == 0) error("lfit: no parameters to be fitted");
  for (j=0;j<mfit;j++) {
    for (k=0;k<mfit;k++) covar[j][k]=0.0;
    beta[j][0]=0.0;
  }
  for (i=0;i<ndat;i++) {
    afunc = X[i];

    ym=y[i];
    if (mfit < ma) {
      for (j=0;j<ma;j++)
	if (!ia[j]) ym -= a[j]*afunc[j];
    }
    sig2i=1.0/SQR(sig[i]);
    for (j=0,l=0;l<ma;l++) {
      if (ia[l]) {
	wt=afunc[l]*sig2i;
	for (k=0,m=0;m<=l;m++)
	  if (ia[m]) covar[j][k++] += wt*afunc[m];
	beta[j++][0] += ym*wt;
      }
    }
  }
  for (j=1;j<mfit;j++)
    for (k=0;k<j;k++)
      covar[k][j]=covar[j][k];
  vector<vector<double> >  temp;
  sizeMatrix(temp,mfit,mfit);
  for (j=0;j<mfit;j++)
    for (k=0;k<mfit;k++)
      temp[j][k]=covar[j][k];
  gaussj(temp,beta);
  for (j=0;j<mfit;j++)
    for (k=0;k<mfit;k++)
      covar[j][k]=temp[j][k];
  for (j=0,l=0;l<ma;l++)
    if (ia[l]) a[l]=beta[j++][0];
  chisq=0.0;
  for (i=0;i<ndat;i++) {
    afunc = X[i];
    sum=0.0;
    for (j=0;j<ma;j++) sum += a[j]*afunc[j];
    chisq += SQR((y[i]-sum)/sig[i]);
  }
  covsrt(covar,ia,mfit);
}
예제 #3
0
vector_t next_vec( const vector_t &current, const vector_t &grad,
                   const vector_t &lapl ) {
    vector_t next( current.size() );
//    std::cout << "next_vec" << std::endl;
//    std::cout << current.size() << " " << grad.size() << " "
//        << lapl.size() << std::endl;
    for ( unsigned int i = 0; i < current.size(); ++i ) {
        next[i] = current[i] - grad[i] / lapl[i]; }
    return next; }
예제 #4
0
bool ModelUtil::EarlyStop(vector_t val_losses, size_t patience, float delta) {
  // Check for edge cases
  PELOTON_ASSERT(patience > 1);
  PELOTON_ASSERT(delta > 0);
  if (val_losses.size() < patience) return false;
  float cur_loss = val_losses[val_losses.size() - 1];
  float pat_loss = val_losses[val_losses.size() - patience];
  // Loss should have at least dropped by delta at this point
  return (pat_loss - cur_loss) < delta;
}
예제 #5
0
		static std::uint32_t to(state_t &state, const vector_t &val)
		{
			::lua_createtable(state, (int)val.size(), (int)val.size());

			std::uint32_t i = 1;
			std::for_each(val.cbegin(), val.cend(), 
				[&state, &i](const T &t)
			{
				convertion_t<T>::to(state, t);
				::lua_rawseti(state, -2, i++);
			});

			return 1;
		}
예제 #6
0
void pvt_base::check_gas_common (const vector_t &pressure, const vector_t &fvf, const vector_t &visc)
{
    for (t_long i = 1, cnt = (t_long)pressure.size (); i < cnt; ++i)
    {
        if (pressure[i] - pressure[i - 1] < EPS_DIFF)
        {
            throw bs_exception ("", "pressure curve should be monotonically increasing function");
        }
        if (fvf[i] - fvf[i - 1] >= 0)
        {
            BOSOUT (section::pvt, level::critical) << "gas: fvf" << bs_end;
            for (t_long j = 0; j < cnt; ++j)
            {
                BOSOUT (section::pvt, level::critical) << fvf[j] << bs_end;
            }

            throw bs_exception ("", "FVF curve should be monotonically decreasing function");
        }
        if (visc[i] - visc[i - 1] <= 0)
        {
            BOSOUT (section::pvt, level::critical) << "gas: visc" << bs_end;
            for (t_long j = 0; j < cnt; ++j)
            {
                BOSOUT (section::pvt, level::critical) << visc[j] << bs_end;
            }

            throw bs_exception ("", "Viscosity curve should be monotonically increasing function");
        }
    }
}
예제 #7
0
void
pvt_base::check_oil_common (const vector_t &pressure, const vector_t &fvf, const vector_t &visc)
{
    for (t_long i = 0, cnt = (t_long)pressure.size (); i < cnt; ++i)
    {
        if (pressure[i] < 0)
        {
            // TODO: LOG
            BS_ASSERT (false) (pressure[i]);
            throw bs_exception ("", "pressure should be greater than 0");
        }
        if (fvf[i] < 0)
        {
            // TODO:LOG
            BS_ASSERT (false) (fvf[i]);
            throw bs_exception ("", "fvf should be greater than 0");
        }
        if (visc[i] < 0)
        {
            // TODO: LOG
            BS_ASSERT (false) (visc[i]);
            throw bs_exception ("", "viscosity should be greater than 0");
        }
    }
}
예제 #8
0
  unsigned countNonZeroEntries(vector_t const & v)
  {

    unsigned n = 0;
    for (unsigned i = 0; i < v.size(); i++)
      if ( v(i) )
	n++;
    return n;
  }
static void CheckVector( const vector_t& cv, size_t expected_size, size_t old_size ) {
    ASSERT( cv.capacity()>=expected_size, NULL );
    ASSERT( cv.size()==expected_size, NULL );
    ASSERT( cv.empty()==(expected_size==0), NULL );
    for( int j=0; j<int(expected_size); ++j ) {
        if( cv[j].bar()!=~j )
            REPORT("ERROR on line %d for old_size=%ld expected_size=%ld j=%d\n",__LINE__,long(old_size),long(expected_size),j);
    }
}
예제 #10
0
  boost::shared_ptr<BaseTRRateMatrix> TRRateMatrixDispatcher(string const & substModel, vector_t const & rateParameters, vector_t const & equiFreq, StateMap const & alphabet)
  {
    boost::shared_ptr<BaseTRRateMatrix> rmPtr;

    if (substModel == "GTR")
      rmPtr = boost::shared_ptr<GTRRateMatrix>( new GTRRateMatrix(alphabet) );
    // define other substitution models here ...
    else
      errorAbort("substitution model '" + substModel + "' not defined. If newly implemented, add to TRRateMatrixDispatcher.");


    if (equiFreq.size() > 0) // not defined, stick with default
      rmPtr->resetEquiFreqs(equiFreq);
    if (rateParameters.size() > 0) // not defined, stick with default
      rmPtr->resetRateParameters(rateParameters);

    return rmPtr;
  }
예제 #11
0
 scalar_t vgrad(const vector_t& x, vector_t* gx) const override
 {
         m_idata = map_tensor(x.data(), m_idata.dims());
         m_op.output(m_idata, m_wdata, m_bdata, m_odata);
         if (gx)
         {
                 gx->resize(x.size());
                 auto idata = map_tensor(gx->data(), m_idata.dims());
                 m_op.ginput(idata, m_wdata, m_bdata, m_odata);
         }
         return m_odata.array().square().sum() / 2;
 }
예제 #12
0
 scalar_t vgrad(const vector_t& x, vector_t* gx) const override
 {
         m_wdata = map_matrix(x.data(), m_wdata.rows(), m_wdata.cols());
         m_bdata = map_vector(x.data() + m_wdata.size(), m_bdata.size());
         m_op.output(m_idata, m_wdata, m_bdata, m_odata);
         if (gx)
         {
                 gx->resize(x.size());
                 auto wdata = map_matrix(gx->data(), m_wdata.rows(), m_wdata.cols());
                 auto bdata = map_vector(gx->data() + m_wdata.size(), m_bdata.size());
                 m_op.gparam(m_idata, wdata, bdata, m_odata);
         }
         return m_odata.array().square().sum() / 2;
 }
예제 #13
0
// QR Factorization of a MxN General Matrix A.
//    a       (IN/OUT - matrix(M,N)) On entry, the coefficient matrix A. On exit , the upper triangle and diagonal is the min(M,N) by N upper triangular matrix R.  The lower triangle, together with the tau vector, is the orthogonal matrix Q as a product of min(M,N) elementary reflectors.
//    tau     (OUT - vector (min(M,N))) Vector of the same numerical type as A. The scalar factors of the elementary reflectors.
//    info    (OUT - int)
//   0   : function completed normally
//   < 0 : The ith argument, where i = abs(return value) had an illegal value.
int geqrf (matrix_t& a, vector_t& tau)
{
	int              _m = int(a.size1());
	int              _n = int(a.size2());
	int              _lda = int(a.size1());
	int              _info;

	// make_sure tau's size is greater than or equal to min(m,n)
	if (int(tau.size()) < (_n<_m ? _n : _m) )
		return -104;

	int ldwork = _n*_n;
	vector_t dwork(ldwork);
	rawLAPACK::geqrf (_m, _n, a.data().begin(), _lda, tau.data().begin(), dwork.data().begin(), ldwork, _info);

	return _info;
}
예제 #14
0
 BaseTRRateMatrix::BaseTRRateMatrix (vector_t const & rateParameters, vector_t const & equiFreqs, string const & name, bool equiFreqsFixed) 
   : BaseRateMatrix(name), rateParameterCount_(rateParameters.size() ), equiFreqCount_(equiFreqs.size() ), rateParameters_(rateParameters), equiFreqs_(equiFreqs), equiFreqsFixed_(equiFreqsFixed)
 {}
예제 #15
0
void build_morton(vector_t<PrimRef>& prims, isa::PrimInfo& pinfo)
{
  size_t N = pinfo.size();
  /* array for morton builder */
  vector_t<isa::MortonID32Bit> morton_src(N);
  vector_t<isa::MortonID32Bit> morton_tmp(N);
  for (size_t i=0; i<N; i++) 
    morton_src[i].index = i;

  /* fast allocator that supports thread local operation */
  FastAllocator allocator;

  for (size_t i=0; i<2; i++)
  {
    std::cout << "iteration " << i << ": building BVH over " << N << " primitives, " << std::flush;
    double t0 = getSeconds();
    
    allocator.reset();

    std::pair<Node*,BBox3fa> node_bounds = isa::bvh_builder_morton<Node*>(

      /* thread local allocator for fast allocations */
      [&] () -> FastAllocator::ThreadLocal* { 
        return allocator.threadLocal(); 
      },

      BBox3fa(empty),

      /* lambda function that allocates BVH nodes */
      [&] ( isa::MortonBuildRecord<Node*>& current, isa::MortonBuildRecord<Node*>* children, size_t N, FastAllocator::ThreadLocal* alloc ) -> InnerNode*
      {
        assert(N <= 2);
        InnerNode* node = new (alloc->malloc(sizeof(InnerNode))) InnerNode;
        *current.parent = node;
        for (size_t i=0; i<N; i++) 
          children[i].parent = &node->children[i];
        return node;
      },

      /* lambda function that sets bounds */
      [&] (InnerNode* node, const BBox3fa* bounds, size_t N) -> BBox3fa
      {
        BBox3fa res = empty;
        for (size_t i=0; i<N; i++) {
          const BBox3fa b = bounds[i];
          res.extend(b);
          node->bounds[i] = b;
        }
        return res;
      },

      /* lambda function that creates BVH leaves */
      [&]( isa::MortonBuildRecord<Node*>& current, FastAllocator::ThreadLocal* alloc, BBox3fa& box_o) -> Node*
      {
        assert(current.size() == 1);
        const size_t id = morton_src[current.begin].index;
        const BBox3fa bounds = prims[id].bounds(); // FIXME: dont use morton_src, should be input
        Node* node = new (alloc->malloc(sizeof(LeafNode))) LeafNode(id,bounds);
        *current.parent = node;
        box_o = bounds;
        return node;
      },

      /* lambda that calculates the bounds for some primitive */
      [&] (const isa::MortonID32Bit& morton) -> BBox3fa {
        return prims[morton.index].bounds();
      },

      /* progress monitor function */
      [&] (size_t dn) { 
        // throw an exception here to cancel the build operation
      },

      morton_src.data(),morton_tmp.data(),prims.size(),2,1024,1,1);

    Node* root = node_bounds.first;
    
    double t1 = getSeconds();

    std::cout << 1000.0f*(t1-t0) << "ms, " << 1E-6*double(N)/(t1-t0) << " Mprims/s, sah = " << root->sah() << " [DONE]" << std::endl;
  }
}
예제 #16
0
vector_eig EigenUtil::ToEigenVec(const vector_t &mat) {
  return vector_eig::Map(mat.data(), mat.size());
}
예제 #17
0
 void BaseTRRateMatrix::resetRateParameters(vector_t const & rateParameters) 
 {
   assert(rateParameters.size() == rateParameterCount_);
   rateParameters_ = rateParameters;
 }
예제 #18
0
 // additional functionality:
 size_type size() const {
     return stack.size();
 }
예제 #19
0
 vector_t cauchy_loss_t::vgrad(const vector_t& targets, const vector_t& scores) const
 {
         assert(targets.size() == scores.size());
         
         return 2.0 * (scores - targets).array() / (1.0 + (scores - targets).array().square());
 }
예제 #20
0
 void BaseTRRateMatrix::resetEquiFreqs(vector_t const & equiFreqs) 
 {
   assert(not equiFreqsFixed() );
   assert(equiFreqs.size() == equiFreqCount_);
   equiFreqs_ = equiFreqs;
 }
예제 #21
0
 scalar_t cauchy_loss_t::error(const vector_t& targets, const vector_t& scores) const
 {
         assert(targets.size() == scores.size());
         
         return (targets - scores).array().abs().sum();
 }
예제 #22
0
 size_type size() const
 {
     return data_.size();
 }
예제 #23
0
 scalar_t cauchy_loss_t::value(const vector_t& targets, const vector_t& scores) const
 {
         assert(targets.size() == scores.size());
         
         return ((targets - scores).array().square() + 1.0).log().sum();
 }