30 #define HDF5_STATUS_CHECK(status) { \
32 std::cerr << __FILE__ << ":" << __LINE__ << \
33 ": Problem with writing to file. Status code=" \
34 << status << std::endl; \
58 mat.reset(
new double [
n*
m]);
68 mat.reset(
new double [
n*
m]);
80 mat = std::move(orig.mat);
87 mat.reset(
new double [
n*
m]);
98 for(
int i=0;i<
n*
m;i++)
161 assert(A.
n ==
n && B.
m ==
m);
163 dgemm_(&trans,&trans,&A.
n,&B.
m,&A.
m,&alpha,A.
mat.get(),&A.
n,B.
mat.get(),&B.
n,&beta,
mat.get(),&A.
n);
178 int count_sing = std::min(
n,
m);
180 std::unique_ptr<double []> sing_vals(
new double[count_sing]);
183 int lwork = std::max( 3*count_sing + std::max(
n,
m), 5*count_sing);
184 std::unique_ptr<double []> work(
new double[lwork]);
186 std::unique_ptr<double []> vt(
new double[
n*
n]);
190 dgesvd_(&jobu,&jobvt,&n,&
m,
mat.get(),&
n,sing_vals.get(),vt.get(),&
n,0,&
m,work.get(),&lwork,&info);
193 std::cerr <<
"svd failed. info = " << info << std::endl;
209 std::unique_ptr<double []> eigs(
new double[
n]);
216 std::unique_ptr<double []> work (
new double [lwork]);
220 dsyev_(&jobz,&uplo,&n,
mat.get(),&
n,eigs.get(),work.get(),&lwork,&info);
223 std::cerr <<
"dsyev failed. info = " << info << std::endl;
232 std::cout << i <<
" " << j <<
"\t" << (*
this)(i,j) << std::endl;
239 for(
int i=0;i<std::min(
m,
n);i++)
240 result += (*
this)(i,i);
248 hid_t file_id, dataset_id, dataspace_id, attribute_id;
251 file_id = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
254 hsize_t dimarray =
n*
m;
255 dataspace_id = H5Screate_simple(1, &dimarray, NULL);
257 dataset_id = H5Dcreate(file_id,
"matrix", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
259 status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
mat.get() );
262 status = H5Sclose(dataspace_id);
265 dataspace_id = H5Screate(H5S_SCALAR);
267 attribute_id = H5Acreate (dataset_id,
"n", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
268 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &
n );
271 status = H5Aclose(attribute_id);
274 attribute_id = H5Acreate (dataset_id,
"m", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
275 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &m );
278 status = H5Aclose(attribute_id);
281 status = H5Sclose(dataspace_id);
284 status = H5Dclose(dataset_id);
287 status = H5Fclose(file_id);
293 hid_t file_id, dataset_id, attribute_id;
297 file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
300 dataset_id = H5Dopen(file_id,
"matrix", H5P_DEFAULT);
303 attribute_id = H5Aopen(dataset_id,
"n", H5P_DEFAULT);
306 status = H5Aread(attribute_id, H5T_NATIVE_INT, &n_);
309 status = H5Aclose(attribute_id);
312 attribute_id = H5Aopen(dataset_id,
"m", H5P_DEFAULT);
315 status = H5Aread(attribute_id, H5T_NATIVE_INT, &m_);
318 status = H5Aclose(attribute_id);
322 std::cerr <<
"Matrix size not compatable with file: " << n_ <<
"x" << m_ << std::endl;
325 status = H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
mat.get());
329 status = H5Dclose(dataset_id);
332 status = H5Fclose(file_id);
355 mat.reset(
new std::complex<double> [
n*
m]);
365 mat.reset(
new std::complex<double> [
n*
m]);
366 std::memcpy(
mat.get(), orig.
getpointer(),
n*m*
sizeof(std::complex<double>));
377 mat = std::move(orig.mat);
384 mat.reset(
new std::complex<double> [
n*
m]);
385 std::memcpy(
mat.get(), orig.
getpointer(),
n*m*
sizeof(std::complex<double>));
395 for(
int i=0;i<
n*
m;i++)
450 std::cout << i <<
" " << j <<
"\t" << (*
this)(i,j) << std::endl;
472 assert(n_>0 && m_>0);
475 mat.reset(
new T [n*m]);
486 mat.reset(
new T [n*m]);
487 std::memcpy(mat.get(), orig.
getpointer(), n*m*
sizeof(T));
499 mat = std::move(orig.mat);
507 mat.reset(
new T [n*m]);
508 std::memcpy(mat.get(), orig.
getpointer(), n*m*
sizeof(T));
519 for(
int i=0;i<n*m;i++)
582 std::cout << i <<
" " << j <<
"\t" << (*
this)(i,j) << std::endl;
cmatrix & operator=(const cmatrix &orig)
void SaveToFile(std::string filename) const
matrix & operator=(const matrix &orig)
tmatrix< T > & operator=(const tmatrix< T > &orig)
void ReadFromFile(std::string filename) const
matrix & prod(matrix const &A, matrix const &B)
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 dsyev_(char *jobz, char *uplo, int *n, double *A, int *lda, double *W, double *work, int *lwork, int *info)
unsigned int getn() const
std::unique_ptr< double[]> mat
n by m array of double
unsigned int m
number of columns
std::unique_ptr< double[]> svd()
#define HDF5_STATUS_CHECK(status)
std::complex< double > & operator[](int x)
std::complex< double > operator()(int x, int y) const
std::unique_ptr< double[]> sym_eig()
std::unique_ptr< std::complex< double >[]> mat
n by m array of complex
double operator()(int x, int y) const
double & operator[](int x)
T operator()(int x, int y) const
unsigned int getm() const
void dgesvd_(char *jobu, char *jobvt, int *m, int *n, double *a, int *lda, double *s, double *u, int *ldu, double *vt, int *ldvt, double *work, int *lwork, int *info)
std::complex< double > * getpointer() const
double * getpointer() const
unsigned int n
number of rows