Ejemplo n.º 1
0
    template <> void LapHessenberg(
        MatrixView<double> A, VectorView<double> Ubeta)
    {
        TMVAssert(A.iscm());
        TMVAssert(A.colsize() == A.rowsize());
        TMVAssert(Ubeta.size() == A.rowsize()-1);
        TMVAssert(A.ct()==NonConj);

        int n = A.rowsize();
        int ilo = 1;
        int ihi = n;
        int lda = A.stepj();
        int Lap_info=0;
#ifndef LAPNOWORK
        int lwork = n*LAP_BLOCKSIZE;
        double* work = LAP_DWork(lwork);
#endif
        LAPNAME(dgehrd) (
            LAPCM LAPV(n),LAPV(ilo),LAPV(ihi),
            LAPP(A.ptr()),LAPV(lda),LAPP(Ubeta.ptr())
            LAPWK(work) LAPVWK(lwork) LAPINFO);
#ifdef LAPNOWORK
        LAP_Results(Lap_info,"dgehrd");
#else
        LAP_Results(Lap_info,int(work[0]),m,n,lwork,"dgehrd");
#endif
    }
Ejemplo n.º 2
0
    template <> void LapHessenberg(
        MatrixView<std::complex<float> > A, 
        VectorView<std::complex<float> > Ubeta) 
    {
        TMVAssert(A.iscm());
        TMVAssert(A.colsize() >= A.rowsize());
        TMVAssert(Ubeta.size() == A.rowsize());
        TMVAssert(A.ct()==NonConj);

        int n = A.rowsize();
        int ilo = 1;
        int ihi = n;
        int lda = A.stepj();
        int Lap_info=0;
#ifndef LAPNOWORK
        int lwork = n*LAP_BLOCKSIZE;
        std::complex<float>* work = LAP_CWork(lwork);
#endif
        LAPNAME(cgehrd) (
            LAPCM LAPV(n),LAPV(ilo),LAPV(ihi),
            LAPP(A.ptr()),LAPV(lda),LAPP(Ubeta.ptr())
            LAPWK(work) LAPVWK(lwork) LAPINFO);
        Ubeta.ConjugateSelf();
#ifdef LAPNOWORK
        LAP_Results(Lap_info,"cgehrd");
#else
        LAP_Results(Lap_info,int(REAL(work[0])),m,n,lwork,"cgehrd");
#endif
    }
Ejemplo n.º 3
0
    static void NonBlockHessenberg(
        MatrixView<T> A, VectorView<T> Ubeta)
    {
#ifdef XDEBUG
        cout<<"Start NonBlock Hessenberg Reduction: A = "<<A<<endl;
        Matrix<T> A0(A);
#endif
        // Decompose A into U H Ut
        // H is a Hessenberg Matrix
        // U is a Unitary Matrix
        // On output, H is stored in the upper-Hessenberg part of A
        // U is stored in compact form in the rest of A along with 
        // the vector Ubeta.
        const ptrdiff_t N = A.rowsize();

        TMVAssert(A.colsize() == A.rowsize());
        TMVAssert(N > 0);
        TMVAssert(Ubeta.size() == N-1);
        TMVAssert(A.iscm() || A.isrm());
        TMVAssert(!Ubeta.isconj());
        TMVAssert(Ubeta.step()==1);

        // We use Householder reflections to reduce A to the Hessenberg form:
        T* Uj = Ubeta.ptr();
        T det = 0; // Ignore Householder det calculations
        for(ptrdiff_t j=0;j<N-1;++j,++Uj) {
#ifdef TMVFLDEBUG
            TMVAssert(Uj >= Ubeta._first);
            TMVAssert(Uj < Ubeta._last);
#endif
            *Uj = Householder_Reflect(A.subMatrix(j+1,N,j,N),det);
            if (*Uj != T(0))
                Householder_LMult(A.col(j+2,N),*Uj,A.subMatrix(0,N,j+1,N).adjoint());
        }

#ifdef XDEBUG
        Matrix<T> U(N,N,T(0));
        U.subMatrix(1,N,1,N) = A.subMatrix(1,N,0,N-1);
        U.upperTri().setZero();
        Vector<T> Ubeta2(N);
        Ubeta2.subVector(1,N) = Ubeta;
        Ubeta2(0) = T(0);
        GetQFromQR(U.view(),Ubeta2);
        Matrix<T> H = A;
        if (N>2) LowerTriMatrixViewOf(H).offDiag(2).setZero();
        Matrix<T> AA = U*H*U.adjoint();
        if (Norm(A0-AA) > 0.001*Norm(A0)) {
            cerr<<"NonBlock Hessenberg: A = "<<Type(A)<<"  "<<A0<<endl;
            cerr<<"A = "<<A<<endl;
            cerr<<"Ubeta = "<<Ubeta<<endl;
            cerr<<"U = "<<U<<endl;
            cerr<<"H = "<<H<<endl;
            cerr<<"UHUt = "<<AA<<endl;
            abort();
        }
#endif
    }
Ejemplo n.º 4
0
 void StateModel::simulate_initial_state(VectorView eta)const{
   if(eta.size() != state_dimension()){
     std::ostringstream err;
     err << "output vector 'eta' has length " << eta.size()
         << " in StateModel::simulate_initial_state.  Expected length "
         << state_dimension();
     report_error(err.str());
   }
   eta = rmvn(initial_state_mean(), initial_state_variance());
 }
Ejemplo n.º 5
0
 inline ::Eigen::Map<const ::Eigen::VectorXd, ::Eigen::Unaligned,
                     ::Eigen::InnerStride<::Eigen::Dynamic>>
 EigenMap(const VectorView &view) {
   return ::Eigen::Map<const ::Eigen::VectorXd,
                       ::Eigen::Unaligned,
                       ::Eigen::InnerStride<::Eigen::Dynamic>>(
       view.data(),
       view.size(),
       ::Eigen::InnerStride<::Eigen::Dynamic>(view.stride()));
 }
Ejemplo n.º 6
0
inline
void invnorm2( const VectorView & x ,
               const ValueView  & r ,
               const ValueView  & r_inv )
{
  Kokkos::parallel_reduce( x.dimension_0() , InvNorm2< VectorView , ValueView >( x , r , r_inv ) );
}
Ejemplo n.º 7
0
inline
void dot_neg( const VectorView & x ,
              const VectorView & y ,
              const ValueView  & r ,
              const ValueView  & r_neg )
{
  Kokkos::parallel_reduce( x.dimension_0() , DotM< VectorView , ValueView >( x , y , r , r_neg ) );
}
Ejemplo n.º 8
0
void VectorView::set_to_product(const MatrixView& m, const VectorView& v,
                                const bool transpose)
{
  CBLAS_TRANSPOSE tr;
  if (transpose){
    tr = CblasTrans;
    assert(m.cols() == length());
    assert(m.rows() == v.length());
  } else {
    tr = CblasNoTrans;
    assert(m.cols() == v.length());
    assert(m.rows() == length());
  }

  cblas_dgemv(CblasColMajor, tr, m.rows(), m.cols(), 1.0, m.data(),
              m.stride(), v.data(), 1, 0.0, data_, 1);
}
Ejemplo n.º 9
0
 double ZGS::increment_log_prior_gradient(const ConstVectorView &parameters,
                                          VectorView gradient) const {
   if (parameters.size() != 1 || gradient.size() != 1) {
     report_error(
         "Wrong size arguments passed to "
         "ZeroMeanGaussianConjSampler::increment_log_prior_gradient.");
   }
   return log_prior(parameters[0], &gradient[0], nullptr);
 }
Ejemplo n.º 10
0
    static inline void Hessenberg(
        MatrixView<T> A, VectorView<T> Ubeta)
    {
        TMVAssert(A.colsize() == A.rowsize());
        TMVAssert(Ubeta.size() == A.rowsize()-1);
        TMVAssert(A.isrm() || A.iscm());
        TMVAssert(A.ct()==NonConj);
        TMVAssert(Ubeta.step() == 1);

        if (A.rowsize() > 0) {
#ifdef LAP
            if (A.iscm()) 
                LapHessenberg(A,Ubeta);
            else 
#endif
                NonLapHessenberg(A,Ubeta);
        }
    }
Ejemplo n.º 11
0
double compute_allele_freq(const VectorView snp_genotypes)
{
  double macount = 0;
  size_t ngenos = 0;
  for (size_t i = 0; i < snp_genotypes.length(); ++i){
    if (snp_genotypes(i) >= 0){
      macount += snp_genotypes(i);
      ++ngenos;
    }
  }
  return macount / (2.0 * ngenos);
}
void LMAT::Tmult(VectorView lhs, const ConstVectorView &rhs)const {
    if(lhs.size()!=3) {
        report_error("lhs is the wrong size in LMAT::Tmult");
    }
    if(rhs.size()!=3) {
        report_error("rhs is the wrong size in LMAT::Tmult");
    }
    lhs[0] = rhs[0];
    double phi = phi_->value();
    lhs[1] = rhs[0] + phi * rhs[1];
    lhs[2] = (1-phi) * rhs[1] + rhs[2];
}
// TODO(stevescott):  test
void ASSR::simulate_initial_state(VectorView state0)const {
    // First, simulate the initial state of the client state vector.
    VectorView client_state(state0, 0, state0.size()-2);
    StateSpaceModelBase::simulate_initial_state(client_state);

    // Next simulate the initial value of the first latent weekly
    // observation.
    double mu = StateSpaceModelBase::observation_matrix(0).dot(client_state);
    state0[state_dimension() - 2] = rnorm(mu, regression_->sigma());

    // Finally, the initial state of the cumulator variable is zero.
    state0[state_dimension() - 1] = 0;
}
Ejemplo n.º 14
0
    void MultMV(
        const T alpha, const GenDiagMatrix<Ta>& A, const GenVector<Tx>& x,
        VectorView<T> y)
        // y (+)= alpha * A * x 
        // yi (+)= alpha * Ai * xi
    {
        TMVAssert(A.size() == x.size());
        TMVAssert(A.size() == y.size());
#ifdef XDEBUG
        //cout<<"MultMV: \n";
        //cout<<"alpha = "<<alpha<<endl;
        //cout<<"A = "<<TMV_Text(A)<<"  "<<A<<endl;
        //cout<<"x = "<<TMV_Text(x)<<"  "<<x<<endl;
        //cout<<"y = "<<TMV_Text(y)<<"  "<<y<<endl;
        Vector<T> y0 = y;
        Vector<Tx> x0 = x;
        Matrix<Ta> A0 = A;
        Vector<T> y2 = alpha*A0*x0;
        if (add) y2 += y0;
#endif

        ElemMultVV<add>(alpha,A.diag(),x,y); 

#ifdef XDEBUG
        if (!(Norm(y-y2) <=
              0.001*(TMV_ABS(alpha)*Norm(A0)*Norm(x0)+
                     (add?Norm(y0):TMV_RealType(T)(0))))) {
            cerr<<"MultMV: alpha = "<<alpha<<endl;
            cerr<<"add = "<<add<<endl;
            cerr<<"A = "<<TMV_Text(A)<<" step "<<A.diag().step()<<"  "<<A0<<endl;
            cerr<<"x = "<<TMV_Text(x)<<" step "<<x.step()<<"  "<<x0<<endl;
            cerr<<"y = "<<TMV_Text(y)<<" step "<<y.step()<<"  "<<y0<<endl;
            cerr<<"-> y = "<<y<<endl;
            cerr<<"y2 = "<<y2<<endl;
            abort();
        }
#endif
    }
Ejemplo n.º 15
0
    static inline void NonLapHessenberg(
        MatrixView<T> A, VectorView<T> Ubeta)
    {
        TMVAssert(A.rowsize() == A.colsize());
        TMVAssert(A.rowsize() > 0);
        TMVAssert(Ubeta.size() == A.rowsize()-1);

#if 0
        if (A.rowsize() > HESS_BLOCKSIZE)
            BlockHessenberg(A,Ubeta,Vbeta,D,E,det);
        else
#endif
            NonBlockHessenberg(A,Ubeta);
    }
Ejemplo n.º 16
0
/****************************************************************************
*
*   StreamInfo1::Configure( )
*
*   Set configuration of stream information.
*
****************************************************************************/
bool StreamInfo1::Configure
(
    VectorView& sieves,                     // vector of sieve sizes
    std::vector<PMineralInfo1>& minerals,   // collection of minerals
    double liquidSG                         // density of liquid phase
)
{
    // ensure reasonable number of sieves
    if( sieves.size() <= 2 )
        goto initFailed;

    // ensure reasonable number of minerals
    if( minerals.size() < 1 )
        goto initFailed;

    // set implementation to supplied size distribution
    nSize_ = sieves.size();
    sizes_.resize( nSize_ );
    sizes_ = sieves;

    // set implementation to supplied mineral collection
    nType_ = static_cast<long>( minerals.size() );
    minerals_.clear( );
    minerals_.assign( minerals.begin(), minerals.end() );

    // should probably check for NULL
    //  pointer entries in minerals_

    // succeeded
    return true;

initFailed:

    // Initialization failed - object should not be used
    return false;
}
Ejemplo n.º 17
0
 void DRSM::increment_expected_gradient(
     VectorView gradient, int t, const ConstVectorView &state_error_mean,
     const ConstSubMatrix &state_error_variance) {
   if (gradient.size() != xdim_ || state_error_mean.size() != xdim_ ||
       state_error_variance.nrow() != xdim_ ||
       state_error_variance.ncol() != xdim_) {
     report_error(
         "Wrong size arguments passed to "
         "DynamicRegressionStateModel::increment_expected_gradient.");
   }
   for (int i = 0; i < xdim_; ++i) {
     double mean = state_error_mean[i];
     double var = state_error_variance(i, i);
     double sigsq = DynamicRegressionStateModel::sigsq(i);
     ;
     double tmp = (var + mean * mean) / (sigsq * sigsq) - 1.0 / sigsq;
     gradient[i] += .5 * tmp;
   }
 }
Ejemplo n.º 18
0
 void swap(VectorView<T>& in_oLHS, VectorView<T>& in_oRHS) { in_oLHS.swap(in_oRHS); }
Ejemplo n.º 19
0
 void DynamicRegressionStateModel::simulate_state_error(VectorView eta, int t)const{
   check_size(eta.size());
   for (int i = 0; i < eta.size(); ++i) {
     eta[i] = rnorm(0, coefficient_transition_model_[i]->sigma());
   }
 }
Ejemplo n.º 20
0
    static void BlockHessenberg(
        MatrixView<T> A, VectorView<T> Ubeta)
    {
        // Much like the block version of Bidiagonalize, we try to maintain
        // the operation of several successive Householder matrices in
        // a block form, where the net Block Householder is I - YZYt.
        //
        // But as with the bidiagonlization algorithm (and unlike a simple
        // block QR decomposition), we update the matrix from both the left 
        // and the right, so we also need to keep track of the product
        // ZYtm in addition.
        //
        // The block update at the end of the block loop is
        // m' = (I-YZYt) m (I-YZtYt)
        //
        // The Y matrix is stored in the first K columns of m,
        // and the Hessenberg portion of these columns is updated as we go.
        // For the right-hand-side update, m -= mYZtYt, the m on the right
        // needs to be the full original matrix m, including the original
        // versions of these K columns.  Therefore, we can't wait until 
        // the end for this calculation.  
        //
        // Instead, we keep track of mYZt as we progress, so the final update
        // is:
        //
        // m' = (I-YZYt) (m - mYZt Y)
        //
        // We also need to do this same calculation for each column as we
        // progress through the block.
        //
        const ptrdiff_t N = A.rowsize();

#ifdef XDEBUG
        Matrix<T> A0(A);
#endif

        TMVAssert(A.rowsize() == A.colsize());
        TMVAssert(N > 0);
        TMVAssert(Ubeta.size() == N-1);
        TMVAssert(!Ubeta.isconj());
        TMVAssert(Ubeta.step()==1);

        ptrdiff_t ncolmax = MIN(HESS_BLOCKSIZE,N-1);
        Matrix<T,RowMajor> mYZt_full(N,ncolmax);
        UpperTriMatrix<T,NonUnitDiag|ColMajor> Z_full(ncolmax);

        T det(0); // Ignore Householder Determinant calculations
        T* Uj = Ubeta.ptr();
        for(ptrdiff_t j1=0;j1<N-1;) {
            ptrdiff_t j2 = MIN(N-1,j1+HESS_BLOCKSIZE);
            ptrdiff_t ncols = j2-j1;
            MatrixView<T> mYZt = mYZt_full.subMatrix(0,N-j1,0,ncols);
            UpperTriMatrixView<T> Z = Z_full.subTriMatrix(0,ncols);

            for(ptrdiff_t j=j1,jj=0;j<j2;++j,++jj,++Uj) { // jj = j-j1

                // Update current column of A
                //
                // m' = (I - YZYt) (m - mYZt Yt)
                // A(0:N,j)' = A(0:N,j) - mYZt(0:N,0:j) Y(j,0:j)t
                A.col(j,j1+1,N) -= mYZt.Cols(0,j) * A.row(j,0,j).Conjugate();
                //
                // A(0:N,j)'' = A(0:N,j) - Y Z Yt A(0:N,j)'
                // 
                // Let Y = (L)     where L is unit-diagonal, lower-triangular,
                //         (M)     and M is rectangular
                //
                LowerTriMatrixView<T> L = 
                    LowerTriMatrixViewOf(A.subMatrix(j1+1,j+1,j1,j),UnitDiag);
                MatrixView<T> M = A.subMatrix(j+1,N,j1,j);
                // Use the last column of Z as temporary storage for Yt A(0:N,j)'
                VectorView<T> YtAj = Z.col(jj,0,jj);
                YtAj = L.adjoint() * A.col(j,j1+1,j+1);
                YtAj += M.adjoint() * A.col(j,j+1,N);
                YtAj = Z.subTriMatrix(0,jj) * YtAj;
                A.col(j,j1+1,j+1) -= L * YtAj;
                A.col(j,j+1,N) -= M * YtAj;

                // Do the Householder reflection 
                VectorView<T> u = A.col(j,j+1,N);
                T bu = Householder_Reflect(u,det);
#ifdef TMVFLDEBUG
                TMVAssert(Uj >= Ubeta._first);
                TMVAssert(Uj < Ubeta._last);
#endif
                *Uj = bu;

                // Save the top of the u vector, which isn't actually part of u
                T& Atemp = *u.cptr();
                TMVAssert(IMAG(Atemp) == RealType(T)(0));
                RealType(T) Aorig = REAL(Atemp);
                Atemp = RealType(T)(1);

                // Update Z
                VectorView<T> Zj = Z.col(jj,0,jj);
                Zj = -bu * M.adjoint() * u;
                Zj = Z * Zj;
                Z(jj,jj) = -bu;

                // Update mYtZt:
                //
                // mYZt(0:N,j) = m(0:N,0:N) Y(0:N,0:j) Zt(0:j,j)
                //             = m(0:N,j+1:N) Y(j+1:N,j) Zt(j,j)
                //             = bu* m(0:N,j+1:N) u 
                //
                mYZt.col(jj) = CONJ(bu) * A.subMatrix(j1,N,j+1,N) * u;

                // Restore Aorig, which is actually part of the Hessenberg matrix.
                Atemp = Aorig;
            }

            // Update the rest of the matrix:
            // A(j2,j2-1) needs to be temporarily changed to 1 for use in Y
            T& Atemp = *(A.ptr() + j2*A.stepi() + (j2-1)*A.stepj());
            TMVAssert(IMAG(Atemp) == RealType(T)(0));
            RealType(T) Aorig = Atemp;
            Atemp = RealType(T)(1);

            // m' = (I-YZYt) (m - mYZt Y)
            MatrixView<T> m = A.subMatrix(j1,N,j2,N);
            ConstMatrixView<T> Y = A.subMatrix(j2+1,N,j1,j2);
            m -= mYZt * Y.adjoint();
            BlockHouseholder_LMult(Y,Z,m);

            // Restore A(j2,j2-1)
            Atemp = Aorig;
            j1 = j2;
        }

#ifdef XDEBUG
        Matrix<T> U(N,N,T(0));
        U.subMatrix(1,N,1,N) = A.subMatrix(1,N,0,N-1);
        U.upperTri().setZero();
        U(0,0) = T(1);
        Vector<T> Ubeta2(N);
        Ubeta2.subVector(1,N) = Ubeta;
        Ubeta2(0) = T(0);
        GetQFromQR(U.view(),Ubeta2);
        Matrix<T> H = A;
        if (N>2) LowerTriMatrixViewOf(H).offDiag(2).setZero();
        Matrix<T> AA = U*H*U.adjoint();
        if (Norm(A0-AA) > 0.001*Norm(A0)) {
            cerr<<"NonBlock Hessenberg: A = "<<Type(A)<<"  "<<A0<<endl;
            cerr<<"A = "<<A<<endl;
            cerr<<"Ubeta = "<<Ubeta<<endl;
            cerr<<"U = "<<U<<endl;
            cerr<<"H = "<<H<<endl;
            cerr<<"UHUt = "<<AA<<endl;
            Matrix<T,ColMajor> A2 = A0;
            Vector<T> Ub2(Ubeta.size());
            NonBlockHessenberg(A2.view(),Ub2.view());
            cerr<<"cf NonBlock: A -> "<<A2<<endl;
            cerr<<"Ubeta = "<<Ub2<<endl;
            abort();
        }
#endif
    }
Ejemplo n.º 21
0
/****************************************************************************
*
*   WhitenCrusher1::Intialize( )
*
*   Configure WhitenCrusher1 model.
*
****************************************************************************/
bool WhitenCrusher1::Initialize
( 
 PStreamInfo1      Config,
 const VectorView&  ParamVec
 )
{
	// Test object arguments

	if( Config==0 ) goto initFailed;

	// Test size of parameter vector

	if( ParamVec.size() < 26 ) goto initFailed;

	// Set parameters

	config_		= Config;

	CSS		    = ParamVec[ 0];
	LLen	    = ParamVec[ 1];
	ET		    = ParamVec[ 2];
	LHr		    = ParamVec[ 3];
	MotorPower  = ParamVec[ 4];
	NoLoadPower = ParamVec[ 5];
	a0			= ParamVec[ 6];
	a1			= ParamVec[ 7];
	a2			= ParamVec[ 8];
	a3			= ParamVec[ 9];	   
	a4			= ParamVec[10];
	b0			= ParamVec[11];
	b1			= ParamVec[12];
	b2			= ParamVec[13];
	b3			= ParamVec[14];
	b4			= ParamVec[15];
	b5			= ParamVec[16];
	c0			= ParamVec[17];
	d0			= ParamVec[18];
	d1			= ParamVec[19];
	d2			= ParamVec[20];
	e0			= ParamVec[21];
	e1			= ParamVec[22];
	f0			= ParamVec[23];
	f1			= ParamVec[24];
	f2			= ParamVec[25];

	// Retrieve stream model dimensions
	nSize_ = config_->nSize();
	nType_ = config_->nType();

	// Test that product stream is valid
	if( Discharge==0 )
		goto initFailed;

	// Configure Discharge stream
	if( !Discharge->SetConfig(config_) )
		goto initFailed;

	// Resize internal vectors

	C_.resize( nSize_ );
	T10.resize( nType_ );
	nomSize_.resize( nSize_ );

	Content_.resize( nSize_ );
	BreakFeed_.resize( nSize_ );
	BreakProd_.resize( nSize_ );

	ModelOutput.resize( 11 );

	return true;

initFailed:

	return false;
}
Ejemplo n.º 22
0
 // static methods
 static double dotproduct(const VectorView &v1,
                              const VectorView &v2)
 {
   assert(v1.length() == v2.length());
   return cblas_ddot(v1.length(), v1.data_, 1, v2.data_, 1);
 }
Ejemplo n.º 23
0
 SpdMatrix outer(const VectorView &v){
   SpdMatrix ans(v.size(), 0.0);
   ans.add_outer(v);
   return ans; }
Ejemplo n.º 24
0
double RioTintoTS::SPNV3
(
    int         N,
    VectorView  X,
    VectorView  Y,
    VectorView& S,
    double      XX
)
{
	double XIXI1 = 0.0;
	double DX    = 0.0;
	double XXI   = 0.0;
	double YY    = 0.0;
	double T     = 0.0;

    // Reverse vectors if X descending order

    if( X[N-1] < X[0] )
    {
        X.reset( X.reverse() );
        Y.reset( Y.reverse() );
    }

	// Find interval containing XX

	if( XX < X[0] )
	{
		// before X[0]

		XIXI1 = X[0] - X[1];
		DX    = (Y[0]-Y[1])/(XIXI1+1e-30) + XIXI1*(S[1]+2*S[0])/6.0;
		XXI   = XX - X[0];

		YY = Y[0] + XXI * ( DX + XXI*S[0] );
	}
	else if( XX > X[N-1] )
	{
		// after X[N-1]

		XIXI1 = X[N-1] - X[N-2];
		DX    = (Y[N-1]-Y[N-2])/(XIXI1+1e-30) + XIXI1*(2*S[N-1]+S[N-2])/6.0;
		XXI	 = XX - X[N-1];

		YY = Y[N-1] + XXI * ( DX + XXI*S[N-1] );
	}
	else
	{
		int i     = 0;
		int left  = 0;
		int right = N-1;
		int mid   = 0;

		while( XX>=X[left] && XX<=X[right] )
		{
			mid = left + (right-left)/2;

			if( mid==left )
			{
				i = left;
				break;
			}
			else if( XX < X[mid] )
			{
				right = mid;
			}
			else
			{
				left = mid;
			}
		}

		// between X[i] and X[i+1]

        double x1 = X[i];
        double x2 = X[i+1];
        double y1 = Y[i];
        double y2 = Y[i+1];


		XXI   = XX   - X[i];
		XIXI1 = X[i] - X[i+1];
		T     = 2.0*S[i] + XXI*(S[i]-S[i+1])/(XIXI1+1e-30) + S[i+1];

		YY = Y[i] + XXI*(Y[i]-Y[i+1])/(XIXI1+1e-30) + (XX-X[i+1])*T/6.0;
	}

	return YY;
}
Ejemplo n.º 25
0
bool CubicSpline::SetSpline( VectorView& X, VectorView& Y, bool logX, bool logY )
{
    int i,k;
    double p,sig;

    // determine element count
    N_ = X.size();

    // test that X and Y same size
    if( N_ < 2 || N_ > 100 ||  N_ != Y.size() )
        goto initFailed;

    // dimension structures for N elements
    X_.resize( N_ );
    Y_.resize( N_ );
    Y2_.resize( N_ );

    // put values into local X_ and Y_ vectors

    if( X[N_-1] < X[0] )
    {
        // Case 1: X_ values descending
        X_ = X.reverse();
        Y_ = Y.reverse();
    }
    else
    {
        // Caes 2: X_ values ascending
        X_ = X;
        Y_ = Y;
    }

    // convert X axis to its log if required
    
    if( logX )
    {
        logX_ = true;
        if( !X_.setLog() )
            goto initFailed;
    }

    // convert Y axis to its log if required

    if( logY )
    {
        logY_ = true;
        if( !Y_.setLog() )
            goto initFailed;
    }

    // use natural spline at boundaries

    Y2_[0]    = u[0]    = 0;
    Y2_[N_-1] = u[N_-1] = 0;

    // decomposition loop - tri-diagonal algorithm

    for( i=1; i<(N_-1); i++ )
    {
        sig    = ( X_[i] - X_[i-1] ) / ( X_[i+1] - X_[i-1] );

        p      = sig * Y2_[i-1] + 2.0;

        Y2_[i] = ( sig - 1.0 ) / p;

        u[i]   = ( Y_[i+1] - Y_[i] ) / ( X_[i+1] - X_[i] )
               - ( Y_[i] - Y_[i-1] ) / ( X_[i] - X_[i-1] );

        u[i]   = ( ( 6.0 * u[i] ) / ( X_[i+1] - X_[i-1] ) - sig * u[i-1] ) / p;
    }

    // backsubstitition loop - tri-diagonal algorithm

    for( k = N_-2; k >= 0; k -- )
    {
        Y2_[k] = Y2_[k] * Y2_[k+1] + u[k];
    }

    // SetSpline succeeded
    return true;

initFailed:

    // Set empty state
    SetEmpty( );

    // SetSpline failed
    return false;
}
Ejemplo n.º 26
0
void RioTintoTS::SPN3( int N, VectorView X, VectorView Y, VectorView& S )
{
	double DI;
	double AI;
	double AI1;
	double CI;
	double PI;
	double PI1;
	double Z;

	int	   i;
	int    j;
	int    NM1;

	static double C[100];

	if( N>2 && N<100 )
	{
        // Reverse vectors if X descending order
        if( X[N-1] < X[0] )
        {
            X.reset( X.reverse() );
            Y.reset( Y.reverse() );
        }

		// End points

		NM1    = N-1;
		S[0]   = 0;
		S[NM1] = 0;

		// Initial values

		DI = 0.0;
		AI = X[1] - X[0];
		CI = -AI * S[0];
		PI = ( Y[1] - Y[0] ) / ( AI + 1e-30 );

		// Form equations and reduce to triangular form

		for( i=1; i<NM1; i++ )
		{
			// Segment width
			AI1 = X[i+1] - X[i];
			
			// Diagonal elements
			Z = 2.0 * (AI1+AI) - DI;

			// Constant term
			PI1  = ( Y[i+1] - Y[i] ) / ( AI1 + 1e-30 );
			C[i] = ( 6.0*(PI1-PI) - CI ) / ( Z + 1e-30 );
			CI   = C[i] * AI1;

			// Upper daigonal
			S[i] = AI1 / ( Z + 1e-30 );
			DI   = S[i] * AI1;
			AI   = AI1;
		}

		// Back substitute

		for( j=(NM1-1); j>0; j-- )
		{
			S[j] = C[j] - S[j]*S[j+1];
		}
	}
}
Ejemplo n.º 27
0
EdgeSelector::EdgeSelector(const VectorView &vector) {
  SafeCall(igraph_es_vector_copy(ptr(), vector.ptr()));
}
Ejemplo n.º 28
0
Matrix::Matrix(const VectorView &elements, long int nrow)
    : Matrix(elements.cbegin(), elements.cend(), nrow, elements.size() / nrow) {
}
Ejemplo n.º 29
0
 Vector(const VectorView &v) : VectorView(NULL, v.length_mem(), v.length())
 {
   init();
   for (size_t i = 0; i < length_; ++i)
     data_[i] = v.data()[i];
 }
Ejemplo n.º 30
0
EdgeSelector EdgeSelector::FromVector(const VectorView &vector) {
  static EdgeSelector instance(igraph_ess_vector(vector.ptr()));
  return instance;
}