Subversion Repositories gelsvn

Rev

Rev 595 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 595 Rev 630
1
/* ----------------------------------------------------------------------- *
1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
5
 * ----------------------------------------------------------------------- */
6
 
6
 
7
#include "LinAlgIO.h"
7
#include "LinAlgIO.h"
8
#include <fstream>
8
#include <fstream>
9
#include <iostream>
9
#include <iostream>
10
#include <algorithm>
10
#include <algorithm>
11
#include <vector>
11
#include <vector>
12
 
12
 
13
using namespace std;
13
using namespace std;
14
 
14
 
15
namespace
15
namespace
16
{
16
{
17
 
17
 
18
		bool Comp(istreambuf_iterator<char>& Itt,const string str)
18
		bool Comp(istreambuf_iterator<char>& Itt,const string str)
19
		{
19
		{
20
		
20
		
21
				size_t cChar=0;
21
				size_t cChar=0;
22
				bool Ret=true;
22
				bool Ret=true;
23
				while(cChar<str.size() && Ret)
23
				while(cChar<str.size() && Ret)
24
				{
24
				{
25
						Ret=(*Itt==str[cChar]);
25
						Ret=(*Itt==str[cChar]);
26
						++Itt;
26
						++Itt;
27
						cChar++;
27
						cChar++;
28
				}
28
				}
29
		
29
		
30
				return Ret;
30
				return Ret;
31
		}
31
		}
32
	
32
	
33
		bool FindVar(istreambuf_iterator<char>& Itt,
33
		bool FindVar(istreambuf_iterator<char>& Itt,
34
								 istreambuf_iterator<char> End,
34
								 istreambuf_iterator<char> End,
35
								 const string VarName)
35
								 const string VarName)
36
		{
36
		{
37
				bool bFound=false;
37
				bool bFound=false;
38
		
38
		
39
				while(!bFound)
39
				while(!bFound)
40
				{
40
				{
41
						Itt=find(Itt,End,VarName[0]);
41
						Itt=find(Itt,End,VarName[0]);
42
						if(Itt==End)
42
						if(Itt==End)
43
								return false;
43
								return false;
44
						bFound=Comp(Itt,VarName);
44
						bFound=Comp(Itt,VarName);
45
				}
45
				}
46
		
46
		
47
				return true;
47
				return true;
48
		}
48
		}
49
	
49
	
50
 
50
 
51
}
51
}
52
 
52
 
53
namespace LinAlg
53
namespace LinAlg
54
{
54
{
55
		void ToMatlab(const CMatrix& A,
55
		void ToMatlab(const CMatrix& A,
56
									const std::string& VarName,
56
									const std::string& VarName,
57
									const std::string& FileName,
57
									const std::string& FileName,
58
									const bool append,
58
									const bool append,
59
									const std::string& Comment)
59
									const std::string& Comment)
60
		{
60
		{
61
				std::ofstream Mfile(FileName.c_str(), 
61
				std::ofstream Mfile(FileName.c_str(), 
62
														append ? std::ios::app : std::ios::trunc);
62
														append ? std::ios::app : std::ios::trunc);
63
																				 
63
																				 
64
	
64
	
65
	
65
	
66
				if(!Comment.empty())
66
				if(!Comment.empty())
67
						Mfile << "% " << Comment.c_str() << "\n";
67
						Mfile << "% " << Comment.c_str() << "\n";
68
				Mfile << VarName.c_str() << "=[";
68
				Mfile << VarName.c_str() << "=[";
69
	
69
	
70
 
70
 
71
				const double* pRow;
71
				const double* pRow;
72
 
72
 
73
				for(int cRow=0;cRow<A.Rows();cRow++)
73
				for(int cRow=0;cRow<A.Rows();cRow++)
74
				{
74
				{
75
						pRow=A[cRow];
75
						pRow=A[cRow];
76
						for(int cCol=0;cCol<A.Cols();cCol++)
76
						for(int cCol=0;cCol<A.Cols();cCol++)
77
						{
77
						{
78
								Mfile << "\t" << pRow[cCol];
78
								Mfile << "\t" << pRow[cCol];
79
						}
79
						}
80
						Mfile << ";\n";
80
						Mfile << ";\n";
81
				}
81
				}
82
	
82
	
83
				Mfile << "];\n\n";
83
				Mfile << "];\n\n";
84
		}
84
		}
85
 
85
 
86
 
86
 
87
		void FromMatlab(CMatrix& A,
87
		void FromMatlab(CMatrix& A,
88
										const std::string& VarName,
88
										const std::string& VarName,
89
										const std::string& FileName)
89
										const std::string& FileName)
90
		{
90
		{
91
				ifstream file(FileName.c_str());
91
				ifstream file(FileName.c_str());
92
				istreambuf_iterator<char> Itt(file);
92
				istreambuf_iterator<char> Itt(file);
93
				istreambuf_iterator<char> End;
93
				istreambuf_iterator<char> End;
94
 
94
 
95
				string VarId(VarName);
95
				string VarId(VarName);
96
				VarId+="=[";
96
				VarId+="=[";
97
 
97
 
98
				FindVar(Itt,End,VarId);
98
				FindVar(Itt,End,VarId);
99
 
99
 
100
				vector<double> data;
100
				vector<double> data;
101
				double elem;
101
				double elem;
102
				int nCols=0;
102
				int nCols=0;
103
 
103
 
104
				while((*Itt)!=';')	
104
				while((*Itt)!=';')	
105
				{
105
				{
106
						Itt=(file >> elem );
106
						Itt=(file >> elem );
107
						data.push_back(elem);
107
						data.push_back(elem);
108
						nCols++;
108
						nCols++;
109
				}
109
				}
110
 
110
 
111
				Itt++;
111
				Itt++;
112
				Itt++;
112
				Itt++;
113
				int nElems=nCols;
113
				int nElems=nCols;
114
 
114
 
115
				while((*Itt)!=']')
115
				while((*Itt)!=']')
116
				{
116
				{
117
						while((*Itt)!=';')	
117
						while((*Itt)!=';')	
118
						{
118
						{
119
								Itt=(file >> elem );
119
								Itt=(file >> elem );
120
								data.push_back(elem);
120
								data.push_back(elem);
121
								nElems++;
121
								nElems++;
122
						}
122
						}
123
						Itt++;
123
						Itt++;
124
						Itt++;
124
						Itt++;
125
				}
125
				}
126
 
126
 
127
				A.Resize(nElems/nCols,nCols);
127
				A.Resize(nElems/nCols,nCols);
128
 
128
 
129
				memcpy(A[0],&(data[0]),sizeof(double)*data.size());
129
				memcpy(A[0],&(data[0]),sizeof(double)*data.size());
130
 
130
 
131
 
131
 
132
/*	for(int cI=0;cI<20;cI++)
132
/*	for(int cI=0;cI<20;cI++)
133
		{
133
		{
134
		cout << *Itt;
134
		cout << *Itt;
135
		++Itt;
135
		++Itt;
136
		}
136
		}
137
*/
137
*/
138
/*	for(int cI=0;cI<data.size();cI++)
138
/*	for(int cI=0;cI<data.size();cI++)
139
		cout << data[cI] << "\t";
139
		cout << data[cI] << "\t";
140
 
140
 
141
		cout << endl << nCols << endl << nElems << endl;
141
		cout << endl << nCols << endl << nElems << endl;
142
*/
142
*/
143
	
143
	
144
		}
144
		}
145
 
145
 
146
}
146
}
147
 
147