40 #define HDF5_STATUS_CHECK(status) { \
42 std::cerr << __FILE__ << ":" << __LINE__ << \
43 ": Problem with writing to file. Status code=" \
44 << status << std::endl; \
59 matrix.reset(
new double[n*n]);
72 std::memcpy(
matrix.get(), orig.
matrix.get(), n*n*
sizeof(double));
78 matrix = std::move(orig.matrix);
94 assert(
n == matrix_copy.
n);
96 std::memcpy(
matrix.get(), matrix_copy.
matrix.get(),
n*
n*
sizeof(double));
103 assert(
n == matrix_copy.n);
105 matrix = std::move(matrix_copy.matrix);
116 for(
int i = 0;i <
n*
n;++i)
248 for(
int i = 0;i <
n;++i)
268 std::unique_ptr<double []> work (
new double [lwork]);
275 std::cerr <<
"dsyev failed. info = " << info << std::endl;
287 assert(
n==2 &&
"Only works for 2x2 matrices");
297 double discr = a*a + d*d + 4*c*c - 2*a*d;
304 eig[0] = ((a+d) - std::sqrt(discr))/2;
305 eig[1] = ((a+d) + std::sqrt(discr))/2;
307 double norm = 1.0/std::sqrt(1 + (eig[0]-a)*(eig[0]-a)/(c*c));
310 matrix[1] = norm*(eig[0]-a)/c;
312 norm = 1.0/std::sqrt(1 + (eig[1]-a)*(eig[1]-a)/(c*c));
315 matrix[3] = norm*(eig[1]-a)/c;
344 std::cerr <<
"dpotrf failed. info = " << info << std::endl;
349 std::cerr <<
"dpotri failed. info = " << info << std::endl;
360 assert(
n==2 &&
"Only works for 2x2 matrices");
369 double fac = 1.0/(a*d-c*c);
405 for(
int i = 0;i <
n;++i)
406 for(
int j = i;j <
n;++j)
407 matrix[i*n+j] =
matrix[j*n+i] = (
double) rand()/RAND_MAX;
421 for(
int i = 0;i <
n;++i)
422 eigen[i] = std::sqrt(eigen[i]);
424 for(
int i = 0;i <
n;++i)
425 eigen[i] = 1.0/std::sqrt(eigen[i]);
440 dgemm_(&transA,&transB,&n,&n,&n,&alpha,hulp_c.
matrix.get(),&
n,hulp.
matrix.get(),&
n,&beta,
matrix.get(),&
n);
450 assert(
n==2 &&
"Only works for 2x2 matrices");
460 double discr = a*a + d*d + 4*c*c - 2*a*d;
467 double x1 = ((a+d) - std::sqrt(discr))/2;
468 double x2 = ((a+d) + std::sqrt(discr))/2;
470 double norm1 = 1.0/(1 + (x1-a)*(x1-a)/(c*c));
471 double norm2 = 1.0/(1 + (x2-a)*(x2-a)/(c*c));
478 x1_s = std::sqrt(x1);
479 x2_s = std::sqrt(x2);
481 x1_s = 1.0/std::sqrt(x1);
482 x2_s = 1.0/std::sqrt(x2);
485 matrix[0] = norm1*x1_s + norm2*x2_s;
486 matrix[1] =
matrix[2] = norm1*x1_s*(x1-a)/c + norm2*x2_s*(x2-a)/c;
487 matrix[3] = norm1*x1_s*(x1-a)/c*(x1-a)/c + norm2*x2_s*(x2-a)/c*(x2-a)/c;
498 for(
int i = 0;i <
n;++i)
518 dsymm_(&side,&uplo,&
n,&
n,&alpha,map.
matrix.get(),&
n,
object.matrix.get(),&
n,&beta,hulp.
matrix.get(),&
n);
522 dsymm_(&side,&uplo,&
n,&
n,&alpha,map.
matrix.get(),&
n,hulp.
matrix.get(),&
n,&beta,
matrix.get(),&
n);
535 assert(
n==2 && map.
n==2 &&
object.n==2 &&
"Only works for 2x2 matrices");
540 auto &a1 =
object.matrix[0];
541 auto &c1 =
object.matrix[1];
542 auto &d1 =
object.matrix[3];
548 matrix[0] = a2*(a1*a2+c1*c2)+c2*(a2*c1+c2*d1);
549 matrix[1] =
matrix[2] = a2*(a1*c2+c1*d2)+c2*(c1*c2+d1*d2);
550 matrix[3] = c2*(a1*c2+c1*d2)+d2*(c1*c2+d1*d2);
565 dgemm_(&trans,&trans,&
n,&
n,&
n,&alpha,A.
matrix.get(),&
n,B.
matrix.get(),&
n,&beta,
matrix.get(),&
n);
575 for(
int i = 0;i <
n;++i)
576 for(
int j = i + 1;j <
n;++j)
596 for(
int i = 0;i < matrix_p.
gn();++i)
597 for(
int j = 0;j < matrix_p.
gn();++j)
598 output << i <<
"\t" << j <<
"\t" << matrix_p(i,j) << endl;
608 hid_t file_id, dataset_id, dataspace_id, attribute_id;
611 file_id = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
614 hsize_t dimarr =
n*
n;
616 dataspace_id = H5Screate_simple(1, &dimarr, NULL);
618 dataset_id = H5Dcreate(file_id,
"matrix", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
620 status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
matrix.get() );
623 status = H5Sclose(dataspace_id);
626 dataspace_id = H5Screate(H5S_SCALAR);
628 attribute_id = H5Acreate (dataset_id,
"n", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
629 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &n );
631 status = H5Aclose(attribute_id);
634 status = H5Sclose(dataspace_id);
637 status = H5Dclose(dataset_id);
640 status = H5Fclose(file_id);
652 #ifdef __INTEL_COMPILER
660 std::unique_ptr<double []> eigenvalues(
new double [
n]);
670 int lwork = 2*n*n+6*n+1;
673 std::unique_ptr<double []> work(
new double [lwork]);
674 std::unique_ptr<int []> iwork(
new int [liwork]);
676 dsyevd_(&jobz,&uplo,&n,
matrix.get(),&
n,eigenvalues.get(),work.get(),&lwork,iwork.get(),&liwork,&info);
679 std::cerr <<
"dsyevd in sep_pm failed..." << std::endl;
683 std::unique_ptr<double []> work(
new double [lwork]);
685 dsyev_(&jobz,&uplo,&n,
matrix.get(),&
n,eigenvalues.get(),work.get(),&lwork,&info);
688 std::cerr <<
"dsyev in sep_pm failed..." << std::endl;
691 #ifdef __INTEL_COMPILER
692 if( eigenvalues[0] >= 0 )
699 if( eigenvalues[n-1] < 0)
704 while(i < n && eigenvalues[i] < 0.0)
705 eigenvalues[i++] = 0;
721 dgemm_(&transA,&transB,&n,&n,&n,&alpha,
matrix.get(),&
n,copy.
matrix.get(),&
n,&beta,p.
matrix.get(),&
n);
725 if( eigenvalues[0] >= 0 )
728 if( eigenvalues[n-1] < 0)
738 while(i < n && eigenvalues[i] < 0.0)
740 for(
int j = 0;j <
n;++j)
741 for(
int k = j;k <
n;++k)
742 m(j,k) += eigenvalues[i] * (*this)(j,i) * (*
this)(k,i);
760 assert(
n==2 &&
"Only works for 2x2 matrices");
772 double discr = a*a + d*d + 4*c*c - 2*a*d;
778 double x1 = (a+d) - std::sqrt(discr);
779 double x2 = (a+d) + std::sqrt(discr);
799 double norm = 1.0/(1 + (x1-a)*(x1-a)/(c*c));
802 neg(1,0) = neg(0,1) = x1*norm*(x1-a)/c;
803 neg(1,1) = x1*norm*(x1-a)*(x1-a)/(c*c);
double ddot_(int *n, double *x, int *incx, double *y, int *incy)
void SaveRawToFile(const std::string) const
void L_map(const Matrix &, const Matrix &)
#define HDF5_STATUS_CHECK(status)
Matrix & operator=(const Matrix &)
Matrix & mprod(const Matrix &, const Matrix &)
void dsymm_(char *side, char *uplo, int *m, int *n, double *alpha, double *A, int *lda, double *B, int *ldb, double *beta, double *C, int *ldc)
std::ostream & operator<<(std::ostream &output, const doci2DM::BlockStructure< MyBlockType > &blocks_p)
void dgemm_(char *transA, char *transB, int *m, int *n, int *k, double *alpha, double *A, int *lda, double *B, int *ldb, double *beta, double *C, int *ldc)
void mdiag(const Vector &)
void dsyev_(char *jobz, char *uplo, int *n, double *A, int *lda, double *W, double *work, int *lwork, int *info)
double & operator()(int i, int j)
void dsyevd_(char *jobz, char *uplo, int *n, double *a, int *lda, double *w, double *work, int *lwork, int *iwork, int *liwork, int *info)
void daxpy_(int *n, double *alpha, double *x, int *incx, double *y, int *incy)
void sep_pm_2x2(Matrix &, Matrix &)
Matrix & operator/=(double)
Matrix & operator*=(double)
void dpotrf_(char *uplo, int *n, double *A, int *lda, int *INFO)
double ddot(const Matrix &) const
int n
dimension of the matrix
Matrix & operator-=(const Matrix &)
void L_map_2x2(const Matrix &, const Matrix &)
void sqrt_2x2(int option)
Matrix & operator+=(const Matrix &)
std::unique_ptr< double[]> matrix
pointer of doubles, contains the numbers, the matrix
void dscal_(int *n, double *alpha, double *x, int *incx)
void sep_pm(Matrix &, Matrix &)
void dpotri_(char *uplo, int *n, double *A, int *lda, int *INFO)
Matrix & daxpy(double alpha, const Matrix &)