/* a Guss-Seidel sweep over a set of blocks */ static int gauss_seidel_sweep (SET *set, int reverse, GAUSS_SEIDEL *gs, short dynamic, double step, int loops, double *errup, double *errlo) { SET* (*first) (SET*); SET* (*next) (SET*); int di, dimax, n; double up, lo; if (reverse) first = SET_Last, next = SET_Prev; else first = SET_First, next = SET_Next; dimax = 0; for (SET *item = first (set); item; item = next (item)) /* first loop contributes to the outputed error components */ { di = gauss_seidel (gs, dynamic, step, item->data, errup, errlo); dimax = MAX (dimax, di); } for (n = 0, up = lo = 0.0; n < loops-1; n ++) /* remaining inner loops do not contribute to the outputed error components */ { for (SET *item = first (set); item; item = next (item)) { di = gauss_seidel (gs, dynamic, step, item->data, &up, &lo); dimax = MAX (dimax, di); } } return dimax; }
void test_gauss_seidel(void) { /* int * gauss_seidel( * double **a, * double *x, * const double *b, * int n, * double epsilon * ); */ int i, j; double *expect; expect = (double *)malloc(sizeof(double) * n); for (i = 0; i < n; ++i) { a[i][i] = 1.; for (j = 0; j < i; ++j) { a[i][j] = 0.; } for (j = i + 1; j < n; ++j) { a[i][j] = 0.; } } for (i = 0; i < n; ++i) { x[i] = 0.; expect[i] = y[i] = i * 1.; } gauss_seidel(a, x, y, n, 1.e-7); for (i = 0; i < n; ++i) { CU_ASSERT_EQUAL(expect[i], x[i]); } free(expect); }
#include<stdio.h> #define max 50 #define e 0.00001/*e is the prescribed accuracy*/ main() { float a[max][max],x[max]; int n,i,j; int gauss_seidel(float [][max],float [],int); void printmat(float [][max],int); printf("Enter the order of the matrix(n x n) : "); scanf("%d",&n); if(n>0) { /*The user enters the augmented matrix*/ printf("\nEnter the augmented matrix row wise :-\n"); for(i=0;i<n;i++) { printf("\nEnter row %d :-\n",i+1); for(j=0;j<(n+1);j++) { printf("Enter element %d: ",j+1); scanf("%f",&a[i][j]); } } printf("\nThe augmented matrix entered is :-\n"); printmat(a,n); if(gauss_seidel(a,x,n))/*The gauss_seidel function return true value */ { printf("\nThe solutions of the given equations are->\n"); for(i=0;i<n;i++) printf("\ntx[%d]=%10.2f\n",i+1,x[i]); } } else printf("\nNo solution."); }
void wrapperxxxxxxx(int first, int second, long N, long BS, double A[BS][BS], double top[BS][BS], double left[BS][BS], double bottom[BS][BS], double right[BS][BS]) { gauss_seidel(first, second, N, BS, A, top, left, bottom, right); }
bool newton(const Fnc& f, const VarSet* vars, IntervalVector& full_box, double prec, double ratio_gauss_seidel) { int n=vars? vars->nb_var : f.nb_var(); int m=f.image_dim(); assert(full_box.size()==f.nb_var()); IntervalMatrix J(m, n); IntervalVector* p=NULL; // Parameter box IntervalVector* midp=NULL; // Parameter box midpoint IntervalMatrix* Jp=NULL; // Jacobian % parameters if (vars) { p=new IntervalVector(vars->param_box(full_box)); midp=new IntervalVector(p->mid()); Jp=new IntervalMatrix(m,vars->nb_param); } IntervalVector y(n); IntervalVector y1(n); IntervalVector mid(n); IntervalVector Fmid(m); bool reducted=false; double gain; IntervalVector& box = vars ? *new IntervalVector(vars->var_box(full_box)) : full_box; IntervalVector& full_mid = vars ? *new IntervalVector(full_box) : mid; y1 = box.mid(); do { if (vars) f.hansen_matrix(full_box,J,*Jp,*vars); else f.hansen_matrix(full_box,J); // f.jacobian(box,J); if (J.is_empty() || (vars && Jp->is_empty())) break; /* remove this block * for (int i=0; i<m; i++) for (int j=0; j<n; j++) if (J[i][j].is_unbounded()) return false; */ mid = box.mid(); if (vars) vars->set_var_box(full_mid, mid); Fmid = f.eval_vector(full_mid); // Use the jacobian % parameters to calculate // a mean-value form for Fmid if (vars) { Fmid &= f.eval_vector(vars->full_box(mid,*midp))+(*Jp)*(*p-*midp); } y = mid-box; if (y==y1) break; y1=y; try { precond(J, Fmid); gauss_seidel(J, Fmid, y, ratio_gauss_seidel); if (y.is_empty()) { reducted=true; if (vars) full_box.set_empty(); else box.set_empty(); break; } } catch (LinearException& ) { assert(!reducted); break; } IntervalVector box2=mid-y; if ((box2 &= box).is_empty()) { reducted=true; if (vars) full_box.set_empty(); else box.set_empty(); break; } gain = box.maxdelta(box2); if (gain >= prec) reducted = true; box=box2; if (vars) vars->set_var_box(full_box, box); } while (gain >= prec); if (vars) { delete p; delete midp; delete Jp; delete &box; delete &full_mid; } return reducted; }
main() { FILE *fp; char code[5],openfile[10]; int *msg_id,num,**sub,i,j,k,size,cnt,status,min,temp; float **coeff,*b,*x,**st_coeff,*st_b,*st_x,rng; printf("enter the value of min :"); scanf("%d",&min); coeff=(float**)malloc((min+1)*sizeof(float*)); for(i=0;i<min+1;i++) coeff[i]=(float*)malloc((min+1)*sizeof(float)); st_coeff=(float**)malloc((min+1)*sizeof(float*)); for(i=0;i<min+1;i++) st_coeff[i]=(float*)malloc((min+1)*sizeof(float)); b=(float*)malloc((min+1)*sizeof(float)); st_b=(float*)malloc((min+1)*sizeof(float)); x=(float*)malloc((min+1)*sizeof(float)); st_x=(float*)malloc((min+1)*sizeof(float)); printf("Enter the number of msgs to open:"); scanf("%d",&num); printf("Enter the ids of the msgs:\n"); msg_id=(int *)malloc(num*sizeof(int)); sub=(int **)malloc(num*sizeof(int *)); for(i=0;i<num;i++) { sub[i]=(int *)malloc(sizeof(int)); } for(i=0;i<num;i++) { scanf("%d",&msg_id[i]); cnt=0; j=msg_id[i]; while(j!=0) { code[cnt]=(char)(48+(j%10)); j=j/10; cnt++; } code[cnt]='\0'; cnt=0; for(j=strlen(code)-1;j>=0;j--) { openfile[cnt]=code[j]; cnt++; } openfile[cnt]='\0'; strcat(openfile,".txt"); fp=fopen(openfile,"r"); j=0; while(!feof(fp)) { fscanf(fp,"%d",&sub[i][j]); j++; sub[i]=(int *)realloc(sub[i],(j+1)*sizeof(int)); } fclose(fp); } size=j; for(i=0;i<num;i++) { for(k=0;k<size;k++) printf("%d ",sub[i][k]); printf("\n"); } for(i=1;i<=min;i++) { srand(msg_id[i-1]-1); for(j=1;j<=min;j++) { coeff[i][j]=(float)(rand()%100+1); } } for(i=1;i<=min;i++) { for(j=1;j<=min;j++) printf(" %f",coeff[i][j]); printf("\n"); } // printf("ooo"); for(i=0;i<size;i++) { for(j=1;j<=min;j++) b[j]=(float)sub[j-1][i]; for(j=1;j<=min;j++) st_b[j]=b[j]; for(k=1;k<=min;k++) { for(j=1;j<=min;j++) st_coeff[k][j]=coeff[k][j]; } // printf("pppp"); gauss_seidel(min,st_coeff,st_b,x,&status); if(status!=0) { //printf("\nSOLUTION VECTOR ARE RESPECTIVELY:"); for(k=1;k<=min;k++) { //printf("hi-"); temp=(int)x[k]; rng=x[k]-(float)(temp); if(rng>0.5000) printf("%c",temp+1); else printf("%c",temp); //printf(" %10.6f",x[k]); //printf("\n"); } } else { printf("\nSingular matrix, reorder equation."); break; } } }
void CtcKhunTucker::contract(IntervalVector& box) { if (df==NULL) return; // if (sys.nb_ctr==0) { // if (box.is_strict_interior_subset(sys.box)) { // // for unconstrained optimization we benefit from a cheap // // contraction with gradient=0, before running Newton. // // if (nb_var==1) // df->backward(Interval::ZERO,box); // else // df->backward(IntervalVector(nb_var,Interval::ZERO),box); // } // } // // if (box.is_empty()) return; //if (box.max_diam()>1e-1) return; // do we need a threshold? // ========================================================================================= BitSet active=sys.active_ctrs(box); FncKhunTucker fjf(sys,df,dg,box,BitSet::all(sys.nb_ctr)); //active); // we consider that Newton will not succeed if there are more // than n active constraints. if ((fjf.eq.empty() && fjf.nb_mult > sys.nb_var+1) || (!fjf.eq.empty() && fjf.nb_mult > sys.nb_var)) { //cout << fjf.nb_mult << endl; too_many_active++; return; } IntervalVector lambda=fjf.multiplier_domain(); IntervalVector old_lambda(lambda); int *pr; int *pc=new int[fjf.nb_mult]; IntervalMatrix A(fjf.eq.empty() ? sys.nb_var+1 : sys.nb_var, fjf.nb_mult); if (fjf.eq.empty()) { A.put(0,0, fjf.gradients(box)); A.put(sys.nb_var, 0, Vector::ones(fjf.nb_mult), true); // normalization equation pr = new int[sys.nb_var+1]; // selected rows } else { A = fjf.gradients(box); pr = new int[sys.nb_var]; // selected rows } IntervalMatrix LU(A); bool preprocess_success=false; try { IntervalMatrix A2(fjf.nb_mult, fjf.nb_mult); // if (A.nb_rows()>A.nb_cols()) { //cout << "A=" << A << endl; interval_LU(A,LU,pr,pc); lu_OK++; //cout << "LU success\n"; // } else // cout << "bypass LU\n"; Vector b2=Vector::zeros(fjf.nb_mult); for (int i=0; i<fjf.nb_mult; i++) { A2.set_row(i, A.row(pr[i])); if (pr[i]==sys.nb_var) // right-hand side of normalization is 1 b2[i]=1; } //cout << "\n\nA2=" << A2 << endl; precond(A2); //cout << "precond success\n"; precond_OK++; gauss_seidel(A2,b2,lambda); double maxdiam=0; for (int i=0; i<A.nb_rows(); i++) { double d=A.row(i).max_diam(); if (d>maxdiam) maxdiam=d; } if (old_lambda.rel_distance(lambda)>0.1 //&& maxdiam<10 && lambda[1].lb()>0 ) { preprocess_success=true; } if (lambda.is_empty()) { box.set_empty(); return; } } catch(SingularMatrixException&) { } delete[] pr; delete[] pc; if (!preprocess_success) return; CtcNewton newton(fjf,1); IntervalVector full_box = cart_prod(box, lambda); // ========================================================================================= // CtcNewton newton(fjc.f,1); // // IntervalVector full_box = cart_prod(box, IntervalVector(fjc.M+fjc.R+fjc.K+1,Interval(0,1))); // // full_box.put(fjc.n+fjc.M+fjc.R+1,IntervalVector(fjc.K,Interval::ZERO)); // ========================================================================================= IntervalVector save(full_box); newton.contract(full_box); if (full_box.is_empty()) { if (preprocess_success) newton_OK_preproc_OK++; else newton_OK_preproc_KO++; box.set_empty(); } else { if (save.subvector(0,sys.nb_var-1)!=full_box.subvector(0,sys.nb_var-1)) { if (preprocess_success) newton_OK_preproc_OK++; else newton_OK_preproc_KO++; box = full_box.subvector(0,sys.nb_var-1); } else { if (preprocess_success) { newton_KO_preproc_OK++; } else newton_KO_preproc_KO++; } } // ========================================================================================= }