Subversion Repositories gelsvn

Rev

Rev 39 | Rev 136 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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