int cg(MAT &A,VEC b,VEC &x,int maxIter,double tol){
	int n = A.dim();
	int k = 0;
	VEC x_next(n);
	VEC r_next(n);
	VEC p_next(n);
	VEC r(n);
	VEC p(n);
	MAT L(n);
    VEC cpr(n);
	double alpha,beta;
	double err;
	double diff;
	r = b - A*x;					//initial condition
	p = r;
	while(k<maxIter){				//conjugate gradient decent
		alpha = (r*r)/(p*(A*p));
		x_next = x + alpha*p;
		r_next = r - alpha*(A*p);
		beta = (r_next*r_next)/(r*r);
		p_next = r_next + beta*p;
		k++;
		x = x_next;					//assign to the x,r,p of the next iteration 
		r = r_next;
		p = p_next;
		err = pow((r*r)/n,0.5);
		if(err<tol)					//see if the error is smaller than the defined tolerance. if so, break out of the loop 
			break;
	}
        diff = 0;
        //the answer from hw4

        L = cholesky(A);                        //in-place Cholesky Decomposition
        cpr = choSolve(L,b);                      //solve the linear system by forward and backward substitution
        //use the same method to compute the error between hw4 and hw5, see if < 10^-7. if not, decrease the tol
        for(int i=0;i<n;i++){
                diff = max(diff,fabs(x[i]-cpr[i]));		//use infinity norm to compute the error
        }
        printf("error between hw4 and hw6(infinity-norm): %e\n",diff);

	return k;	
}
Example #2
0
WORD
dofiles( BYTE *s, BYTE *d, WORD code, LONG *ndirs, LONG *nfiles, LONG *tsize, 
		 WORD type, WORD multiple, WORD handle)
{
	WORD	ret;  /*trash; */
	BYTE	buffer[2];

	if ( *d == 'c' )
	  return( FALSE );
	
/*	desk_mice( HOURGLASS );	*/
				/* get the dialog box string */
	whandle = handle;
	cpbox = get_tree( CPBOX );

	f_cancel = 0;
	f_level = 0;		/* the current depth of the directory path */
	f_abort = 0;		/* No abortion	*/
	rename	= 0;
	buffer[1] = 0;

	ret = FALSE;

	if ( (opcode = code) == OP_COUNT )
	{
	  numfiles = 0x0L;	/* the number of files show in the dialog box */
	  numdirs = 0x0L;	/* the number of directories */
	  tolsize = 0x0L;	/* the total size of all files in the path */
	}
	else
	{
	  numdirs = *ndirs;
	  numfiles = *nfiles;
	  tolsize = *tsize;
	}

	do
	{
	  if ( *s == 'c' )	/* skip cartridge	*/	
	    goto cc_4;

	  buffer[0] = *s;

	  if ( ( d_display ) && ( code != OP_COUNT ) )
	  {
	    inf_sset( (LONG)cpbox, CSDRIVE, buffer );
	    wind_update( 1 );
	    w_draw_fld( handle, cpbox, CSDRIVE );
	    wind_update( 0 );
	  }
			/* do the counting only	*/
   	  if (code == OP_COUNT)
	  {
	    if ( !(ret = count( s ) ) )
	      goto clnup;		/* error	*/
	  }
	  else			/* do the actual operation	*/
	  {
	    if ( !ch_undo() || f_cancel )	/* check for undo key		*/
	    {	
	       f_abort = 1;
	       goto clnup;
	    }
				/* delete the whole disk	*/
	    if ( ( type == DISK ) && ( code == OP_DELETE ) )
	    {
	      if ( fill_string( buffer, DELDISK ) != 1 )
		goto cc_4;
	    }

	    if ( ( code == OP_COPY ) || ( code == OP_MOVE ) )
	    {
	      if ( ( type == SUBDIR ) || ( type == XDIR ) || ( type == DISK ) ) 	
	      {
		if ( !chk_par( s, d ) )	/* illegal	*/
		{
		  if ( do1_alert( NODIRCOP ) == 2 )
		    goto clnup;		/* abort	*/
		  else
		  {
		    ret = TRUE;
		    goto cc_3;		/* go to next one	*/
		  }
		}
	      }
	    }

	    if (!(ret = getinfo(s, d)))
	      goto clnup;

	    if ( ret == SKIP )
	      continue;

	    if (!(ret = doright(ret)))
	      goto clnup;

	    if ( ret == SKIP )
	      continue;
	  }
cc_3:
	  if ( code != OP_COUNT )
	    x_deselect();
					/* deselect object	*/
	  if ( ( code == OP_DELETE ) || ( code == OP_MOVE ) )	
	    dr[ *s - 'A' ] = 1;

	  if ( ( code == OP_COPY ) || ( code == OP_MOVE ) )
	    dr[ *d - 'A' ] = 1;

cc_4:	  if ( !multiple )		/* do only one device	*/
	    break;

	}while( x_next( &s, &type ) );	

	*ndirs = numdirs;
	*nfiles = numfiles;
	*tsize = tolsize;

clnup:
					/* deselect object	*/
	if ( ( code == OP_DELETE ) || ( code == OP_MOVE ) )	
	{
	  if ( *s != 'c' ) 
	    dr[ *s - 'A' ] = 1;
	}

	if ( ( code == OP_COPY ) || ( code == OP_MOVE ) )
	  dr[ *d - 'A' ] = 1;

/*	desk_mice( ARROW );	*/

	if ( f_abort )
	  return( TRUE );

	return( ret );
}
Example #3
0
/* SRC: classes/splobjectstorage.php line 74 */
void c_SplObjectStorage::t_next() {
  INSTANCE_METHOD_INJECTION_BUILTIN(SplObjectStorage, SplObjectStorage::next);
  ObjectData *obj_tmp UNUSED;
  x_next(ref(m_storage));
  m_index++;
}