BELFEM 0.9.0
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
54 Vector() = default;
55
56// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57
61 Vector( const size_t aNumRows ) :
62 mVector( aNumRows, 1 ) {}
63
64// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65
69 Vector( const size_t aNumRows, const T & aValue ) :
70 mVector( aNumRows, 1 )
71 {
72 this->fill( aValue );
73 }
74
75// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
76
80 Vector( std::initializer_list<T> aInitList )
81 {
82 if( aInitList.size() == 1 )
83 {
84 // Single element: interpret as Vector(1, value)
85 mVector.set_size( 1, 1 );
86 mVector( 0 ) = *aInitList.begin();
87 }
88 else
89 {
90 // Multiple elements: interpret as list of values
91 mVector.set_size( aInitList.size(), 1 );
92 size_t i = 0;
93 for( const T & val : aInitList )
94 {
95 mVector( i++ ) = val;
96 }
97 }
98 }
99// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100
101 Vector( const Cell< T > & aCell )
102 {
103 mVector.set_size( aCell.size(), 1 );
104 std::copy( aCell.begin(), aCell.end(), mVector.begin() );
105 }
106
107// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108
109 Vector( const std::vector< T > & aVector )
110 {
111 mVector.set_size( aVector.size(), 1 );
112 std::copy( aVector.begin(), aVector.end(), mVector.begin() );
113 }
114
115// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116
120 Vector( const VectorType & aExpression ) :
121 mVector( aExpression )
122 {}
123
124// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125
129 template < typename ET, typename OP>
130 Vector( const arma::Op<ET,OP> & aExpression )
131 : mVector( aExpression ) {}
132
133// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134
138 Vector( const arma::subview_col< T > & aCol ) :
139 mVector( aCol )
140 {}
141
142// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143
147 Vector( const arma::subview_row< T > & aRow ) :
148 mVector( arma::trans( aRow ) )
149 {}
150
151//------------------------------------------------------------------------------
152
156 Vector( const Vector< T > & aVector ) :
157 mVector( aVector.mVector )
158 {}
159
163 Vector( Vector< T > && aVector ) noexcept :
164 mVector( std::move( aVector.mVector ) )
165 {}
166
167//------------------------------------------------------------------------------
168
172 virtual ~Vector() = default;
173
174//------------------------------------------------------------------------------
175// MEMORY
176//------------------------------------------------------------------------------
177
181 T *
183 {
184 return mVector.memptr();
185 }
186
187// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
188
192 const T *
193 data() const
194 {
195 return mVector.memptr();
196 }
197
198//------------------------------------------------------------------------------
199
203 VectorType &
205 {
206 return mVector;
207 }
208
209// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
210
211
212 const VectorType &
214 {
215 return mVector;
216 }
217
218//------------------------------------------------------------------------------
219// UTILITIES
220//------------------------------------------------------------------------------
221
225 void
226 fill( const T & aValue )
227 {
228 mVector.fill( aValue );
229 }
230
231//------------------------------------------------------------------------------
232
236 void
237 set_size( const size_t aNumRows )
238 {
239 mVector.set_size( aNumRows, 1 );
240 }
241
242// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
243
244 void
245 set_size( const size_t aNumRows, const T & aValue )
246 {
247 this->set_size( aNumRows );
248 this->fill( aValue );
249 }
250
251//------------------------------------------------------------------------------
252
256 size_t
257 length() const
258 {
259 return mVector.n_rows;
260 }
261
262//------------------------------------------------------------------------------
263// ACCESS OPERATORS
264//------------------------------------------------------------------------------
265
270 operator=( Vector< T > && aVector ) noexcept
271 {
272 if ( this != & aVector )
273 {
274 mVector = std::move( aVector.mVector );
275 }
276 return *this;
277 }
278
283 operator=( const Vector< T > & aVector )
284 {
285 if ( this != &aVector )
286 {
287 mVector = aVector.mVector;
288 }
289 return *this;
290 }
291
292
293//------------------------------------------------------------------------------
294 T &
295 operator()( const size_t aIndex )
296 {
297 BELFEM_ASSERT( aIndex < this->length(),
298 "Index %lu out of bounds, which must be smaller than %lu.",
299 ( long unsigned int ) aIndex,
300 ( long unsigned int ) this->length());
301
302 return mVector( aIndex );
303 }
304
305// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
306
307 const T &
308 operator()( const size_t aIndex ) const
309 {
310 BELFEM_ASSERT( aIndex < this->length(),
311 "Index %lu out of bounds, which must be smaller than %lu.",
312 ( long unsigned int ) aIndex,
313 ( long unsigned int ) this->length());
314
315 return mVector( aIndex );
316 }
317
318//------------------------------------------------------------------------------
319// ITERATORS
320//------------------------------------------------------------------------------
321
322 auto
323 begin() -> decltype( mVector.begin() )
324 {
325 return mVector.begin();
326 }
327
328 auto
329 end() -> decltype( mVector.end() )
330 {
331 return mVector.end();
332 }
333
334 auto
335 begin() const -> decltype( mVector.begin() )
336 {
337 return mVector.begin();
338 }
339
340 auto
341 end() const -> decltype( mVector.end() )
342 {
343 return mVector.end();
344 }
345
346//------------------------------------------------------------------------------
347// EQUAL OPERATORS
348//------------------------------------------------------------------------------
349
351 operator=( const T & aValue )
352 {
353 this->fill( aValue );
354 return *this;
355 }
356
357// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
358
360 operator=( std::initializer_list<T> aInitList )
361 {
362 if( aInitList.size() == 1 )
363 {
364 // Single element: interpret as set_size(1, value)
365 this->set_size( 1, *aInitList.begin() );
366 }
367 else
368 {
369 // Multiple elements: interpret as list of values
370 this->set_size( aInitList.size() );
371 size_t i = 0;
372 for( const T & val : aInitList )
373 {
374 mVector( i++ ) = val;
375 }
376 }
377 return *this;
378 }
379
380// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
381
382 template < typename ET >
384 operator=( const ET & aExpression )
385 {
386 mVector = aExpression;
387 return *this;
388 }
389
390//------------------------------------------------------------------------------
391// ADD OPERATORS
392//------------------------------------------------------------------------------
393
395 operator+=( const T & aValue )
396 {
397 mVector += aValue;
398 return *this;
399 }
400
401// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
402
404 operator+=( const Vector< T > & aVector )
405 {
406 mVector += aVector.vector_data();
407 return *this;
408 }
409
410// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
411
413 operator+=( const VectorType & aExpression )
414 {
415 mVector += aExpression;
416 return *this;
417 }
418
419// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
420
421 template < typename ET >
422 Vector<T> &
423 operator+=( const ET & aExpression )
424 {
425 mVector += aExpression;
426 return *this;
427 }
428
429//------------------------------------------------------------------------------
430// SUBTRACT OPERATORS
431//------------------------------------------------------------------------------
432
434 operator-=( const T & aValue )
435 {
436 mVector -= aValue;
437 return *this;
438 }
439
440// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
441
443 operator-=( const Vector< T > & aVector )
444 {
445 mVector -= aVector.vector_data();
446 return *this;
447 }
448
449// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
450
452 operator-=( const VectorType & aExpression )
453 {
454 mVector -= aExpression;
455 return *this;
456 }
457
458// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
459
460 template < typename ET >
461 Vector<T> &
462 operator-=( const ET & aExpression )
463 {
464 mVector -= aExpression;
465 return *this;
466 }
467
468//------------------------------------------------------------------------------
469// MULTIPLY OPERATORS
470//------------------------------------------------------------------------------
471
473 operator*=( const T & aValue )
474 {
475 mVector *= aValue;
476 return *this;
477 }
478
479// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
480
482 operator*=( const Vector< T > & aVector )
483 {
484 mVector *= aVector.vector_data();
485 return *this;
486 }
487
488// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
489
491 operator*=( const VectorType & aExpression )
492 {
493 mVector *= aExpression;
494 return *this;
495 }
496
497// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
498
500 operator%=( const Vector< T > & aVector )
501 {
502 BELFEM_ASSERT( aVector.length() == this->length(),
503 "Vector sizes do not match ( %lu and %lu ).",
504 ( long unsigned int ) this->length(),
505 ( long unsigned int ) aVector.length() );
506
507 mVector %= aVector.vector_data();
508 return *this;
509 }
510
511// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
512
513 template < typename ET >
514 Vector<T> &
515 operator*=( const ET & aExpression )
516 {
517 mVector *= aExpression;
518 return *this;
519 }
520
521//------------------------------------------------------------------------------
522// DEVIDE OPERATORS
523//------------------------------------------------------------------------------
524
526 operator/=( const T & aValue )
527 {
528 mVector /= aValue;
529 return *this;
530 }
531
532//------------------------------------------------------------------------------
533// PRINT OPERATION
534//------------------------------------------------------------------------------
535
536 void
537 print( const std::string aLabel="Vector" ) const;
538
539//------------------------------------------------------------------------------
540 };
541
542// turn off annoying waning
543#ifdef BELFEM_GCC
544#pragma GCC diagnostic push
545#pragma GCC diagnostic ignored "-Wformat"
546#endif
547
548 template < typename T > void
549 Vector< T >::print( const std::string aLabel ) const
550 {
551 FILE * tOutFile = stdout;
552
553 fprintf( tOutFile, "\n%s = [ ... \n", aLabel.c_str() );
554
555 // get a ref of this
556 const Vector< T > & tThis = *this;
557
558 uint tLength = tThis.length();
559
560 for( uint i=0; i< tLength; ++i )
561 {
562
563 fprintf( tOutFile, "%d; ",
564 ( int ) tThis( i ) );
565
566
567 if( i < tLength-1 )
568 {
569 fprintf( tOutFile, "...\n" );
570 }
571 else
572 {
573 fprintf( tOutFile, "];\n" );
574 }
575 }
576 }
577
578// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
579
580 template <>
581 inline void
582 Vector< real >::print( const std::string aLabel ) const
583 {
584 FILE * tOutFile = stdout;
585
586 fprintf( tOutFile, "%s = [ ... \n", aLabel.c_str() );
587
588 const Vector< real > & tThis = *this;
589
590 uint tLength = tThis.length();
591
592 for( uint i=0; i< tLength; ++i )
593 {
594
595 fprintf( tOutFile, "%+.15e; ", ( double ) tThis( i ) );
596
597
598 if( i < tLength-1 )
599 {
600 fprintf( tOutFile, "...\n" );
601 }
602 else
603 {
604 fprintf( tOutFile, "];\n" );
605 }
606 }
607 }
608
609#ifdef BELFEM_GCC
610#pragma GCC diagnostic pop
611#endif
612
613}
614#endif //BELFEM_CL_AR_VECTOR_HPP
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
Cell is a wrapper around the standard vector.
Definition cl_Cell.hpp:42
size_t size() const
return the size of the Cell
Definition cl_Cell.hpp:181
auto begin() -> decltype(mCell.begin())
Definition cl_Cell.hpp:205
auto end() -> decltype(mCell.end())
Definition cl_Cell.hpp:213
Column vector.
Definition cl_BZ_Vector.hpp:41
void set_size(const size_t aNumRows, const T &aValue)
Definition cl_AR_Vector.hpp:245
void print(const std::string aLabel="Vector") const
Definition cl_AR_Vector.hpp:549
Vector< T > & operator+=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:404
Vector(const std::vector< T > &aVector)
Definition cl_AR_Vector.hpp:109
void fill(const T &aValue)
write value into all entries of the vector
Definition cl_AR_Vector.hpp:226
Vector< T > & operator-=(const ET &aExpression)
Definition cl_AR_Vector.hpp:462
auto end() -> decltype(mVector.end())
Definition cl_AR_Vector.hpp:329
auto begin() const -> decltype(mVector.begin())
Definition cl_AR_Vector.hpp:335
T & operator()(const size_t aIndex)
Definition cl_AR_Vector.hpp:295
T * data()
expose the underlying raw pointer ( writable version )
Definition cl_AR_Vector.hpp:182
Vector< T > & operator=(const T &aValue)
Definition cl_AR_Vector.hpp:351
Vector< T > & operator+=(const ET &aExpression)
Definition cl_AR_Vector.hpp:423
Vector< T > & operator*=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:491
Vector< T > & operator*=(const ET &aExpression)
Definition cl_AR_Vector.hpp:515
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_AR_Vector.hpp:237
Vector< T > & operator=(Vector< T > &&aVector) noexcept
move assignment operator
Definition cl_AR_Vector.hpp:270
Vector(const size_t aNumRows)
Constructor without fill value.
Definition cl_AR_Vector.hpp:61
Vector< T > & operator=(const ET &aExpression)
Definition cl_AR_Vector.hpp:384
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:257
VectorType & vector_data()
expose the underlying matrix implementation ( writable version )
Definition cl_AR_Vector.hpp:204
Vector(const arma::subview_col< T > &aCol)
Constructor from column.
Definition cl_AR_Vector.hpp:138
const T & operator()(const size_t aIndex) const
Definition cl_AR_Vector.hpp:308
Vector< T > & operator/=(const T &aValue)
Definition cl_AR_Vector.hpp:526
Vector< T > & operator%=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:500
Vector< T > & operator-=(const T &aValue)
Definition cl_AR_Vector.hpp:434
auto end() const -> decltype(mVector.end())
Definition cl_AR_Vector.hpp:341
Vector(const size_t aNumRows, const T &aValue)
Constructor with fill value.
Definition cl_AR_Vector.hpp:69
Vector()=default
empty constructor
Vector(Vector< T > &&aVector) noexcept
move constructor
Definition cl_AR_Vector.hpp:163
const VectorType & vector_data() const
Definition cl_AR_Vector.hpp:213
Vector< T > & operator=(const Vector< T > &aVector)
copy assignment operator
Definition cl_AR_Vector.hpp:283
virtual ~Vector()=default
empty destructor
Vector< T > & operator+=(const T &aValue)
Definition cl_AR_Vector.hpp:395
Vector< T > & operator-=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:452
Vector< T > & operator*=(const T &aValue)
Definition cl_AR_Vector.hpp:473
Vector< T > & operator-=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:443
const T * data() const
expose the underlying raw pointer ( const version )
Definition cl_AR_Vector.hpp:193
Vector< T > & operator*=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:482
Vector(const Cell< T > &aCell)
Definition cl_AR_Vector.hpp:101
Vector(const VectorType &aExpression)
Constructor from expression.
Definition cl_AR_Vector.hpp:120
Vector(const arma::subview_row< T > &aRow)
Constructor from row.
Definition cl_AR_Vector.hpp:147
Vector(std::initializer_list< T > aInitList)
Constructor with initializer list.
Definition cl_AR_Vector.hpp:80
auto begin() -> decltype(mVector.begin())
Definition cl_AR_Vector.hpp:323
Vector(const arma::Op< ET, OP > &aExpression)
Constructor from expression.
Definition cl_AR_Vector.hpp:130
Vector(const Vector< T > &aVector)
copy constructor
Definition cl_AR_Vector.hpp:156
Vector< T > & operator=(std::initializer_list< T > aInitList)
Definition cl_AR_Vector.hpp:360
Vector< T > & operator+=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:413
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