Subversion Repositories gelsvn

Rev

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

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