File: Matio.h

package info (click to toggle)
fflas-ffpack 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,128 kB
  • ctags: 1,369
  • sloc: sh: 10,959; cpp: 7,935; makefile: 263; perl: 11; csh: 5
file content (291 lines) | stat: -rw-r--r-- 7,689 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
/* Copyright (C) LinBox,FFLAS-FFPACK
 *
 * ========LICENCE========
 * This file is part of the library FFLAS-FFPACK.
 *
 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 */

#ifndef __FFLASFFPACK_matio_H
#define __FFLASFFPACK_matio_H

#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include "fflas-ffpack/fflas/fflas.h"

// Reading and writing matrices over double

#if 0
// Reading a matrice from a (eventually zipped) file
double * read_dbl(char * mat_file,int* tni,int* tnj)
{
	char *UT, *File_Name;
	int is_gzipped = 0;
	size_t s = strlen(mat_file);
	double* X;
	if ((mat_file[--s] == 'z') &&
	    (mat_file[--s] == 'g') &&
	    (mat_file[--s] == '.')) {
		is_gzipped = 1;
		File_Name = "/tmp/bbXXXXXX_";
		mkstemp(File_Name);
		UT = new char[s+34+strlen(File_Name)];
		sprintf(UT,"gunzip -c %s > %s", mat_file, File_Name);
		system(UT);
		sprintf(UT,"\\rm %s", File_Name);
	} else
		File_Name = mat_file;

	FILE* FileDes = fopen(File_Name, "r");
	if (FileDes != NULL) {
		char * tmp = new char[200];// usigned long tni, tnj;
		fscanf(FileDes,"%d %d %s\n",tni, tnj, &tmp) ;
		int n=*tni;
		int p=*tnj;
		X = new double[n*p];
		for (int i=0;i<n*p;++i)
			X[i] = (double) 0;
		long i,j; long val;
		fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
		while(i && j) {
			X[p*(i-1)+j-1] = (double) val;
			fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
		}
	}

	fclose(FileDes);
	if (is_gzipped) system(UT);
	return X;
}

// Displays a matrix
std::ostream& write_dbl(std::ostream& c,
			double* E,
			int n, int m, int id)
{

	for (int i = 0; i<n;++i){
		for (int j=0; j<m;++j)
			c << *(E+j+id*i) << " ";
		c << std::endl;
	}
	return c << std::endl;
}
#endif
// Reading and writing matrices over field

// Reading a matrice from a (eventually zipped) file
template<class Field>
typename Field::Element * read_field(const Field& F,char * mat_file,int* tni,int* tnj)
{
	char *UT, *File_Name;
	int is_gzipped = 0;
	size_t s = strlen(mat_file);
	typename Field::Element zero;
	F.init(zero,0UL);
	typename Field::Element * X = NULL;
	if ((mat_file[--s] == 'z') &&
	    (mat_file[--s] == 'g') &&
	    (mat_file[--s] == '.')) {
		is_gzipped = 1;
		char tmp_nam[] = "/tmp/bbXXXXXX_";
		File_Name  = tmp_nam;
		mkstemp(File_Name);
		UT = new char[s+34+strlen(File_Name)];
		sprintf(UT,"gunzip -c %s > %s", mat_file, File_Name);
		system(UT);
		sprintf(UT,"\\rm %s", File_Name);
	} else
		File_Name = mat_file;
	FILE* FileDes = fopen(File_Name, "r");
	if (FileDes != NULL) {
		char  tmp [200];// usigned long tni, tnj;
		fscanf(FileDes,"%d %d %s\n",tni, tnj, tmp) ;
		int n=*tni;
		int p=*tnj;
		X = new typename Field::Element[n*p];
		for (int i=0;i<n*p;++i)
			X[i] = zero;
		long i,j; long val;
		fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
		while(i && j) {
			F.init(X[p*(i-1)+j-1],val);
			fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
		}
	}

	fclose(FileDes);
	if (is_gzipped) system(UT);
	return X;
}

template<class Field>
void read_field4(const Field& F,char * mat_file,int* tni,int* tnj,
		 typename Field::Element *& NW,typename Field::Element *& NE,
		 typename Field::Element *& SW,typename Field::Element *& SE)
{
	char *UT, *File_Name;
	int is_gzipped = 0;
	size_t s = strlen(mat_file);
	typename Field::Element zero;
	F.init(zero,0);
	typename Field::Element * X;
	if ((mat_file[--s] == 'z') &&
	    (mat_file[--s] == 'g') &&
	    (mat_file[--s] == '.')) {
		is_gzipped = 1;
		// XXX on fait pas ├ža !
		File_Name = "/tmp/bbXXXXXX_";
		mkstemp(File_Name);
		UT = new char[s+34+strlen(File_Name)];
		sprintf(UT,"gunzip -c %s > %s", mat_file, File_Name);
		system(UT);
		sprintf(UT,"\\rm %s", File_Name);
	} else
		File_Name = mat_file;
	FILE* FileDes = fopen(File_Name, "r");
	if (FileDes != NULL) {
		char * tmp = new char[200];// usigned long tni, tnj;
		fscanf(FileDes,"%d %d %s\n",tni, tnj, &tmp) ;
		int n=*tni;
		int p=*tnj;
		int no2= n>>1;
		int po2 = p>>1;
		NW = new typename Field::Element[no2*po2];
		NE = new typename Field::Element[no2*(p-po2)];
		SW = new typename Field::Element[(n-no2)*po2];
		SE = new typename Field::Element[(n-no2)*(p-po2)];

		for (int i=0;i<no2*po2;++i)
			NW[i] = zero;
		for (int i=0;i<no2*(p-po2);++i)
			NE[i] = zero;
		for (int i=0;i<(n-no2)*po2;++i)
			SW[i] = zero;
		for (int i=0;i<(n-no2)*(p-po2);++i)
			SE[i] = zero;
		long i,j; long val;
		fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
		while(i && j) {
			if (i<=no2){
				if (j<=po2){
					F.init(NW[po2*(i-1)+j-1],val);
					fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
				}
				else{
					F.init(NE[po2*(i-1)+j-1-po2],val);
					fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
				}
			}
			else{
				if (j<=po2){
					F.init(SW[(p-po2)*(i-1-no2)+j-1],val);
					fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
				}
				else{
					F.init(SE[(p-po2)*(i-1-no2)+j-1-po2],val);
					fscanf(FileDes,"%ld %ld %ld\n",&i, &j, &val) ;
				}
			}
		}
		//    *A1 = NW;
		//*A2 = NE;
		//*A3 = SW;
		//*A4 = SE;

	}

	fclose(FileDes);
	if (is_gzipped) system(UT);
}

// Displays a matrix
template<class Field>
std::ostream& write_field(const Field& F,std::ostream& c,
			  const typename Field::Element* E,
			  int n, int m, int id, bool mapleFormat = false)
{

	double tmp;
	if (mapleFormat) c << "Matrix(" << n <<',' << m << ", [" ;
	for (int i = 0; i<n;++i){
		if (mapleFormat) c << '[';
		for (int j=0; j<m;++j){
			F.convert(tmp,*(E+j+id*i));
			c << tmp;
			if (mapleFormat && j<m-1) c << ',';
			c << ' ';
		}
		if (mapleFormat) c << ']';
		if (mapleFormat && i<n-1) c << ',';
		if (!mapleFormat) c << std::endl;
	}
	if (mapleFormat) c << "])";
	return c ;
}

// Displays a triangular matrix
//! @todo let the user choose to convert to a non destructive format (not double but long or Integer...)
template<class Field>
std::ostream& write_field(const Field& F,std::ostream& c,
			  const FFLAS::FFLAS_UPLO uplo, const FFLAS::FFLAS_DIAG unit,
			  const typename Field::Element* E,
			  int n, int m, int id, bool mapleFormat = false)
{

	double tmp;
	if (mapleFormat) c << "Matrix(" << n <<',' << m << ",[";
	for (int i = 0; i<n;++i){
		if (mapleFormat) c << '[';
		// under diag
		for (int j=0; j<i ;++j){
			if (uplo == FFLAS::FflasLower)
				F.convert(tmp,*(E+j+id*i));
			else tmp = 0 ;
			c << tmp;
			if (mapleFormat && j<m-1) c << ',';
			c << ' ';
		}
		// on diag
		if (unit == FFLAS::FflasNonUnit)
			F.convert(tmp,*(E+i+id*i));
		else
			tmp = 1.;
		c << tmp;
		if (mapleFormat && i<m-1) c << ',';
		c << ' ';
		// over diag
		for (int j=i+1; j<m;++j){
			if (uplo == FFLAS::FflasUpper)
				F.convert(tmp,*(E+j+id*i));
			else
				tmp = 0 ;
			c << tmp;
			if (mapleFormat && j<m-1) c << ',';
			c << ' ';
		}
		if (mapleFormat) c << ']';
		if (mapleFormat && i<n-1) c << ',';
		if (!mapleFormat) c << std::endl;
	}
	if (mapleFormat) c << "])";
	return c ;
}

#endif //__FFLASFFPACK_matio_H