BELFEM 0.9.4
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 <type_traits>
21#include "cl_Cell.hpp"
22
23namespace belfem
24{
32 template < typename T >
33 class Vector final
34 {
35//------------------------------------------------------------------------------
36 public :
37//------------------------------------------------------------------------------
38
39 typedef arma::Mat <T> VectorType;
40
41
42//------------------------------------------------------------------------------
43 private :
44//------------------------------------------------------------------------------
45
46 // member class of underlying vector implementation
47 VectorType mVector;
48
58 template < typename ET >
59 struct IsTransposeOp { static constexpr bool value = false; };
60
61 template < typename T1 >
62 struct IsTransposeOp< arma::Op< T1, arma::op_htrans > > { static constexpr bool value = true; };
63
64 template < typename T1 >
65 struct IsTransposeOp< arma::Op< T1, arma::op_strans > > { static constexpr bool value = true; };
66
67 template < typename ET >
68 struct IsOrientedOperand
69 {
70 static constexpr bool value =
71 arma::is_eOp< ET >::value || arma::is_eGlue< ET >::value ||
72 arma::is_mtOp< ET >::value || arma::is_mtGlue< ET >::value ||
73 IsTransposeOp< ET >::value ;
74 };
75
76 template < typename ET >
77 typename std::enable_if< ! IsOrientedOperand< ET >::value, void >::type
78 assign_oriented( const ET & aExpression )
79 {
80 mVector = aExpression;
81 BELFEM_ASSERT( mVector.n_cols == 1 || mVector.n_elem == 0,
82 "Vector: a %lu x %lu result was assigned; products and raw views are not transposed",
83 ( unsigned long ) mVector.n_rows, ( unsigned long ) mVector.n_cols );
84 }
85
86 template < typename ET >
87 typename std::enable_if< IsOrientedOperand< ET >::value, void >::type
88 assign_oriented( const ET & aExpression )
89 {
90 const arma::Proxy< ET > tP( aExpression );
91 if ( tP.get_n_rows() == 1 && tP.get_n_cols() > 1 )
92 {
93 mVector = arma::strans( aExpression );
94 }
95 else
96 {
97 mVector = aExpression;
98 }
99 }
100
101 template < typename ET >
102 typename std::enable_if< ! IsOrientedOperand< ET >::value, void >::type
103 plus_oriented( const ET & aExpression ) { mVector += aExpression; }
104
105 template < typename ET >
106 typename std::enable_if< IsOrientedOperand< ET >::value, void >::type
107 plus_oriented( const ET & aExpression )
108 {
109 const arma::Proxy< ET > tP( aExpression );
110 if ( tP.get_n_rows() == 1 && tP.get_n_cols() > 1 )
111 {
112 mVector += arma::strans( aExpression );
113 }
114 else
115 {
116 mVector += aExpression;
117 }
118 }
119
120 template < typename ET >
121 typename std::enable_if< ! IsOrientedOperand< ET >::value, void >::type
122 minus_oriented( const ET & aExpression ) { mVector -= aExpression; }
123
124 template < typename ET >
125 typename std::enable_if< IsOrientedOperand< ET >::value, void >::type
126 minus_oriented( const ET & aExpression )
127 {
128 const arma::Proxy< ET > tP( aExpression );
129 if ( tP.get_n_rows() == 1 && tP.get_n_cols() > 1 )
130 {
131 mVector -= arma::strans( aExpression );
132 }
133 else
134 {
135 mVector -= aExpression;
136 }
137 }
138
139//------------------------------------------------------------------------------
140 public :
141//------------------------------------------------------------------------------
142
143 Vector() = default;
144
145// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146
147 Vector( const size_t aNumRows ) :
148 mVector( aNumRows, 1 ) {}
149
150// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151
152 Vector( const size_t aNumRows, const T & aValue ) :
153 mVector( aNumRows, 1 )
154 {
155 this->fill( aValue );
156 }
157
158// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159
160 Vector( std::initializer_list<T> aInitList )
161 {
162 if( aInitList.size() == 1 )
163 {
164 // Single element: interpret as Vector(1, value)
165 mVector.set_size( 1, 1 );
166 mVector( 0 ) = *aInitList.begin();
167 }
168 else
169 {
170 // Multiple elements: interpret as list of values
171 mVector.set_size( aInitList.size(), 1 );
172 size_t i = 0;
173 for( const T & val : aInitList )
174 {
175 mVector( i++ ) = val;
176 }
177 }
178 }
179// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180
181 Vector( const Cell< T > & aCell )
182 {
183 mVector.set_size( aCell.size(), 1 );
184 std::copy( aCell.begin(), aCell.end(), mVector.begin() );
185 }
186
187// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
188
189 Vector( const std::vector< T > & aVector )
190 {
191 mVector.set_size( aVector.size(), 1 );
192 std::copy( aVector.begin(), aVector.end(), mVector.begin() );
193 }
194
195// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
196
197 Vector( const VectorType & aExpression ) :
198 mVector( aExpression )
199 {}
200
201// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
202
206 Vector( const arma::subview_col< T > & aCol ) :
207 mVector( aCol )
208 {}
209
210// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
211
215 Vector( const arma::subview_row< T > & aRow ) :
216 mVector( arma::strans( aRow ) )
217 {}
218
219// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
220
225 template< typename ET >
226 Vector( const arma::Base< T, ET > & aExpression )
227 {
228 this->assign_oriented( aExpression.get_ref() );
229 }
230
231//------------------------------------------------------------------------------
232
236 Vector( const Vector< T > & aVector ) :
237 mVector( aVector.mVector )
238 {}
239
243 Vector( Vector< T > && aVector ) noexcept :
244 mVector( std::move( aVector.mVector ) )
245 {}
246
247//------------------------------------------------------------------------------
248
252 ~Vector() = default;
253
254//------------------------------------------------------------------------------
255// MEMORY
256//------------------------------------------------------------------------------
257
260 T *
262 {
263 return mVector.memptr();
264 }
265
266// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
267
268 const T *
269 data() const
270 {
271 return mVector.memptr();
272 }
273
274//------------------------------------------------------------------------------
275
279 VectorType &
281 {
282 return mVector;
283 }
284
285// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
286
287
288 const VectorType &
290 {
291 return mVector;
292 }
293
294//------------------------------------------------------------------------------
295// UTILITIES
296//------------------------------------------------------------------------------
297
301 void
302 fill( const T & aValue )
303 {
304 mVector.fill( aValue );
305 }
306
307//------------------------------------------------------------------------------
308
312 void
313 set_size( const size_t aNumRows )
314 {
315 mVector.set_size( aNumRows, 1 );
316 }
317
318// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
319
320 void
321 set_size( const size_t aNumRows, const T & aValue )
322 {
323 this->set_size( aNumRows );
324 this->fill( aValue );
325 }
326
327//------------------------------------------------------------------------------
328
332 size_t
333 length() const
334 {
335 return mVector.n_rows;
336 }
337
338//------------------------------------------------------------------------------
339// ACCESS OPERATORS
340//------------------------------------------------------------------------------
341
346 operator=( Vector< T > && aVector ) noexcept
347 {
348 if ( this != & aVector )
349 {
350 mVector = std::move( aVector.mVector );
351 }
352 return *this;
353 }
354
359 operator=( const Vector< T > & aVector )
360 {
361 if ( this != &aVector )
362 {
363 mVector = aVector.mVector;
364 }
365 return *this;
366 }
367
368
369//------------------------------------------------------------------------------
370 T &
371 operator()( const size_t aIndex )
372 {
373 BELFEM_ASSERT( aIndex < this->length(),
374 "Index %lu out of bounds, which must be smaller than %lu.",
375 ( long unsigned int ) aIndex,
376 ( long unsigned int ) this->length());
377
378 return mVector( aIndex );
379 }
380
381// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
382
383 const T &
384 operator()( const size_t aIndex ) const
385 {
386 BELFEM_ASSERT( aIndex < this->length(),
387 "Index %lu out of bounds, which must be smaller than %lu.",
388 ( long unsigned int ) aIndex,
389 ( long unsigned int ) this->length());
390
391 return mVector( aIndex );
392 }
393
394//------------------------------------------------------------------------------
395// ITERATORS
396//------------------------------------------------------------------------------
397
398 auto
399 begin() -> decltype( mVector.begin() )
400 {
401 return mVector.begin();
402 }
403
404 auto
405 end() -> decltype( mVector.end() )
406 {
407 return mVector.end();
408 }
409
410 auto
411 begin() const -> decltype( mVector.begin() )
412 {
413 return mVector.begin();
414 }
415
416 auto
417 end() const -> decltype( mVector.end() )
418 {
419 return mVector.end();
420 }
421
422//------------------------------------------------------------------------------
423// EQUAL OPERATORS
424//------------------------------------------------------------------------------
425
427 operator=( const T & aValue )
428 {
429 this->fill( aValue );
430 return *this;
431 }
432
433// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
434
436 operator=( std::initializer_list<T> aInitList )
437 {
438 if( aInitList.size() == 1 )
439 {
440 // Single element: interpret as set_size(1, value)
441 this->set_size( 1, *aInitList.begin() );
442 }
443 else
444 {
445 // Multiple elements: interpret as list of values
446 this->set_size( aInitList.size() );
447 size_t i = 0;
448 for( const T & val : aInitList )
449 {
450 mVector( i++ ) = val;
451 }
452 }
453 return *this;
454 }
455
456// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
457
458 template < typename ET,
459 typename = typename std::enable_if< arma::is_arma_type< ET >::value >::type >
461 operator=( const ET & aExpression )
462 {
463 this->assign_oriented( aExpression );
464 return *this;
465 }
466
467// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
468
470 operator=( const arma::subview_row< T > & aRow )
471 {
472 mVector = arma::strans( aRow );
473 return *this;
474 }
475
476//------------------------------------------------------------------------------
477// ADD OPERATORS
478//------------------------------------------------------------------------------
479
481 operator+=( const T & aValue )
482 {
483 mVector += aValue;
484 return *this;
485 }
486
487// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
488
490 operator+=( const Vector< T > & aVector )
491 {
492 mVector += aVector.vector_data();
493 return *this;
494 }
495
496// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
497
499 operator+=( const VectorType & aExpression )
500 {
501 mVector += aExpression;
502 return *this;
503 }
504
505// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
506
512 operator+=( const arma::subview_row< T > & aRow )
513 {
514 mVector += arma::strans( aRow );
515 return *this;
516 }
517
518// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
519
520 template < typename ET,
521 typename = typename std::enable_if< arma::is_arma_type< ET >::value >::type >
522 Vector<T> &
523 operator+=( const ET & aExpression )
524 {
525 this->plus_oriented( aExpression );
526 return *this;
527 }
528
529//------------------------------------------------------------------------------
530// SUBTRACT OPERATORS
531//------------------------------------------------------------------------------
532
534 operator-=( const T & aValue )
535 {
536 mVector -= aValue;
537 return *this;
538 }
539
540// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
541
543 operator-=( const Vector< T > & aVector )
544 {
545 mVector -= aVector.vector_data();
546 return *this;
547 }
548
549// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
550
552 operator-=( const VectorType & aExpression )
553 {
554 mVector -= aExpression;
555 return *this;
556 }
557
558// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
559
561 operator-=( const arma::subview_row< T > & aRow )
562 {
563 mVector -= arma::strans( aRow );
564 return *this;
565 }
566
567// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
568
569 template < typename ET,
570 typename = typename std::enable_if< arma::is_arma_type< ET >::value >::type >
571 Vector<T> &
572 operator-=( const ET & aExpression )
573 {
574 this->minus_oriented( aExpression );
575 return *this;
576 }
577
578//------------------------------------------------------------------------------
579// MULTIPLY OPERATORS
580//------------------------------------------------------------------------------
581
583 operator*=( const T & aValue )
584 {
585 mVector *= aValue;
586 return *this;
587 }
588
589// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
590
592 operator*=( const Vector< T > & aVector )
593 {
594 mVector *= aVector.vector_data();
595 return *this;
596 }
597
598// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
599
601 operator*=( const VectorType & aExpression )
602 {
603 mVector *= aExpression;
604 return *this;
605 }
606
607// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
608
610 operator%=( const Vector< T > & aVector )
611 {
612 BELFEM_ASSERT( aVector.length() == this->length(),
613 "Vector sizes do not match ( %lu and %lu ).",
614 ( long unsigned int ) this->length(),
615 ( long unsigned int ) aVector.length() );
616
617 mVector %= aVector.vector_data();
618 return *this;
619 }
620
621// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
622
623 template < typename ET >
624 Vector<T> &
625 operator*=( const ET & aExpression )
626 {
627 mVector *= aExpression;
628 return *this;
629 }
630
631//------------------------------------------------------------------------------
632// DEVIDE OPERATORS
633//------------------------------------------------------------------------------
634
636 operator/=( const T & aValue )
637 {
638 mVector /= aValue;
639 return *this;
640 }
641
642//------------------------------------------------------------------------------
643// PRINT OPERATION
644//------------------------------------------------------------------------------
645
646 void
647 print( const std::string aLabel="Vector" ) const;
648
649//------------------------------------------------------------------------------
650 };
651
652// turn off annoying waning
653#ifdef BELFEM_GCC
654#pragma GCC diagnostic push
655#pragma GCC diagnostic ignored "-Wformat"
656#endif
657
658 template < typename T > void
659 Vector< T >::print( const std::string aLabel ) const
660 {
661 FILE * tOutFile = stdout;
662
663 fprintf( tOutFile, "\n%s = [ ... \n", aLabel.c_str() );
664
665 // get a ref of this
666 const Vector< T > & tThis = *this;
667
668 uint tLength = tThis.length();
669
670 for( uint i=0; i< tLength; ++i )
671 {
672
673 fprintf( tOutFile, "%d; ",
674 ( int ) tThis( i ) );
675
676
677 if( i < tLength-1 )
678 {
679 fprintf( tOutFile, "...\n" );
680 }
681 else
682 {
683 fprintf( tOutFile, "];\n" );
684 }
685 }
686 }
687
688// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
689
690 template <>
691 inline void
692 Vector< real >::print( const std::string aLabel ) const
693 {
694 FILE * tOutFile = stdout;
695
696 fprintf( tOutFile, "%s = [ ... \n", aLabel.c_str() );
697
698 const Vector< real > & tThis = *this;
699
700 uint tLength = tThis.length();
701
702 for( uint i=0; i< tLength; ++i )
703 {
704
705 fprintf( tOutFile, "%+.15e; ", ( double ) tThis( i ) );
706
707
708 if( i < tLength-1 )
709 {
710 fprintf( tOutFile, "...\n" );
711 }
712 else
713 {
714 fprintf( tOutFile, "];\n" );
715 }
716 }
717 }
718
719#ifdef BELFEM_GCC
720#pragma GCC diagnostic pop
721#endif
722
723}
724#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:42
void set_size(const size_t aNumRows, const T &aValue)
Definition cl_AR_Vector.hpp:321
void print(const std::string aLabel="Vector") const
Definition cl_AR_Vector.hpp:659
Vector< T > & operator+=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:490
Vector(const arma::Base< T, ET > &aExpression)
constructor from any Armadillo expression; a 1 x n result is a row and is transposed into the column,...
Definition cl_AR_Vector.hpp:226
Vector(const std::vector< T > &aVector)
Definition cl_AR_Vector.hpp:189
void fill(const T &aValue)
write value into all entries of the vector
Definition cl_AR_Vector.hpp:302
auto end() -> decltype(mVector.end())
Definition cl_AR_Vector.hpp:405
auto begin() const -> decltype(mVector.begin())
Definition cl_AR_Vector.hpp:411
T & operator()(const size_t aIndex)
Definition cl_AR_Vector.hpp:371
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:261
Vector< T > & operator=(const T &aValue)
Definition cl_AR_Vector.hpp:427
Vector< T > & operator=(const ET &aExpression)
Definition cl_AR_Vector.hpp:461
Vector< T > & operator*=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:601
Vector< T > & operator*=(const ET &aExpression)
Definition cl_AR_Vector.hpp:625
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_AR_Vector.hpp:313
Vector< T > & operator=(Vector< T > &&aVector) noexcept
move assignment operator
Definition cl_AR_Vector.hpp:346
Vector(const size_t aNumRows)
Definition cl_AR_Vector.hpp:147
arma::Mat< T > VectorType
Definition cl_AR_Vector.hpp:39
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:333
VectorType & vector_data()
expose the underlying matrix implementation ( writable version )
Definition cl_AR_Vector.hpp:280
Vector(const arma::subview_col< T > &aCol)
Constructor from column.
Definition cl_AR_Vector.hpp:206
const T & operator()(const size_t aIndex) const
Definition cl_AR_Vector.hpp:384
Vector< T > & operator/=(const T &aValue)
Definition cl_AR_Vector.hpp:636
Vector< T > & operator%=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:610
Vector< T > & operator-=(const T &aValue)
Definition cl_AR_Vector.hpp:534
auto end() const -> decltype(mVector.end())
Definition cl_AR_Vector.hpp:417
Vector(const size_t aNumRows, const T &aValue)
Definition cl_AR_Vector.hpp:152
~Vector()=default
empty destructor
Vector()=default
Vector(Vector< T > &&aVector) noexcept
move constructor
Definition cl_AR_Vector.hpp:243
Vector< T > & operator+=(const arma::subview_row< T > &aRow)
a row view is transposed into the column; strans, so complex entries are not conjugated ( the same or...
Definition cl_AR_Vector.hpp:512
const VectorType & vector_data() const
Definition cl_AR_Vector.hpp:289
Vector< T > & operator=(const Vector< T > &aVector)
copy assignment operator
Definition cl_AR_Vector.hpp:359
Vector< T > & operator+=(const T &aValue)
Definition cl_AR_Vector.hpp:481
Vector< T > & operator-=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:552
Vector< T > & operator-=(const ET &aExpression)
Definition cl_AR_Vector.hpp:572
Vector< T > & operator*=(const T &aValue)
Definition cl_AR_Vector.hpp:583
Vector< T > & operator-=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:543
const T * data() const
Definition cl_AR_Vector.hpp:269
Vector< T > & operator*=(const Vector< T > &aVector)
Definition cl_AR_Vector.hpp:592
Vector< T > & operator+=(const ET &aExpression)
Definition cl_AR_Vector.hpp:523
Vector(const Cell< T > &aCell)
Definition cl_AR_Vector.hpp:181
Vector(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:197
Vector(const arma::subview_row< T > &aRow)
Constructor from row.
Definition cl_AR_Vector.hpp:215
Vector(std::initializer_list< T > aInitList)
Definition cl_AR_Vector.hpp:160
auto begin() -> decltype(mVector.begin())
Definition cl_AR_Vector.hpp:399
Vector< T > & operator-=(const arma::subview_row< T > &aRow)
Definition cl_AR_Vector.hpp:561
Vector< T > & operator=(const arma::subview_row< T > &aRow)
Definition cl_AR_Vector.hpp:470
Vector(const Vector< T > &aVector)
copy constructor
Definition cl_AR_Vector.hpp:236
Vector< T > & operator=(std::initializer_list< T > aInitList)
Definition cl_AR_Vector.hpp:436
Vector< T > & operator+=(const VectorType &aExpression)
Definition cl_AR_Vector.hpp:499
USER GUIDES:
Definition cl_Capacitor.cpp:16
std::pair< real, unit > value
Definition typedefs.hpp:73
unsigned int uint
Definition typedefs.hpp:30