-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonOutput.cxx
127 lines (102 loc) · 2.86 KB
/
CommonOutput.cxx
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
#ifndef LINALG_FILE_COMMON_OUTPUT_CXX
#include "CommonOutput.hxx"
namespace linalg
{
//! writes u in a file readable by Medit (.bb file)
void WriteMedit(const string& nom, const Vector<complex<double> >& u, int type)
{
ofstream file_out(nom.data());
file_out.precision(8);
file_out << "2 1 " << u.GetM() << " 2 \n";
if (type == 0)
for (int i = 0; i < u.GetM(); i++)
file_out << real(u(i)) << '\n';
else if (type == 1)
for (int i = 0; i < u.GetM(); i++)
file_out << imag(u(i)) << '\n';
else
for (int i = 0; i < u.GetM(); i++)
file_out << abs(u(i)) << '\n';
}
//! writes a vector of integers in binary with eventual swapping
void WriteBinaryInteger(Vector<int>& output_vector,
ostream& file_out, bool with_size, bool swap)
{
int N = output_vector.GetM();
if (swap)
for (int i = 0; i < N; i++)
output_vector(i) = swapEndian(output_vector(i));
output_vector.Write(file_out, with_size);
}
//! writing a real vector in single or double precision
template<class T, class Allocator>
void WriteBinaryDoubleOrFloat(const Vector<T, Allocator>& output_vector,
ostream& file_out, bool double_prec, bool with_size, bool swap)
{
int N = output_vector.GetM();
if (double_prec)
{
Vector<double> output(N);
if (swap)
for (int i = 0; i < N; i++)
output(i) = swapEndian(double(output_vector(i)));
else
for (int i = 0; i < N; i++)
output(i) = double(output_vector(i));
output.Write(file_out, with_size);
}
else
{
Vector<float> output(N);
if (swap)
for (int i = 0; i < N; i++)
output(i) = swapEndian(float(output_vector(i)));
else
for (int i = 0; i < N; i++)
output(i) = float(output_vector(i));
output.Write(file_out, with_size);
}
}
//! swapping octets of a real number (conversion big endian <-> little endian)
double swapEndian(double d)
{
double a;
char *dst = reinterpret_cast<char*>(&a);
char *src = reinterpret_cast<char*>(&d);
dst[0] = src[7];
dst[1] = src[6];
dst[2] = src[5];
dst[3] = src[4];
dst[4] = src[3];
dst[5] = src[2];
dst[6] = src[1];
dst[7] = src[0];
return a;
}
//! swapping octets of a real number (conversion big endian <-> little endian)
float swapEndian(float d)
{
float a;
char *dst = reinterpret_cast<char*>(&a);
char *src = reinterpret_cast<char*>(&d);
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
return a;
}
//! swapping octets of an integer (conversion big endian <-> little endian)
int swapEndian(int d)
{
int a;
char *dst = reinterpret_cast<char*>(&a);
char *src = reinterpret_cast<char*>(&d);
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
return a;
}
}
#define LINALG_FILE_COMMON_OUTPUT_CXX
#endif