65 const int LENGTH_VAR_NAME = 8;
66 const int LENGTH_DBL_DATA = 16;
67 const int LENGTH_INT_DATA = 5;
68 const int LENGTH_VAR_UNIT = 6;
69 const int LENGTH_DBL_PREC = 8;
70 const int LENGTH_VAR_DATA = 16;
80 inline Int PrintBlock(std::ostream &os,
const std::string name){
82 os << std::endl<<
"*********************************************************************" << std::endl;
83 os << name << std::endl;
84 os <<
"*********************************************************************" << std::endl << std::endl;
89 inline Int Print(std::ostream &os,
const std::string name) {
90 os << std::setiosflags(std::ios::left) << name << std::endl;
94 inline Int Print(std::ostream &os,
const char* name) {
95 os << std::setiosflags(std::ios::left) << std::string(name) << std::endl;
99 inline Int Print(std::ostream &os,
const std::string name, std::string val) {
100 os << std::setiosflags(std::ios::left)
101 << std::setw(LENGTH_VAR_NAME) << name
102 << std::setw(LENGTH_VAR_DATA) << val
107 inline Int Print(std::ostream &os,
const std::string name,
const char* val) {
108 os << std::setiosflags(std::ios::left)
109 << std::setw(LENGTH_VAR_NAME) << name
110 << std::setw(LENGTH_VAR_DATA) << std::string(val)
120 inline Int Print(std::ostream &os,
const std::string name, Real val) {
121 os << std::setiosflags(std::ios::left)
122 << std::setw(LENGTH_VAR_NAME) << name
123 << std::setiosflags(std::ios::scientific)
124 << std::setiosflags(std::ios::showpos)
125 << std::setw(LENGTH_DBL_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val
126 << std::resetiosflags(std::ios::scientific)
127 << std::resetiosflags(std::ios::showpos)
132 inline Int Print(std::ostream &os,
const char* name, Real val) {
133 os << std::setiosflags(std::ios::left)
134 << std::setw(LENGTH_VAR_NAME) << std::string(name)
135 << std::setiosflags(std::ios::scientific)
136 << std::setiosflags(std::ios::showpos)
137 << std::setw(LENGTH_DBL_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val
138 << std::resetiosflags(std::ios::scientific)
139 << std::resetiosflags(std::ios::showpos)
145 inline Int Print(std::ostream &os,
const std::string name, Real val,
const std::string unit) {
146 os << std::setiosflags(std::ios::left)
147 << std::setw(LENGTH_VAR_NAME) << name
148 << std::setiosflags(std::ios::scientific)
149 << std::setiosflags(std::ios::showpos)
150 << std::setw(LENGTH_DBL_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val
151 << std::resetiosflags(std::ios::scientific)
152 << std::resetiosflags(std::ios::showpos)
153 << std::setw(LENGTH_VAR_UNIT) << unit
158 inline Int Print(std::ostream &os,
const char *name, Real val,
const char *unit) {
159 os << std::setiosflags(std::ios::left)
160 << std::setw(LENGTH_VAR_NAME) << std::string(name)
161 << std::setiosflags(std::ios::scientific)
162 << std::setiosflags(std::ios::showpos)
163 << std::setw(LENGTH_DBL_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val
164 << std::resetiosflags(std::ios::scientific)
165 << std::resetiosflags(std::ios::showpos)
166 << std::setw(LENGTH_VAR_UNIT) << std::string(unit)
172 inline Int Print(std::ostream &os,
const std::string name1, Real val1,
const std::string unit1,
173 const std::string name2, Real val2,
const std::string unit2) {
174 os << std::setiosflags(std::ios::left)
175 << std::setw(LENGTH_VAR_NAME) << name1
176 << std::setiosflags(std::ios::scientific)
177 << std::setiosflags(std::ios::showpos)
178 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val1
179 << std::setw(LENGTH_VAR_UNIT) << unit1
180 << std::setw(LENGTH_VAR_NAME) << name2
181 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
182 << std::resetiosflags(std::ios::scientific)
183 << std::resetiosflags(std::ios::showpos)
184 << std::setw(LENGTH_VAR_UNIT) << unit2
189 inline Int Print(std::ostream &os,
const char *name1, Real val1,
const char *unit1,
190 char *name2, Real val2,
char *unit2) {
191 os << std::setiosflags(std::ios::left)
192 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
193 << std::setiosflags(std::ios::scientific)
194 << std::setiosflags(std::ios::showpos)
195 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val1
196 << std::setw(LENGTH_VAR_UNIT) << std::string(unit1)
197 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
198 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
199 << std::resetiosflags(std::ios::scientific)
200 << std::resetiosflags(std::ios::showpos)
201 << std::setw(LENGTH_VAR_UNIT) << std::string(unit2)
207 inline Int Print(std::ostream &os,
const std::string name1, Int val1,
const std::string unit1,
208 const std::string name2, Real val2,
const std::string unit2) {
209 os << std::setiosflags(std::ios::left)
210 << std::setw(LENGTH_VAR_NAME) << name1
211 << std::setw(LENGTH_INT_DATA) << val1
212 << std::setw(LENGTH_VAR_UNIT) << unit1
213 << std::setw(LENGTH_VAR_NAME) << name2
214 << std::setiosflags(std::ios::scientific)
215 << std::setiosflags(std::ios::showpos)
216 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
217 << std::resetiosflags(std::ios::scientific)
218 << std::resetiosflags(std::ios::showpos)
219 << std::setw(LENGTH_VAR_UNIT) << unit2
224 inline Int Print(std::ostream &os,
const char *name1, Int val1,
const char *unit1,
225 char *name2, Real val2,
char *unit2) {
226 os << std::setiosflags(std::ios::left)
227 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
228 << std::setw(LENGTH_INT_DATA) << val1
229 << std::setw(LENGTH_VAR_UNIT) << std::string(unit1)
230 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
231 << std::setiosflags(std::ios::scientific)
232 << std::setiosflags(std::ios::showpos)
233 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
234 << std::resetiosflags(std::ios::scientific)
235 << std::resetiosflags(std::ios::showpos)
236 << std::setw(LENGTH_VAR_UNIT) << std::string(unit2)
241 inline Int Print(std::ostream &os,
242 const char *name1, Int val1,
243 const char *name2, Real val2,
244 char *name3, Real val3) {
245 os << std::setiosflags(std::ios::left)
246 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
247 << std::setw(LENGTH_INT_DATA) << val1
248 << std::setiosflags(std::ios::scientific)
249 << std::setiosflags(std::ios::showpos)
250 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
251 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
252 << std::setw(LENGTH_VAR_NAME) << std::string(name3)
253 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val3
254 << std::resetiosflags(std::ios::scientific)
255 << std::resetiosflags(std::ios::showpos)
261 inline Int Print(std::ostream &os,
262 const char *name1, Int val1,
263 const char *name2, Real val2,
264 const char *name3, Real val3,
265 const char *name4, Real val4 ) {
266 os << std::setiosflags(std::ios::left)
267 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
268 << std::setw(LENGTH_INT_DATA) << val1
269 << std::setiosflags(std::ios::scientific)
270 << std::setiosflags(std::ios::showpos)
271 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
272 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val2
273 << std::setw(LENGTH_VAR_NAME) << std::string(name3)
274 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val3
275 << std::setw(LENGTH_VAR_NAME) << std::string(name4)
276 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC)<< val4
277 << std::resetiosflags(std::ios::scientific)
278 << std::resetiosflags(std::ios::showpos)
286 inline Int Print(std::ostream &os, std::string name, Int val) {
287 os << std::setiosflags(std::ios::left)
288 << std::setw(LENGTH_VAR_NAME) << name
289 << std::setw(LENGTH_VAR_DATA) << val
295 inline Int Print(std::ostream &os,
const char *name, Int val) {
296 os << std::setiosflags(std::ios::left)
297 << std::setw(LENGTH_VAR_NAME) << std::string(name)
298 << std::setw(LENGTH_VAR_DATA) << val
304 inline Int Print(std::ostream &os,
const std::string name, Int val,
const std::string unit) {
305 os << std::setiosflags(std::ios::left)
306 << std::setw(LENGTH_VAR_NAME) << name
307 << std::setw(LENGTH_VAR_DATA) << val
308 << std::setw(LENGTH_VAR_UNIT) << unit
314 inline Int Print(std::ostream &os,
const char* name, Int val,
const std::string unit) {
315 os << std::setiosflags(std::ios::left)
316 << std::setw(LENGTH_VAR_NAME) << std::string(name)
317 << std::setw(LENGTH_VAR_DATA) << val
318 << std::setw(LENGTH_VAR_UNIT) << unit
326 inline Int Print(std::ostream &os,
const std::string name1, Int val1,
const std::string unit1,
327 const std::string name2, Int val2,
const std::string unit2) {
328 os << std::setiosflags(std::ios::left)
329 << std::setw(LENGTH_VAR_NAME) << name1
330 << std::setw(LENGTH_VAR_DATA) << val1
331 << std::setw(LENGTH_VAR_UNIT) << unit1
332 << std::setw(LENGTH_VAR_NAME) << name2
333 << std::setw(LENGTH_VAR_DATA) << val2
334 << std::setw(LENGTH_VAR_UNIT) << unit2
339 inline Int Print(std::ostream &os,
const char *name1, Int val1,
const char *unit1,
340 char *name2, Int val2,
char *unit2) {
341 os << std::setiosflags(std::ios::left)
342 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
343 << std::setw(LENGTH_VAR_DATA) << val1
344 << std::setw(LENGTH_VAR_UNIT) << std::string(unit1)
345 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
346 << std::setw(LENGTH_VAR_DATA) << val2
347 << std::setw(LENGTH_VAR_UNIT) << std::string(unit2)
355 inline Int Print(std::ostream &os,
const std::string name,
bool val) {
356 os << std::setiosflags(std::ios::left)
357 << std::setw(LENGTH_VAR_NAME) << name;
359 os << std::setw(LENGTH_VAR_NAME) <<
"true" << std::endl;
361 os << std::setw(LENGTH_VAR_NAME) <<
"false" << std::endl;
366 inline Int Print(std::ostream &os,
const char* name,
bool val) {
367 os << std::setiosflags(std::ios::left)
368 << std::setw(LENGTH_VAR_NAME) << std::string(name);
370 os << std::setw(LENGTH_VAR_NAME) <<
"true" << std::endl;
372 os << std::setw(LENGTH_VAR_NAME) <<
"false" << std::endl;
378 inline Int Print(std::ostream &os,
379 const char *name1, Index3 val ) {
380 os << std::setiosflags(std::ios::left)
381 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
382 << std::setw(LENGTH_VAR_DATA) << val[0]
383 << std::setw(LENGTH_VAR_DATA) << val[1]
384 << std::setw(LENGTH_VAR_DATA) << val[2]
389 inline Int Print(std::ostream &os,
390 const char *name1, Point3 val ) {
391 os << std::setiosflags(std::ios::left)
392 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
393 << std::setiosflags(std::ios::scientific)
394 << std::setiosflags(std::ios::showpos)
395 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) << val[0]
396 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) << val[1]
397 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) << val[2]
398 << std::resetiosflags(std::ios::scientific)
399 << std::resetiosflags(std::ios::showpos)
404 inline Int Print(std::ostream &os,
405 const char *name1, Int val1,
406 const char *name2, Point3 val ) {
407 os << std::setiosflags(std::ios::left)
408 << std::setw(LENGTH_VAR_NAME) << std::string(name1)
409 << std::setw(LENGTH_INT_DATA) << val1
410 << std::setw(LENGTH_VAR_NAME) << std::string(name2)
411 << std::setiosflags(std::ios::scientific)
412 << std::setiosflags(std::ios::showpos)
413 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) <<val[0]
414 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) <<val[1]
415 << std::setw(LENGTH_VAR_DATA) << std::setprecision(LENGTH_DBL_PREC) <<val[2]
416 << std::resetiosflags(std::ios::scientific)
417 << std::resetiosflags(std::ios::showpos)
427 template <
class F>
inline std::ostream& operator<<( std::ostream& os, const std::vector<F>& vec)
429 os<<vec.size()<<std::endl;
430 os.setf(std::ios_base::scientific, std::ios_base::floatfield);
431 for(Int i=0; i<vec.size(); i++)
438 template <
class F>
inline std::ostream& operator<<( std::ostream& os, const NumVec<F>& vec)
440 os<<vec.m()<<std::endl;
441 os.setf(std::ios_base::scientific, std::ios_base::floatfield);
442 for(Int i=0; i<vec.m(); i++)
457 template <
class F>
inline std::ostream& operator<<( std::ostream& os, const NumMat<F>& mat)
459 os<<mat.m()<<
" "<<mat.n()<<std::endl;
460 os.setf(std::ios_base::scientific, std::ios_base::floatfield);
461 os << std::setprecision(16);
462 for(Int i=0; i<mat.m(); i++) {
463 for(Int j=0; j<mat.n(); j++)
471 template <
class F>
inline std::ostream& operator<<( std::ostream& os, const NumTns<F>& tns)
473 os<<tns.m()<<
" "<<tns.n()<<
" "<<tns.p()<<std::endl;
474 os.setf(std::ios_base::scientific, std::ios_base::floatfield);
475 for(Int i=0; i<tns.m(); i++) {
476 for(Int j=0; j<tns.n(); j++) {
477 for(Int k=0; k<tns.p(); k++) {
494 const std::vector<Int> NO_MASK(1);
499 template <
typename T>
500 inline Int serialize(
const T& val, std::ostream& os,
const std::vector<Int>& mask)
502 os.write((
char*)&val,
sizeof(T));
506 template <
typename T>
507 inline Int deserialize(T& val, std::istream& is,
const std::vector<Int>& mask)
509 is.read((
char*)&val,
sizeof(T));
516 inline Int serialize(
const bool& val, std::ostream& os,
const std::vector<Int>& mask)
518 os.write((
char*)&val,
sizeof(
bool));
522 inline Int deserialize(
bool& val, std::istream& is,
const std::vector<Int>& mask)
524 is.read((
char*)&val,
sizeof(
bool));
529 inline Int serialize(
const char& val, std::ostream& os,
const std::vector<Int>& mask)
531 os.write((
char*)&val,
sizeof(
char));
535 inline Int deserialize(
char& val, std::istream& is,
const std::vector<Int>& mask)
537 is.read((
char*)&val,
sizeof(
char));
541 inline Int combine(
char& val,
char& ext)
543 throw std::logic_error(
"Combine operation not implemented." );
548 inline Int serialize(
const Int& val, std::ostream& os,
const std::vector<Int>& mask)
550 os.write((
char*)&val,
sizeof(Int));
554 inline Int deserialize(Int& val, std::istream& is,
const std::vector<Int>& mask)
556 is.read((
char*)&val,
sizeof(Int));
560 inline Int combine(Int& val, Int& ext)
568 inline Int serialize(
const LongInt& val, std::ostream& os,
const std::vector<Int>& mask)
570 os.write((
char*)&val,
sizeof(LongInt));
574 inline Int deserialize(LongInt& val, std::istream& is,
const std::vector<Int>& mask)
576 is.read((
char*)&val,
sizeof(LongInt));
580 inline Int combine(LongInt& val, LongInt& ext)
589 inline Int serialize(
const Real& val, std::ostream& os,
const std::vector<Int>& mask)
591 os.write((
char*)&val,
sizeof(Real));
595 inline Int deserialize(Real& val, std::istream& is,
const std::vector<Int>& mask)
597 is.read((
char*)&val,
sizeof(Real));
601 inline Int combine(Real& val, Real& ext)
609 inline Int serialize(
const Complex& val, std::ostream& os,
const std::vector<Int>& mask)
611 os.write((
char*)&val,
sizeof(Complex));
615 inline Int deserialize(Complex& val, std::istream& is,
const std::vector<Int>& mask)
617 is.read((
char*)&val,
sizeof(Complex));
621 inline Int combine(Complex& val, Complex& ext)
669 inline Int serialize(
const Index3& val, std::ostream& os,
const std::vector<Int>& mask)
671 os.write((
char*)&(val[0]), 3*
sizeof(Int));
675 inline Int deserialize(Index3& val, std::istream& is,
const std::vector<Int>& mask)
677 is.read((
char*)&(val[0]), 3*
sizeof(Int));
681 inline Int combine(Index3& val, Index3& ext)
683 throw std::logic_error(
"Combine operation not implemented." );
688 inline Int serialize(
const Point3& val, std::ostream& os,
const std::vector<Int>& mask)
690 os.write((
char*)&(val[0]), 3*
sizeof(Real));
694 inline Int deserialize(Point3& val, std::istream& is,
const std::vector<Int>& mask)
696 is.read((
char*)&(val[0]), 3*
sizeof(Real));
700 inline Int combine(Point3& val, Point3& ext)
702 throw std::logic_error(
"Combine operation not implemented." );
708 Int serialize(
const std::vector<T>& val, std::ostream& os,
const std::vector<Int>& mask)
711 os.write((
char*)&sz,
sizeof(Int));
712 for(Int k=0; k<sz; k++)
713 serialize(val[k], os, mask);
718 Int deserialize(std::vector<T>& val, std::istream& is,
const std::vector<Int>& mask)
721 is.read((
char*)&sz,
sizeof(Int));
723 for(Int k=0; k<sz; k++)
724 deserialize(val[k], is, mask);
729 Int combine(std::vector<T>& val, std::vector<T>& ext)
731 throw std::logic_error(
"Combine operation not implemented." );
738 Int serialize(
const std::set<T>& val, std::ostream& os,
const std::vector<Int>& mask)
741 os.write((
char*)&sz,
sizeof(Int));
742 for(
typename std::set<T>::const_iterator mi=val.begin(); mi!=val.end(); mi++)
743 serialize((*mi), os, mask);
748 Int deserialize(std::set<T>& val, std::istream& is,
const std::vector<Int>& mask)
752 is.read((
char*)&sz,
sizeof(Int));
753 for(Int k=0; k<sz; k++) {
754 T t; deserialize(t, is, mask);
761 Int combine(std::set<T>& val, std::set<T>& ext)
763 throw std::logic_error(
"Combine operation not implemented." );
769 template<
class T,
class S>
770 Int serialize(
const std::map<T,S>& val, std::ostream& os,
const std::vector<Int>& mask)
773 os.write((
char*)&sz,
sizeof(Int));
774 for(
typename std::map<T,S>::const_iterator mi=val.begin(); mi!=val.end(); mi++) {
775 serialize((*mi).first, os, mask);
776 serialize((*mi).second, os, mask);
781 template<
class T,
class S>
782 Int deserialize(std::map<T,S>& val, std::istream& is,
const std::vector<Int>& mask)
786 is.read((
char*)&sz,
sizeof(Int));
787 for(Int k=0; k<sz; k++) {
788 T t; deserialize(t, is, mask);
789 S s; deserialize(s, is, mask);
795 template<
class T,
class S>
796 Int combine(std::map<T,S>& val, std::map<T,S>& ext)
798 throw std::logic_error(
"Combine operation not implemented." );
804 template<
class T,
class S>
805 Int serialize(
const std::pair<T,S>& val, std::ostream& os,
const std::vector<Int>& mask)
807 serialize(val.first, os, mask);
808 serialize(val.second, os, mask);
812 template<
class T,
class S>
813 Int deserialize(std::pair<T,S>& val, std::istream& is,
const std::vector<Int>& mask)
815 deserialize(val.first, is, mask);
816 deserialize(val.second, is, mask);
820 template<
class T,
class S>
821 Int combine(std::pair<T,S>& val, std::pair<T,S>& ext)
823 throw std::logic_error(
"Combine operation not implemented." );
896 inline Int serialize(
const IntNumVec& val, std::ostream& os,
const std::vector<Int>& mask)
899 os.write((
char*)&m,
sizeof(Int));
900 os.write((
char*)(val.Data()), m*
sizeof(Int));
904 inline Int deserialize(IntNumVec& val, std::istream& is,
const std::vector<Int>& mask)
907 is.read((
char*)&m,
sizeof(Int));
909 is.read((
char*)(val.Data()), m*
sizeof(Int));
913 inline Int combine(IntNumVec& val, IntNumVec& ext)
916 assert(val.m()==ext.m());
917 for(Int i=0; i<val.m(); i++) val(i) += ext(i);
924 inline Int serialize(
const IntNumMat& val, std::ostream& os,
const std::vector<Int>& mask)
928 os.write((
char*)&m,
sizeof(Int));
929 os.write((
char*)&n,
sizeof(Int));
930 os.write((
char*)(val.Data()), m*n*
sizeof(Int));
934 inline Int deserialize(IntNumMat& val, std::istream& is,
const std::vector<Int>& mask)
938 is.read((
char*)&m,
sizeof(Int));
939 is.read((
char*)&n,
sizeof(Int));
941 is.read((
char*)(val.Data()), m*n*
sizeof(Int));
945 inline Int combine(IntNumMat& val, IntNumMat& ext)
948 assert(val.m()==ext.m() && val.n()==ext.n());
949 for(Int i=0; i<val.m(); i++)
950 for(Int j=0; j<val.n(); j++)
951 val(i,j) += ext(i,j);
957 inline Int serialize(
const IntNumTns& val, std::ostream& os,
const std::vector<Int>& mask)
959 Int m = val.m(); Int n = val.n(); Int p = val.p();
960 os.write((
char*)&m,
sizeof(Int));
961 os.write((
char*)&n,
sizeof(Int));
962 os.write((
char*)&p,
sizeof(Int));
963 os.write((
char*)(val.Data()), m*n*p*
sizeof(Int));
967 inline Int deserialize(IntNumTns& val, std::istream& is,
const std::vector<Int>& mask)
970 is.read((
char*)&m,
sizeof(Int));
971 is.read((
char*)&n,
sizeof(Int));
972 is.read((
char*)&p,
sizeof(Int));
974 is.read((
char*)(val.Data()), m*n*p*
sizeof(Int));
978 inline Int combine(IntNumTns& val, IntNumTns& ext)
981 assert(val.m()==ext.m() && val.n()==ext.n() && val.p()==ext.p());
982 for(Int i=0; i<val.m(); i++)
983 for(Int j=0; j<val.n(); j++)
984 for(Int k=0; k<val.p(); k++)
985 val(i,j,k) += ext(i,j,k);
991 inline Int serialize(
const DblNumVec& val, std::ostream& os,
const std::vector<Int>& mask)
994 os.write((
char*)&m,
sizeof(Int));
995 os.write((
char*)(val.Data()), m*
sizeof(Real));
999 inline Int deserialize(DblNumVec& val, std::istream& is,
const std::vector<Int>& mask)
1002 is.read((
char*)&m,
sizeof(Int));
1004 is.read((
char*)(val.Data()), m*
sizeof(Real));
1008 inline Int combine(DblNumVec& val, DblNumVec& ext)
1011 assert(val.m()==ext.m());
1012 for(Int i=0; i<val.m(); i++) val(i) += ext(i);
1018 inline Int serialize(
const DblNumMat& val, std::ostream& os,
const std::vector<Int>& mask)
1022 os.write((
char*)&m,
sizeof(Int));
1023 os.write((
char*)&n,
sizeof(Int));
1024 os.write((
char*)(val.Data()), m*n*
sizeof(Real));
1028 inline Int deserialize(DblNumMat& val, std::istream& is,
const std::vector<Int>& mask)
1032 is.read((
char*)&m,
sizeof(Int));
1033 is.read((
char*)&n,
sizeof(Int));
1035 is.read((
char*)(val.Data()), m*n*
sizeof(Real));
1039 inline Int combine(DblNumMat& val, DblNumMat& ext)
1042 assert(val.m()==ext.m() && val.n()==ext.n());
1043 for(Int i=0; i<val.m(); i++)
1044 for(Int j=0; j<val.n(); j++)
1045 val(i,j) += ext(i,j);
1052 inline Int serialize(
const DblNumTns& val, std::ostream& os,
const std::vector<Int>& mask)
1054 Int m = val.m(); Int n = val.n(); Int p = val.p();
1055 os.write((
char*)&m,
sizeof(Int));
1056 os.write((
char*)&n,
sizeof(Int));
1057 os.write((
char*)&p,
sizeof(Int));
1058 os.write((
char*)(val.Data()), m*n*p*
sizeof(Real));
1062 inline Int deserialize(DblNumTns& val, std::istream& is,
const std::vector<Int>& mask)
1065 is.read((
char*)&m,
sizeof(Int));
1066 is.read((
char*)&n,
sizeof(Int));
1067 is.read((
char*)&p,
sizeof(Int));
1069 is.read((
char*)(val.Data()), m*n*p*
sizeof(Real));
1073 inline Int combine(DblNumTns& val, DblNumTns& ext)
1076 assert(val.m()==ext.m() && val.n()==ext.n() && val.p()==ext.p());
1077 for(Int i=0; i<val.m(); i++)
1078 for(Int j=0; j<val.n(); j++)
1079 for(Int k=0; k<val.p(); k++)
1080 val(i,j,k) += ext(i,j,k);
1086 inline Int serialize(
const CpxNumVec& val, std::ostream& os,
const std::vector<Int>& mask)
1089 os.write((
char*)&m,
sizeof(Int));
1090 os.write((
char*)(val.Data()), m*
sizeof(Complex));
1094 inline Int deserialize(CpxNumVec& val, std::istream& is,
const std::vector<Int>& mask)
1097 is.read((
char*)&m,
sizeof(Int));
1099 is.read((
char*)(val.Data()), m*
sizeof(Complex));
1103 inline Int combine(CpxNumVec& val, CpxNumVec& ext)
1106 assert(val.m()==ext.m());
1107 for(Int i=0; i<val.m(); i++) val(i) += ext(i);
1113 inline Int serialize(
const CpxNumMat& val, std::ostream& os,
const std::vector<Int>& mask)
1117 os.write((
char*)&m,
sizeof(Int));
1118 os.write((
char*)&n,
sizeof(Int));
1119 os.write((
char*)(val.Data()), m*n*
sizeof(Complex));
1123 inline Int deserialize(CpxNumMat& val, std::istream& is,
const std::vector<Int>& mask)
1127 is.read((
char*)&m,
sizeof(Int));
1128 is.read((
char*)&n,
sizeof(Int));
1130 is.read((
char*)(val.Data()), m*n*
sizeof(Complex));
1134 inline Int combine(CpxNumMat& val, CpxNumMat& ext)
1137 assert(val.m()==ext.m() && val.n()==ext.n());
1138 for(Int i=0; i<val.m(); i++)
1139 for(Int j=0; j<val.n(); j++)
1140 val(i,j) += ext(i,j);
1146 inline Int serialize(
const CpxNumTns& val, std::ostream& os,
const std::vector<Int>& mask)
1148 Int m = val.m(); Int n = val.n(); Int p = val.p();
1149 os.write((
char*)&m,
sizeof(Int));
1150 os.write((
char*)&n,
sizeof(Int));
1151 os.write((
char*)&p,
sizeof(Int));
1152 os.write((
char*)(val.Data()), m*n*p*
sizeof(Complex));
1156 inline Int deserialize(CpxNumTns& val, std::istream& is,
const std::vector<Int>& mask)
1159 is.read((
char*)&m,
sizeof(Int));
1160 is.read((
char*)&n,
sizeof(Int));
1161 is.read((
char*)&p,
sizeof(Int));
1163 is.read((
char*)(val.Data()), m*n*p*
sizeof(Complex));
1167 inline Int combine(CpxNumTns& val, CpxNumTns& ext)
1170 assert(val.m()==ext.m() && val.n()==ext.n() && val.p()==ext.p());
1171 for(Int i=0; i<val.m(); i++)
1172 for(Int j=0; j<val.n(); j++)
1173 for(Int k=0; k<val.p(); k++)
1174 val(i,j,k) += ext(i,j,k);
1181 Int
inline serialize(
const NumVec<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1184 os.write((
char*)&m,
sizeof(Int));
1185 for(Int i=0; i<m; i++)
1186 serialize(val(i), os, mask);
1191 Int
inline deserialize(NumVec<T>& val, std::istream& is,
const std::vector<Int>& mask)
1194 is.read((
char*)&m,
sizeof(Int));
1196 for(Int i=0; i<m; i++)
1197 deserialize(val(i), is, mask);
1202 Int
inline combine(NumVec<T>& val, NumVec<T>& ext)
1204 throw std::logic_error(
"Combine operation not implemented." );
1211 Int
inline serialize(
const NumMat<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1215 os.write((
char*)&m,
sizeof(Int));
1216 os.write((
char*)&n,
sizeof(Int));
1217 for(Int j=0; j<n; j++)
1218 for(Int i=0; i<m; i++)
1219 serialize(val(i,j), os, mask);
1223 Int
inline deserialize(NumMat<T>& val, std::istream& is,
const std::vector<Int>& mask)
1227 is.read((
char*)&m,
sizeof(Int));
1228 is.read((
char*)&n,
sizeof(Int));
1230 for(Int j=0; j<n; j++)
1231 for(Int i=0; i<m; i++)
1232 deserialize(val(i,j), is, mask);
1237 Int
inline combine(NumMat<T>& val, NumMat<T>& ext)
1239 throw std::logic_error(
"Combine operation not implemented." );
1247 Int
inline serialize(
const NumTns<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1252 os.write((
char*)&m,
sizeof(Int));
1253 os.write((
char*)&n,
sizeof(Int));
1254 os.write((
char*)&p,
sizeof(Int));
1255 for(Int k=0; k<p; k++)
1256 for(Int j=0; j<n; j++)
1257 for(Int i=0; i<m; i++)
1258 serialize(val(i,j,k), os, mask);
1263 Int
inline deserialize(NumTns<T>& val, std::istream& is,
const std::vector<Int>& mask)
1268 is.read((
char*)&m,
sizeof(Int));
1269 is.read((
char*)&n,
sizeof(Int));
1270 is.read((
char*)&p,
sizeof(Int));
1272 for(Int k=0; k<p; k++)
1273 for(Int j=0; j<n; j++)
1274 for(Int i=0; i<m; i++)
1275 deserialize(val(i,j,k), is, mask);
1280 Int
inline combine(NumTns<T>& val, NumTns<T>& ext)
1282 throw std::logic_error(
"Combine operation not implemented." );
1289 Int
inline serialize(
const DistSparseMatrix<T>& val, std::ostream& os,
const std::vector<Int>& mask)
1291 serialize( val.size, os, mask );
1292 serialize( val.nnz, os, mask );
1293 serialize( val.nnzLocal, os, mask );
1294 serialize( val.colptrLocal, os, mask );
1295 serialize( val.rowindLocal, os, mask );
1296 serialize( val.nzvalLocal, os, mask );
1302 Int
inline deserialize(DistSparseMatrix<T>& val, std::istream& is,
const std::vector<Int>& mask)
1304 deserialize( val.size, is, mask );
1305 deserialize( val.nnz, is, mask );
1306 deserialize( val.nnzLocal, is, mask );
1307 deserialize( val.colptrLocal, is, mask );
1308 deserialize( val.rowindLocal, is, mask );
1309 deserialize( val.nzvalLocal, is, mask );
1315 Int
inline combine(DistSparseMatrix<T>& val, DistSparseMatrix<T>& ext)
1317 throw std::logic_error(
"Combine operation not implemented." );
1326 Int SeparateRead(std::string name, std::istringstream& is);
1328 Int SeparateWrite(std::string name, std::ostringstream& os);
1330 Int SeparateWriteAscii(std::string name, std::ostringstream& os);
1332 Int SharedRead(std::string name, std::istringstream& is);
1334 Int SharedWrite(std::string name, std::ostringstream& os);
1339 inline void IdentityCol( Int col, NumVec<Real>& vec )
1341 for(Int i=0; i<std::min(col,vec.m()); i++)
1348 for(Int i=col+1; i<vec.m(); i++)
1352 inline void IdentityCol( Int col, NumVec<Complex>& vec )
1354 for(Int i=0; i<std::min(col,vec.m()); i++)
1355 vec(i) = Complex(0.0,0.0);
1358 vec(col) = Complex(1.0,0.0);
1361 for(Int i=col+1; i<vec.m(); i++)
1362 vec(i) = Complex(0.0,0.0);
1367 inline void IdentityCol( IntNumVec & cols, NumMat<Real>& mat )
1369 for(Int j=0;j<cols.m();j++){
1371 for(Int i=0; i<std::min(col,mat.m()); i++)
1378 for(Int i=col+1; i<mat.m(); i++)
1384 inline void IdentityCol( IntNumVec & cols, NumMat<Complex>& mat )
1386 for(Int j=0;j<cols.m();j++){
1388 for(Int i=0; i<std::min(col,mat.m()); i++)
1389 mat(i,j) = Complex(0.0,0.0);
1392 mat(col,j) = Complex(1.0,0.0);
1395 for(Int i=col+1; i<mat.m(); i++)
1396 mat(i,j) = Complex(0.0,0.0);
1406 inline void SetRandomSeed(
long int seed){
1410 inline Real UniformRandom(){
1411 return (Real)drand48();
1414 inline void UniformRandom( NumVec<Real>& vec )
1416 for(Int i=0; i<vec.m(); i++)
1417 vec(i) = UniformRandom();
1420 inline void UniformRandom( NumVec<Complex>& vec )
1422 for(Int i=0; i<vec.m(); i++)
1423 vec(i) = Complex(UniformRandom(), UniformRandom());
1426 inline void UniformRandom( NumMat<Real>& M )
1428 Real *ptr = M.Data();
1429 for(Int i=0; i < M.m() * M.n(); i++)
1430 *(ptr++) = UniformRandom();
1433 inline void UniformRandom( NumMat<Complex>& M )
1435 Complex *ptr = M.Data();
1436 for(Int i=0; i < M.m() * M.n(); i++)
1437 *(ptr++) = Complex(UniformRandom(), UniformRandom());
1441 inline void UniformRandom( NumTns<Real>& T )
1443 Real *ptr = T.Data();
1444 for(Int i=0; i < T.m() * T.n() * T.p(); i++)
1445 *(ptr++) = UniformRandom();
1448 inline void UniformRandom( NumTns<Complex>& T )
1450 Complex *ptr = T.Data();
1451 for(Int i=0; i < T.m() * T.n() * T.p(); i++)
1452 *(ptr++) = Complex(UniformRandom(), UniformRandom());
1458 inline void GetTime(Real& t){
1467 inline bool PairLtComparator(
const std::pair<Real, Int>& l,
1468 const std::pair<Real, Int>& r ){
1469 return l.first < r.first;
1472 inline bool PairGtComparator(
const std::pair<Real, Int>& l,
1473 const std::pair<Real, Int>& r ){
1474 return l.first > r.first;
1485 IndexComp (
const T indices) : indices_(indices) {}
1486 bool operator()(
const size_t a,
const size_t b)
const
1487 {
return indices_[a] < indices_[b]; }
1506 void ReadDistSparseMatrixFormatted(
const char* filename,
DistSparseMatrix<Real>& pspmat, MPI_Comm comm );
1508 template <
class F1,
class F2>
1513 PushCallStack(
"CopyPattern");
1517 B.colptr = A.colptr;
1518 B.rowind = A.rowind;
1519 B.nzval.Resize( A.nnz );
1529 GetDiagonal (
const DistSparseMatrix<Complex>& A,
1530 NumVec<Complex>& diag );
1535 template <
class F1,
class F2>
1537 CopyPattern (
const DistSparseMatrix<F1>& A, DistSparseMatrix<F2>& B )
1540 PushCallStack(
"CopyPattern");
1544 B.nnzLocal = A.nnzLocal;
1545 B.colptrLocal = A.colptrLocal;
1546 B.rowindLocal = A.rowindLocal;
1547 B.nzvalLocal.Resize( A.nnzLocal );
1582 PushCallStack(
"DistSparseMatMultGlobalVec");
1584 if( B.m() != AMat.
size ){
1585 std::ostringstream msg;
1587 <<
"The size of the matrix A is " << AMat.
size << std::endl
1588 <<
"The size of the vector B is " << B.m() << std::endl
1589 <<
"and they must agree with each other.";
1590 throw std::runtime_error( msg.str().c_str() );
1592 if( C.m() != AMat.
size ){
1593 std::ostringstream msg;
1595 <<
"The size of the matrix A is " << AMat.
size << std::endl
1596 <<
"The size of the vector C is " << C.m() << std::endl
1597 <<
"and they must agree with each other.";
1598 throw std::runtime_error( msg.str().c_str() );
1601 MPI_Comm comm = AMat.
comm;
1603 Int mpirank, mpisize;
1604 MPI_Comm_rank( comm, &mpirank );
1605 MPI_Comm_size( comm, &mpisize );
1617 Int numColLocalFirst = AMat.
size / mpisize;
1618 Int firstCol = mpirank * numColLocalFirst;
1622 for( Int j = 0; j < numColLocal; j++ ){
1623 Int jcol = firstCol + j;
1627 DeltaClocal( irow ) += AMat.
nzvalLocal(i) * B(jcol);
1631 mpi::Allreduce( DeltaClocal.Data(), DeltaC.Data(),
1632 AMat.
size, MPI_SUM, comm );
1634 for( Int i = 0; i < AMat.
size; i++ ){
1635 C(i) = beta * C(i) + alpha * DeltaC(i);
1650 const std::vector<Real>& x,
1651 const std::vector<Real>& y,
1652 const std::vector<Real>& xx,
1653 std::vector<Real>& yy );
1658 #endif // _UTILITY_HPP_
Numerical vector.
Definition: numvec_decl.hpp:61
IntNumVec colptrLocal
Dimension numColLocal + 1, storing the pointers to the nonzero row indices and nonzero values in rowp...
Definition: sparse_matrix_decl.hpp:108
Implementation of Numerical Vector.
void SetValue(NumMat< F > &M, F val)
SetValue sets a numerical matrix to a constant val.
Definition: nummat_impl.hpp:61
NumVec< F > nzvalLocal
Dimension nnzLocal, storing the nonzero values.
Definition: sparse_matrix_decl.hpp:116
SparseMatrix describes a sequential sparse matrix saved in compressed sparse column format...
Definition: sparse_matrix_decl.hpp:66
IntNumVec rowindLocal
Dimension nnzLocal, storing the nonzero row indices. The indices are 1-based (FORTRAN-convention), i.e. the first row index is 1.
Definition: sparse_matrix_decl.hpp:113
Interface with MPI to facilitate communication.
Int size
Matrix dimension.
Definition: sparse_matrix_decl.hpp:93
void DistSparseMatMultGlobalVec(const F alpha, const DistSparseMatrix< F > &AMat, const NumVec< F > &B, const F beta, NumVec< F > &C)
Multiply a DistSparseMatrix with a vector that is distributed across all processors participating the...
Definition: utility.hpp:1574
Implementation of numerical matrix.
Implementation of numerical tensor.
MPI_Comm comm
MPI communicator.
Definition: sparse_matrix_decl.hpp:119
Implementation of sparse matrices.
Definition: utility.hpp:1481
void LinearInterpolation(const std::vector< Real > &x, const std::vector< Real > &y, const std::vector< Real > &xx, std::vector< Real > &yy)
Linear interpolates from (x,y) to (xx,yy)
Definition: utility.cpp:953
Implementation of tiny vectors.