1 /* Copyright (C) 2012 LinBox
2  * Written by bds
3  *
4  *
5  *
6  * ========LICENCE========
7  * This file is part of the library LinBox.
8  *
9   * LinBox is free software: you can redistribute it and/or modify
10  * it under the terms of the  GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  * ========LICENCE========
23  */
24 
25 #ifndef __LINBOX_write_mm_h
26 #define __LINBOX_write_mm_h
27 
28 /* write-mm.h
29  * Tools to help write fields and matrices in matrix market formats.
30  *
31  */
32 
33 #include <iostream>
34 #include <string>
35 #include "linbox/integer.h"
36 
37 namespace LinBox
38 {
39 
40 /// Write second line and comment part of matrix market header
41 template <class Field>
writeMMComment(std::ostream & os,Field & F,std::string name,std::string comment)42 std::ostream& writeMMComment(std::ostream& os, Field& F, std::string name, std::string comment) {
43 	F.write(os << "% written by LinBox::" << name << "<field>, field = ") << std::endl;
44 	//F.write(os << name << "<"/*, std::string("")*/) << " >(F)" << std::endl;
45 	if (comment.size() > 0)
46 		os << "%" << std::endl << "% " << comment << std::endl << "%" << std::endl;
47     return os;
48 }
49 
50 /// Write matrix market header (up to the i,j,val lines) for a sparse or structured matrix.
51 template <class BB>
52 std::ostream& writeMMCoordHeader(std::ostream& os, BB& A, size_t nnz, std::string name, std::string comment = "") {
53 	os << "%%MatrixMarket matrix coordinate integer general" << std::endl;
54 	writeMMComment(os, A.field(), name, comment);
55 	os << A.rowdim() << " " << A.coldim() << " " << nnz << std::endl;
56 	return os;
57 }
58 
59 /// Write matrix market header (up to the i,j lines) for a {0,1} sparse or structured matrix.
60 template <class BB>
61 std::ostream& writeMMPatternHeader(std::ostream& os, BB& A, size_t nnz, std::string name, std::string comment = "") {
62 	os << "%%MatrixMarket matrix coordinate pattern general" << std::endl;
63 	writeMMComment(os, A.field(), name, comment);
64 	os << A.rowdim() << " " << A.coldim() << " " << nnz << std::endl;
65 	return os;
66 }
67 
68 /// Write matrix market header (up to the entry lines) for a dense matrix.
69 template <class BB>
70 std::ostream& writeMMArrayHeader(std::ostream& os, BB& A, std::string name, std::string comment = "") {
71 	os << "%%MatrixMarket matrix array integer general" << std::endl;
72 	writeMMComment(os, A.field(), name, comment);
73 	os << A.rowdim() << " " << A.coldim() << std::endl;
74 	return os;
75 }
76 
77 /// Generic dense matrix writer to matrix market array format (col major).
78 template <class Mat>
79 std::ostream& writeMMArray(std::ostream& os, Mat& A, std::string name, std::string comment = "") {
80 	writeMMArrayHeader(os, A, name, comment);
81 	typename Mat::Field::Element x; A.field().assign(x, A.field().zero);
82 	for (size_t j = 0; j < A.coldim(); ++j)
83 		for (size_t i = 0; i < A.rowdim(); ++i)
84 			os << A.getEntry(x, i, j) << std::endl;
85 	return os;
86 }
87 
88 /// eltype(x) returns a string containing the name of the type of field or ring element x.
eltype(float x)89 inline std::string eltype(float x) { return "float"; }
eltype(double x)90 inline std::string eltype(double x) { return "double"; }
eltype(int8_t x)91 inline std::string eltype(int8_t x) { return "int8_t"; }
eltype(int16_t x)92 inline std::string eltype(int16_t x) { return "int16_t"; }
eltype(int32_t x)93 inline std::string eltype(int32_t x) { return "int32_t"; }
eltype(int64_t x)94 inline std::string eltype(int64_t x) { return "int64_t"; }
eltype(integer x)95 inline std::string eltype(integer x) { return "integer"; }
eltype(uint8_t x)96 inline std::string eltype(uint8_t x) { return "uint8_t"; }
eltype(uint16_t x)97 inline std::string eltype(uint16_t x) { return "uint16_t"; }
eltype(uint32_t x)98 inline std::string eltype(uint32_t x) { return "uint32_t"; }
eltype(uint64_t x)99 inline std::string eltype(uint64_t x) { return "uint64_t"; }
100 
101 }  // end of namespace LinBox
102 #endif // __write_mm_h
103 
104 // Local Variables:
105 // mode: C++
106 // tab-width: 4
107 // indent-tabs-mode: nil
108 // c-basic-offset: 4
109 // End:
110 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
111