int main()
{
    size_t dx = 2;
    size_t dim = dx;
        
    struct OpeOpts * opts = ope_opts_alloc(LEGENDRE);
    ope_opts_set_start(opts,3);
    ope_opts_set_coeffs_check(opts,3);
    ope_opts_set_maxnum(opts,3);
    struct OneApproxOpts * qmopts = one_approx_opts_alloc(POLYNOMIAL,opts);    
    struct C3Approx * c3a = c3approx_create(CROSS,dim);

    int verbose = 1;
    size_t init_rank = 4;
    double ** start = malloc_dd(dim);
    for (size_t ii = 0; ii < dim; ii++){
        c3approx_set_approx_opts_dim(c3a,ii,qmopts);
        start[ii] = linspace(-1.0,1.0,init_rank);
    }
    c3approx_init_cross(c3a,init_rank,verbose,start);
    c3approx_set_cross_tol(c3a,1e-4);
    c3approx_set_round_tol(c3a,1e-4);
    
    
    size_t counter = 0;
    struct Fwrap * fw = fwrap_create(dx,"general");
    fwrap_set_f(fw,doug,&counter);
    
    struct FunctionTrain * f = c3approx_do_cross(c3a,fw,1);
    free_dd(dim,start);
    one_approx_opts_free_deep(&qmopts);
    c3approx_destroy(c3a);
    fwrap_destroy(fw);

    printf("ranks are "); iprint_sz(3,f->ranks);
    printf("Number of evaluations is %zu\n",counter);
    
    double derr = 0.0;
    double dden = 0.0;
    size_t N = 100;
    double * xtest = linspace(-1.0,1.0,N);
    size_t ii,jj;
    double x[2];
    size_t counter2 = 0;
    for (ii = 0; ii < N; ii++){
        for(jj = 0; jj < N; jj++){
            x[0] = xtest[ii];
            x[1] = xtest[jj];
            double tval = doug(x,&counter2);
            double aval = function_train_eval(f,x);
            derr += pow(tval-aval,2);
            dden += pow(tval,2);
        }
    }
    printf("L2 error for displacement is %G\n",derr/dden);

    function_train_free(f);

    return 0;
}
Exemple #2
0
void Michalewicz::runProblem()
{
    double pi = atan(1)*4;

    std::vector<VariablePtr> vars = {
        std::make_shared<Variable>(0, 0, pi),
        std::make_shared<Variable>(0, 0, pi),
        std::make_shared<Variable>(1)
    };

    // Set starting points
    vars.at(0)->setValue(1.0);
    vars.at(1)->setValue(1.0);

    DataTable data;

    unsigned int nums = 10; // 60x60 yields is sufficient to model function around optimum
    auto x1 = linspace(0, 4, nums);
    auto x2 = linspace(0, 4, nums);

    for (auto x1i : x1)
    {
        for (auto x2i : x2)
        {
            DenseVector xd(2); xd << x1i, x2i;
            double yd = michalewiczFunction(xd);
            data.addSample(xd, yd);
        }
    }

    // Test accuracy of B-spline
//    DenseVector (*foo)(DenseVector);
//    foo = &michalewiczFunction;
//    BSpline* bs = new BSpline(*data, 3);
//    bool testpassed = bs->testBspline(foo);
//    if (testpassed)
//    {
//        cout << "B-spline is very accurate:)" << endl;
//    }
//    else
//    {
//        cout << "B-spline is NOT very accurate:(" << endl;
//    }

    BSpline bs = BSpline::Builder(data).degree(3).build();
    auto constraint = std::make_shared<ConstraintBSpline>(vars, bs, false);

    //SolverIpopt solver(constraint);
    BB::BranchAndBound solver(constraint);

    // Optimize
    SolverResult result = solver.optimize();

    cout << result << endl;

    fopt_found = result.objectiveValue;
    zopt_found = result.primalVariables;

    cout << zopt_found << endl;
}
/* Almacena en matrices de la misma dimensión que la malla MixMi */
int meshgrid_imaginario(float ** xxpr, float ** yypr, complex double ** tt, float * dperfil)
{
	int i, j;

	int xmax = -dperfil[15]+dperfil[16]+1.25*dperfil[14];  //Rango dinamico dependiendo de las dimensiones del perfil elegido
	int ymax = -dperfil[17]+dperfil[18]+1.15*dperfil[14];

	float * rangox;
	rangox = (float *) malloc(Mi * sizeof(float));

	float * rangoy;
	rangoy = (float *) malloc(Mi * sizeof(float));

	linspace(rangox, -xmax, xmax, Mi);
	linspace(rangoy, -ymax, ymax, Mi);

	for (i = 0; i < Mi; i++) //Matriz XXpr
	{
		xxpr[i] = rangox;
	}

	for (i = 0; i < Mi; i++) //Matriz YYpr
		for (j = 0; j < Mi; j++)
		{
			yypr[i][j] = rangoy[i];
		}

	for (i = 0; i < Mi; i++) //Matriz tt
		for (j = 0; j < Mi; j++)
		{
			tt[i][j] = xxpr[i][j]+yypr[i][j]*I;
		}

	return(0);
}	
bool compareBSplines(BSpline &bs, const BSpline &bs_orig)
{
    auto lb = bs.getDomainLowerBound();
    auto ub = bs.getDomainUpperBound();

    auto x0_vec = linspace(lb.at(0), ub.at(0), 10);
    auto x1_vec = linspace(lb.at(1), ub.at(1), 10);

    DenseVector x(2);
    for (auto x0 : x0_vec)
    {
        for (auto x1 : x1_vec)
        {
            x(0) = x0;
            x(1) = x1;

            double yb = bs.eval(x);
            double yb_orig = bs_orig.eval(x);
            if (std::abs(yb-yb_orig) > 1e-8)
            {
                cout << yb << endl;
                cout << yb_orig << endl;
                return false;
            }
        }
    }

    return true;
}
Exemple #5
0
MatrixFloodPlotData::MatrixFloodPlotData(const Matrix& matrix)
: m_xVector(linspace(0, matrix.size1()-1, matrix.size1())),
  m_yVector(linspace(0, matrix.size2()-1, matrix.size2())),
  m_matrix(matrix),
  m_interpMethod(NearestInterp)
{
  init();
}
Exemple #6
0
MatrixFloodPlotData::MatrixFloodPlotData(const Matrix& matrix,  QwtDoubleInterval colorMapRange)
: m_xVector(linspace(0, matrix.size1()-1, matrix.size1())),
  m_yVector(linspace(0, matrix.size2()-1, matrix.size2())),
  m_matrix(matrix),
  m_interpMethod(NearestInterp)
{
  init();
  m_colorMapRange = colorMapRange;
}
  std::vector<Point3d> IlluminanceMap_Impl::referencePoints() const
  {
    std::vector<Point3d> result;

    for (double y : linspace(0, this->yLength(), this->numberofYGridPoints())){
      for (double x : linspace(0, this->xLength(), this->numberofXGridPoints())){
        result.push_back(Point3d(x, y, 0));
      }
    }

    return result;
  }
void Test_lin_elem_exp_inner2(CuTest * tc){

    printf("Testing function: lin_elem_exp_inner (2)\n");
    
    // function
    struct Fwrap * fw1 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw1,powX2,NULL);

    struct Fwrap * fw2 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw2,TwoPowX3,NULL);

    double lb=-2.0,ub=3.0;
    
    size_t N1 = 10;
    double * p1 = linspace(lb,0.5,N1);
    double f[1000];
    fwrap_eval(N1,p1,f,fw1);

    size_t N2 = 20;
    double * p2 = linspace(0.0,ub,N2);
    double g[1000];
    fwrap_eval(N2,p2,g,fw2);
    
    le_t fa = lin_elem_exp_init(N1,p1,f);
    le_t fb = lin_elem_exp_init(N2,p2,g);
        
    double intis = lin_elem_exp_inner(fa,fb);
    double intis2 = lin_elem_exp_inner(fb,fa);

    size_t ntest = 10000000;
    double * xtest = linspace(lb,ub,ntest);
    double integral = 0.0;
    for (size_t ii = 0; ii < ntest; ii++){
        integral += (LINELEM_EVAL(fa,xtest[ii]) *
                     LINELEM_EVAL(fb,xtest[ii]));
    }
    integral /= (double) ntest;
    integral *= (ub - lb);
    double intshould = integral;
    double diff = fabs(intshould-intis)/fabs(intshould);
    CuAssertDblEquals(tc, 0.0, diff, 1e-6);
    CuAssertDblEquals(tc,intis,intis2,1e-15);
    free(xtest);

    LINELEM_FREE(fa);
    LINELEM_FREE(fb);
    free(p1);
    free(p2);
    fwrap_destroy(fw1);
    fwrap_destroy(fw2);
    
}
void Test_lin_elem_exp_axpy2(CuTest * tc){

    printf("Testing function: lin_elem_exp_axpy (2) \n");

        // function
    struct Fwrap * fw1 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw1,powX2,NULL);

    struct Fwrap * fw2 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw2,TwoPowX3,NULL);

    double lb=-2.0, ub=1.0;
    
    size_t N1 = 302;
    double * x1 = linspace(lb,0.2,N1);
    double f1[1000];
    fwrap_eval(N1,x1,f1,fw2);

    size_t N2 = 20;
    double * x2 = linspace(-0.15,ub,N2);
    double f2[1000];
    fwrap_eval(N2,x2,f2,fw1);

    le_t le1 = lin_elem_exp_init(N1,x1,f1);
    le_t le2 = lin_elem_exp_init(N2,x2,f2);
    le_t le3 = lin_elem_exp_copy(le2);

    int success = lin_elem_exp_axpy(2.0,le1,le3);
    CuAssertIntEquals(tc,0,success);
    
    size_t N = 200;
    double * pts = linspace(lb-0.5,ub+0.5,N);
    size_t ii;
    for (ii = 0; ii < N; ii++){
        double eval1 = LINELEM_EVAL(le3,pts[ii]);
        double eval2 = 2.0 * LINELEM_EVAL(le1,pts[ii]) +
            LINELEM_EVAL(le2,pts[ii]);
        double diff= fabs(eval1-eval2);
        CuAssertDblEquals(tc, 0.0, diff, 4e-15);
    }
    free(pts); pts = NULL;

    
    free(x1); x1 = NULL;
    free(x2); x2 = NULL;
    LINELEM_FREE(le1);
    LINELEM_FREE(le2);
    LINELEM_FREE(le3);
    fwrap_destroy(fw1);
    fwrap_destroy(fw2);

}
Exemple #10
0
int main(){

	std::cout << "linspace(1,10,10):" << std::endl;
	std::cout << linspace(1,10,10) << std::endl;

	std::cout << "linspace(1,10,9):" << std::endl;
	std::cout << linspace(1,10,9) << std::endl;
	
	std::cout << "linspace(0,1,20):" << std::endl;
	std::cout << linspace(0,1,20) << std::endl;

	std::cout << "linspace(-5,5,19):" << std::endl;
	std::cout << linspace(-5,5,19) << std::endl;
	return 0;
}
Exemple #11
0
    void DisplayProblem2D::prepareContourPlot()
    {
      cX=linspace(0,1,c_points);
      cY=linspace(0,1,c_points);
      
      for(int i=0;i<c_points;++i)
	{
	  for(int j=0;j<c_points;++j)
	    {
	      vectord q(2);
	      q(0) = cX[j]; q(1) = cY[i];
	      cZ[i][j]= bopt_model->evaluateSample(q);
	    }
	}
    }
Exemple #12
0
vec sqtrain(const vec &inDB, int SIZE)
{
  vec  DB(inDB);
  vec  Levels, Levels_old;
  ivec indexlist(SIZE + 1);
  int  il, im, ih, i;
  int  SIZEDB = inDB.length();
  double x;

  sort(DB);
  Levels = DB(round_i(linspace(0.01 * SIZEDB, 0.99 * SIZEDB, SIZE)));
  Levels_old = zeros(SIZE);

  while (energy(Levels - Levels_old) > 0.0001) {
    Levels_old = Levels;
    for (i = 0;i < SIZE - 1;i++) {
      x = (Levels(i) + Levels(i + 1)) / 2;
      il = 0;
      ih = SIZEDB - 1;
      while (il < ih - 1) {
        im = (il + ih) / 2;
        if (x < DB(im)) ih = im;
        else il = im;
      }
      indexlist(i + 1) = il;
    }
    indexlist(0) = -1;
    indexlist(SIZE) = SIZEDB - 1;
    for (i = 0;i < SIZE;i++) Levels(i) = mean(DB(indexlist(i) + 1, indexlist(i + 1)));
  }
  return Levels;
}
Exemple #13
0
int main() {
	int i;
	int n;
	double h;
	double x[N];
	double xi, xf;
	double y[N] = {-310.0, -179.8, -82.3, -13.6, 30.0, 52.6, 57.8, 49.6, 31.8, 8.2, -17.2, -40.8, -58.6, -66.8, -61.5, -39.0, 4.6, 73.3, 170.8, 301.0};
	double xint[2*N], yint[2*N];
	/* Linear Interpolation */
	xi = -6.0; /* first value of x */
	xf = 7.0; /* last value */
	linspace(x, xi, xf, N);
	printf("Discrete Points for Cubic Spline Interpolation\n");
	printf(" i x y\n");
	for(i = 0; i < N; i++)
		printf("%3d %25.17e %25.17e\n", i, x[i], y[i]);
	printf("\n");
	/* Evaluation */
	h = 0.45; // x increment
	n = arraycol(xint, -5.0, xf, h); /* fill new array */
	csinterp(x, y, xint, yint, N, n);
	printf(" x interpolated y \n");
	for(i = 0; i < n; i++)
		printf("%25.17e %25.17e \n", xint[i], yint[i]);
	return 0;
}
void Test_lin_elem_exp_norm(CuTest * tc){
    
    printf("Testing function: lin_elem_exp_norm\n");
    
    // function
    struct Fwrap * fw1 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw1,powX2,NULL);

    double lb=-2.0,ub=3.0;
    size_t N = 1000;
    double * x = linspace(lb,ub,N);
    double f[1000];
    fwrap_eval(N,x,f,fw1);
    
    le_t fa = lin_elem_exp_init(N,x,f);

    double intshould = (pow(ub,5) - pow(lb,5))/5;
    double intis = lin_elem_exp_norm(fa);
    double diff = fabs(sqrt(intshould) - intis)/fabs(sqrt(intshould));
    CuAssertDblEquals(tc, 0.0, diff, 1e-6);

    free(x); x = NULL;
    LINELEM_FREE(fa);
    fwrap_destroy(fw1);
}
Exemple #15
0
rvec logspace( double a, double b, int N )
{
	double alog = log(a);
	double blog = log(b);
	rvec xlog   = linspace( alog, blog, N );
	return exp(xlog);
}
int main() {
  double * xs = linspace(-1, 1, 5);
  print_array(xs, 5);
  delete [] xs;

  return 0;
}
Exemple #17
0
void computeWeights(const cv::Mat &imagePatch, const spatiogram &qTarget, const spatiogram &pCurrent,
                    const cv::Mat &w, cv::Mat &weights){

    CV_Assert(qTarget.bins==pCurrent.bins);
    cv::Mat sqco;
    cv::divide(qTarget.cd,pCurrent.cd,sqco);
    cv::sqrt(sqco, sqco);
    cv::Mat rel = w.mul( sqco );
    std::vector<cv::Mat> weightC;
    cv::Mat tc =cv::Mat::zeros(imagePatch.rows, imagePatch.cols, CV_64FC1);

    int n = 256/qTarget.bins;
    std::vector<double> bins;
    linspace(bins, 0, 256, n);
    int m = pCurrent.bins/imagePatch.channels();
    for (int l=0; l<imagePatch.channels(); l++){
        for (int j=0; j<m; j++){
            cv::Mat temp;
            binelements(imagePatch, bins, l, j, temp);
            tc = tc + (rel.at<double>(0,l*m+j))*temp;
        }
        weightC.push_back(qTarget.C*tc);
    }
    mat3min( weightC, weights );
    //weights=weightC[0];
}
Exemple #18
0
/* reparameterize srvf q by gamma */
void group_action_by_gamma(int *n1, int *T1, double *q, double *gam, double *qn){
    int T = *T1, n = *n1;
	int n2 = 1;
    double dt = 1.0/T, max=1, min=0;
    int j, k;
    double val;
    double gammadot[T], ti[T], tmp[T], tmp1[T];
    double *gammadot_ptr, *time_ptr, *tmp_ptr, *tmp1_ptr;

    time_ptr = ti;
    linspace(min, max, T, time_ptr);
    gammadot_ptr = gammadot;
    gradient(T1,&n2,gam,&dt,gammadot_ptr);

    for (k=0; k<n; k++){
		tmp_ptr = tmp;
		tmp1_ptr = tmp1;
        for (j=0; j<T; j++)
            tmp[j] = q[n*j+k];
        spline(T, time_ptr, tmp_ptr, T, gam, tmp1_ptr);
        for (j=0; j<T; j++)
            qn[n*j+k] = tmp1[j]* sqrt(gammadot[j]);

    }

    val = innerprod_q2(T1, qn, qn);

    for (k=0; k<T*n; k++)
        qn[k] = qn[k] / sqrt(val);

    return;
}
Exemple #19
0
void CrankNicolson(vec &v, int Nx, int Nt, double dx, double dt){
    double d1 = dt/(dx*dx);
    double d2 = 2-2*d1;
    double d3 = 2+2*d1;

    vec a = -d1*ones(Nx);
    vec b = d3*ones(Nx);
    vec c = a;

    vec vv     = v;
    mat sol    = zeros(Nx,Nt);
    vec us     = 1-linspace(0,1,Nx);
    sol.col(0) = v+us; // Save real solution u = v + us

    for (int j=1; j<Nt; j++){
        for (int i=1; i<Nx-1; i++){
            vv(i) = d1*v(i-1) + d2*v(i) + d1*v(i+1);
            cout << "j=" << j << ", i=" << i << endl;

        }
        v = TriDiag(a, b, c, vv, Nx);
        sol.col(j) = v+us;
    }
    sol.save("CrankNicolson.dat",raw_ascii);

}
void Test_lin_elem_exp_orth_basis(CuTest * tc)
{
    printf("Testing function: lin_elem_exp_orth_basis\n");
    double lb = -2.0;
    double ub = 0.2;
    size_t N = 100;
    double * x = linspace(lb,ub,N);
    struct LinElemExpAopts * opts = lin_elem_exp_aopts_alloc(N,x);
    
    /* double * coeff = calloc_double(N); */
    le_t f[100];
    for (size_t ii = 0; ii < N; ii++){
        f[ii] = NULL;// lin_elem_exp_init(N,x,coeff);
    }

    lin_elem_exp_orth_basis(N,f,opts);
    for (size_t ii = 0; ii < N; ii++){
        for (size_t jj = 0; jj < N; jj++){
            double val = lin_elem_exp_inner(f[ii],f[jj]);
            if (ii == jj){
                CuAssertDblEquals(tc,1.0,val,1e-15);
            }
            else{
                CuAssertDblEquals(tc,0.0,val,1e-15);
            }
        }
    }

    for (size_t ii = 0; ii < N; ii++){
        LINELEM_FREE(f[ii]);
    }
    free(x); x = NULL;
    /* free(coeff); coeff = NULL; */
    lin_elem_exp_aopts_free(opts);
}
Exemple #21
0
int main()
{
	const int NP = 8; // Number of given data points
	const int M = 50; // number of computed data points
	/* const int NC = 2 ; Number of coefficients */
	double x[] = {1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002};
	double y[] = {5500, 8500, 13500, 20500, 29500, 40500, 53500, 68500};
	double coeff[NC]; // vector of coefficients
	int i;
	double xc[M]; /* computed values for x using polynomial */
	double yc[M];
	printf("Program finds best fit polynomial of degree %d \n",
		NC-1);
	printf("Data points (x,y): \n");
	for (i = 0; i < NP; i++)
		printf(" %f %f \n", x[i], y[i]);
	polynomialfit(NP, NC, x, y, coeff); /* find coefficients */
	printf("\n\nCoefficients of polynomial found\n");
	for(i=0; i < NC; i++) {
		printf("%lf\n", coeff[i]);
	}
	/* Evaluate the fitted polynomial */
	linspace(xc, 0.0, 12.0, M);
	polyval(coeff, xc, yc, NC, M);
	printf("\nData points calculated with the polynomial \n");
	for(i=0; i < M; i++)
		printf("%d %+.18f %+.18f \n", i, xc[i], yc[i]);
	return 0;
} /* end main */
Exemple #22
0
Array<T> radon(const Array<T>& A, const Array<T>& xi, const Array<T>& gamma)
{
  size_t L = 256;

  size_t M = size(xi);
  size_t D = size(gamma);

  Array<T> B = zeros(M, D);

  Array<T> eta = linspace(-0.5,0.5,L)*sqrt(2);

  for(size_t m=0; m<M; m++)
     for(size_t d=0; d<D; d++)
        for(size_t l=0; l<L; l++)
        {
            T x = xi(m)*cos(gamma(d))-eta(l)*sin(gamma(d));
            T y = xi(m)*sin(gamma(d))+eta(l)*cos(gamma(d));

            //std::cout << "x = " << x << " y = " << y << " val = " << _interp2d(A,x,y) << std::endl;

            B(m,d) += _interp2d(A,x,y);
        }

  return B;
}
void Test_pw_approx_nonnormal(CuTest * tc){

    printf("Testing function: piecewise_poly_approx on (a,b)\n");

    // function
    struct Fwrap * fw = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw,Sin3xTx2,NULL);

    // approximation
    double lb=-3.0, ub=2.0;
    struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub);
    size_t N = 15;
    double * pts = linspace(lb,ub,N);
    pw_poly_opts_set_pts(opts,N,pts);
    opoly_t pw = piecewise_poly_approx1(opts,fw);

    double lb1 = piecewise_poly_lb(pw->branches[0]);
    double ub1 = piecewise_poly_ub(pw->branches[0]);
    CuAssertDblEquals(tc,pts[0],lb1,1e-14);
    CuAssertDblEquals(tc,pts[1],ub1,1e-14);

    // compute error
    double abs_err;
    double func_norm;
    compute_error_vec(lb,ub,1000,pw,Sin3xTx2,NULL,&abs_err,&func_norm);
    double err = abs_err / func_norm;
    CuAssertDblEquals(tc, 0.0, err, 1e-13);

    fwrap_destroy(fw);
    POLY_FREE(pw);
    pw_poly_opts_free(opts);
    free(pts);
}
/*
 * Creates a regular knot vector of n+p+1 equidistant knots,
 * where the first and last knot is repeated p+1 times,
 * and n is the number of unique values in x.
 *
 * This knot vector can be used for all degrees > 0.
 */
std::vector<double> BSplineBasis1D::knotVectorEquidistant(std::vector<double> &X) const
{
    // Copy X -> sort -> remove duplicates -> resize = a sorted vector of unique values
    std::vector<double> uniqueX(X);
    sort(uniqueX.begin(), uniqueX.end());
    std::vector<double>::iterator it = unique_copy(uniqueX.begin(), uniqueX.end(), uniqueX.begin());
    uniqueX.resize(distance(uniqueX.begin(),it));

    // Minimum number of interpolation points
    if(uniqueX.size() < degree+1)
    {
        std::ostringstream e;
        e << "BSplineBasis1D::knotVectorFree: Only " << uniqueX.size()
          << " unique interpolation points are given. A minimum of degree+1 = " << degree+1
          << " unique points are required to build a B-spline basis of degree " << degree << ".";
        throw Exception(e.str());
    }

    std::vector<double> knots;
    for(unsigned int i = 0; i < degree; i++)
        knots.push_back(uniqueX.front());

    std::vector<double> equiKnots = linspace(uniqueX.front(), uniqueX.back(), uniqueX.size() - degree + 1);
    for(auto it = equiKnots.begin(); it != equiKnots.end(); ++it)
        knots.push_back(*it);

    for(unsigned int i = 0; i < degree; i++)
        knots.push_back(uniqueX.back());

    return knots;
}
void Test_lin_elem_exp_inner(CuTest * tc){

    printf("Testing function: lin_elem_exp_inner (1) \n");

    // function
    struct Fwrap * fw1 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw1,powX2,NULL);

    struct Fwrap * fw2 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw2,TwoPowX3,NULL);

    size_t N = 1000; double lb=-2.0,ub=3.0;
    double * x = linspace(lb,ub,N);
    double f[1000], g[1000];
    fwrap_eval(N,x,f,fw1);
    fwrap_eval(N,x,g,fw2);
    
    le_t fa = lin_elem_exp_init(N,x,f);
    le_t fb = lin_elem_exp_init(N,x,g);
        
    double intshould = (pow(ub,6) - pow(lb,6))/3;
    double intis = lin_elem_exp_inner(fa,fb);
    double diff = fabs(intshould-intis)/fabs(intshould);
    CuAssertDblEquals(tc, 0.0, diff, 1e-5);

    LINELEM_FREE(fa);
    LINELEM_FREE(fb);
    free(x);
    fwrap_destroy(fw1);
    fwrap_destroy(fw2);
    
}
Exemple #26
0
void FloodPlot::dataAutoRange()
{
  m_dataRange = m_spectrogram->data().range();
  if (m_dataRange.minValue() == m_dataRange.maxValue())
    m_dataRange = QwtDoubleInterval(m_dataRange.minValue(), m_dataRange.minValue() + 1.0);

  m_colorLevels=linspace(m_dataRange.minValue(), m_dataRange.maxValue(),m_colorMapLength);
}
Exemple #27
0
void ScatterBiasArea(int roi, float scan_width, int steps, int samples, int qi_it, float angstep)
{
	std::vector<float> u=linspace(roi/2-scan_width/2,roi/2+scan_width/2, steps);
	
	QTrkComputedConfig cfg;
	cfg.width=cfg.height=roi;
	cfg.qi_angstep_factor = angstep;
	cfg.qi_iterations = qi_it;
	cfg.qi_angular_coverage = 0.7f;
	cfg.qi_roi_coverage = 1;
	cfg.qi_radial_coverage = 1.5f;
	cfg.qi_minradius=0;
	cfg.zlut_minradius=0;
	cfg.zlut_angular_coverage = 0.7f;
	cfg.zlut_roi_coverage = 1;
	cfg.zlut_radial_coverage = 1.5f;
	cfg.zlut_minradius = 0;
	cfg.qi_minradius = 0;
	cfg.com_bgcorrection = 0;
	cfg.xc1_profileLength = roi*0.8f;
	cfg.xc1_profileWidth = roi*0.2f;
	cfg.xc1_iterations = 1;
	cfg.Update();

	ImageData lut,orglut = ReadLUTFile("10x.radialzlut#4");
	vector3f ct(roi/2,roi/2,lut.h/2 + 0.123f);
	float dx = scan_width/steps;

	QueuedCPUTracker trk(cfg);
	ResampleLUT(&trk, &orglut, orglut.h, &lut);
	int maxval = 10000;

	ImageData tmp=ImageData::alloc(roi,roi);
	GenerateImageFromLUT(&tmp, &lut, 0, cfg.zlut_maxradius, vector3f(roi/2,roi/2,lut.h/2));
	ApplyPoissonNoise(tmp, maxval);

	std::string fn = SPrintf( "sb_area_roi%d_scan%d_steps%d_qit%d_N%d", roi, (int)scan_width, steps, qi_it, samples);
	WriteJPEGFile( (fn + ".jpg").c_str(), tmp);
	tmp.free();

	fn += ".txt";
	for (int y=0;y<steps;y++)  {
		for (int x=0;x<steps;x++)
		{
			vector3f cpos( (x+0.5f-steps/2) * dx, (y+0.5f-steps/2) * dx, 0 );

			cfg.qi_iterations = qi_it;
			auto r= AccBiasTest(orglut, &trk, samples, cpos+ct, vector3f(), 0, maxval, qi_it < 0 ? LT_XCor1D : 0);
			
			float row[] = { r.acc.x, r.acc.y, r.acc.z, r.bias.x, r.bias.y, r.bias.z,  r.crlb.x, r.crlb.z, samples };
			WriteArrayAsCSVRow(fn.c_str(), row, 9, x+y>0);

			dbgprintf("X=%d,Y=%d\n", x,y);
		}
	}
	orglut.free();
	lut.free();
}
int main(int argc, char *argv[])
{
    int i, j, k;
    double Ti, Mj, percent;
    vector *T, *M;
    matrix *t, *Jij, *betaij, *output, *ttmp;

    /*
    if(argc < 3) {
        printf("Usage:\n"
               "fitcreep: <file> <t1> <t2> ... <tn>\n"
               "<file>: Filename containing the creep function data.\n"
               "<t1>: First retardation time\n"
               "<t2>: Second retardation time\n"
               "...\n"
               "<tn>: Nth retardation time.\n");
        exit(0);
    }
    */

    T = linspaceV(333, 363, 10);
    M = linspaceV(.05, .4, 10);

    ttmp = linspace(1e-3, 1e3, 1000);
    t = mtxtrn(ttmp);
    DestroyMatrix(ttmp);

    output = CreateMatrix(len(T)*len(M), 2+5);

    for(i=0; i<len(T); i++) {
        Ti = valV(T, i);
        for(j=0; j<len(M); j++) {
            Mj = valV(M, j);
            Jij = makedata(t, Ti, Mj);
            betaij = fitdata(t, Jij);

            setval(output, Ti, i*len(M)+j, 0);
            setval(output, Mj, i*len(M)+j, 1);
            setval(output, val(Jij, 0, 0), i*len(M)+j, 2);
            for(k=0; k<nRows(betaij); k++)
                setval(output, pow(val(betaij, k, 0), 2), i*len(T)+j, k+3);
            DestroyMatrix(Jij);
            DestroyMatrix(betaij);

            /* Print the percent done */
            percent = (1.*i*len(M)+j)/(len(M)*len(T))*100.;
            printf("%3.2f %%\r", percent);
            fflush(stdout);
        }
    }
    
    DestroyMatrix(t);
    DestroyVector(T);
    DestroyVector(M);
    mtxprntfilehdr(output, "output.csv", "T,M,J0,J1,tau1,J2,tau2\n");
    DestroyMatrix(output);
    return 0;
}
Exemple #29
0
void sample_radial_coord(double *r_vec, struct Params *p, double r_min=1e-4,
                         double r_max=20*1e3, int num_pt1=100, int num_pt2=100)
{
	/**The comoving radial coordinate r is assumed to be in mega parsecs
	num_pt1: 
			must be an integer divisible by 5. Then 3/5th of num_pt1 is linearly
	distributed from r0-1.5delta_r to r0+1.5delta_r1 and 1/5th is linearly
	distributed from r0-3delta_r to r0-1.5delta_r and 1/5th is linearly
	distributed from r0+1.5delta_r to r0+3delta_r
	num_pt2:
			must be divisible by 2. Half of it is logrithmicly distributed
	between r_init and r0-3delta_r and the other half between r0+3deltar_r
	and r_max
	**/
	
	assert( (num_pt1 % 5) ==0 &&"num_pt1 must be an integer divisible by 5");
	assert( (num_pt2 % 2) ==0 && "num_pt2 must be even");

	if ( (p->r0 - 3.*p->delta_r ) > 0. )
	{
		linspace(r_vec,
		         log(r_min),log(p->r0-3.*p->delta_r),num_pt2/2,false);
		for (int i = 0; i<num_pt2/2; i++)
			r_vec[i] = exp(r_vec[i]);

		linspace(&r_vec[num_pt2/2],
	         	p->r0-3.*p->delta_r,p->r0-1.5*p->delta_r,num_pt1/5,false);
	
		linspace(&r_vec[num_pt2/2+num_pt1/5],
	         	p->r0-1.5*p->delta_r,p->r0+1.5*p->delta_r,(num_pt1/5)*3,false);
	
		linspace(&r_vec[num_pt2/2+(num_pt1/5)*4],
	    	    p->r0+1.5*p->delta_r,p->r0+3.*p->delta_r,num_pt1/5,false);
	
		linspace(&r_vec[num_pt2/2+num_pt1],
	    	     log(p->r0+3.*p->delta_r),log(r_max),num_pt2/2,true);
		for(int i=num_pt2/2+num_pt1; i<num_pt2+num_pt1; i++)
			r_vec[i] = exp(r_vec[i]);
		}
	else{
		linspace(r_vec,
		         log(r_min),log(1.),num_pt2/2,false);
		for (int i=0; i<num_pt2/2; i++)
			r_vec[i] = exp(r_vec[i]);
		
		linspace(&r_vec[num_pt2/2],
		         1.,p->r0+3.*p->delta_r,num_pt1,false);
		
		linspace(&r_vec[num_pt2/2+num_pt1],
		         log(p->r0+3.*p->delta_r),log(r_max),num_pt2/2,true);
		for(int i=num_pt2/2+num_pt1; i<num_pt2+num_pt1; i++)
			r_vec[i] = exp(r_vec[i]);
		}

}
double * buildRHS(size_t N, double lb, double ub)
{
    double * x = linspace(lb,ub,N);
    size_t ii;
    for (ii = 0; ii < N; ii++){
        x[ii] = pow(x[ii],2);
    }
    return x;
}