Subversion Repositories gelsvn

Rev

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

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