int main() { int result; int year, month, day, hour1, hour2, min1, min2, sec1, sec2; long int min; year = 2012; month = 6; day = 5; hour1 = 12; min1 = 30; sec1 = 0; hour2 = 16; min2 = 30; sec2 = 0; min = 30; result = patients(year, month, day, hour1, min1, sec1, hour2, min2, sec2, min); printf("Number of patients %d", result); return 0; }
int main() { int i,j,k; double s; #if 1 std::vector< std::vector< std::vector<double> > > patients(count); for(i = 0; i<count; ++i) { std::cout << files[i] << std::endl; patients[i] = Patient(folder_name + files[i]).data; } gsl_matrix* A_avg = gsl_matrix_alloc(3, 64); for(i = 0; i<3; ++i) { for(j = 0; j<64; ++j) { s = 0; for(k = 0; k < count; ++k) { s+=patients[k][i][j]; } gsl_matrix_set(A_avg, i, j, s/count); } } // gsl_matrix_print(A_avg, 3, 64); gsl_matrix* S = gsl_matrix_alloc(3, 64); for(i = 0; i<3; ++i) { for(j = 0; j<64; ++j) { s = 0; for(k = 0; k < count; ++k) { s += pow((patients[k][i][j] - gsl_matrix_get(A_avg, i, j)), 2); } gsl_matrix_set(S, i, j, sqrt(s/count)); } } // gsl_matrix_print(S, 3, 64); gsl_matrix *T[count]; for(k = 0; k < count; ++k) { T[k] = gsl_matrix_alloc(3, 64); for(i = 0; i<3; ++i) { for(j = 0; j<64; ++j) { s = (patients[k][i][j] - gsl_matrix_get(A_avg, i, j)) / gsl_matrix_get(S, i, j); gsl_matrix_set(T[k], i, j, s); } } } size_t cube_sizes[3] = {3, 64, count}; Tensor<3> cube(cube_sizes); size_t matrix_sizes[3] = {cube.size(), 1}; Tensor<2> matrix(matrix_sizes); int M, N; std::cout << cube.size() << std::endl; for (size_t offset = 0; offset < cube.size(); ++offset) { i = offset/(3*64); j = (offset%(3*64))/64; k = (offset%(3*64))%64; cube.data[offset] = gsl_matrix_get(T[i], j, k); // patients[i][j][k]; } #else size_t cube_sizes[3] = {3, 3, 3}; Tensor<3> cube(cube_sizes); size_t matrix_sizes[3] = {cube.size(), 1}; Tensor<2> matrix(matrix_sizes); int M, N; double tdata[27] = { 0.9073, 0.7158, -0.3698, 0.8924, -0.4898, 2.4288, 2.1488, 0.3054, 2.3753, 1.7842, 1.6970, 0.0151, 1.7753, -1.5077, 4.0337, 4.2495, 0.3207, 4.7146, 2.1236, -0.0740, 1.4429, -0.6631, 1.9103, -1.7495, 1.8260, 2.1335, -0.2716 }; for (size_t offset = 0; offset < cube.size(); ++offset) { cube.data[offset] = tdata[offset]; } #endif /* FLATTEN X */ // std::cout << "X flatten:" << std::endl; cube.flatten(matrix, 0); // print(matrix); M = (int) matrix.sizes[1]; N = (int) matrix.sizes[0]; gsl_matrix* Ax = TensorToGSLMatrix(matrix); gsl_matrix* Ux; gsl_matrix* Vx; gsl_vector* Sx; gsl_vector* workx; //gsl_matrix_print(Ax, Ax->size1, Ax->size2); workx = gsl_vector_alloc(N); Sx = gsl_vector_alloc(N); Vx = gsl_matrix_alloc(N, N); gsl_linalg_SV_decomp(Ax, Vx, Sx, workx); Ux = Ax; //std::cout << "A = U S V^T" << std::endl; //std::cout << "U" << std::endl; //gsl_matrix_print(Ux, M, N); std::cout << "S1" << std::endl; gsl_vector_print(Sx, N); //std::cout << "V" << std::endl; //gsl_matrix_print(Vx, Vx->size1, Vx->size2); //std::cout << "work" << std::endl; //gsl_vector_print(workx, N); if (M < N) { Vx = gsl_matrix_alloc(Ux->size1, Ux->size1); for(i = 0; i < Ux->size1; ++i) { for(j = 0; j < Ux->size1; ++j) { if (j < Ux->size2 ) gsl_matrix_set(Vx, j, i, gsl_matrix_get(Ux,i,j)); else gsl_matrix_set(Vx, j, i, 0.0); } } } /* FLATTEN Y */ //std::cout << "Y flatten:" << std::endl; cube.flatten(matrix, 1); //print(matrix); M = (int) matrix.sizes[1]; N = (int) matrix.sizes[0]; gsl_matrix* Ay = TensorToGSLMatrix(matrix); gsl_matrix* Uy; gsl_matrix* Vy; gsl_vector* Sy; gsl_vector* worky; //gsl_matrix_print(Ay, Ay->size1, Ay->size2); worky = gsl_vector_alloc(Ay->size2); Sy = gsl_vector_alloc(Ay->size2); Vy = gsl_matrix_alloc(Ay->size2, Ay->size2); gsl_linalg_SV_decomp(Ay, Vy, Sy, worky); Uy = Ay; //std::cout << "A = U S V^T" << std::endl; //std::cout << "U" << std::endl; //gsl_matrix_print(Uy, Uy->size1, Uy->size2); std::cout << "S2" << std::endl; gsl_vector_print(Sy, Ay->size2); //std::cout << "V" << std::endl; //gsl_matrix_print(Vy, Vy->size1, Vy->size2); //std::cout << "work" << std::endl; //gsl_vector_print(worky, Ay->size2); if (M < N) { Vy = gsl_matrix_alloc(Uy->size1, Uy->size1); for(i = 0; i < Uy->size1; ++i) { for(j = 0; j < Uy->size1; ++j) { if (j < Uy->size2 ) gsl_matrix_set(Vy, j, i, gsl_matrix_get(Uy,i,j)); else gsl_matrix_set(Vy, j, i, 0.0); } } } /* FLATTEN Z */ //std::cout << "Z flatten:" << std::endl; cube.flatten(matrix, 2); //print(matrix); M = (int) matrix.sizes[1]; N = (int) matrix.sizes[0]; gsl_matrix* Az = TensorToGSLMatrix(matrix); gsl_matrix* Uz; gsl_matrix* Vz; gsl_vector* Sz; gsl_vector* workz; //gsl_matrix_print(Az, Az->size1, Az->size2); workz = gsl_vector_alloc(Az->size2); Sz = gsl_vector_alloc(Az->size2); Vz = gsl_matrix_alloc(Az->size2, Az->size2); gsl_linalg_SV_decomp(Az, Vz, Sz, workz); Uz = Az; //std::cout << "A = U S V^T" << std::endl; //std::cout << "U" << std::endl; //gsl_matrix_print(Uz, Uz->size2, Uz->size2); std::cout << "S3" << std::endl; gsl_vector_print(Sz, Az->size2); //std::cout << "V" << std::endl; //gsl_matrix_print(Vz, Vz->size1, Vz->size2); //std::cout << "work" << std::endl; //gsl_vector_print(workz, Az->size2); if (M < N) { Vz = gsl_matrix_alloc(Uz->size1, Uz->size1); for(i = 0; i < Uz->size1; ++i) { for(j = 0; j < Uz->size1; ++j) { if (j < Uy->size2 ) gsl_matrix_set(Vz, j, i, gsl_matrix_get(Uz,i,j)); else gsl_matrix_set(Vz, j, i, 0.0); } } } /* gsl_matrix *Vx_t, *Uy_t, *Vz_t; Vx_t = gsl_matrix_alloc(Vx->size2, Vx->size1); gsl_matrix_transpose_memcpy (Vx_t, Vx); std::cout << "M = " << Uy->size1 << " N = " << Uy->size2 << std::endl; Uy_t = gsl_matrix_alloc(Uy->size1, Uy->size1); for(i = 0; i < Uy->size1; ++i) { for(j = 0; j < Uy->size1; ++j) { if (j < Uy->size2 ) gsl_matrix_set(Uy_t, j, i, gsl_matrix_get(Uy,i,j)); else gsl_matrix_set(Uy_t, j, i, 0.0); } } //gsl_matrix_transpose_memcpy (Uy_t, Uy); gsl_matrix_print(Uy_t,64,64); Vz_t = gsl_matrix_alloc(Vz->size2, Vz->size1); gsl_matrix_transpose_memcpy (Vz_t, Vz); std::cout << "M = " << Uy_t->size1 << " N = " << Uy_t->size2 << std::endl; */ Tensor<3> G(cube_sizes); /* compute G */ { gsl_matrix *Vs[3] = {Vx, Vy, Vz}; double component, summand; for(IndexIterator<3> IJK(cube_sizes); IJK; ++IJK) { component = 0.0; for(IndexIterator<3> ijk(cube_sizes); ijk; ++ijk) { summand = cube[*ijk]; for (size_t dim = 0; dim < 3; ++dim) { summand *= gsl_matrix_get(Vs[dim], (*IJK)[dim], (*ijk)[dim]); } component += summand; } G[*IJK] = component; } } /* HOSVD(cube) = G * Vs[0] * Vs[1] * Vs[2] */ /* validate result */ { Tensor<3> T(cube_sizes); gsl_matrix *Vs[3] = {Vx, Vy, Vz}; double component, summand; for(IndexIterator<3> ijk(cube_sizes); ijk; ++ijk) { component = 0.0; for(IndexIterator<3> IJK(cube_sizes); IJK; ++IJK) { summand = G[*IJK]; for (size_t dim = 0; dim < 3; ++dim) { summand *= gsl_matrix_get(Vs[dim], (*IJK)[dim], (*ijk)[dim]); } component += summand; } T[*ijk] = component; } /* T is restored cube tensor */ bool valid_HOSVD = true; for(IndexIterator<3> ijk(cube_sizes); ijk; ++ijk) { if (!nearly_equal(cube[*ijk], T[*ijk], 1e-3)) { // wrong HOSVD valid_HOSVD = false; printf("Error in HOSVD: ["); for (size_t dim = 0; dim < 3; ++dim) { printf("%d%c ", (*ijk)[dim], dim == 2 ? ']' : ','); } printf("%.15lf != %.15lf\n", cube[*ijk], T[*ijk]); } } if (valid_HOSVD) { printf("OK: HOSVD work!\n"); } else { printf("HOSVD computation FAILED.\n"); } } gsl_matrix *Vx_t, *tmp[count], *Z[count], *Z_avg; Vx_t = gsl_matrix_alloc(Vx->size2, Vx->size1); gsl_matrix_transpose_memcpy (Vx_t, Vx); gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, tmp[i], Vy, 0.0, Z[i]); }
int main() { int i,j,k; double s; std::vector< std::vector< std::vector<double> > > patients(count); for(i = 0; i<count; ++i) { std::cout << files[i] << std::endl; patients[i] = Patient(files[i]).data; } gsl_matrix* A_avg = gsl_matrix_alloc(3, 64); for(i = 0; i<3; ++i) { for(j = 0; j<64; ++j) { s = 0; for(k = 0; k < count; ++k) { s+=patients[k][i][j]; } gsl_matrix_set(A_avg, i, j, s/count); } } //std::cout << "A_avg" << std::endl; //gsl_matrix_print(A_avg, 3, 64); gsl_matrix* S = gsl_matrix_alloc(3, 64); for(i = 0; i<3; ++i) { for(j = 0; j<64; ++j) { s = 0; for(k = 0; k < count; ++k) { s += pow((patients[k][i][j] - gsl_matrix_get(A_avg, i, j)), 2); } gsl_matrix_set(S, i, j, sqrt(s/count)); } } // std::cout << "S" << std::endl; gsl_matrix_print(S, 3, 64); gsl_matrix *T[count]; for(k = 0; k < count; ++k) { T[k] = gsl_matrix_alloc(3, 64); for(i = 0; i<3; ++i) { for(j = 0; j<64; ++j) { s = (patients[k][i][j] - gsl_matrix_get(A_avg, i, j)) / gsl_matrix_get(S, i, j); gsl_matrix_set(T[k], i, j, s); } } } size_t cube_sizes[3] = {3, 64, count}; Tensor<3> cube(cube_sizes); size_t matrix_sizes[3] = {cube.size(), 1}; Tensor<2> matrix(matrix_sizes); int M, N; std::cout << cube.size() << std::endl; for (size_t offset = 0; offset < cube.size(); ++offset) { i = offset/(3*64); //0...20 j = (offset%(3*64))/64; //0...3 k = (offset%(3*64))%64; //0...64 cube.data[offset] = gsl_matrix_get(T[i], j, k); // patients[i][j][k]; } // start calculating, flatten std::cout << "HOSVD 3x(64*20)" << std::endl; cube.flatten(matrix, 0); gsl_matrix* Ax = TensorToGSLMatrix(matrix); gsl_matrix* Ux; gsl_matrix* Vx; gsl_vector* Sx; gsl_vector* workx; //gsl_matrix_print(Ax, 1536, 3); N = Ax->size2; workx = gsl_vector_alloc(N); Sx = gsl_vector_alloc(N); Vx = gsl_matrix_alloc(N, N); gsl_linalg_SV_decomp(Ax, Vx, Sx, workx); //gsl_matrix_transpose (Vx); std::cout << "S1" << std::endl; gsl_vector_print(Sx, N); std::cout << "V" << std::endl; gsl_matrix_print(Vx, N , N); //std::cout << "work" << std::endl; //gsl_vector_print(workx, N); //Нам нужен Vx, это наше U; V(3x3) /* FLATTEN HOSVD 64x(3*20) */ std::cout << "HOSVD 64x(3*24)" << std::endl; cube.flatten(matrix, 1); //print(matrix); gsl_matrix* Ay = TensorToGSLMatrix(matrix); gsl_matrix* Uy; gsl_matrix* Vy; gsl_vector* Sy; gsl_vector* worky; N = Ay->size2; //gsl_matrix_print(Ay, Ay->size1, Ay->size2); worky = gsl_vector_alloc(N); Sy = gsl_vector_alloc(N); Vy = gsl_matrix_alloc(N, N); gsl_linalg_SV_decomp(Ay, Vy, Sy, worky); //gsl_matrix_transpose(Vy); std::cout << "S2" << std::endl; gsl_vector_print(Sy, N); //std::cout << "V" << std::endl; //gsl_matrix_print(Vy, Vy->size1, Vy->size2); //Нам нужен Vy, это наше V; V(3x3) /* FLATTEN HOSVD 20x(3*64) */ std::cout << "HOSVD 20x(3*64)" << std::endl; cube.flatten(matrix, 2); //print(matrix); gsl_matrix* Az = TensorToGSLMatrix(matrix); gsl_matrix* Uz; gsl_matrix* Vz; gsl_vector* Sz; gsl_vector* workz; N = Az->size2; //gsl_matrix_print(Ay, Ay->size1, Ay->size2); workz = gsl_vector_alloc(N); Sz = gsl_vector_alloc(N); Vz = gsl_matrix_alloc(N, N); gsl_linalg_SV_decomp(Az, Vz, Sz, workz); // gsl_matrix_transpose(Vz); std::cout << "S3" << std::endl; gsl_vector_print(Sz, Az->size2); //std::cout << "V" << std::endl; //gsl_matrix_print(Vz, Vz->size1, Vz->size2); //Нам нужен Vz, это наше W; W(20x20) gsl_matrix *Vx_t, *tmp[count], *Z[count], *Z_avg; Vx_t = gsl_matrix_alloc(Vx->size2, Vx->size1); gsl_matrix_transpose (Vz); for(i =0; i<count; ++i) { Z[i] = gsl_matrix_alloc(3,64); tmp[i] = gsl_matrix_alloc(3,64); gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, Vx_t, T[i], 0.0, tmp[i]); gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, tmp[i], Vy, 0.0, Z[i]); } std::cout << "qqq111" <<std::endl; for(i = 0; i<1; ++i) { for(k =0; k<10; ++k) { for(j =0; j<3; ++j) { std::cout << gsl_matrix_get(Z[i], j, k) << " "; } std::cout << std::endl; } // if(i == 11) std::cout << std::endl; } return 0; }