Exemplo n.º 1
0
int main(void){
  int i,j,k,step;
  char filename[20]="matrix1.txt",
    filenamep[20]="matrixp.txt",filenameq[20]="matrixq.txt";
  struct matrix *p,*q,*r;
  double err;
  FILE *gp;
  srand((unsigned)time(NULL));
  r=mat_read(filename);
  M=r->row;N=r->col;
  p=mat_read(filenamep);
  q=mat_read(filenameq);

  printf("R\n");mat_print(r);
  printf("P\n");mat_print(p);
  printf("Q\n");mat_print(q);

  for(step=0;step<STEP;step++){
    for(i=0;i<M;i++){
      for(j=0;j<N;j++){
	if(*(r->element+j+i*r->col)==0)continue;
	err=rating_er(*(r->element+j+i*r->col),p,q,i,j);
	for(k=0;k<K;k++){
	  *(p->element+k+i*p->col)
	    +=ALPHA*(2*err* *(q->element+j+k*q->col)-BETA* *(p->element+k+i*p->col));
	  *(q->element+j+k*q->col)
	    +=ALPHA*(2*err* *(p->element+k+i*p->col)-BETA* *(q->element+j+k*q->col));
	}
      }
    }
    if((step%100)==0){
      //printf("%f \n",get_er(r,p,q));
      if(get_er(r,p,q)<THT){printf("step %d\n",step);break;}
    }
  }
  printf("========================\n");
  printf("error=%f\n",get_er(r,p,q));
  printf("P\n"); mat_print(p);
  printf("Q\n");mat_print(q);
  printf("P*Q\n");mat_print(mat_mul(p,q));

  return(0);
}
Exemplo n.º 2
0
/* 
 *      FUNCTION  
 *         Name:  main
 *  Description:  
 * 
 */
int main ( int argc, char *argv[] )
{
	gsl_ieee_env_setup () ;			/* read GSL_IEEE_MODE */

	double beta = 1.0/T ;           /* Boltzmann factor: beta */
	double omega_1 = gsl_hypot(OMEGA,D) ;   /* omega' */

	struct f_params params;
	params.omega_c = omega_c ;
	params.beta = beta ;
	params.Omega = OMEGA ;
	params.omega_1 = omega_1 ;
	params.alpha = alpha ;

	/* read the Redfield matrix from a file */
	gsl_matrix* red_m = gsl_matrix_calloc ( 4, 4 ) ;
	mat_read ( red_m, "REDFIELD_MATRIX" ) ;

	/* 
	 * Find the stationary state by solving the linear systems
	 * and the associated stationary currents
	 */
	gsl_vector* req_red = gsl_vector_calloc (4) ;
	FILE* f_red = fopen ( "RED_STATIONARY.dat", "r" ) ;
	if ( f_red == NULL )
		printf("Error: %s.\nFailed to open RED_STATIONARY.dat",
				strerror(errno)) ;
	gsl_vector_fscanf ( f_red, req_red ) ;
	fclose ( f_red ) ;

	printf("REDFIELD DYNAMICS\n") ;
	printf("Stationary state: ( %.1f , %.9f , %.9f , %.9f )\n", 
			VECTOR(req_red,0), VECTOR(req_red,1),
			VECTOR(req_red,2), VECTOR(req_red,3) ) ;
	printf("Stationary normalized (I/I0) DC current: %.9f\n\n",
			-VECTOR(req_red,3)*OMEGA/omega_1) ;
	
	int status = red_evol ( &params, R, t_end, STEP, req_red, red_m ) ;
	if ( status != 0 )
		exit (EXIT_FAILURE) ;

	/* free memory for matrix */
	gsl_matrix_free(red_m) ;

	return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
Exemplo n.º 3
0
Matrix *mat_new_from_file(FILE *F, int nrows, int ncols) {
  Matrix *m = mat_new(nrows, ncols);
  mat_read(m, F);
  return m;
}
Exemplo n.º 4
0
int main()
{
    unsigned r1, c1, r2, c2, ch;
    mat m1, m2, t;

    puts("Enter row and column numbers of matrices 1 and 2:");
    scanf(" %u %u %u %u%*c", &r1, &c1, &r2, &c2);
    putchar('\n');

    m1=mat_alloc(r1, c1);
    m2=mat_alloc(r2, c2);

    printf("Enter value of Matrix 1 (%ux%u):\n", r1, c1);
    mat_read(m1);
    putchar('\n');
    printf("Enter value of Matrix 2 (%ux%u):\n", r2, c2);
    mat_read(m2);
    putchar('\n');

    do{
        puts("What would you like to do?");
        puts(" ( 0) Exit");
        puts(" ( 1) Display");
        puts(" ( 2) Transpose");
        puts(" ( 3) Sum");
        puts(" ( 4) Difference");
        puts(" ( 5) Product");
        puts(" ( 6) Greatest Element of Rows");
        puts(" ( 7) Sum of Major Diagonal");
        puts(" ( 8) Sum of Minor Diagonal");
        puts(" ( 9) Check Symmetricity");
        puts(" (10) Upper-Triangular Matrix");
        puts(" (11) Lower-Triangular Matrix");
        scanf(" %u%*c", &ch);
        switch(ch){
        case 0:
            puts("Bye!");
            break;
        case 1:
            puts("Matrix 1:");
            mat_write(m1);
            putchar('\n');
            puts("Matrix 2:");
            mat_write(m2);
            break;
        case 2:
            t=mat_trn(m1);
            mat_write(t);
            mat_free(t);
            break;
        case 3:
            if((t=mat_add(m1, m2)).r){
                mat_write(t);
                mat_free(t);
            }
            else puts("Not Possible");
            break;
        case 4:
            if((t=mat_sub(m1, m2)).r){
                mat_write(t);
                mat_free(t);
            }
            else puts("Not Possible");
            break;
        case 5:
            if((t=mat_mul(m1, m2)).r){
                mat_write(t);
                mat_free(t);
            }
            else puts("Not Possible");
            break;
        case 6:
            row_great(m1);
            break;
        case 7:
            add_major(m1);
            break;
        case 8:
            add_minor(m1);
            break;
        case 9:
            if(issymm(m1)) puts("Symmetric");
            else puts("Unsymmetric");
            break;
        case 10:
            up_tri(m1);
            break;
        case 11:
            lo_tri(m1);
            break;
        default:
            puts("Incorrect Choice!");
            break;
        }
        putchar('\n');
    } while(ch);

    mat_free(m1);
    mat_free(m2);
    return 0;
}