Example #1
0
int
PFEMElement3D::update()
{
    //  get nodal coordinates 
    Matrix A(4,4);
    for(int i=0; i<4; i++) {
        const Vector& coord = nodes[2*i]->getCrds();
        const Vector& disp = nodes[2*i]->getTrialDisp();
        A(i,0) = 1.0;
        for(int j=1; j<4; j++) {
            A(i,j) = coord(j-1) + disp(j-1);
        }
    }

    // get jacobi
    Matrix coef(4,4), sub(3,3);
    Vector jacobi(4);
    for(int i=0; i<4; i++) {
        for(int j=0; j<4; j++) {

            // sub matrix
            for(int k=0; k<4; k++) {
                if(k==i) continue;
                int k1 = k;
                if(k>i) k1--;
                for(int l=0; l<4; l++) {
                    if(l==j) continue;
                    int l1 = l;
                    if(l>j) l1--;
                    sub(k1,l1) = A(k,l);
                }
            }

            // cofactor
            int sign = 1;
            if((i+j)%2 == 1) sign = -1;
            coef(i,j) = sign*det33(sub);
            jacobi(i) += coef(i,j) * A(i,j);
        }
    }

    // opserr<<"J = "<<jacobi;
    J = jacobi(0);

    // if(fabs(J) <= 1e-6) {
    //     opserr<<"WARNING: zero Jacobian for element ";
    //     opserr<<J<<" --PFEMElement3D::update\n";
    //     //return -1;
    // }

    // get derivatives
    for(int i=0; i<4; i++) {
        dNdx[i] = coef(i,1)/J;
        dNdy[i] = coef(i,2)/J;
        dNdz[i] = coef(i,3)/J;
        //opserr<<"dNdx = "<<dNdx[i]<<"\n";
    }

    return 0;
}
Example #2
0
static int _jacobi_main_handler(int n, int nsteps) {

  double h = 1.0/n;

  // allocate and initialize arrays 
  hpx_addr_t u = hpx_gas_calloc_local_attr((n+1), BSIZE, 0, HPX_GAS_ATTR_LB);
  hpx_addr_t f = hpx_gas_alloc_local((n+1), BSIZE, 0);
  hpx_addr_t and = hpx_lco_and_new(n+1);
  for (int i = 0; i <= n; ++i) {
    double val = i*h;
    hpx_gas_memput_lsync(IDX(f,i), &val, sizeof(val), and);
  }
  hpx_lco_wait(and);
  hpx_lco_delete(and, HPX_NULL);

  printf("starting jacobi iterations...\n");

  hpx_time_t start = hpx_time_now();
  jacobi(n, nsteps, u, f);
  double elapsed = hpx_time_elapsed_ms(start)/1e3;

  // run the solver
  printf("n: %d\n", n);
  printf("nsteps: %d\n", nsteps);
  printf("seconds: %.7f\n", elapsed);

  // write the results
  if (fname) {
    write_solution(n, u, fname);
  }

  hpx_gas_free(f, HPX_NULL);
  hpx_gas_free(u, HPX_NULL);
  hpx_exit(HPX_SUCCESS);
}
Example #3
0
int main(int argc, char *argv[]) {
	int num;
	Matrix *m;
	for ( int i = 1; i < argc; i++ ) {
		num = findNumber( argv[i] );
		if ( num == -1 )
			FileNameError();

		jacobi( loadMatrix( argv[i] ), num );
		printf( " : finish!\n" );
	}

	if ( argc == 1 ) {
		printf(
			"コマンドライン引数で固有値を求めたい対称行列を入力してください.(複数可)\n"
			"入力:\n"
			"   ファイル名:sigmaxx.txt\n"
			"   中身:n*nの対称行列\n"
			"         x.x x.x x.x ... x.x\n"
			"          :               : \n"
			"         x.x x.x x.x ... x.x\n"
			"出力:\n"
			"   ファイル名:eigenxx.txt\n"
			"   中身:固有値とそれに対応する固有ベクトル(λk > λk+1)\n"
			"         λ1 v1-1 v1-2 ... v1-n\n"
			"         :                  : \n"
			"         λn vn-1 vn-2 ... vn-n\n"
		);
	}

	return 0;
}
void poisson(int n, double *grid, double th, int kmax, int choice) {

	double *grid_old = (double *)malloc( (n+2) * (n+2) * sizeof(double));
	init_array(grid_old, 10, n);

	/* double d = 1.0; */
	/* while (d > th) { */

/*    double *tmp = grid_old;
    grid_old = grid;
    grid = tmp;	*/	

    if (choice == 0) {
//        printf("Poisson calculating. Using the jacobi method.");
        jacobi(grid, grid_old, n, kmax, th);
    }
    else if (choice == 1) {
//        printf("Poisson calculating. Using the gauss method.");
        gauss(grid, grid_old, n, kmax, th);
    }
    else if(choice == 2) {
//        printf("Poisson calculating. Using the jacobi_mp method.");
        jacobi_mp(grid, grid_old, n, kmax);
    }
    else {
//        printf("Poisson calculating. Using the gauss_mp method.");
        gauss_mp(grid, n, kmax);
    }
        
		/* d = threshold(grid, grid_old, n); */
		/* printf("d %f\n", d); */

	/* } */
}
Example #5
0
bigInt getDD(const bigInt &n) // original
{
    bigInt dd = 5;
    for (bool positiveSign = true;;positiveSign ^= true)
    {
        bigInt g;
        if (positiveSign)
        {
            g = gcd (n, dd);
        }
        else
        {
            dd.changeSign();
            g = gcd (n, dd);
            dd.changeSign();
        }
        if (1 < g && g < n)
            // нашли делитель - d_abs
            return false;
        if (jacobi (dd, n) == -1)
            break;
        dd += (positiveSign ? 2 : -2);
        dd.changeSign();
    }
    return dd;
}
Example #6
0
/*
	Function for setting up and running the fluid simulation kernels
*/
void solveFluid(struct Configuration* config) { 
	// Jacobi settings
	float alpha = -1.0f;
	// Should use alpha -1 here, but this gives nicer results
	//float alpha = -(1.0f/invhalfgridscale);
	float rbeta = 0.25;
	int iterations = 100;

	// grid scaling. this is currently not used
    if(rank == 0){
        float gridscale = 1.0f;
        float invgridscale = 1.0f/gridscale;
        float invhalfgridscale = 0.5f/gridscale; 

        // Timstep value
        float timestep = 0.05f;

        // Emitter settings
        float amount = 2.0f;
        float radius = 0.5*config->N/10.0f;
        float emitterposx = config->N/2;
        float emitterposy = config->N/3;

        // buoyancy settings
        float bdiry = 1.0f;
        float bdirx = 0.0f;
        float bstrength = 0.1f;

        // advection settings
        float veldamp = 0.01f;	


        // Velocity advection
        float *tmp = config->velx0; config->velx0 = config->velx; config->velx = tmp;
        float *tmp1 = config->vely0; config->vely0 = config->vely; config->vely = tmp1;
        advect(config->N, config->velx, config->velx0, config->velx0, config->vely0, timestep, veldamp, 1);
        advect(config->N, config->vely, config->vely0, config->velx0, config->vely0, timestep, veldamp, 2);

        // Density advection
        float *tmp2 = config->dens0; config->dens0 = config->dens; config->dens = tmp2;
        advect(config->N, config->dens, config->dens0, config->velx, config->vely, timestep, 0.0f, 0);

        // Add density and density buoyancy
        addDensity(config->N, config->dens, timestep, emitterposx, emitterposy, radius, amount);
        addDensityBuoyancy(config->N, config->velx, config->vely, config->dens, bdirx, bdiry, bstrength, timestep);

        // Divergence calculation
        divergence(config->N, config->velx, config->vely, config->div);	

        // Pressure jacobi calculation. First set pres array to zero as initial guess
        setMem(config->N, config->pres);
    }

	jacobi(iterations);
	
    if(rank == 0){
        // Calculate projection
        projection(config->N, config->velx, config->vely, config->pres);	
    }
}
Example #7
0
static void gyro_eigen(double **gyr, double *eig, double **eigv, int *ord)
{
    int nrot, d;

    jacobi(gyr, DIM, eig, eigv, &nrot);
    /* Order the eigenvalues */
    ord[0] = 0;
    ord[2] = 2;
    for (d = 0; d < DIM; d++)
    {
        if (eig[d] > eig[ord[0]])
        {
            ord[0] = d;
        }
        if (eig[d] < eig[ord[2]])
        {
            ord[2] = d;
        }
    }
    for (d = 0; d < DIM; d++)
    {
        if (ord[0] != d && ord[2] != d)
        {
            ord[1] = d;
        }
    }
}
Example #8
0
int main(int argc,char* argv[]){
	int N;
	if(argc > 1){
		N = atoi(argv[1]);
	}
	else{
		N = 7;
	}

	double ** f = dmalloc_2d(N+2,N+2);
	double ** u1 = dmalloc_2d(N+2,N+2);
	double ** u2 = dmalloc_2d(N+2,N+2);
		
	initialize(u1,u2,f,N+2);	
	
	
	//print(f,N+2);
	//print(u1,N+2);
	
	if(argc>2){
		if(strcmp(argv[2],"jacobi")==0){
			u1 = jacobi(u1,u2,f,N+2,200,0.01);
		}
		else if(strcmp(argv[2],"gseidel")==0){
			u1 = gseidel(u1,f,N+2,1000,0.01);
		}
	}

	printf("Final result:\n");
	print(u1,N+2);

	return(0);	
}
Example #9
0
int main(int argc, char* argv[])
{
    int j, k, n, n2, i3, n3, iter, niter;
    float **a, *e, **v, s2;
    sf_file mat, val, eig;

    sf_init(argc,argv);
    mat = sf_input("in");
    val = sf_output("out");

    if (SF_FLOAT != sf_gettype(mat)) sf_error("Need float input");
    if (!sf_histint(mat,"n1",&n)) sf_error("No n1= in input");
    if (!sf_histint(mat,"n2",&n2) || n2 != n) sf_error("Need n1=n2 in input");
    n3 = sf_leftsize(mat,2);

    sf_putint(val,"n2",1);

    if (!sf_getint("niter",&niter)) niter=10;

    a = sf_floatalloc2(n,n);
    e = sf_floatalloc(n);

    if (NULL != sf_getstring("eig")) {
	eig = sf_output("eig"); /* eigenvectors */
	v = sf_floatalloc2(n,n);
	for (j=0; j < n; j++) {
	    for (k=0; k < n; k++) {
		v[j][k] = (j==k)? 1.0:0.0;
	    }
	}
    } else {
	eig = NULL;
	v = NULL;
    }

    jacobi_init(n);

    for (i3=0; i3 < n3; i3++) {
	sf_floatread(a[0],n*n,mat);
	
	for (iter=0; iter < niter; iter++) {
	    s2 = 0.;
	    for (j=0; j < n-1; j++) {
		for (k=j+1; k < n; k++) {
		    s2 += jacobi(a,j,k,v);
		}
	    }
	    sf_warning("iter=%d s2=%g",iter+1,s2);
	}

	for (j=0; j < n; j++) {
	    e[j]=a[j][j];
	}

	sf_floatwrite(e,n, val);
	if (NULL != v) 	sf_floatwrite(v[0],n*n, eig);
    }

    exit(0);
}
Example #10
0
  /*! This method employs the static method matrix3x3::jacobi(...)
    to find the eigenvalues and eigenvectors of a symmetric
    matrix. On entry it is checked if the matrix really is
    symmetric: if isSymmetric() returns 'false', an OBError is
    thrown.
 
    \note The jacobi algorithm is should work great for all
    symmetric 3x3 matrices. If you need to find the eigenvectors
    of a non-symmetric matrix, you might want to resort to the
    sophisticated routines of LAPACK.
 
    @param eigenvals a reference to a vector3 where the
    eigenvalues will be stored. The eigenvalues are ordered so
    that eigenvals[0] <= eigenvals[1] <= eigenvals[2].
 
    @return an orthogonal matrix whose ith column is an
    eigenvector for the eigenvalue eigenvals[i]. Here 'orthogonal'
    means that all eigenvectors have length one and are mutually
    orthogonal. The ith eigenvector can thus be conveniently
    accessed by the GetColumn() method, as in the following
    example.
    \code
    // Calculate eigenvectors and -values
    vector3 eigenvals;
    matrix3x3 eigenmatrix = somematrix.findEigenvectorsIfSymmetric(eigenvals);
  
    // Print the 2nd eigenvector
    cout << eigenmatrix.GetColumn(1) << endl;
    \endcode
    With these conventions, a matrix is diagonalized in the following way:
    \code
    // Diagonalize the matrix
    matrix3x3 diagonalMatrix = eigenmatrix.inverse() * somematrix * eigenmatrix;
    \endcode
  
  */
  matrix3x3 matrix3x3::findEigenvectorsIfSymmetric(vector3 &eigenvals) const
#ifdef OB_OLD_MATH_CHECKS
  throw(OBError)
#endif
  {
    matrix3x3 result;

#ifdef OB_OLD_MATH_CHECKS
    if (!isSymmetric())
      {
        OBError er("matrix3x3::findEigenvectorsIfSymmetric(vector3 &eigenvals) const throw(OBError)",
                   "The method was called on a matrix that was not symmetric, i.e. where isSymetric() == false.",
                   "This is a runtime or a programming error in your application.");
        throw er;
      }
#endif

    double d[3];
    matrix3x3 copyOfThis = *this;

    jacobi(3, copyOfThis.ele[0], d, result.ele[0]);
    eigenvals.Set(d);

    return result;
  }
Example #11
0
/* ************************************************************ */
void driver()
{
    double dx, dy;

    initialize(N, M, ALPHA, &dx, &dy, u, f);
    jacobi(N, M,  &dx, &dy, ALPHA, RELAX, u, f, TOL, MITS);
    error_check(N, M, ALPHA, &dx, &dy, u, f);

}
Example #12
0
int solovay_strassen(mpz_t n, int k)
{
	int i;
	for (i = 0; i < primes_count && mpz_cmp_si(n, primes[i]*primes[i]) >= 0; i++)
		if (mpz_divisible_ui_p(n, primes[i])) // Check if current prime divides n
			return 0;

	if (mpz_cmp_si(n, 2) < 0)
		return 0;
	if (mpz_cmp_si(n, 2) == 0)
		return 1;
	if (mpz_divisible_ui_p(n, 2))
		return 0;

	gmp_randstate_t RAND;
	gmp_randinit_default(RAND);

	mpz_t a, jac, mod, exp, n1;
	mpz_init(a);
	mpz_init(jac);
	mpz_init(mod);
	mpz_init(exp);
	mpz_init(n1);
	mpz_sub_ui(n1, n, 1);
	while (k > 0) {
		k--;
		mpz_urandomm(a, RAND, n1);
		mpz_add_ui(a, a, 1);

		int j = jacobi(a, n);
		if (j == -1) { // jac = n + jac(a,n)
			mpz_sub_ui(jac, n, 1);
		} else if (j == 1)
			mpz_set_si(jac, j);

		mpz_divexact_ui(exp, n1, 2); // exp = (n-1)/2
		mpz_powm(mod, a, exp, n); // mod = a^((n-1)/2) % n
		if (mpz_cmp_ui(jac, 0) == 0 || mpz_cmp(mod, jac) != 0) { // Is it a liar?
			mpz_clear(a);
			mpz_clear(jac);
			mpz_clear(mod);
			mpz_clear(exp);
			mpz_clear(n1);

			return 0;
		}
	}

	mpz_clear(a);
	mpz_clear(jac);
	mpz_clear(mod);
	mpz_clear(exp);
	mpz_clear(n1);

	return 1;
}
int main(int argc, char **argv) {
    int n_iter;			/* number of iterations */
    n = atoi(argv[1]);
    error = atof(argv[2]);

    init();		   /* initalize a, x0 and b - DO not change */
    n_iter = jacobi();

    return 0;
}
Example #14
0
void jacobi(
	float alpha,  // Iteration parameters
	float beta,
	Field2f &b,
	Field2f &x0, // The previous value for x (input)
	Field2f &x1)
{
	for (int y = 1; y < GRID_RES - 1; ++y)
	for (int x = 1; x < GRID_RES - 1; ++x)
		jacobi(x, y, alpha, beta, b, x0, x1);
}
void SizeCalculator::inertia(double& l0, double& l1, double& l2, Vector3d& v0,
		Vector3d& v1, Vector3d& v2)
{
	DEBUG_START;

	double **JJ, **v, *l;

	JJ = new double*[3];
	JJ[0] = new double[3];
	JJ[1] = new double[3];
	JJ[2] = new double[3];

	v = new double*[3];
	v[0] = new double[3];
	v[1] = new double[3];
	v[2] = new double[3];

	l = new double[3];

	double Jxx, Jyy, Jzz, Jxy, Jyz, Jxz;
	J(Jxx, Jyy, Jzz, Jxy, Jyz, Jxz);

	JJ[0][0] = Jxx;
	JJ[0][1] = Jxy;
	JJ[0][2] = Jxz;
	JJ[1][0] = Jxy;
	JJ[1][1] = Jyy;
	JJ[1][2] = Jyz;
	JJ[2][0] = Jxz;
	JJ[2][1] = Jyz;
	JJ[2][2] = Jzz;

	jacobi(3, JJ, l, v);

	l0 = l[0];
	l1 = l[1];
	l2 = l[2];

	v0 = Vector3d(v[0][0], v[1][0], v[2][0]);
	v1 = Vector3d(v[0][1], v[1][1], v[2][1]);
	v2 = Vector3d(v[0][2], v[1][2], v[2][2]);

	delete[] JJ[0];
	delete[] JJ[1];
	delete[] JJ[2];
	delete[] JJ;
	delete[] v[0];
	delete[] v[1];
	delete[] v[2];
	delete[] v;
	delete[] l;

	DEBUG_END;
}
Example #16
0
File: jacobi.c Project: pousa/minas
int main(int argc, char *argv[]) {
  int i, j;
  double tmp=0,tma=0;

  /* read command line and initialize grids */
  if (argc < 2) {
	printf("jacobi size n_iter\n");
	exit(0);
  }
  gridSize = atoi(argv[1]);
  numIters = atoi(argv[3]);

#ifndef MAI
 tma=0.0;
 tma = myms();
grid1 = (double**)malloc((gridSize+3)*sizeof(double*));
  grid2 = (double**)malloc((gridSize+3)*sizeof(double*));
  for(i = 0; i <= gridSize; i++) {
	grid1[i] = (double*)malloc((gridSize+3)*sizeof(double));	
	grid2[i] = (double*)malloc((gridSize+3)*sizeof(double));	
  }
 tma = myms() - tma;
 printf("Malloc time %f\n",tma/1.e+6);
#endif  

#ifdef MAI  
  mai_init(NULL);
  tma=0.0;
  tma = myms();
  grid1 = mai_alloc_2D(gridSize,gridSize, sizeof(double),DOUBLE);
  grid2 = mai_alloc_2D(gridSize,gridSize, sizeof(double),DOUBLE);
  tma = myms() - tma;
  printf("MAi alloc time: %f\n",tma/1.e+6);
#endif

  mai_bind_rows(grid1);
  mai_bind_rows(grid2);
  InitializeGrids();

  tma=0.0;
  tma = myms();

  jacobi();

#ifdef IRREGULAR
  mai_cyclic(grid1);
  mai_cyclic(grid2);
  irregular(gridSize);
#endif

  tma = myms() - tma;
  printf("Execution time %f\n",tma/1.e+6);
}
Example #17
0
void
driver ()
{

  initialize ();

  /* Solve Helmholtz equation */
  jacobi ();

  /* error_check (n,m,alpha,dx,dy,u,f) */
  error_check ();
}
Example #18
0
int main(int argc, char **argv){
    double *u, *f, dx, dy;
    double dt, mflops;
    int NUMTHREADS;
    char *PARAM_NAMES[NUM_ARGS] = {"Grid dimension: X dir =", "Grid dimension: Y dir =", "Helmhotlz constant =",
                                   "Successive over-relaxation parameter =",
                                   "error tolerance for iterative solver =", "Maximum iterations for solver ="};
    char *TIMERS_NAMES[NUM_TIMERS] = {"Total_time"};
    char *DEFAULT_VALUES[NUM_ARGS] = {"5000", "5000", "0.8", "1.0", "1e-7", "1000"};



   NUMTHREADS = omp_get_max_threads();
   OSCR_init (NUMTHREADS, "Jacobi Solver v1", "Use 'jacobi01' <n> <m> <alpha> <relax> <tol> <mits>", NUM_ARGS,
                PARAM_NAMES, DEFAULT_VALUES , NUM_TIMERS, NUM_TIMERS, TIMERS_NAMES,
                argc, argv);

    n = OSCR_getarg_int(1);
    m = OSCR_getarg_int(2);
    alpha = OSCR_getarg_double(3);
    relax = OSCR_getarg_double(4);
    tol = OSCR_getarg_double(5);
    mits = OSCR_getarg_int(6);

    printf("-> %d, %d, %g, %g, %g, %d\n",
           n, m, alpha, relax, tol, mits);

    u = (double *) OSCR_malloc(n*m*sizeof(double));
    f = (double *) OSCR_malloc(n*m*sizeof(double));


    /* arrays are allocated and initialzed */
    initialize(n, m, alpha, &dx, &dy, u, f);


    /* Solve Helmholtz eqiation */
    OSCR_timer_start(0);
    jacobi(n, m, dx, dy, alpha, relax, u,f, tol, mits);

    OSCR_timer_stop(0);
    dt = OSCR_timer_read(0);

   // printf(" elapsed time : %12.6f\n", dt);
    mflops = (0.000001*mits*(m-2)*(n-2)*13) / dt;
  //  printf(" MFlops       : %12.6g (%d, %d, %d, %g)\n",mflops, mits, m, n, dt);

    error_check(n, m, alpha, dx, dy, u, f);

    OSCR_report(1, TIMERS_NAMES);

  return 0;
}
Example #19
0
void cResOptInterFaisceaux::Init(const ElMatrix<double> & aMat)
{
   ElMatrix<double> aVecP(3,3),aValP(1,3);
   std::vector<int> aInd = jacobi(aMat,aValP,aVecP);

    mVal1 = aValP(aInd[0],0);
    mVal2 = aValP(aInd[1],0);
    mVal3 = aValP(aInd[2],0);

    aVecP.GetCol(aInd[0],mVec1);
    aVecP.GetCol(aInd[1],mVec2);
    aVecP.GetCol(aInd[2],mVec3);
}
Example #20
0
int main(int argc, char *argv[]) {
	int num;
	Matrix *m;
	for ( int i = 1; i < argc; i++ ) {
		num = findNumber( argv[i] );
		if ( num == -1 )
			FileNameError();

		jacobi( loadMatrix( argv[i] ), num );
	}

	return 0;
}
Example #21
0
main ()
{ int n; matrice a,M; vettore b,d,x_k,diff;float y;
  leggi_dim (n);
  leggi_matrice (n,a);
  leggi_vett (n,b);
  vett_iniziale(n,x_k);
  matrix_M (n,a,M);
  vettore_d (n,a,b,d);
  jacobi(n,x_k,M,d,diff,a);
  printf("\n\n");
  system("pause");
  return 0;
}
Example #22
0
void find_point()
{
	bigmod h,x;
	bigint q,a4,a6,y;
	
	char* sa4=new char[MAX];
	char* sa6=new char[MAX];
	char* sq=new char[MAX];
	
	banner();
	cout<<"\nFIND POINT";
	cout<<"\n----------";
	do
	{
		cout<<"\nPlease enter prime field (p).";
     	cout<<"Its length should be 160 or 192-bit : \n";
      cin>>sq;
		string_to_bigint(sq,q);
	} while((!is_prime(q)) || ((q.bit_length() != 160) && 
     (q.bit_length()!=192)));
	bigmod::set_modulus(q);

	cout<<"\nPlease input numbers to the following : "<<endl;
	do
	{
		cout<<"Coefficient a = ";cin>>sa4;
	} while(!is_number(sa4));

	do
	{
		cout<<"Coefficient b = ";cin>>sa6;
	} while(!is_number(sa6));

	string_to_bigint(sa4,a4);
	string_to_bigint(sa6,a6);

	q=next_prime(q-1);

	do
	{
		x.randomize();
		h = (x*x+a4)*x+a6;
	} while(jacobi(h.mantissa(),q) !=1);

	ressol_p(y,h.mantissa(),q);

	cout<<"Point P : ("<<x<<","<<y<<")"<<endl;

	delete[] sa4;
	delete[] sa6;
}
Example #23
0
File: jacobi-v3.c Project: 8l/rose
void driver( )
{
  initialize();

  time1 = time_stamp();
  /* Solve Helmholtz equation */
  jacobi ();
  time2 = time_stamp();

  printf("------------------------\n");     
  printf("Execution time = %f\n",time2-time1);
  /* error_check (n,m,alpha,dx,dy,u,f)*/
  error_check ( );
}
Example #24
0
/// Radial polynomials
double
radpoly(unsigned int n, unsigned int m, double rho)
{
  if (m > n)
    throw std::range_error("radpoly: m > n");
  else if ((n - m) % 2 == 1)
    return 0.0;
  else
    {
      auto k = (n - m) / 2;
      return (k % 2 == 0 ? +1 : -1) * std::pow(rho, double(m))
	   * jacobi(k, double(m), 0.0, 1.0 - 2.0 * rho * rho);
    }
}
Example #25
0
void bend (double **mat, double **mat_ext, int tr)
{
   int j;
   double **v, *d;
   
   v = (double **) malloc (tr*sizeof(double *));
   for (j = 0; j < tr; j++)
      v[j] = (double *) malloc (tr*sizeof(double));

   d = (double *) malloc (tr*sizeof(double));

   jacobi(mat, v, d, tr);
   print_eigen (v, d, tr);
}
Example #26
0
void main( )
{
  Matrix *m, *mT, *eig_vec_J, *eig_vec_T, *L;
  Vector *eig_val_J, *eig_val_T;
  int i, j, tt_ja, tt_ho;

  m = matrix_alloc( 100, 50 );
  for( i = 0; i < m->dim_M; i++ )
    for( j = 0; j < m->dim_N; j++ )
      { 
        M( m, i, j) = (double) i*i + 120.0;
      }

  mT = matrix_alloc( 50, 100 );
  matrix_transpose( m, mT );
  
  eig_vec_J = matrix_alloc( 100, 100 );
  eig_val_J = vector_alloc( 100  );
  L       = matrix_alloc( 100, 100 );

  matrix_prod( m, mT, L );
  Start_Clock_once( &tt_ja );
  jacobi( L, eig_val_J, eig_vec_J );
  End_ms_Clock_once( tt_ja, 1, "jacobi used " );

  printf("done with jacobi\n" );
  /* for( i = 0; i < 100; i++ ) printf(" %f", V(eig_val_J,i) );
     printf("\n");
     for( i = 0; i < 100; i++ ) printf(" %f", M(eig_vec_J,i,0) );
     printf("\n");
   */

  matrix_prod( m, mT, L );
  eig_vec_T = matrix_alloc( 100, 100 );
  eig_val_T = vector_alloc( 100  );

  Start_Clock_once( &tt_ho );
  eigen_householder( L, eig_val_T, eig_vec_T );  
  End_ms_Clock_once( tt_ho, 1, "householder used " );

  printf(" done with householder\n" );
  /* for( i = 0; i < 100; i++ ) printf(" %f", V(eig_val_T,i) );
     printf("\n");
     for( i = 0; i < 100; i++ ) printf(" %f", M(eig_vec_T,i,0) );
     printf("\n");
   */


  
}
Example #27
0
/* Test with test.mat, but only with k = 2!
   (eigen vectors = (0.577, 0.577, 0.5770), (-0.707, 0, 0,707), and a mysterious third?
eig values = (12, 6, 0) */
int main(int argc, char *argv[]) {

  if (argc != 6) {
      fprintf(stderr, "Error: wrong number of arguments provided. Program expects 5 arguments (k, log(epsilon), filename, matrix dimension n, mode). \n Mode 1 = read from file. Mode 2 = generate matrix. \n\n");
      exit(1);
  }

  //int k = atoi(argv[1]);
  double epsilon = (double)atof(argv[2]);
  char *input = argv[3];
  int n = atoi(argv[4]);
  int mode = atoi(argv[5]);
  
  //printf("\nRunning eigP with arguments (%i, %f, %s, %i)\n\n", k, epsilon, input, n);

  /* Get input matrix A */
  double **A = newM(n, n);
  switch(mode) {
  case(1):
    A = readM(input, n, n);
    break;
  case(2):
    A = symRandomM(n);
    writeM(A, "test.mat", n, n);
    break;
  }
  printM(A, n, n);

  double **eVecs = newM(n, n);
  double *eVals = (double *)malloc(sizeof(double)*n);

  jacobi(A, eVecs, eVals, n, n);
  eVecs = sortEigenVecs(eVecs, eVals, n, n);

  // For Power method
  /*double *eVec1 = powerConverge(A, n, epsilon);
  printM(&eVec1, 1, n);
  eVecs = powerConvergeK(A, n, epsilon, k);
  eVals =  eigenVals(A, eVecs, n);  */

  /*
  printf("Results:\n\n");
  printM(eVecs, n, n);
  printM(&eVals, 1, n); */

  writeM(eVecs, "eVecs.data", n, n);
  writeM(&eVals, "eVals.data", 1, n);
  return 0;
}
Example #28
0
int
jacobi(int a, int b)
{
	int a1, a2;

	if (a >= b)			/* 4 */
		a %= b;
	if (a == 0)			/* 0 */
		return 0;
	if (a == 1)			/* 1 */
		return 1;
	if (a == 2)			/* 3 */
		if (((b*b-1) / 8) % 2 == 0)
			return 1;
		else
			return -1;

	/* 5' */
	if (a & b & 1)	/* both a and b are odd */
		if (((a-1)*(b-1)/4) % 2 == 0)
			return +jacobi(b, a);
		else
			return -jacobi(b, a);
	/* 5 */
/* is the if-case correct?  If so remove these comment markers...
	if (gcd(a, b) == 1)
		if (((a-1)*(b-1)/4) % 2 == 0)
			return +jacobi(b, a);
		else
			return -jacobi(b, a);
*/

	/* 2 */
	factor2(a, &a1, &a2);
	return jacobi(a1, b) * jacobi(a2, b);
}
Example #29
0
int getDD1(const bigInt &n) // changed
{
    int dd;
	for (int d_abs = 5, d_sign = 1; ; d_sign = -d_sign, ++++d_abs)
	{
		dd = d_abs * d_sign;
		bigInt g = gcd (n, d_abs);
		if (1 < g && g < n)
			// нашли делитель - d_abs
			return false;
		if (jacobi (bigInt(dd), n) == -1)
			break;
	}
    return dd;
}
Example #30
0
int main(void)
{
	int i,j,nrot;
	static float c[NP][NP]=
		{5.0,4.3,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0,-4.0,
		4.3,5.1,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0,
		3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0,
		2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,-1.0,
		1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,0.0,
		0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,1.0,
		-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,2.0,
		-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,3.0,
		-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,4.0,
		-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0};
	float *d,**v,**e;

	d=vector(1,NP);
	v=matrix(1,NP,1,NP);
	e=convert_matrix(&c[0][0],1,NP,1,NP);
	printf("****** Finding Eigenvectors ******\n");
	jacobi(e,NP,d,v,&nrot);
	printf("unsorted eigenvectors:\n");
	for (i=1;i<=NP;i++) {
		printf("eigenvalue %3d = %12.6f\n",i,d[i]);
		printf("eigenvector:\n");
		for (j=1;j<=NP;j++) {
			printf("%12.6f",v[j][i]);
			if ((j % 5) == 0) printf("\n");
		}
		printf("\n");
	}
	printf("\n****** Sorting Eigenvectors ******\n\n");
	eigsrt(d,v,NP);
	printf("sorted eigenvectors:\n");
	for (i=1;i<=NP;i++) {
		printf("eigenvalue %3d = %12.6f\n",i,d[i]);
		printf("eigenvector:\n");
		for (j=1;j<=NP;j++) {
			printf("%12.6f",v[j][i]);
			if ((j % 5) == 0) printf("\n");
		}
		printf("\n");
	}
	free_convert_matrix(e,1,NP,1,NP);
	free_matrix(v,1,NP,1,NP);
	free_vector(d,1,NP);
	return 0;
}