BELFEM 0.9.3
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_AR_Vector.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef BELFEM_CL_AR_VECTOR_HPP
13#define BELFEM_CL_AR_VECTOR_HPP
14
15#include <memory>
16#include "armadillo.hpp"
17
18#include "typedefs.hpp"
19#include "assert.hpp"
20#include "cl_Cell.hpp"
21
22namespace belfem
23{
31 template < typename T >
32 class Vector
33 {
34//------------------------------------------------------------------------------
35 public :
36//------------------------------------------------------------------------------
37
38 typedef arma::Mat <T> VectorType;
39
40//------------------------------------------------------------------------------
41 private :
42//------------------------------------------------------------------------------
43
44 // member class of underlying vector implementation
45 VectorType mVector;
46
47//------------------------------------------------------------------------------
48 public :
49//------------------------------------------------------------------------------
50
51 Vector() = default;
52
53// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54
55 Vector( const size_t aNumRows ) :
56 mVector( aNumRows, 1 ) {}
57
58// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59
60 Vector( const size_t aNumRows, const T & aValue ) :
61 mVector( aNumRows, 1 )
62 {
63 this->fill( aValue );
64 }
65
66// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
67
68 Vector( std::initializer_list<T> aInitList )
69 {
70 if( aInitList.size() == 1 )
71 {
72 // Single element: interpret as Vector(1, value)
73 mVector.set_size( 1, 1 );
74 mVector( 0 ) = *aInitList.begin();
75 }
76 else
77 {
78 // Multiple elements: interpret as list of values
79 mVector.set_size( aInitList.size(), 1 );
80 size_t i = 0;
81 for( const T & val : aInitList )
82 {
83 mVector( i++ ) = val;
84 }
85 }
86 }
87// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88
89 Vector( const Cell< T > & aCell )
90 {
91 mVector.set_size( aCell.size(), 1 );
92 std::copy( aCell.begin(), aCell.end(), mVector.begin() );
93 }
94
95// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96
97 Vector( const std::vector< T > & aVector )
98 {
99 mVector.set_size( aVector.size(), 1 );
100 std::copy( aVector.begin(), aVector.end(), mVector.begin() );
101 }
102
103// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104
105 Vector( const VectorType & aExpression ) :
106 mVector( aExpression )
107 {}
108
109// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
110
111 template < typename ET, typename OP>
112 Vector( const arma::Op<ET,OP> & aExpression )
113 : mVector( aExpression ) {}
114
115// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116
120 Vector( const arma::subview_col< T > & aCol ) :
121 mVector( aCol )
122 {}
123
124// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125
129 Vector( const arma::subview_row< T > & aRow ) :
130 mVector( arma::trans( aRow ) )
131 {}
132
133//------------------------------------------------------------------------------
134
138 Vector( const Vector< T > & aVector ) :
139 mVector( aVector.mVector )
140 {}
141
145 Vector( Vector< T > && aVector ) noexcept :
146 mVector( std::move( aVector.mVector ) )
147 {}
148
149//------------------------------------------------------------------------------
150
154 virtual ~Vector() = default;
155
156//------------------------------------------------------------------------------
157// MEMORY
158//------------------------------------------------------------------------------
159
162 T *
164 {
165 return mVector.memptr();
166 }
167
168// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
169
170 const T *
171 data() const
172 {
173 return mVector.memptr();
174 }
175
176//------------------------------------------------------------------------------
177
181 VectorType &
183 {
184 return mVector;
185 }
186
187// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
188
189
190 const VectorType &
192 {
193 return mVector;
194 }
195
196//------------------------------------------------------------------------------
197// UTILITIES
198//------------------------------------------------------------------------------
199
203 void
204 fill( const T & aValue )
205 {
206 mVector.fill( aValue );
207 }
208
209//------------------------------------------------------------------------------
210
214 void
215 set_size( const size_t aNumRows )
216 {
217 mVector.set_size( aNumRows, 1 );
218 }
219
220// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
221
222 void
223 set_size( const size_t aNumRows, const T & aValue )
224 {
225 this->set_size( aNumRows );
226 this->fill( aValue );
227 }
228
229//------------------------------------------------------------------------------
230
234 size_t
235 length() const
236 {
237 return mVector.n_rows;
238 }
239
240//------------------------------------------------------------------------------
241// ACCESS OPERATORS
242//------------------------------------------------------------------------------
243
248 operator=( Vector< T > && aVector ) noexcept
249 {
250 if ( this != & aVector )
251 {
252 mVector = std::move( aVector.mVector );
253 }
254 return *this;
255 }
256
261 operator=( const Vector< T > & aVector )
262 {
263 if ( this != &aVector )
264 {
265 mVector = aVector.mVector;
266 }
267 return *this;
268 }
269
270
271//------------------------------------------------------------------------------
272 T &
273 operator()( const size_t aIndex )
274 {
275 BELFEM_ASSERT( aIndex < this->length(),
276 "Index %lu out of bounds, which must be smaller than %lu.",
277 ( long unsigned int ) aIndex,
278 ( long unsigned int ) this->length());
279
280 return mVector( aIndex );
281 }
282
283// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
284
285 const T &
286 operator()( const size_t aIndex ) const
287 {
288 BELFEM_ASSERT( aIndex < this->length(),
289 "Index %lu out of bounds, which must be smaller than %lu.",
290 ( long unsigned int ) aIndex,
291 ( long unsigned int ) this->length());
292
293 return mVector( aIndex );
294 }
295
296//------------------------------------------------------------------------------
297// ITERATORS
298//------------------------------------------------------------------------------
299
300 auto
301 begin() -> decltype( mVector.begin() )
302 {
303 return mVector.begin();
304 }
305
306 auto
307 end() -> decltype( mVector.end() )
308 {
309 return mVector.end();
310 }
311
312 auto
313 begin() const -> decltype( mVector.begin() )
314 {
315 return mVector.begin();
316 }
317
318 auto
319 end() const -> decltype( mVector.end() )
320 {
321 return mVector.end();
322 }
323
324//------------------------------------------------------------------------------
325// EQUAL OPERATORS
326//------------------------------------------------------------------------------
327
329 operator=( const T & aValue )
330 {
331 this->fill( aValue );
332 return *this;
333 }
334
335// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
336
338 operator=( std::initializer_list<T> aInitList )
339 {
340 if( aInitList.size() == 1 )
341 {
342 // Single element: interpret as set_size(1, value)
343 this->set_size( 1, *aInitList.begin() );
344 }
345 else
346 {
347 // Multiple elements: interpret as list of values
348 this->set_size( aInitList.size() );
349 size_t i = 0;
350 for( const T & val : aInitList )
351 {
352 mVector( i++ ) = val;
353 }
354 }
355 return *this;
356 }
357
358// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
359
360 template < typename ET >
362 operator=( const ET & aExpression )
363 {
364 mVector = aExpression;
365 return *this;
366 }
367
368//------------------------------------------------------------------------------
369// ADD OPERATORS
370//------------------------------------------------------------------------------
371
373 operator+=( const T & aValue )
374 {
375 mVector += aValue;
376 return *this;
377 }
378
379// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
380
382 operator+=( const Vector< T > & aVector )
383 {
384 mVector += aVector.vector_data();
385 return *this;
386 }
387
388// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
389
391 operator+=( const VectorType & aExpression )
392 {
393 mVector += aExpression;
394 return *this;
395 }
396
397// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
398
399 template < typename ET >
400 Vector<T> &
401 operator+=( const ET & aExpression )
402 {
403 mVector += aExpression;
404 return *this;
405 }
406
407//------------------------------------------------------------------------------
408// SUBTRACT OPERATORS
409//------------------------------------------------------------------------------
410
412 operator-=( const T & aValue )
413 {
414 mVector -= aValue;
415 return *this;
416 }
417
418// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
419
421 operator-=( const Vector< T > & aVector )
422 {
423 mVector -= aVector.vector_data();
424 return *this;
425 }
426
427// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
428
430 operator-=( const VectorType & aExpression )
431 {
432 mVector -= aExpression;
433 return *this;
434 }
435
436// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
437
438 template < typename ET >
439 Vector<T> &
440 operator-=( const ET & aExpression )
441 {
442 mVector -= aExpression;
443 return *this;
444 }
445
446//------------------------------------------------------------------------------
447// MULTIPLY OPERATORS
448//------------------------------------------------------------------------------
449
451 operator*=( const T & aValue )
452 {
453 mVector *= aValue;
454 return *this;
455 }
456
457// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
458
460 operator*=( const Vector< T > & aVector )
461 {
462 mVector *= aVector.vector_data();
463 return *this;
464 }
465
466// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
467
469 operator*=( const VectorType & aExpression )
470 {
471 mVector *= aExpression;
472 return *this;
473 }
474
475// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
476
478 operator%=( const Vector< T > & aVector )
479 {
480 BELFEM_ASSERT( aVector.length() == this->length(),
481 "Vector sizes do not match ( %lu and %lu ).",
482 ( long unsigned int ) this->length(),
483 ( long unsigned int ) aVector.length() );
484
485 mVector %= aVector.vector_data();
486 return *this;
487 }
488
489// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
490
491 template < typename ET >
492 Vector<T> &
493 operator*=( const ET & aExpression )
494 {
495 mVector *= aExpression;
496 return *this;
497 }
498
499//------------------------------------------------------------------------------
500// DEVIDE OPERATORS
501//------------------------------------------------------------------------------
502
504 operator/=( const T & aValue )
505 {
506 mVector /= aValue;
507 return *this;
508 }
509
510//------------------------------------------------------------------------------
511// PRINT OPERATION
512//------------------------------------------------------------------------------
513
514 void
515 print( const std::string aLabel="Vector" ) const;
516
517//------------------------------------------------------------------------------
518 };
519
520// turn off annoying waning
521#ifdef BELFEM_GCC
522#pragma GCC diagnostic push
523#pragma GCC diagnostic ignored "-Wformat"
524#endif
525
526 template < typename T > void
527 Vector< T >::print( const std::string aLabel ) const
528 {
529 FILE * tOutFile = stdout;
530
531 fprintf( tOutFile, "\n%s = [ ... \n", aLabel.c_str() );
532
533 // get a ref of this
534 const Vector< T > & tThis = *this;
535
536 uint tLength = tThis.length();
537
538 for( uint i=0; i< tLength; ++i )
539 {
540
541 fprintf( tOutFile, "%d; ",
542 ( int ) tThis( i ) );
543
544
545 if( i < tLength-1 )
546 {
547 fprintf( tOutFile, "...\n" );
548 }
549 else
550 {
551 fprintf( tOutFile, "];\n" );
552 }
553 }
554 }
555
556// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
557
558 template <>
559 inline void
560 Vector< real >::print( const std::string aLabel ) const
561 {
562 FILE * tOutFile = stdout;
563
564 fprintf( tOutFile, "%s = [ ... \n", aLabel.c_str() );
565
566 const Vector< real > & tThis = *this;
567
568 uint tLength = tThis.length();
569
570 for( uint i=0; i< tLength; ++i )
571 {
572
573 fprintf( tOutFile, "%+.15e; ", ( double ) tThis( i ) );
574
575
576 if( i < tLength-1 )
577 {
578 fprintf( tOutFile, "...\n" );
579 }
580 else
581 {
582 fprintf( tOutFile, "];\n" );
583 }
584 }
585 }
586
587#ifdef BELFEM_GCC
588#pragma GCC diagnostic pop
589#endif
590
591}
592#endif //BELFEM_CL_AR_VECTOR_HPP
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
A bounds-checked wrapper around std::vector.
Definition cl_Cell.hpp:45
size_t size() const
Definition cl_Cell.hpp:189
auto begin() -> decltype(mCell.begin())
Definition cl_Cell.hpp:215
auto end() -> decltype(mCell.end())
Definition cl_Cell.hpp:223
Column vector.
Definition cl_BZ_Vector.hpp:41
void set_size(const size_t aNumRows, const T &aValue)
Definition cl_AR_Vector.hpp:223
void print(const std::string aLabel="Vector") const
Definition cl_AR_Vector.hpp:527
Vector< T > & operator+=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:382
Vector(const std::vector< T > &aVector)
Definition cl_AR_Vector.hpp:97
void fill(const T &aValue)
write value into all entries of the vector
Definition cl_AR_Vector.hpp:204
Vector< T > & operator-=(const ET &aExpression)
Definition cl_AR_Vector.hpp:440
auto end() -> decltype(mVector.end())
Definition cl_AR_Vector.hpp:307
auto begin() const -> decltype(mVector.begin())
Definition cl_AR_Vector.hpp:313
T & operator()(const size_t aIndex)
Definition cl_AR_Vector.hpp:273
T * data()
Returns a borrowed raw buffer under the layout contract stated in cl_Vector.hpp; may be nullptr when ...
Definition cl_AR_Vector.hpp:163
Vector< T > & operator=(const T &aValue)
Definition cl_AR_Vector.hpp:329
Vector< T > & operator+=(const ET &aExpression)
Definition cl_AR_Vector.hpp:401
Vector< T > & operator*=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:469
Vector< T > & operator*=(const ET &aExpression)
Definition cl_AR_Vector.hpp:493
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_AR_Vector.hpp:215
Vector< T > & operator=(Vector< T > &&aVector) noexcept
move assignment operator
Definition cl_AR_Vector.hpp:248
Vector(const size_t aNumRows)
Definition cl_AR_Vector.hpp:55
Vector< T > & operator=(const ET &aExpression)
Definition cl_AR_Vector.hpp:362
arma::Mat< T > VectorType
Definition cl_AR_Vector.hpp:38
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:235
VectorType & vector_data()
expose the underlying matrix implementation ( writable version )
Definition cl_AR_Vector.hpp:182
Vector(const arma::subview_col< T > &aCol)
Constructor from column.
Definition cl_AR_Vector.hpp:120
const T & operator()(const size_t aIndex) const
Definition cl_AR_Vector.hpp:286
Vector< T > & operator/=(const T &aValue)
Definition cl_AR_Vector.hpp:504
Vector< T > & operator%=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:478
Vector< T > & operator-=(const T &aValue)
Definition cl_AR_Vector.hpp:412
auto end() const -> decltype(mVector.end())
Definition cl_AR_Vector.hpp:319
Vector(const size_t aNumRows, const T &aValue)
Definition cl_AR_Vector.hpp:60
Vector()=default
Vector(Vector< T > &&aVector) noexcept
move constructor
Definition cl_AR_Vector.hpp:145
const VectorType & vector_data() const
Definition cl_AR_Vector.hpp:191
Vector< T > & operator=(const Vector< T > &aVector)
copy assignment operator
Definition cl_AR_Vector.hpp:261
virtual ~Vector()=default
empty destructor
Vector< T > & operator+=(const T &aValue)
Definition cl_AR_Vector.hpp:373
Vector< T > & operator-=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:430
Vector< T > & operator*=(const T &aValue)
Definition cl_AR_Vector.hpp:451
Vector< T > & operator-=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:421
const T * data() const
Definition cl_AR_Vector.hpp:171
Vector< T > & operator*=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:460
Vector(const Cell< T > &aCell)
Definition cl_AR_Vector.hpp:89
Vector(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:105
Vector(const arma::subview_row< T > &aRow)
Constructor from row.
Definition cl_AR_Vector.hpp:129
Vector(std::initializer_list< T > aInitList)
Definition cl_AR_Vector.hpp:68
auto begin() -> decltype(mVector.begin())
Definition cl_AR_Vector.hpp:301
Vector(const arma::Op< ET, OP > &aExpression)
Definition cl_AR_Vector.hpp:112
Vector(const Vector< T > &aVector)
copy constructor
Definition cl_AR_Vector.hpp:138
Vector< T > & operator=(std::initializer_list< T > aInitList)
Definition cl_AR_Vector.hpp:338
Vector< T > & operator+=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:391
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30
auto trans(Matrix< T > &aMatrix) -> decltype(trans(aMatrix.matrix_data()))
Definition fn_trans.hpp:74