-
Notifications
You must be signed in to change notification settings - Fork 0
/
inner_prod_vector.hpp
179 lines (157 loc) · 5.14 KB
/
inner_prod_vector.hpp
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
/**
* \file inner_prod_vector.hpp
* \brief header file for InnerProdVector class
* \author Jason Hicken <[email protected]>
* \version 1.0
*/
#pragma once
#include <ostream>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/numeric/ublas/vector.hpp>
using std::fstream;
using std::ofstream;
using std::ifstream;
using std::string;
using std::ios;
using std::cerr;
using std::endl;
namespace ublas = boost::numeric::ublas;
/*!
* \class InnerProdVector
* \brief defines a vector from a finite-dim inner-product space
*
* This is simply a front end for ublas::vector<double>, with some
* additional functionality that we need for the Krylov iterative
* solvers. Also provides a simplified notation.
*/
class InnerProdVector : public ublas::vector<double> {
public:
/*!
* \brief default constructor, creates an empty vector
*/
InnerProdVector() : ublas::vector<double>() {}
/*!
* \brief constructor, creates and initializes a vector of given size
* \param[in] size - number of elements in vector
* \param[in] val - all elements are given value val
*/
InnerProdVector(const int & size, const double & val = 0.0) :
ublas::vector<double>(size, val) {}
/*!
* \brief constructor, creates a vector from a ublas::vector<double>
* \param[in] u - ublas vector to initialize vector to
*/
InnerProdVector(const ublas::vector<double> & u) :
ublas::vector<double>(u) {}
/*!
* \brief copy constructor
* \param[in] u - existing vector that we want to copy
*/
InnerProdVector(const InnerProdVector & u) :
ublas::vector<double>(static_cast<ublas::vector<double> >(u)) {}
/*!
* \brief default destructor
*/
~InnerProdVector() {}
/*!
* \brief inner product between two InnerProdVectors
* \param[in] u - first InnerProdVector in dot product
* \param[in] v - second InnerProdVector in dot product
*/
friend double InnerProd(const InnerProdVector & u,
const InnerProdVector & v) {
return ublas::inner_prod(u, v);
}
/*!
* \brief the L2 norm of the InnerProdVector
*/
double Norm2() const {
return norm_2(*this);
}
/*!
* \brief general linear combination of two InnerProdVectors
* \param[in] a - scalar factor for x
* \param[in] x - first InnerProdVector in linear combination
* \param[in] b - scalar factor for y
* \param[in] y - second InnerProdVector in linear combination
*/
void EqualsAXPlusBY(const double & a, const InnerProdVector & x,
const double & b, const InnerProdVector & y) {
for (int i = 0; i < size(); i++)
this->operator()(i) = a*x(i) + b*y(i);
}
/*!
* \brief assign all elements of a vector the same value
* \param[in] val - scalar value that we want the elements to be set to
*/
void operator=(const double & val) {
ublas::vector<double>::operator=(
ublas::scalar_vector<double>(size(), val));
}
void TextWrite(ofstream & fout) {
if (!fout.is_open()) {
cerr << "InnerProdVector::TextWrite(): fout is not open!" << endl;
throw(-1);
}
for (int i = 0; i < size(); i++) {
fout << this->operator()(i) << endl;
}
}
void BinaryWrite(ofstream & fout) const {
if (!fout.is_open()) {
cerr << "InnerProdVector::BinaryWrite(): fout is not open!" << endl;
throw(-1);
}
// using &data() causes problems for BinaryRead
fout.write(reinterpret_cast<const char*>(&operator[](0)),
size()*sizeof(double));
}
void BinaryWrite(ofstream & fout, const unsigned long & ptr) const {
if (!fout.is_open()) {
cerr << "InnerProdVector::BinaryWrite(): fout is not open!" << endl;
throw(-1);
}
// using &data() causes problems for BinaryRead
fout.seekp(ptr);
fout.write(reinterpret_cast<const char*>(&operator[](0)),
size()*sizeof(double));
}
void BinaryWrite(fstream & fout) const {
if (!fout.is_open()) {
cerr << "InnerProdVector::BinaryWrite(): fout is not open!" << endl;
throw(-1);
}
// using &data() causes problems for BinaryRead
fout.write(reinterpret_cast<const char*>(&operator[](0)),
size()*sizeof(double));
}
void BinaryWrite(fstream & fout, const unsigned long & ptr) const {
if (!fout.is_open()) {
cerr << "InnerProdVector::BinaryWrite(): fout is not open!" << endl;
throw(-1);
}
// using &data() causes problems for BinaryRead
fout.seekp(ptr);
fout.write(reinterpret_cast<const char*>(&operator[](0)),
size()*sizeof(double));
}
void BinaryRead(ifstream & fin) {
if (!fin.is_open()) {
cerr << "InnerProdVector::BinaryRead(): fin is not open!" << endl;
throw(-1);
}
// using &data() causes problems for BinaryRead
fin.read(reinterpret_cast<char*>(&operator[](0)), size()*sizeof(double));
}
void BinaryRead(ifstream & fin, const unsigned long & ptr) {
if (!fin.is_open()) {
cerr << "InnerProdVector::BinaryRead(): fin is not open!" << endl;
throw(-1);
}
// using &data() causes problems for BinaryRead
fin.seekg(ptr);
fin.read(reinterpret_cast<char*>(&operator[](0)), size()*sizeof(double));
}
};