Subversion Repositories gelsvn

Rev

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

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