17#ifndef __B2VECTOR_INDEX_H__
18#define __B2VECTOR_INDEX_H__
20#include "b2linear_algebra_def.H"
22namespace b2000 {
namespace b2linalg {
24struct Vindex_constref {
25 typedef Vindex_constref base;
26 typedef Vindex_scale_constref const_base;
31class Vector<T, Vindex_constref> {
33 Vector() : s(0), v(0), index(0) {}
35 Vector(
size_t s_,
const T* v_,
const size_t* index_) : s(s_), v(v_), index(index_) {}
37 Vector(
const Vector& v_) : s(v_.s), v(v_.v), index(v_.index) {}
39 Vector(
const Vector<T, Vindex_ref>& v_) : s(v_.s), v(v_.v), index(v_.index) {}
41 bool is_null()
const {
return v == 0; }
43 size_t size()
const {
return s; }
45 const T& operator[](
size_t i)
const {
return v[index[i]]; }
47 Vector operator()(
const Interval& i)
const {
return Vector(i.size(), v, index + i[0]); }
59Vector<T, Vindex_constref> Vector<T, Vindex_constref>::null;
61struct Vindex1_constref {};
64class Vector<T, Vindex1_constref> :
public Vector<T, Vindex_constref> {
66 Vector(T default_value_ = T(0))
67 : Vector<T, Vindex_constref>(), s_v(0), default_value(default_value_) {}
69 Vector(
size_t s_,
const T* v_,
size_t s_v_,
const size_t* index_)
70 : Vector<T, Vindex_constref>(s_, v_, index_), s_v(s_v_), default_value(T(0)) {}
72 Vector(
const Vector& v_)
73 : Vector<T, Vindex_constref>(v_), s_v(v_.s_v), default_value(v_.default_value) {}
75 void set_default_value(T default_value_) { default_value = default_value_; }
77 const T& operator[](
size_t i)
const {
78 if (s_v == 0 || Vector<T, Vindex_constref>::index[i] < s_v) {
79 return Vector<T, Vindex_constref>::v[Vector<T, Vindex_constref>::index[i]];
90 typedef Vindex_ref base;
91 typedef Vindex_scale_constref const_base;
96class Vector<T, Vindex_ref> {
98 Vector() : s(0), v(0), index(0) {}
100 Vector(
size_t s_ = 0, T* v_ = 0,
const size_t* index_ = 0) : s(s_), v(v_), index(index_) {}
102 Vector(
const Vector& v_) : s(v_.s), v(v_.v), index(v_.index) {}
104 bool is_null()
const {
return v == 0; }
106 size_t size()
const {
return s; }
109 for (
size_t i = 0; i != s; ++i) { v[i] = T(0); }
112 const T& operator[](
size_t i)
const {
return v[index[i]]; }
114 T& operator[](
size_t i) {
return v[index[i]]; }
116 Vector operator()(
const Interval& i)
const {
return Vector(i.size(), v, index + i[0]); }
118 Vector& operator=(
const Vector& v_) {
119 if (s != v_.s) { Exception() <<
"The two vectors do not have the same size." <<
THROW; }
120 for (
size_t i = 0; i != s; ++i) { v[index[i]] = v_[v_.index[i]]; }
124 Vector& operator=(
const Vector<T, Vdense>& v_) {
125 if (s != v_.size()) {
126 Exception() <<
"The two vectors do not have the same size." <<
THROW;
128 for (
size_t i = 0; i != s; ++i) { v[index[i]] = v_[i]; }
132 Vector& operator=(
const Vector<T, Vindex_scale_constref>& v_) {
133 if (s != v_.s) { Exception() <<
"The two vectors do not have the same size." <<
THROW; }
134 if (v_.v == v && v_.index == index && v_.scale == T(1)) {
return *
this; }
135 for (
size_t i = 0; i != s; ++i) { v[index[i]] = v_[i]; }
139 Vector& operator+=(
const Vector<T, Vindex_scale_constref>& v_) {
140 if (s != v_.s) { Exception() <<
"The two vectors do not have the same size." <<
THROW; }
141 for (
size_t i = 0; i != s; ++i) { v[index[i]] += v_[i]; }
145 Vector& operator=(
const Vector<T, Vincrement_scale_constref>& v_) {
146 if (s != v_.s) { Exception() <<
"The two vectors do not have the same size." <<
THROW; }
147 for (
size_t i = 0; i != s; ++i) { v[index[i]] = v_[i]; }
151 Vector& operator+=(
const Vector<T, Vincrement_scale_constref>& v_) {
152 if (s != v_.s) { Exception() <<
"The two vectors do not have the same size." <<
THROW; }
153 for (
size_t i = 0; i != s; ++i) { v[index[i]] += v_[i]; }
157 Vector& operator+=(
const Vector<T, Sum<Vindex_scale_constref, Vindex_scale_constref> >& v_) {
158 if (s != v_.s) { Exception() <<
"The two vectors do not have the same size." <<
THROW; }
159 for (
size_t i = 0; i != s; ++i) { v[index[i]] += v_[i]; }
164 const Vector<T, MVProd<Mrectangle_increment_st_constref, Vindex_scale_constref> >& v_) {
165 if (s != v_.size()) {
166 Exception() <<
"The two vectors do not have the same size, " << s <<
" and "
167 << v_.size() <<
"." <<
THROW;
169 Vector<T, Vdense> tmp1(s);
170 Vector<T, Vdense> tmp2(v_.v);
171 tmp1 = v_.scale * (v_.m * tmp2);
177 const Vector<T, MVProd<Mrectangle_increment_st_constref, Vindex_scale_constref> >& v_) {
178 if (s != v_.size()) {
179 Exception() <<
"The two vectors do not have the same size, " << s <<
" and "
180 << v_.size() <<
"." <<
THROW;
182 Vector<T, Vdense> tmp1(s);
183 Vector<T, Vdense> tmp2(v_.v);
184 tmp1 = v_.scale * (v_.m * tmp2);
191 T, Sum<Vindex_scale_constref,
192 MVProd<Mrectangle_increment_st_constref, Vindex_scale_constref> > >& v_) {
193 if (s != v_.size()) {
194 Exception() <<
"The two vectors do not have the same size, " << s <<
" and "
195 << v_.size() <<
"." <<
THROW;
197 Vector<T, Vdense> tmp1(s);
198 Vector<T, Vdense> tmp2(v_.v2.v);
199 tmp1 = (v_.scale * v_.v2.scale) * (v_.v2.m * tmp2);
200 *
this = v_.scale * v_.v1;
205 Vector& operator=(
const Vector<T, MVProd<Mpacked_st_constref, Vindex_scale_constref> >& v_) {
206 if (s != v_.size()) {
207 Exception() <<
"The two vectors do not have the same size, " << s <<
" and "
208 << v_.size() <<
"." <<
THROW;
210 Vector<T, Vdense> tmp1(s);
211 Vector<T, Vdense> tmp2(v_.v);
212 tmp1 = v_.scale * (v_.m * tmp2);
217 Vector& operator+=(
const Vector<T, MVProd<Mpacked_st_constref, Vindex_scale_constref> >& v_) {
218 if (s != v_.size()) {
219 Exception() <<
"The two vectors do not have the same size, " << s <<
" and "
220 << v_.size() <<
"." <<
THROW;
222 Vector<T, Vdense> tmp1(s);
223 Vector<T, Vdense> tmp2(v_.v);
224 tmp1 = v_.scale * (v_.m * tmp2);
229 Vector& operator=(
const Vector<
230 T, Sum<Vindex_scale_constref,
231 MVProd<Mpacked_st_constref, Vindex_scale_constref> > >& v_) {
232 if (s != v_.size()) {
233 Exception() <<
"The two vectors do not have the same size, " << s <<
" and "
234 << v_.size() <<
"." <<
THROW;
236 Vector<T, Vdense> tmp1(s);
237 Vector<T, Vdense> tmp2(v_.v2.v);
238 tmp1 = (v_.scale * v_.v2.scale) * (v_.v2.m * tmp2);
239 *
this = v_.scale * v_.v1;
245 const Vector<T, MVProd<Mcompressed_col_st_constref, Vindex_scale_constref> >& v_) {
246 if (s != v_.size()) {
247 Exception() <<
"The two vectors do not have the same size, " << s <<
" and "
248 << v_.size() <<
"." <<
THROW;
251 const size_t* colind_begin = v_.m.si;
252 const size_t* rowind_begin = v_.m.index + *colind_begin;
253 const T* value_begin = v_.m.m + *colind_begin;
254 T scale = v_.scale * v_.v.scale * v_.m.scale;
256 for (
size_t i = 0; i != s; ++i) {
257 const size_t*
const rowind_end = v_.m.index + *++colind_begin;
259 while (rowind_begin != rowind_end) {
260 val += *value_begin++ * v_.v[*rowind_begin++];
262 (*this)[i] = scale * val;
266 for (
size_t i = 0; i != v_.v.s; ++i) {
267 const size_t*
const rowind_end = v_.m.index + *++colind_begin;
268 const T val = v_.v[i];
269 while (rowind_begin != rowind_end) {
270 (*this)[*rowind_begin++] += scale * *value_begin++ * val;
277 Vector& operator=(
const Vector<
278 T, Sum<Vindex_scale_constref,
279 MVProd<Mcompressed_col_st_constref, Vindex_scale_constref> > >& v_) {
280 if (s != v_.size()) {
281 Exception() <<
"The two vectors do not have the same size, " << s <<
" and "
282 << v_.size() <<
"." <<
THROW;
285 (*this) = v_.scale * v_.v1;
286 const size_t* colind_begin = v_.v2.m.si;
287 const size_t* rowind_begin = v_.v2.m.index + *colind_begin;
288 const T* value_begin = v_.v2.m.m + *colind_begin;
289 T scale = v_.scale * v_.v.scale * v_.m.scale;
291 for (
size_t i = 0; i != s; ++i) {
292 const size_t*
const rowind_end = v_.v2.m.index + *++colind_begin;
294 while (rowind_begin != rowind_end) {
295 val += *value_begin++ * v_.v2.v[*rowind_begin++];
297 (*this)[i] += scale * val;
300 for (
size_t i = 0; i != v_.v2.v.s; ++i) {
301 const size_t*
const rowind_end = v_.v2.m.index + *++colind_begin;
302 const T val = v_.v2[i];
303 while (rowind_begin != rowind_end) {
304 (*this)[*rowind_begin++] += scale * *value_begin++ * val;
321Vector<T, Vindex_ref> Vector<T, Vindex_ref>::null;
323struct Vindex_scale_constref {
324 typedef Vindex_scale_constref base;
325 typedef Vindex_scale_constref const_base;
330class Vector<T, Vindex_scale_constref> {
332 Vector() : s(0), v(0), index(0), scale(0) {}
334 Vector(
size_t s_,
const T* v_,
const size_t* index_, T scale_ = T(1))
335 : s(s_), v(v_), index(index_), scale(scale_) {}
337 Vector(
const Vector& v_) : s(v_.s), v(v_.v), index(v_.index), scale(v_.scale) {}
339 Vector(
const Vector<T, Vindex_ref>& v_) : s(v_.s), v(v_.v), index(v_.index), scale(1) {}
341 Vector(
const Vector<T, Vindex_constref>& v_) : s(v_.s), v(v_.v), index(v_.index), scale(1) {}
343 bool is_null()
const {
return v == 0; }
345 size_t size()
const {
return s; }
347 const T operator[](
size_t i)
const {
return scale * v[index[i]]; }
349 Vector operator()(
const Interval& i)
const {
return Vector(i.size(), v, index + i[0], scale); }
351 Vector& operator*(T scale_) {
356 T operator*(
const Vector<T, Vincrement_scale_constref>& v_)
const {
357 if (s != v_.s) { Exception() <<
"The two vectors do not have the same size." <<
THROW; }
359 for (
size_t i = 0; i != s; ++i) { tmp += v[index[i]] * v_.v[v_.index[i]]; }
360 return tmp * scale * v_.scale;
374Vector<T, Vindex_scale_constref> Vector<T, Vindex_scale_constref>::null;
#define THROW
Definition b2exception.H:198
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32