Subversion Repositories gelsvn

Rev

Rev 595 | Details | Compare with Previous | Last modification | View Log | RSS feed

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