Exemplo n.º 1
0
int main( int argc, char **argv) {
    {
        // some fortran
        printf("Hello fortran:\n");
        double array[] = {1, 2, 3, 4};
        int n = sizeof(array)/sizeof(double);
        hello_fortran_( array, &n );
    }



    // some more practical fortran
    {
        printf("Some Fortran Math\n");
        double matrix[COLS][ROWS] = { {1, 3, 5}, {2, 4, 6} };
        double vector[ROWS] = {7, 8, 9};
        double res[COLS];

        int rows = ROWS, cols = COLS;

        printf(" Matrix\n");
        mat_print( &matrix[0][0], rows, cols );

        printf(" Vector\n");
        mat_print( &vector[0], 1, rows );

        better_fortran_(&matrix[0][0], &vector[0], &res[0], &rows, &cols);
        printf(" (matrix' * vector) + 2\n");
        mat_print( &res[0], 1, cols );
    }


}
Exemplo n.º 2
0
Arquivo: vla53.c Projeto: jleffler/soq
int main(void)
{
    int matrix1[][4] =
    {
        //random -n 20 -- -999 999 | commalist -n 4 -B 8 -b '{ ' -T ' },' -R -W 4
        {  421,  174,  389, -713, },
        {  799, -852,  998,  156, },
        { -414, -957,  491, -879, },
        { -461,  290, -274, -209, },
        { -878, -247, -778, -716, },
    };
    int matrix2[4][3] =
    {
        // random -n 12 -- -999 999 | commalist -n 3 -B 8 -b '{ ' -T ' },' -R -W 4
        { -612, -190,  297, },
        { -989,  705,  875, },
        {  961, -870,  599, },
        {   12, -267,  877, },
    };

    int matrix3[5][3];
    mat_multiply(5, 4, 3, matrix1, matrix2, matrix3);
    mat_print("matrix1", 5, 4, matrix1);
    mat_print("matrix2", 4, 3, matrix2);
    mat_print("matrix3", 5, 3, matrix3);
    return 0;
}
Exemplo n.º 3
0
Arquivo: poly.c Projeto: nclack/whisk
// Use SVD to solve linear least squares problem
// result is statically allocated
void polyfit_reuse( double *y, int n, int degree, double *coeffs, double *workspace )
{ int ncoeffs = degree + 1;
  double *u = workspace,
         *w = u + n*ncoeffs,
         *v = w + ncoeffs;

#ifdef DEBUG_POLYFIT_REUSE
  printf("\n---Got---\n");
  printf("\nU:\n");
  mat_print( u, n, ncoeffs);
  printf("\nS:\n");
  mat_print( w, ncoeffs, 1);
  printf("\nV:\n");
  mat_print( v, ncoeffs, ncoeffs);
  printf("\nU S V':\n");
  mat_print(
      matmul_right_transpose_static( 
            matmul_right_vec_as_diag_static( u, n, ncoeffs,
                                             w, ncoeffs),
               n, ncoeffs,
            v, ncoeffs, ncoeffs),
      n, ncoeffs);
#endif
  svd_backsub( u, w, v, n, ncoeffs, y, coeffs );
}
Exemplo n.º 4
0
void mat_assert_eq(const F *A, const F *B, size_t n) {
    if (!mat_eq(A, B, n)) {
        puts("");
        mat_print(A, n);
        puts("");
        mat_print(B, n);
        assert(0);
    }
}
Exemplo n.º 5
0
Arquivo: ps1.c Projeto: lukedeo/CS445
//-----------------------------------------------------------------------------
//	Main function, to be deleted.
//-----------------------------------------------------------------------------
int main(int argc, char const *argv[])
{
	if (argc == 1)
	{
		printf("CPSC 445 PSET 1. Invoke as ./ps1 [b|c] [n] \n");
		return 0;
	}
	char part = (char)(*(argv[1]));
	int n = atoi(argv[2]);
	double *a, *u, *v, *s;
	v = eye(n);
	u = eye(n);
	s = malloc(n * sizeof(double));
	if ((part == 'b') || (part == 'B'))
	{
		a = matrix_part_b(n);
	}
	else if ((part == 'C') || (part == 'c'))
	{
		a = matrix_part_c(n);
	}
	else
	{
		printf("Error: invalid part.\n");
		return -1;
	}

	jacobi(a, n, s, u, v);

	a = matrix_part_b(n);

	printf("\na = \n");
	mat_print(a, n);
	printf("\nu = \n");
	mat_print(u, n);
	printf("\nv = \n");
	mat_print(v, n);
	printf("\ns = \n");
	vec_print(s, n);
	printf("\n");

	free(s);
	free(a);
	free(u);
	free(v);
	return 0;
}
Exemplo n.º 6
0
int mat_inverse(matrix *mat1, matrix mat2){
  matrix square;

  if(check_square(mat2) != 0 || check_square(*mat1) != 0){
    return -1;
  }

  mat_alloc(&square, mat2.row, mat2.col);
  mat_unit(&square);

  mat_solve(mat1, mat2, square);

  mat_print(square);
  mat_print(mat2);
  mat_print(*mat1);

  return 0;
}
Exemplo n.º 7
0
Arquivo: poly.c Projeto: nclack/whisk
int main(int argc, char* argv[])
{ double p[10], *workspace;
  workspace = polyfit_alloc_workspace( N, DEG );
  polyfit( X, Y, N, DEG, p, workspace );
  polyfit_free_workspace(workspace);

  printf("--Expected--\n");
  mat_print( P, DEG+1, 1 );
  printf("--   Got  --\n");
  mat_print( p, DEG+1, 1 );

  { //do polyval check
    int i;
    double tol = 1e-6;
    for(i=0; i<N; i++)
      assert( fabs( Y[i] - polyval(p,DEG,X[i]) ) < tol );
  }
  return 0;
}
Exemplo n.º 8
0
Arquivo: svd.c Projeto: chexenia/whisk
int main(int argc, char* argv[])
{ double v[4], s[2];
  double **ia = mat_index(A,NROWS,NCOLS),
         **iv = mat_index(v,NROWS,NCOLS);

  printf("Input\n");
  mat_print( A, NROWS, NCOLS);

  assert( svd(ia, NROWS, NCOLS, s, iv) );
  free(ia);
  free(iv);

  printf("\n---Got---\n");
  printf("\nU:\n");
  mat_print( A, NROWS, NCOLS);
  printf("\nS:\n");
  mat_print( s, NCOLS, 1);
  printf("\nV:\n");
  mat_print( v, NCOLS, NCOLS);
  printf("\nU S V':\n");
  mat_print(
      matmul_right_transpose_static( 
            matmul_right_vec_as_diag_static( A, NROWS, NCOLS,
                                             s, NCOLS),
               NROWS, NCOLS,
            v, NCOLS, NCOLS),
      NROWS, NCOLS);

  printf("\n---Expected---\n");
  printf("\nU:\n");
  mat_print( U, NROWS, NCOLS);
  printf("\nS:\n");
  mat_print( S, NCOLS, 1);
  printf("\nV:\n");
  mat_print( V, NCOLS, NCOLS);

  printf("\nU S V':\n");
  mat_print(
      matmul_right_transpose_static( 
            matmul_right_vec_as_diag_static( U, NROWS, NCOLS,
                                             S, NCOLS),
               NROWS, NCOLS,
            V, NCOLS, NCOLS),
      NROWS, NCOLS);

  return 0;
}
int main(void) 
{
  Item x = 5, y = 2, x2 = 6, y2 = 3;
  mat m1, m2, k1, k2;
  int i, j;

  printf("\nItems: ");
  item_print(x);
  printf(" ");
  item_print(y);
  printf("\n");
  printf("Item Addition: ");
  item_print(item_add(x, y));
  printf("\n");
  printf("Item Multiplication: ");
  item_print(item_mul(x, y));
  printf("\n");
  printf("Item Substraction: ");
  item_print(item_sub(x, y));
  printf("\n");
  printf("Item Division: ");
  item_print(item_div(x, y));
  printf("\n\n");
  m1 = mat_create(3, 2);
  m2 = mat_create(2, 3);
  k1 = mat_create(2, 2);
  k2 = mat_create(2, 2);
  for (i = 0; i < mat_rows(m1); i++)
    for (j = 0; j < mat_cols(m1); j++)
      mat_add_elem(m1, i, j, x);
  for (i = 0; i < mat_rows(m2); i++)
    for (j = 0; j < mat_cols(m2); j++)
      mat_add_elem(m2, i, j, y);
  for (i = 0; i < mat_rows(k1); i++)
    for (j = 0; j < mat_cols(k1); j++)
      {
  	mat_add_elem(k1, i, j, x2);
  	mat_add_elem(k2, i, j, y2);
      }
  printf("k1 Matrix: \n");
  mat_print(k1);
  printf("k2 Matrix: \n");
  mat_print(k2);
  printf("Addition: \n");
  mat_print(mat_add(k1, k2));
  printf("Substraction: \n");
  mat_print(mat_sub(k1, k2));
  printf("m1 Matrix: \n");
  mat_print(m1);
  printf("m2 Matrix: \n");
  mat_print(m2);
  printf("Multiplication: \n");
  mat_print(mat_mul(m1, m2));
  return 0;
}
Exemplo n.º 10
0
int main(int argc, char** args)
{
    Matrix a; 
    mat_init(a, HEIGHT_OF_A, WIDTH_OF_A);

    Matrix b; 
    mat_init(b, HEIGHT_OF_B, WIDTH_OF_B);

    Matrix c; 
    mat_init(c, HEIGHT_OF_A, WIDTH_OF_B);

    mat_print(a, "The matrix A:\n");
    mat_print(b, "The matrix B:\n");
    printf("APPLYING KERNEL...\n\n");
    MatMul(a, b, c);
//    MatMul_Fast(a, b, c);
    mat_print(c, "The result:\n");

    mat_free(a);
    mat_free(b);
    mat_free(c);
}
Exemplo n.º 11
0
Arquivo: poly.c Projeto: nclack/whisk
void polyfit( double *x, double *y, int n, int degree, double *coeffs, double *workspace )
{ int ncoeffs = degree + 1;
  double *u = workspace,   
         *w = u + n*ncoeffs,
         *v = w + ncoeffs;  
  double **iu = mat_index(u,     n,ncoeffs),
         **iv = mat_index(v,ncoeffs,ncoeffs);
  
  Vandermonde_Build(x,n,ncoeffs,u);
#ifdef DEBUG_POLYFIT
  printf("\nV:\n");
  mat_print( u, n, ncoeffs);
#endif
  svd( iu, n, ncoeffs, w, iv );  
  free(iu);
  free(iv);
  svd_threshold( POLYFIT_SINGULAR_THRESHOLD, w, ncoeffs );
#ifdef DEBUG_POLYFIT
  printf("\n---Got---\n");
  printf("\nU:\n");
  mat_print( u, n, ncoeffs);
  printf("\nS:\n");
  mat_print( w, ncoeffs, 1);
  printf("\nV:\n");
  mat_print( v, ncoeffs, ncoeffs);
  printf("\nU S V':\n");
  mat_print(
      matmul_right_transpose_static( 
            matmul_right_vec_as_diag_static( u, n, ncoeffs,
                                             w, ncoeffs),
               n, ncoeffs,
            v, ncoeffs, ncoeffs),
      n, ncoeffs);
#endif

  polyfit_reuse( y, n, degree, coeffs, workspace );
}
Exemplo n.º 12
0
int main()
{
    int i, j;
    gal8 a[8] = {0}, *aux;
    
    /* copy unity onto a */
    for (i = 0; i < 8; ++i)
        a[i] = unity[i];
    
    /* print each power of gentr */
    for (j = 0; j < 256; ++j) {
        mat_print(a);
        aux = mat_mul(a, gentr);
        for (i = 0; i < 8; ++i)
            a[i] = aux[i];
        sleep(1);
        putchar('\n');
    }
}
Exemplo n.º 13
0
int main(void)
{
    mat *m;
    size_t dim[2];
    
    m = mat_create(6,6);
    
    io_init();
    
    io_set_fmt(IO_ASCII);
    mat_load(m,dim,"ex_io.dat:m");
    printf("%dx%d matrix loaded from %s:\n",(int)(dim[0]),(int)(dim[1]),FNAME);
    mat_print(m,"% .15e");
    io_set_fmt(IO_XML);
    mat_save("ex_io.xml:m",'w',m);

    io_finish();
    
    mat_destroy(m);
    
    return EXIT_SUCCESS;
}
Exemplo n.º 14
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.º 15
0
int main(int argc, char *argv[])
{
    int i,j;
    rs_sample *s1,*s2,*res;
    size_t s1_dim[2],s2_dim[2],s1_nsample,s2_nsample;
    mat *sig;
    bool do_save_res, show_usage;
    strbuf out_elname,out_fname,in_elname[2],in_fname[2],in_path[2],out_path;
    char *spt[2];
    io_fmt_no fmt;
    
    /* argument parsing */
    i           = 1;
    j           = 0;
    show_usage  = false;
    do_save_res = false;
    fmt         = io_get_fmt();
    
    if (argc <= 1)
    {
        show_usage = true;
    }
    else
    {
        while (i < argc)
        {
            if (strcmp(argv[i],"-o") == 0)
            {
                if (i == argc - 1)
                {
                    show_usage = true;
                    break;
                }
                else
                {
                    strbufcpy(out_path,argv[i+1]);
                    do_save_res = true;
                    i += 2;
                }
            }
            else if (strcmp(argv[i],"-f") == 0)
            {
                if (i == argc - 1)
                {
                    show_usage = true;
                    break;
                }
                else
                {
                    if (strcmp(argv[i+1],"xml") == 0)
                    {
                        fmt = IO_XML;
                    }
                    else if (strcmp(argv[i+1],"ascii") == 0)
                    {
                        fmt = IO_ASCII;
                    }
                    else
                    {
                        fprintf(stderr,"error: format %s unknown\n",argv[i+1]);
                        return EXIT_FAILURE;
                    }
                    i += 2;
                }
            }
            else
            {
                if (j < 2)
                {
                    strbufcpy(in_path[j],argv[i]);
                    j++;
                    i++;
                }
                else
                {
                    show_usage = true;
                    break;
                }
            }
        }
    }
    if (show_usage)
    {
        fprintf(stderr,"usage: %s <in sample 1> <in sample 2> [-o <out sample>] [-f {ascii|xml}]\n",\
                argv[0]);
        return EXIT_FAILURE;
    }
    
    /* I/O init */
    io_set_fmt(fmt);
    io_init();
    
    /* getting sizes */
    rs_sample_load(NULL,&s1_nsample,s1_dim,in_path[0]);
    rs_sample_load(NULL,&s2_nsample,s2_dim,in_path[1]);
    
    /* allocation */
    s1  = rs_sample_create(s1_dim[0],s1_dim[1],s1_nsample);
    s2  = rs_sample_create(s2_dim[0],s2_dim[1],s2_nsample);
    res = rs_sample_create(s1_dim[0],s1_dim[1],s1_nsample);
    sig = mat_create(s1_dim[0],s1_dim[1]);
    
    /* loading samples */
    printf("-- loading sample from %s...\n",in_path[0]);
    rs_sample_load(s1,NULL,NULL,in_path[0]);
    printf("-- loading sample from %s...\n",in_path[1]);
    rs_sample_load(s2,NULL,NULL,in_path[1]);
    
    /* multiplying samples */
    printf("-- executing operation on samples...\n");
    rs_sample_binop(res,s1,s2,&BINOP);
    
    /* result output */
    rs_sample_varp(sig,res);
    mat_eqsqrt(sig);
    printf("central value:\n");
    mat_print(rs_sample_pt_cent_val(res),"%e");
    printf("standard deviation:\n");
    mat_print(sig,"%e");
    if (do_save_res)
    {
        get_elname(out_fname,out_elname,out_path);
        if (strlen(out_elname) == 0)
        {
            get_elname(in_fname[0],in_elname[0],in_path[0]);
            get_elname(in_fname[1],in_elname[1],in_path[1]);
            spt[0] = (strlen(in_elname[0]) == 0) ? in_fname[0] : in_elname[0];
            spt[1] = (strlen(in_elname[1]) == 0) ? in_fname[1] : in_elname[1];
            sprintf(out_path,"%s%c%s_%s_%s",out_fname,LATAN_PATH_SEP,argv[0],\
                    spt[0],spt[1]);
        }
        rs_sample_save(out_path,'w',res);
    }
    
    /* desallocation */
    rs_sample_destroy(s1);
    rs_sample_destroy(s2);
    rs_sample_destroy(res);
    mat_destroy(sig);

    /* I/O finish */
    io_finish();
    
    return EXIT_SUCCESS;
}
Exemplo n.º 16
0
int main(int argc, char *argv[])
{
    rs_sample *s1,*res,*buf;
    size_t s1_dim[2],res_dim[2],s1_nsample,a,b,s,k,l;
    int i,j;
    mat *sig;
    bool do_save_res, show_usage, have_s;
    char *spt;
    strbuf out_elname,out_fname,in_elname,in_fname,in_path,out_path;
    io_fmt_no fmt;
    
    /* argument parsing */
    i           = 1;
    j           = 0;
    a           = 0;
    b           = 0;
    s           = 1;
    show_usage  = false;
    do_save_res = false;
    have_s      = false;
    fmt         = io_get_fmt();
    
    if (argc <= 3)
    {
        show_usage = true;
    }
    else
    {
        while (i < argc)
        {
            if (strcmp(argv[i],"-o") == 0)
            {
                if (i == argc - 1)
                {
                    show_usage = true;
                    break;
                }
                else
                {
                    strbufcpy(out_path,argv[i+1]);
                    do_save_res = true;
                    i += 2;
                }
            }
            else if (strcmp(argv[i],"-f") == 0)
            {
                if (i == argc - 1)
                {
                    show_usage = true;
                    break;
                }
                else
                {
                    if (strcmp(argv[i+1],"xml") == 0)
                    {
                        fmt = IO_XML;
                    }
                    else if (strcmp(argv[i+1],"ascii") == 0)
                    {
                        fmt = IO_ASCII;
                    }
                    else
                    {
                        fprintf(stderr,"error: format %s unknown\n",argv[i+1]);
                        return EXIT_FAILURE;
                    }
                    i += 2;
                }
            }
            else if (strcmp(argv[i],"-s") == 0)
            {
                if (i == argc - 1)
                {
                    show_usage = true;
                    break;
                }
                else
                {
                    s       = (size_t)atol(argv[i+1]);
                    have_s  = true;
                    i      += 2;
                }
            }
            else
            {
                if (j == 0)
                {
                    strbufcpy(in_path,argv[i]);
                    j++;
                    i++;
                }
                else if (j == 1)
                {
                    a = (size_t)atol(argv[i]);
                    j++;
                    i++;
                }
                else if (j == 2)
                {
                    b = (size_t)atol(argv[i]);
                    j++;
                    i++;
                }
                else
                {
                    show_usage = true;
                    break;
                }
            }
        }
    }
    if (show_usage)
    {
        fprintf(stderr,"usage: %s <in sample> <a> <b> [-o <out sample>] [-f {ascii|xml}]\n",\
                argv[0]);
        return EXIT_FAILURE;
    }
    
    /* I/O init */
    io_set_fmt(fmt);
    io_init();
    
    /* getting sizes */
    rs_sample_load(NULL,&s1_nsample,s1_dim,in_path);
    l          = b - a + 1;
    s          = (have_s) ? s : l;
    res_dim[0] = s;
    res_dim[1] = s1_dim[1];
    
    /* allocation */
    s1  = rs_sample_create(s1_dim[0],s1_dim[1],s1_nsample);
    buf = rs_sample_create(1,s1_dim[1],s1_nsample);
    res = rs_sample_create(res_dim[0],res_dim[1],s1_nsample);
    sig = mat_create(res_dim[0],res_dim[1]);
    
    /* loading samples */
    printf("-- loading sample from %s...\n",in_path);
    rs_sample_load(s1,NULL,NULL,in_path);
    
    /* multiplying samples */
    printf("-- taking subsample [%d,%d]...\n",(int)a,(int)b);
    for (k=0;k<s;k++)
    {
        rs_sample_get_subsamp(buf,s1,a+(k%l),0,a+(k%l),s1_dim[1]-1);
        rs_sample_set_subsamp(res,buf,k,0,k,s1_dim[1]-1);
    }
    
    
    /* result output */
    rs_sample_varp(sig,res);
    mat_eqsqrt(sig);
    printf("central value:\n");
    mat_print(rs_sample_pt_cent_val(res),"%e");
    printf("standard deviation:\n");
    mat_print(sig,"%e");
    if (do_save_res)
    {
        get_elname(out_fname,out_elname,out_path);
        if (strlen(out_elname) == 0)
        {
            get_elname(in_fname,in_elname,in_path);
            spt = (strlen(in_elname) == 0) ? in_fname : in_elname;
            sprintf(out_path,"%s%c%s_%s",out_fname,LATAN_PATH_SEP,argv[0],spt);
        }
        rs_sample_save(out_path,'w',res);
    }
    
    /* desallocation */
    rs_sample_destroy(s1);
    rs_sample_destroy(res);
    rs_sample_destroy(buf);
    mat_destroy(sig);
    
    /* I/O finish */
    io_finish();
    
    return EXIT_SUCCESS;
}
Exemplo n.º 17
0
int main(void)
{
    mat *a,*b,*c,*d,*e,*f,*g,*h,*i,*j;
    const double b_init[] =
    {
        2.5,\
        2.0,\
        5.0
    };
    const double c_init[] =
    {
        1.0,10.0,6.7
    };
    
    a = mat_create(3,3);
    b = mat_create_from_ar(b_init,3,1);
    c = mat_create_from_ar(c_init,1,3);
    d = mat_create(2,2);
    e = mat_create(3,3);
    f = mat_create(3,3);
    g = mat_create(5,3);
    h = mat_create(3,5);
    i = mat_create(5,3);
    j = mat_create(3,1);
    
    randgen_init(12);
    latan_set_verb(DEBUG1);
    
    printf("---------- %-30s ----------\n","matrix product");
    printf("b =\n");
    mat_print(b,"%8.2f");
    printf("\n");
    printf("c =\n");
    mat_print(c,"%8.2f");
    printf("\n");
    printf("a <- b*c\n");
    mat_mul(a,b,'n',c,'n');
    printf("a =\n");
    mat_print(a,"%8.2f");
    printf("\n");

    printf("\n---------- %-30s ----------\n","sub-matrices");
    printf("d <- a(1:2,0:1)\n");
    mat_get_subm(d,a,1,0,2,1);
    printf("d =\n");
    mat_print(d,"%9.2f");
    printf("\n");
    printf("a(1,0:2) <- c\n");
    mat_set_subm(a,c,1,0,1,2);
    printf("a =\n");
    mat_print(a,"%8.2f");
    printf("\n");

    printf("\n---------- %-30s ----------\n","symmetric matrix product");
    printf("e <- a*t(a)\n");
    mat_mul(e,a,'n',a,'t');
    mat_print(e,"%8.2f");
    printf("\n");
    printf("e is assumed symmetric\n\n");
    mat_assume(e,MAT_SYM);
    printf("j <- e*b\n");
    mat_mul(j,e,'n',b,'t');
    mat_print(j,"%8.2f");
    printf("\n");
    printf("f <- a*e\n");
    mat_mul(f,a,'n',e,'n');
    mat_print(f,"%8.2f");
    printf("\n");
    printf("f <- e*t(a)\n");
    mat_mul(f,e,'n',a,'t');
    mat_print(f,"%8.2f");
    printf("\n");
    printf("g <- random(-1.0,1.0)\n");
    mat_rand_u(g,-1.0,1.0);
    mat_print(g,"%8.2f");
    printf("\n");
    printf("h <- e*t(g) (buffer should be created)\n");
    mat_mul(h,e,'n',g,'t');
    mat_print(h,"%8.2f");
    printf("\n");
    printf("i <- g*e\n");
    mat_mul(i,g,'n',e,'n');
    mat_print(i,"%8.2f");
    printf("\n");
    printf("e is not assumed symmetric anymore\n");
    mat_reset_assump(e);
    
    printf("\n---------- %-30s ----------\n","matrix inversion");
    printf("*** LU decomposition\n");
    printf("a <- random(-6.0,6.0)\n");
    mat_rand_u(a,-6.0,6.0);
    printf("a =\n");
    mat_print(a,"%8.2f");
    printf("\n");
    printf("e <- a^(-1)\n");
    mat_inv_LU(e,a);
    printf("e =\n");
    mat_print(e,"%8.2f");
    printf("\n");
    printf("f <- e*a\n");
    mat_mul(f,e,'n',a,'n');
    printf("f =\n");
    mat_print(f,"%8.2f");
    printf("\n");
    printf("*** pseudo-inverse with good-conditioned matrix\n");
    printf("a =\n");
    mat_print(a,"%8.2f");
    printf("\n");
    printf("e <- a^+\n");
    mat_pseudoinv(e,a);
    printf("e =\n");
    mat_print(e,"%8.2f");
    printf("\n");
    printf("f <- e*a\n");
    mat_mul(f,e,'n',a,'n');
    printf("f =\n");
    mat_print(f,"%8.2f");
    printf("\n");
    printf("*** symmetric pseudo-inverse with good-conditioned matrix\n");
    printf("a <- a*t(a)\n");
    mat_mul(a,a,'n',a,'t');
    mat_print(a,"%8.2f");
    printf("\n");
    printf("a is assumed symmetric and positive\n\n");
    mat_assume(a,(mat_flag)(MAT_SYM|MAT_POS));
    printf("e <- a^+\n");
    mat_pseudoinv(e,a);
    printf("e =\n");
    mat_print(e,"%8.2f");
    printf("\n");
    printf("f <- e*a\n");
    mat_mul(f,e,'n',a,'n');
    printf("f =\n");
    mat_print(f,"%8.2f");
    printf("\n");
    printf("*** Cholesky decomposition\n");
    printf("e <- a^(-1)\n");
    mat_inv_symChol(e,a);
    printf("e =\n");
    mat_print(e,"%8.2f");
    printf("\n");
    printf("f <- e*a\n");
    mat_mul(f,e,'n',a,'n');
    printf("f =\n");
    mat_print(f,"%8.2f");
    printf("\n");
    
    mat_destroy(a);
    mat_destroy(b);
    mat_destroy(c);
    mat_destroy(d);
    mat_destroy(e);
    mat_destroy(f);
    mat_destroy(g);
    mat_destroy(h);
    mat_destroy(i);
    mat_destroy(j);
    
    return EXIT_SUCCESS;
}
Exemplo n.º 18
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("inv... ");
    fflush(stdout);

    _randinit(state);

    /* Check aliasing */

    /* Managed element type (mpq_t) */
    for (i = 0; i < 50; i++)
    {
        long n;
        ctx_t ctx;
        mat_t A, B, C;
        long ansB, ansC;

        n = n_randint(state, 20) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, n, n, ctx);
        mat_init(B, n, n, ctx);
        mat_init(C, n, n, ctx);

        mat_randtest(A, state, ctx);

        mat_set(B, A, ctx);
        ansC = mat_inv(C, B, ctx);
        ansB = mat_inv(B, B, ctx);

        result = ((ansB == ansC) && (!ansB || mat_equal(B, C, ctx)));
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("A: \n"), mat_print(A, ctx), printf("\n");
            printf("B: \n"), mat_print(B, ctx), printf("\n");
            printf("C: \n"), mat_print(C, ctx), printf("\n");
            printf("ansB = %ld\n", ansB);
            printf("ansC = %ld\n", ansC);
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        ctx_clear(ctx);
    }

    /* Check A * A^{-1} == A^{-1} * A == Id */

    /* Managed element type (mpq_t) */
    for (i = 0; i < 50; i++)
    {
        long n;
        ctx_t ctx;
        mat_t A, B, C, D, I;
        long ansB;

        n = n_randint(state, 20) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, n, n, ctx);
        mat_init(B, n, n, ctx);
        mat_init(C, n, n, ctx);
        mat_init(D, n, n, ctx);
        mat_init(I, n, n, ctx);

        mat_randtest(A, state, ctx);

        ansB = mat_inv(B, A, ctx);
        if (!ansB)
        {
            mat_mul(C, A, B, ctx);
            mat_mul(D, B, A, ctx);
        }

        result = (ansB || (mat_equal(C, D, ctx) && mat_is_one(C, ctx)));
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("A: \n"), mat_print(A, ctx), printf("\n");
            printf("B: \n"), mat_print(B, ctx), printf("\n");
            printf("C: \n"), mat_print(C, ctx), printf("\n");
            printf("D: \n"), mat_print(D, ctx), printf("\n");
            printf("ansB = %ld\n", ansB);
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }


    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
Exemplo n.º 19
0
score_t match_positions(const char *needle, const char *haystack, size_t *positions) {
	if (!*needle)
		return SCORE_MIN;

	int n = strlen(needle);
	int m = strlen(haystack);

	if (n == m) {
		/* Since this method can only be called with a haystack which
		 * matches needle. If the lengths of the strings are equal the
		 * strings themselves must also be equal (ignoring case).
		 */
		if (positions)
			for (int i = 0; i < n; i++)
				positions[i] = i;
		return SCORE_MAX;
	}

	if (m > 1024) {
		/*
		 * Unreasonably large candidate: return no score
		 * If it is a valid match it will still be returned, it will
		 * just be ranked below any reasonably sized candidates
		 */
		return SCORE_MIN;
	}

	score_t match_bonus[m];
	score_t D[n][m], M[n][m];

	/*
	 * D[][] Stores the best score for this position ending with a match.
	 * M[][] Stores the best possible score at this position.
	 */

	/* Which positions are beginning of words */
	char last_ch = '\0';
	for (int i = 0; i < m; i++) {
		char ch = haystack[i];

		score_t score = 0;
		if (isalnum(ch)) {
			if (!last_ch || last_ch == '/') {
				score = SCORE_MATCH_SLASH;
			} else if (last_ch == '-' || last_ch == '_' || last_ch == ' ' ||
				   (last_ch >= '0' && last_ch <= '9')) {
				score = SCORE_MATCH_WORD;
			} else if (last_ch >= 'a' && last_ch <= 'z' && ch >= 'A' && ch <= 'Z') {
				/* CamelCase */
				score = SCORE_MATCH_CAPITAL;
			} else if (last_ch == '.') {
				score = SCORE_MATCH_DOT;
			}
		}

		match_bonus[i] = score;
		last_ch = ch;
	}

	for (int i = 0; i < n; i++) {
		score_t prev_score = SCORE_MIN;
		score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;

		for (int j = 0; j < m; j++) {
			if (tolower(needle[i]) == tolower(haystack[j])) {
				score_t score = SCORE_MIN;
				if (!i) {
					score = (j * SCORE_GAP_LEADING) + match_bonus[j];
				} else if (j) { /* i > 0 && j > 0*/
					score = max(
					    M[i - 1][j - 1] + match_bonus[j],

					    /* consecutive match, doesn't stack with match_bonus */
					    D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE);
				}
				D[i][j] = score;
				M[i][j] = prev_score = max(score, prev_score + gap_score);
			} else {
				D[i][j] = SCORE_MIN;
				M[i][j] = prev_score = prev_score + gap_score;
			}
		}
	}

#ifdef DEBUG_VERBOSE
	fprintf(stderr, "\"%s\" =~ \"%s\"\n", needle, haystack);
	mat_print(&D[0][0], 'D', needle, haystack);
	mat_print(&M[0][0], 'M', needle, haystack);
	fprintf(stderr, "\n");
#endif

	/* backtrace to find the positions of optimal matching */
	if (positions) {
		int match_required = 0;
		for (int i = n - 1, j = m - 1; i >= 0; i--) {
			for (; j >= 0; j--) {
				/*
				 * There may be multiple paths which result in
				 * the optimal weight.
				 *
				 * For simplicity, we will pick the first one
				 * we encounter, the latest in the candidate
				 * string.
				 */
				if (D[i][j] != SCORE_MIN &&
				    (match_required || D[i][j] == M[i][j])) {
					/* If this score was determined using
					 * SCORE_MATCH_CONSECUTIVE, the
					 * previous character MUST be a match
					 */
					match_required =
					    i && j &&
					    M[i][j] == D[i - 1][j - 1] + SCORE_MATCH_CONSECUTIVE;
					positions[i] = j--;
					break;
				}
			}
		}
	}

	return M[n - 1][m - 1];
}
Exemplo n.º 20
0
int main(void)
{
    char *str;  /* String for the input polynomial P */
    mpoly_t P;  /* Input polynomial P */
    long n;     /* Number of variables minus one */
    long K;     /* Required t-adic precision */
    long N, Nw;
    long i, b;

    mat_t M;
    ctx_t ctxM;

    mon_t *rows, *cols;

    padic_mat_struct *C;
    fmpz_t p;

    printf("valuations... \n");
    fflush(stdout);

    /* Example 3-1-1 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 1)[1 1 1]"; */

    /* Example 4-4-2 */
    /* str = "4  [4 0 0 0] [0 4 0 0] [0 0 4 0] [0 0 0 4] (2  0 1)[3 1 0 0] (2  0 1)[1 0 1 2] (2  0 1)[0 1 0 3]"; */

    /* Example 3-3-6 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 314)[2 1 0] (2  0 42)[0 2 1] (2  0 271)[1 0 2] (2  0 -23)[1 1 1]"; */

    /* Example 3-3-2 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 1)[2 1 0] (2  0 1)[0 2 1] (2  0 1)[1 0 2]"; */

    /* Example ... */
    /* str = "4  [4 0 0 0] [0 4 0 0] [0 0 4 0] [0 0 0 4] (2  0 1)[1 1 1 1]"; */

    /* Cubic surface from AKR */
    str = "4  (1  3)[0 3 0 0] (2  0 3)[0 1 2 0] "
          "(2  0 -1)[1 1 1 0] (2  0 3)[1 1 0 1] "
          "(2  0 -1)[2 1 0 0] [0 0 3 0] (2  0 -1)[1 0 2 0] "
          "(1  2)[0 0 0 3] [3 0 0 0]";

    n  = atoi(str) - 1;
    N  = 10;
    Nw = 22;
    K  = 616;

    ctx_init_fmpz_poly_q(ctxM);

    mpoly_init(P, n + 1, ctxM);
    mpoly_set_str(P, str, ctxM);

    printf("P = "), mpoly_print(P, ctxM), printf("\n");

    b = gmc_basis_size(n, mpoly_degree(P, -1, ctxM));

    mat_init(M, b, b, ctxM);
    gmc_compute(M, &rows, &cols, P, ctxM);
    mat_print(M, ctxM);
    printf("\n");

    fmpz_init(p);
    fmpz_set_ui(p, 5);

    gmde_solve(&C, K, p, N, Nw, M, ctxM);

    printf("Valuations\n");

    for (i = 0; i < K; i++)
    {
        long v = padic_mat_val(C + i);

        if (v < LONG_MAX)
            printf("  i = %ld val = %ld val/log(i) = %f\n", i, v, 
                (i > 1) ? (double) v / log(i) : 0);
        else
            printf("  i = %ld val = +infty\n", i);
    }

    fmpz_clear(p);
    mpoly_clear(P, ctxM);
    mat_clear(M, ctxM);
    for (i = 0; i < K; i++)
        padic_mat_clear(C + i);
    free(C);
    ctx_clear(ctxM);

    return EXIT_SUCCESS;
}
Exemplo n.º 21
0
int main(void)
{
    char *str;  /* String for the input polynomial P */
    mpoly_t P;  /* Input polynomial P */
    int n;      /* Number of variables minus one */
    long K;     /* Required t-adic precision */
    long N, Nw;
    long b;     /* Matrix dimensions */
    long i, j, k;

    mat_t M;
    ctx_t ctxM;

    mon_t *rows, *cols;

    padic_mat_struct *C;
    fmpz_t p;

    fmpz_poly_mat_t B;
    long vB;

    printf("solve... \n");
    fflush(stdout);

    /* Example 3-1-1 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 1)[1 1 1]"; */

    /* Example 3-3-2 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 1)[2 1 0] (2  0 1)[0 2 1] (2  0 1)[1 0 2]"; */

    /* Example 4-4-2 */
    /* str = "4  [4 0 0 0] [0 4 0 0] [0 0 4 0] [0 0 0 4] (2  0 1)[3 1 0 0] (2  0 1)[1 0 1 2] (2  0 1)[0 1 0 3]"; */

    /* Example ... */
    /* str = "4  [4 0 0 0] [0 4 0 0] [0 0 4 0] [0 0 0 4] (2  0 1)[1 1 1 1]"; */

    /* Example from AKR */
    str = "4  (1  3)[0 3 0 0] (2  0 3)[0 1 2 0] "
          "(2  0 -1)[1 1 1 0] (2  0 3)[1 1 0 1] "
          "(2  0 -1)[2 1 0 0] [0 0 3 0] (2  0 -1)[1 0 2 0] "
          "(1  2)[0 0 0 3] [3 0 0 0]";

    n  = atoi(str) - 1;
    K  = 616;
    N  = 10;
    Nw = 22;

    fmpz_init(p);
    fmpz_set_ui(p, 5);
    ctx_init_fmpz_poly_q(ctxM);

    ctxM->print   = &__fmpz_poly_q_print_pretty;

    mpoly_init(P, n + 1, ctxM);
    mpoly_set_str(P, str, ctxM);

    printf("P = "), mpoly_print(P, ctxM), printf("\n");

    b = gmc_basis_size(n, mpoly_degree(P, -1, ctxM));

    mat_init(M, b, b, ctxM);
    fmpz_poly_mat_init(B, b, b);
    vB = 0;

    gmc_compute(M, &rows, &cols, P, ctxM);

    mat_print(M, ctxM);
    printf("\n");

    gmde_solve(&C, K, p, N, Nw, M, ctxM);
    gmde_convert_soln(B, &vB, C, K, p);

    printf("Solution to (d/dt + M) C = 0:\n");
    fmpz_poly_mat_print(B, "t");
    printf("vB = %ld\n", vB);

    gmde_check_soln(B, vB, p, N, K, M, ctxM);

    mpoly_clear(P, ctxM);
    mat_clear(M, ctxM);
    free(rows);
    free(cols);
    fmpz_poly_mat_clear(B);
    ctx_clear(ctxM);
    fmpz_clear(p);

    for (i = 0; i < K; i++)
        padic_mat_clear(C + i);
    free(C);

    _fmpz_cleanup();

    return EXIT_SUCCESS;
}
Exemplo n.º 22
0
// アレする
int mat_solve(matrix *mat1, matrix mat2, matrix mat3){
  int i, j, k, l;
  double multiplier, divisor;
  matrix a, b;

  if(check_square(mat2) != 0 || mat2.row != mat3.row || mat1->row != mat3.row || mat1->col != mat3.col ){
    return -1;
  }
  
  mat_alloc(&a, mat2.row, mat2.col);
  mat_alloc(&b, mat3.row, mat3.col);

  mat_copy(&a, mat2);
  mat_copy(&b, mat3);

  mat_print(a);
  mat_print(b);


// Gaussの消去法
  for(i=0; i<a.row; i++){
    // i+1行目以降のi列目を消す(i=0のとき、2行目〜最終行の1列目を消去)
    for(j=i+1; j<a.row; j++){
      // j行目に、i行目を何倍したら、i列目が消えるか(例えば、i=0のとき、2行目以降の行全体に、何倍すれば、1列目が0になるか)
      multiplier = a.element[j][i] / a.element[i][i];
      for(k=0; k<a.col; k++){
        a.element[j][k] = a.element[j][k] - a.element[i][k] * multiplier;
      }

      for(l=0; l<b.col; l++){
        b.element[j][l] = b.element[j][l] - b.element[i][l] * multiplier;
      }
    }

  }

// 後退代入

  // 最終行以前の行に対して
  for(i=a.row-1; i>=0; i--){

    // 例えば、今2行目なら、3列目〜最終列に対して処理を行う
    for(j=a.row-1; j>i; j--){
      divisor = a.element[i][j];
      a.element[i][j] = a.element[i][j] - divisor * a.element[j][j];
      for(l=0; l<b.col; l++){
        b.element[i][l] = b.element[i][l] - divisor * b.element[j][l];
      }
    }

    for(l=0; l<b.col; l++){
      b.element[i][l] = b.element[i][l] / a.element[i][i];
    }
    a.element[i][i] = 1.0;
  }
  mat_copy(mat1, b);

  mat_free(&a);
  mat_free(&b);

  return 0;
}
Exemplo n.º 23
0
int32_t main(int32_t argc, char *argv[]) {
    printf("<<watchlist//>>\n");

    if( init_sdl2() ) {
        return 1;
    }

    int32_t width = 1280;
    int32_t height = 720;

    SDL_Window* window;
    sdl2_window("cute3d: " __FILE__, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, &window);

    SDL_GLContext* context;
    sdl2_glcontext(3, 2, window, &context);

    if( init_shader() ) {
        return 1;
    }

    if( init_canvas(width, height) ) {
        return 1;
    }
    canvas_create("global_dynamic_canvas", &global_dynamic_canvas);

    struct Arcball arcball = {0};
    arcball_create(width, height, (Vec4f){0.0,6.0,10.0,1.0}, (Vec4f){0.0,0.0,0.0,1.0}, 0.001f, 100.0, &arcball);

    struct GameTime time = {0};
    gametime_create(1.0f / 60.0f, &time);


    Vec4f a = {0.0f, 0.0f, 1.0f};
    Vec4f b = {1.0f, 0.0f, 1.0f};
    draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){25, 255, 25, 255}, 0.01f, a, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 1.0f);
    draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 25, 25, 255}, 0.01f, b, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 1.0f);

    Quat axis_angle_rot = {0};
    quat_from_axis_angle((Vec4f)Y_AXIS, PI/4, axis_angle_rot);
    draw_quaternion(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 255, 255, 255}, (Color){255, 0, 255, 255}, 0.01f, axis_angle_rot, 2.0f);

    vec_print("axis_angle_rot: ", axis_angle_rot);
    Vec4f axis_angle_result = {0};
    vec_rotate(a, axis_angle_rot, axis_angle_result);
    draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 255, 25, 255}, 0.01f, axis_angle_result, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 2.0f);

    Vec4f axis = {0};
    float angle = 0.0f;
    quat_to_axis_angle(axis_angle_rot, axis, &angle);
    Quat axis_angle_rot2 = {0};
    quat_from_axis_angle(axis, angle, axis_angle_rot2);
    vec_print("axis_angle_rot2: ", axis_angle_rot2);

    Quat euler_angles_rot = {0};
    quat_from_euler_angles(0.0f, PI/4, 0.0f, euler_angles_rot);
    vec_print("euler_angles_rot: ", euler_angles_rot);
    Vec4f euler_angles_result = {0};
    vec_rotate(a, euler_angles_rot, euler_angles_result);
    draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){25, 255, 255, 255}, 0.01f, euler_angles_result, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 3.0f);

    Quat vec_pair_rot = {0};
    quat_from_vec_pair(a, b, vec_pair_rot);
    vec_print("vec_pair_rot: ", vec_pair_rot);
    Vec4f vec_pair_result = {0};
    vec_rotate(a, vec_pair_rot, vec_pair_result);
    draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 25, 255, 255}, 0.01f, vec_pair_result, (Vec3f){0.0f, 0.0f, 0.0f}, 1.0f, 4.0f);

    Mat xaxis_control = {0};
    xaxis_control[0] = 1; xaxis_control[4] = 0;          xaxis_control[8]  = 0;           xaxis_control[12] = 0;
    xaxis_control[1] = 0; xaxis_control[5] = cosf(PI/4); xaxis_control[9]  = -sinf(PI/4); xaxis_control[13] = 0;
    xaxis_control[2] = 0; xaxis_control[6] = sinf(PI/4); xaxis_control[10] = cosf(PI/4);  xaxis_control[14] = 0;
    xaxis_control[3] = 0; xaxis_control[7] = 0;          xaxis_control[11] = 0;           xaxis_control[15] = 1;
    mat_print("xaxis_control: ", xaxis_control);

    Quat xaxis_rot = {0};
    quat_from_axis_angle((Vec4f)X_AXIS, PI/4, xaxis_rot);
    Mat xaxis_mat = {0};
    quat_to_mat(xaxis_rot, xaxis_mat);
    mat_print("xaxis_mat: ", xaxis_mat);

    Mat yaxis_control = {0};
    yaxis_control[0] = cosf(PI/4);  yaxis_control[4] = 0; yaxis_control[8]  = sinf(PI/4); yaxis_control[12] = 0;
    yaxis_control[1] = 0;           yaxis_control[5] = 1; yaxis_control[9]  = 0;          yaxis_control[13] = 0;
    yaxis_control[2] = -sinf(PI/4); yaxis_control[6] = 0; yaxis_control[10] = cosf(PI/4); yaxis_control[14] = 0;
    yaxis_control[3] = 0;           yaxis_control[7] = 0; yaxis_control[11] = 0;          yaxis_control[15] = 1;
    mat_print("yaxis_control: ", yaxis_control);

    Quat yaxis_rot = {0};
    quat_from_axis_angle((Vec4f)Y_AXIS, PI/4, yaxis_rot);
    Mat yaxis_mat = {0};
    quat_to_mat(yaxis_rot, yaxis_mat);
    mat_print("yaxis_mat: ", yaxis_mat);

    Mat zaxis_control = {0};
    zaxis_control[0] = cosf(PI/4); zaxis_control[4] = -sinf(PI/4); zaxis_control[8]  = 0; zaxis_control[12] = 0;
    zaxis_control[1] = sinf(PI/4); zaxis_control[5] = cosf(PI/4);  zaxis_control[9]  = 0; zaxis_control[13] = 0;
    zaxis_control[2] = 0;          zaxis_control[6] = 0;           zaxis_control[10] = 1; zaxis_control[14] = 0;
    zaxis_control[3] = 0;          zaxis_control[7] = 0;           zaxis_control[11] = 0; zaxis_control[15] = 1;
    mat_print("zaxis_control: ", zaxis_control);

    Quat zaxis_rot = {0};
    quat_from_axis_angle((Vec4f)Z_AXIS, PI/4, zaxis_rot);
    Mat zaxis_mat = {0};
    quat_to_mat(zaxis_rot, zaxis_mat);
    mat_print("zaxis_mat: ", zaxis_mat);

    draw_grid(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){120, 120, 120, 255}, 0.01f, 12.0f, 12.0f, 12);

    while(true) {
        SDL_Event event;
        while( sdl2_poll_event(&event) ) {
            if( sdl2_handle_quit(event) ) {
                goto done;
            }
            sdl2_handle_resize(event);

            arcball_handle_resize(&arcball, event);
            arcball_handle_mouse(&arcball, event);
        }

        sdl2_gl_set_swap_interval(0);

        ogl_debug( glClearDepth(1.0f);
                   glClearColor(.0f, .0f, .0f, 1.0f);
                   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); );

        gametime_advance(&time, sdl2_time_delta());
        gametime_integrate(&time);

        canvas_render_layers(&global_static_canvas, 0, MAX_CANVAS_LAYERS, &arcball.camera, (Mat)IDENTITY_MAT);

        sdl2_gl_swap_window(window);
    }
Exemplo n.º 24
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("add... ");
    fflush(stdout);

    _randinit(state);

    /* Check that addition is abelian */

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C, D;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);
        mat_init(D, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_randtest(B, state, ctx);

        mat_add(C, A, B, ctx);
        mat_add(D, B, A, ctx);

        result = mat_equal(C, D, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix A\n");
            mat_print(B, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }

    /* Managed element type (mpq_t) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C, D;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);
        mat_init(D, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_randtest(B, state, ctx);

        mat_add(C, A, B, ctx);
        mat_add(D, B, A, ctx);

        result = mat_equal(C, D, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix A\n");
            mat_print(B, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }

    /* Check that the zero matrix does what it's supposed to do */

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C, D;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);
        mat_init(D, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_zero(B, ctx);

        mat_add(C, A, B, ctx);
        mat_add(D, B, A, ctx);

        result = mat_equal(A, C, ctx) && mat_equal(C, D, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix A\n");
            mat_print(B, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }

    /* Managed element type (mpq_t) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C, D;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);
        mat_init(D, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_zero(B, ctx);

        mat_add(C, A, B, ctx);
        mat_add(D, B, A, ctx);

        result = mat_equal(A, C, ctx) && mat_equal(C, D, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix A\n");
            mat_print(B, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }

    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
Exemplo n.º 25
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("zero... ");
    fflush(stdout);

    _randinit(state);

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A;

        m = n_randint(state, 100) + 1;
        n = n_randint(state, 100) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_zero(A, ctx);

        result = mat_is_zero(A, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            abort();
        }

        mat_clear(A, ctx);
        ctx_clear(ctx);
    }

    /* Managed element type (mpq_t) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A;

        m = n_randint(state, 100) + 1;
        n = n_randint(state, 100) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_zero(A, ctx);

        result = mat_is_zero(A, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            abort();
        }

        mat_clear(A, ctx);
        ctx_clear(ctx);
    }

    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
Exemplo n.º 26
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("transpose... ");
    fflush(stdout);

    _randinit(state);

    /* Check aliasing */

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C;

        m = n_randint(state, 50) + 1;
        n = m;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);

        mat_randtest(A, state, ctx);

        mat_set(B, A, ctx);
        mat_transpose(C, B, ctx);
        mat_transpose(B, B, ctx);

        result = mat_equal(B, C, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix B\n");
            mat_print(B, ctx);
            printf("\n");
            printf("Matrix C\n");
            mat_print(C, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        ctx_clear(ctx);
    }

    /* Check that (A^t)^t == A */

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, n, m, ctx);
        mat_init(C, m, n, ctx);

        mat_randtest(A, state, ctx);

        mat_transpose(B, A, ctx);
        mat_transpose(C, B, ctx);

        result = mat_equal(A, C, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix B\n");
            mat_print(B, ctx);
            printf("\n");
            printf("Matrix C\n");
            mat_print(C, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        ctx_clear(ctx);
    }

    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}