コード例 #1
0
ファイル: alomax_matrix_svd.c プロジェクト: Fran89/seiscomp3
/**
 * Clean up - free allocated matrices and vectors
 */
void clean_SingularValueDecomposition() {
    if (singular_values != NULL) {
        free_vector_double(singular_values);
        singular_values = NULL;
    }
    if (U_matrix != NULL) {
        int nu = Math_min(num_rows, num_columns);
        free_matrix_double(U_matrix, num_rows, nu);
        U_matrix = NULL;
    }
    if (V_matrix != NULL) {
        free_matrix_double(V_matrix, num_columns, num_columns);
        V_matrix = NULL;
    }
}
コード例 #2
0
ファイル: matrix_statistics.c プロジェクト: Fran89/seiscomp3
Ellipsoid3D CalcErrorEllipsoid(Mtrx3D *pcov, double del_chi_2) {
    int ndx, iSwitched;
    MatrixDouble A_matrix, V_matrix;
    VectorDouble W_vector;
    double wtemp, vtemp;
    Ellipsoid3D ell;

    int ierr = 0;


    /* allocate A mtrx */
    A_matrix = matrix_double(3, 3);

    /* load A matrix in NumRec format */
    A_matrix[0][0] = pcov->xx;
    A_matrix[0][1] = A_matrix[1][0] = pcov->xy;
    A_matrix[0][2] = A_matrix[2][0] = pcov->xz;
    A_matrix[1][1] = pcov->yy;
    A_matrix[1][2] = A_matrix[2][1] = pcov->yz;
    A_matrix[2][2] = pcov->zz;


    /* allocate V mtrx and W vector */
    V_matrix = matrix_double(3, 3);
    W_vector = vector_double(3);

    /* do SVD */
    //if ((istat = nll_svdcmp0(A_matrix, 3, 3, W_vector, V_matrix)) < 0) {
    svd_helper(A_matrix, 3, 3, W_vector, V_matrix);
    if (W_vector[0] < SMALL_DOUBLE || W_vector[1] < SMALL_DOUBLE || W_vector[2] < SMALL_DOUBLE) {
        fprintf(stderr, "ERROR: invalid SVD singular value for confidence ellipsoids.");
        ierr = 1;
    } else {

        /* sort by singular values W */
        iSwitched = 1;
        while (iSwitched) {
            iSwitched = 0;
            for (ndx = 0; ndx < 2; ndx++) {
                if (W_vector[ndx] > W_vector[ndx + 1]) {
                    wtemp = W_vector[ndx];
                    W_vector[ndx] = W_vector[ndx + 1];
                    W_vector[ndx + 1] = wtemp;
                    vtemp = V_matrix[0][ndx];
                    V_matrix[0][ndx] = V_matrix[0][ndx + 1];
                    V_matrix[0][ndx + 1] = vtemp;
                    vtemp = V_matrix[1][ndx];
                    V_matrix[1][ndx] = V_matrix[1][ndx + 1];
                    V_matrix[1][ndx + 1] = vtemp;
                    vtemp = V_matrix[2][ndx];
                    V_matrix[2][ndx] = V_matrix[2][ndx + 1];
                    V_matrix[2][ndx + 1] = vtemp;
                    iSwitched = 1;
                }
            }
        }


        /* calculate ellipsoid axes */
        /* length: w in Num Rec, 2nd ed, fig 15.6.5 must be replaced
                by 1/sqrt(w) since we are using SVD of Cov mtrx and not
                SVD of A mtrx (compare eqns 2.6.1  & 15.6.10) */

        ell.az1 = atan2(V_matrix[0][0], V_matrix[1][0]) * RA2DE;
        if (ell.az1 < 0.0)
            ell.az1 += 360.0;
        ell.dip1 = asin(V_matrix[2][0]) * RA2DE;
        ell.len1 = sqrt(del_chi_2) / sqrt(1.0 / W_vector[0]);
        ell.az2 = atan2(V_matrix[0][1], V_matrix[1][1]) * RA2DE;
        if (ell.az2 < 0.0)
            ell.az2 += 360.0;
        ell.dip2 = asin(V_matrix[2][1]) * RA2DE;
        ell.len2 = sqrt(del_chi_2) / sqrt(1.0 / W_vector[1]);
        ell.len3 = sqrt(del_chi_2) / sqrt(1.0 / W_vector[2]);

    }

    free_matrix_double(A_matrix, 3, 3);
    free_matrix_double(V_matrix, 3, 3);
    free_vector_double(W_vector);

    if (ierr) {
        Ellipsoid3D EllipsoidNULL = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
        return (EllipsoidNULL);
    }

    return (ell);

}
コード例 #3
0
ファイル: main.c プロジェクト: warelle/rdft_performance
void run(int dat, int opt, int exe, int band_size, int x_axis){
  alloc_matrix_double(&a,MATRIX_SIZE);
  alloc_vector_double(&b,MATRIX_SIZE);
  alloc_vector_double(&x,MATRIX_SIZE);

  alloc_matrix_double(&d_a,MATRIX_SIZE);
  alloc_vector_double(&d_b,MATRIX_SIZE);
  alloc_vector_double(&d_x,MATRIX_SIZE);

  generate_linear_system(a,x,b, 1.0, band_size);

  if(exe & (GENP | GENP_ITERATION)){
    int dim = MATRIX_SIZE;
    int inc = 1;
    double minus1 = -1;

    copy_linear_system(d_a,d_x,d_b, a,x,b);

    alloc_vector_double(&d_x_np, MATRIX_SIZE);

    solve_no_pivoting(d_a, d_b, d_x_np, NULL, NULL);

    daxpy_(&dim, &minus1, d_x, &inc, d_x_np, &inc);
    d_np_err = dnrm2_(&dim, d_x_np, &inc);

    free_vector_double(&d_x_np);
  }
  if(exe & (RDFT | RDFT_ITERATION)){
    int dim = MATRIX_SIZE;
    int inc = 1;
    double minus1 = -1;
    dcomplex *fra=NULL, *frb=NULL, *r=NULL;

    copy_linear_system(d_a,d_x,d_b, a,x,b);

    alloc_vector_double(&d_x_rdft, MATRIX_SIZE);
    alloc_vector_double(&d_x_rdft_iter, MATRIX_SIZE);
    alloc_vector_double(&d_x_rdft_iter_another, MATRIX_SIZE);

    alloc_matrix_complex_double(&fra, MATRIX_SIZE);
    alloc_vector_complex_double(&r, MATRIX_SIZE);
    alloc_vector_complex_double(&frb, MATRIX_SIZE);
    //rdft_original_slow(d_a, d_b, d_x_rdft, d_x_rdft_iter, d_x_rdft_iter_another);
    fftw_rdft_original(d_a, d_b, d_x_rdft, d_x_rdft_iter, d_x_rdft_iter_another, fra, r, frb);

    daxpy_(&dim, &minus1, d_x, &inc, d_x_rdft, &inc);
    daxpy_(&dim, &minus1, d_x, &inc, d_x_rdft_iter, &inc);
    daxpy_(&dim, &minus1, d_x, &inc, d_x_rdft_iter_another, &inc);
    d_rdft_err              = dnrm2_(&dim, d_x_rdft, &inc);
    d_rdft_iter_err         = dnrm2_(&dim, d_x_rdft_iter, &inc);
    d_rdft_iter_another_err = dnrm2_(&dim, d_x_rdft_iter_another, &inc);

    free_matrix_complex_double(&fra);
    free_vector_complex_double(&r);
    free_vector_complex_double(&frb);

    free_vector_double(&d_x_rdft);
    free_vector_double(&d_x_rdft_iter);
    free_vector_double(&d_x_rdft_iter_another);
  }
  /*
  if(exe & (RDFT_PERM | RDFT_PERM_ITERATION)){}
  if(exe & (RDFT_GIVENS | RDFT_GIVENS_ITERATION)){}
  */
  if(exe & (RDFT_GIVENS_TWO | RDFT_GIVENS_TWO_ITERATION)){
    int dim = MATRIX_SIZE;
    int inc = 1;
    double minus1 = -1;
    dcomplex *fra,*r,*frb;
    double *ass;

    copy_linear_system(d_a,d_x,d_b, a,x,b);

    alloc_vector_double(&d_x_rdft_givens_two, MATRIX_SIZE);
    alloc_vector_double(&d_x_rdft_givens_two_iter, MATRIX_SIZE);
    alloc_vector_double(&d_x_rdft_givens_two_iter_another, MATRIX_SIZE);

    alloc_matrix_complex_double(&fra, MATRIX_SIZE);
    alloc_vector_complex_double(&r, MATRIX_SIZE);
    alloc_vector_complex_double(&frb, MATRIX_SIZE);
    alloc_matrix_double(&ass, MATRIX_SIZE);
    fftw_rdft_right_two_givens(d_a, d_b, d_x_rdft_givens_two, d_x_rdft_givens_two_iter, d_x_rdft_givens_two_iter_another, fra,r,frb, ass);

    daxpy_(&dim, &minus1, d_x, &inc, d_x_rdft_givens_two, &inc);
    daxpy_(&dim, &minus1, d_x, &inc, d_x_rdft_givens_two_iter, &inc);
    daxpy_(&dim, &minus1, d_x, &inc, d_x_rdft_givens_two_iter_another, &inc);
    d_rdft_givens_two_err              = dnrm2_(&dim, d_x_rdft_givens_two, &inc);
    d_rdft_givens_two_iter_err         = dnrm2_(&dim, d_x_rdft_givens_two_iter, &inc);
    d_rdft_givens_two_iter_another_err = dnrm2_(&dim, d_x_rdft_givens_two_iter_another, &inc);

    free_matrix_complex_double(&fra);
    free_vector_complex_double(&r);
    free_matrix_complex_double(&frb);
    free_matrix_double(&ass);

    free_vector_double(&d_x_rdft_givens_two);
    free_vector_double(&d_x_rdft_givens_two_iter);
    free_vector_double(&d_x_rdft_givens_two_iter_another);
  }
  /*
  if(exe & (RDFT_BOTH_GIVENS | RDFT_BOTH_GIVENS_ITERATION)){}
  */

  if(exe & (GAUSS | GAUSS_ITERATION)){
    int dim = MATRIX_SIZE;
    int inc = 1;
    double minus1 = -1;
    double *g=NULL,*ga=NULL;

    copy_linear_system(d_a,d_x,d_b, a,x,b);

    alloc_vector_double(&d_x_gauss, MATRIX_SIZE);
    alloc_vector_double(&d_x_gauss_iter, MATRIX_SIZE);
    alloc_vector_double(&d_x_gauss_iter_another, MATRIX_SIZE);

    alloc_matrix_double(&ga, MATRIX_SIZE);
    alloc_matrix_double(&g, MATRIX_SIZE);
    solve_with_gauss_iteration_double(d_a, d_b, d_x_gauss, d_x_gauss_iter, d_x_gauss_iter_another, g,ga);

    daxpy_(&dim, &minus1, d_x, &inc, d_x_gauss, &inc);
    daxpy_(&dim, &minus1, d_x, &inc, d_x_gauss_iter, &inc);
    daxpy_(&dim, &minus1, d_x, &inc, d_x_gauss_iter_another, &inc);
    d_gauss_err              = dnrm2_(&dim, d_x_gauss, &inc);
    d_gauss_iter_err         = dnrm2_(&dim, d_x_gauss_iter, &inc);
    d_gauss_iter_another_err = dnrm2_(&dim, d_x_gauss_iter_another, &inc);

    free_matrix_double(&g);
    free_matrix_double(&ga);

    free_vector_double(&d_x_gauss);
    free_vector_double(&d_x_gauss_iter);
    free_vector_double(&d_x_gauss_iter_another);
  }
  if(exe & (PP | PP_ITERATION)){
    int dim = MATRIX_SIZE;
    int inc = 1;
    double minus1 = -1;

    copy_linear_system(d_a,d_x,d_b, a,x,b);

    alloc_vector_double(&d_x_pp, MATRIX_SIZE);

    solve_with_partial_pivot(d_a, d_b, d_x_pp, NULL, NULL);

    daxpy_(&dim, &minus1, d_x, &inc, d_x_pp, &inc);
    d_pp_err = dnrm2_(&dim, d_x_pp, &inc);

    free_vector_double(&d_x_pp);
  }

  free_matrix_double(&d_a);
  free_vector_double(&d_b);
  free_vector_double(&d_x);
  free_matrix_double(&a);
  free_vector_double(&b);
  free_vector_double(&x);

	// opt
	//   1: graph data
	//   2: readable data
  if(opt == 1){ // graph data
    printf("%d ", x_axis);
    printf("%d ", band_size);
    printf("%d ", 2*band_size-1);
    print_double(d_rdft_err);
    printf(" ");
    print_double(d_rdft_iter_err);
    printf(" ");
    print_double(d_rdft_iter_another_err);
    printf(" ");
    print_double(d_rdft_perm_err);
    printf(" ");
    print_double(d_rdft_perm_iter_err);
    printf(" ");
    print_double(d_rdft_perm_iter_another_err);
    printf(" ");
    print_double(d_rdft_givens_err);
    printf(" ");
    print_double(d_rdft_givens_iter_err);
    printf(" ");
    print_double(d_rdft_givens_iter_another_err);
    printf(" ");
    print_double(d_rdft_givens_two_err);
    printf(" ");
    print_double(d_rdft_givens_two_iter_err);
    printf(" ");
    print_double(d_rdft_givens_two_iter_another_err);
    printf(" ");
    print_double(d_rdft_both_givens_err);
    printf(" ");
    print_double(d_rdft_both_givens_iter_err);
    printf(" ");
    print_double(d_rdft_both_givens_iter_another_err);
    printf(" ");
    print_double(d_gauss_err);
    printf(" ");
    print_double(d_gauss_iter_err);
    printf(" ");
    print_double(d_gauss_iter_another_err);
    printf(" ");
    print_double(d_pp_err);
    printf(" ");
    print_double(d_pp_iter_err);
    printf(" ");
    print_double(d_pp_iter_another_err);
    printf(" ");
    print_double(d_np_err);
    printf("\n");
  }else if(opt == 2){
    if(exe & RDFT){
      printf("RDFT               :");
      print_double(d_rdft_err);
      printf("\n");
    }
    if(exe & RDFT_ITERATION){
      printf("RDFT iteration     :");
      print_double(d_rdft_iter_err);
      printf("\n");
      printf("RDFT iteration     :");
      print_double(d_rdft_iter_another_err);
      printf("\n");
    }
    if(exe & RDFT_PERM){
      printf("RDFT PERM          :");
      print_double(d_rdft_perm_err);
      printf("\n");
    }
    if(exe & RDFT_PERM_ITERATION){
      printf("RDFT PERM iteration:");
      print_double(d_rdft_perm_iter_err);
      printf("\n");
      printf("RDFT PERM iteration:");
      print_double(d_rdft_perm_iter_another_err);
      printf("\n");
    }
    if(exe & RDFT_GIVENS){
      printf("RDFT GVN           :");
      print_double(d_rdft_givens_err);
      printf("\n");
    }
    if(exe & RDFT_GIVENS_ITERATION){
      printf("RDFT GVN iteration :");
      print_double(d_rdft_givens_iter_err);
      printf("\n");
      printf("RDFT GVN iteration :");
      print_double(d_rdft_givens_iter_another_err);
      printf("\n");
    }
    if(exe & RDFT_GIVENS_TWO){
      printf("RDFT GTWO          :");
      print_double(d_rdft_givens_two_err);
      printf("\n");
    }
    if(exe & RDFT_GIVENS_TWO_ITERATION){
      printf("RDFT GTWO iteration:");
      print_double(d_rdft_givens_two_iter_err);
      printf("\n");
      printf("RDFT GTWO iteration:");
      print_double(d_rdft_givens_two_iter_another_err);
      printf("\n");
    }
    if(exe & RDFT_BOTH_GIVENS){
      printf("RDFT BOTH          :");
      print_double(d_rdft_both_givens_err);
      printf("\n");
    }
    if(exe & RDFT_BOTH_GIVENS_ITERATION){
      printf("RDFT BOTH iteration:");
      print_double(d_rdft_both_givens_iter_err);
      printf("\n");
      printf("RDFT BOTH iteration:");
      print_double(d_rdft_both_givens_iter_another_err);
      printf("\n");
    }
    if(exe & GAUSS){
      printf("GAUSS              :");
      print_double(d_gauss_err);
      printf("\n");
    }
    if(exe & GAUSS_ITERATION){
      printf("GAUSS iteration    :");
      print_double(d_gauss_iter_err);
      printf("\n");
      printf("GAUSS iteration    :");
      print_double(d_gauss_iter_another_err);
      printf("\n");
    }
    if(exe & GENP){
      printf("GENP               :");
      print_double(d_np_err);
      printf("\n");
    }
    if(exe & GENP_ITERATION){
      printf("GENP               :");
      print_double(d_np_iter_err);
      printf("\n");
      printf("GENP               :");
      print_double(d_np_iter_another_err);
      printf("\n");
    }
    if(exe & PP){
      printf("Partial Pivot      :");
      print_double(d_pp_err);
      printf("\n");
    }
  }
}