BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_BZ_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_BZ_VECTOR_HPP
13#define BELFEM_CL_BZ_VECTOR_HPP
14
15#ifdef BELFEM_INTEL
16#pragma warning push
17#pragma warning disable 3058
18#endif
19
20#include <memory>
21#include <algorithm>
22#include "blaze_config.hpp"
23#include <blaze/Blaze.h>
24
25#include "typedefs.hpp"
26#include "assert.hpp"
27#include <blaze/math/Column.h>
28#include "cl_Cell.hpp"
29
30namespace belfem
31{
39 template< typename T >
40 class Vector
41 {
42//------------------------------------------------------------------------------
43 public :
44//------------------------------------------------------------------------------
45
46 typedef blaze::DynamicVector<T, blaze::columnVector> VectorType;
47
48//------------------------------------------------------------------------------
49 private :
50//------------------------------------------------------------------------------
51
52 // member class of underlying vector implementation
53 VectorType mVector;
54
55//------------------------------------------------------------------------------
56 public :
57//------------------------------------------------------------------------------
58
62 Vector() = default;
63
64// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65
69 Vector( const size_t aNumRows ) :
70 mVector( aNumRows ) {}
71
72// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73
77 Vector( const size_t aNumRows, const T & aValue ) :
78 mVector( aNumRows, aValue ) {}
79
80// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81
85 Vector( std::initializer_list<T> aInitList )
86 {
87 if( aInitList.size() == 1 )
88 {
89 // Single element: interpret as Vector(1, value)
90 mVector.resize( 1, false );
91 mVector[ 0 ] = *aInitList.begin();
92 }
93 else
94 {
95 // Multiple elements: interpret as list of values
96 mVector.resize( aInitList.size(), false );
97 size_t i = 0;
98 for( const T & val : aInitList )
99 {
100 mVector[ i++ ] = val;
101 }
102 }
103 }
104
105// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106
110 Vector( const VectorType & aExpression ) :
111 mVector( aExpression ) {}
112
113// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114
115 Vector( const Cell< T > & aCell )
116 {
117 mVector.resize( aCell.size(), false );
118 std::copy( aCell.begin(), aCell.end(), mVector.begin() );
119 }
120
121// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
122
123 Vector( const std::vector< T > & aVector )
124 {
125 mVector.resize( aVector.size(), false );
126 std::copy( aVector.begin(), aVector.end(), mVector.begin() );
127 }
128
129
130// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131
135 template< typename VT >
136 Vector( const blaze::DenseVector<VT, false> & aExpression )
137 : mVector( static_cast< const VT& >( aExpression ) ) {}
138
142 template< typename VT >
143 Vector( const blaze::DenseVector<VT, true> & aExpression )
144 {
145 const VT & tRow = static_cast< const VT& >( aExpression );
146 mVector.resize( tRow.size(), false );
147 for( size_t k = 0; k < tRow.size(); ++k )
148 {
149 mVector[ k ] = tRow[ k ];
150 }
151 }
152
153// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154
158 Vector( const blaze::Columns<blaze::DynamicMatrix<T, true>, true, true, false> & aColumn )
159 {
160 // get length of vector
161 std::size_t tLength = aColumn.rows();
162
163 // make sure that this is a column matrix
164 BELFEM_ASSERT( aColumn.columns() == 1,
165 "aColumn must be a single column, but has %lu columns.",
166 ( long unsigned int ) aColumn.columns() );
167
168 // create data
169 mVector.resize( tLength , false );
170
171 std::copy( aColumn.data(), aColumn.data() + tLength, mVector.data() );
172 }
173
174// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
175
179 Vector( const blaze::Rows<blaze::DynamicMatrix<T, true>, false, true, false> & aRow )
180 {
181 // get length of vector
182 std::size_t tLength = aRow.columns();
183
184 // make sure that this is a row matrix
185 BELFEM_ASSERT( aRow.rows() == 1,
186 "aColumn must be a single row, but has %lu rows.",
187 ( long unsigned int ) aRow.rows());
188
189 // create data
190 mVector.resize( tLength, false );
191
192 // get pointer to data
193 T * tTarget = mVector.data();
194
195 // manual copy of data
196 for ( std::size_t k = 0; k < tLength; ++k )
197 {
198 tTarget[ k ] = aRow.at( 0, k );
199 }
200 }
201
202//------------------------------------------------------------------------------
203
207 Vector( const Vector< T > & aVector ) :
208 mVector( aVector.mVector )
209 {}
210
214 Vector( Vector< T > && aVector ) noexcept :
215 mVector( std::move( aVector.mVector ) )
216 {}
217
218//------------------------------------------------------------------------------
219
223 virtual ~Vector() = default;
224
225//------------------------------------------------------------------------------
226// MEMORY
227//------------------------------------------------------------------------------
228
232 T *
234 {
235 return mVector.data();
236 }
237
238// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
239
243 const T *
244 data() const
245 {
246 return mVector.data();
247 }
248
249//------------------------------------------------------------------------------
250
254 VectorType &
256 {
257 return mVector;
258 }
259
260// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
261
265 const VectorType &
267 {
268 return mVector;
269 }
270
271//------------------------------------------------------------------------------
272// UTILITIES
273//------------------------------------------------------------------------------
274
278 void
279 fill( const T & aValue )
280 {
281 mVector = aValue;
282 }
283
284//------------------------------------------------------------------------------
285
289 void
290 set_size( const size_t aNumRows )
291 {
292 mVector.resize( aNumRows, false );
293 }
294
295// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
296
297 void
298 set_size( const size_t aNumRows, const T & aValue )
299 {
300 this->set_size( aNumRows );
301 this->fill( aValue );
302 }
303
304//------------------------------------------------------------------------------
305
309 size_t
310 length() const
311 {
312 return mVector.size();
313 }
314
315//------------------------------------------------------------------------------
316// ACCESS OPERATORS
317//------------------------------------------------------------------------------
318
323 operator=( Vector< T > && aVector ) noexcept
324 {
325 if ( this != & aVector )
326 {
327 mVector = std::move( aVector.mVector );
328 }
329 return *this;
330 }
331
336 operator=( const Vector< T > & aVector )
337 {
338 if ( this != &aVector )
339 {
340 mVector = aVector.mVector;
341 }
342 return *this;
343 }
344
345//------------------------------------------------------------------------------
346 T &
347 operator()( const size_t aIndex )
348 {
349 BELFEM_ASSERT( aIndex < this->length(),
350 "Index %lu out of bounds, which must be smaller than %lu.",
351 ( long unsigned int ) aIndex,
352 ( long unsigned int ) this->length());
353
354 return mVector[ aIndex ];
355 }
356
357// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
358
359 const T &
360 operator()( const size_t aIndex ) const
361 {
362 BELFEM_ASSERT( aIndex < this->length(),
363 "Index %lu out of bounds, which must be smaller than %lu.",
364 ( long unsigned int ) aIndex,
365 ( long unsigned int ) this->length());
366
367 return mVector[ aIndex ];
368 }
369
370//------------------------------------------------------------------------------
371// ITERATORS
372//------------------------------------------------------------------------------
373
374 T *
376 {
377 return mVector.data();
378 }
379
380 T *
382 {
383 return mVector.data() + this->length();
384 }
385
386 const T *
387 begin() const
388 {
389 return mVector.data();
390 }
391
392 auto
393 end() const
394 {
395 return mVector.data() + this->length();
396 }
397
398
399//------------------------------------------------------------------------------
400// EQUAL OPERATORS
401//------------------------------------------------------------------------------
402
403 Vector<T> &
404 operator=( const T & aValue )
405 {
406 this->fill( aValue );
407 return *this;
408 }
409
410// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
411
412 Vector<T> &
413 operator=( std::initializer_list<T> aInitList )
414 {
415 if( aInitList.size() == 1 )
416 {
417 // Single element: interpret as set_size(1, value)
418 this->set_size( 1, *aInitList.begin() );
419 }
420 else
421 {
422 // Multiple elements: interpret as list of values
423 this->set_size( aInitList.size() );
424 size_t i = 0;
425 for( const T & val : aInitList )
426 {
427 mVector[ i++ ] = val;
428 }
429 }
430 return *this;
431 }
432
433// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
434
435 template < typename ET >
436 Vector<T> &
437 operator=( const ET & aExpression )
438 {
439 mVector = aExpression;
440 return *this;
441 }
442
443//------------------------------------------------------------------------------
444// ADD OPERATORS
445//------------------------------------------------------------------------------
446
447 Vector<T> &
448 operator+=( const T & aValue )
449 {
450 std::for_each( mVector.data(), mVector.data() + this->length(),
451 [ aValue ]( T & tVal )
452 { tVal += aValue; } );
453 return *this;
454 }
455
456
457// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
458
459 //Vector<T> &
460 //operator+=( const VectorType & aExpression )
461 // {
462 // mVector += aExpression;
463 // return *this;
464 // }
465
466// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
467
468 Vector<T> &
469 operator+=( const Vector<T> & aVector )
470 {
471 mVector += aVector.vector_data();
472 return *this;
473 }
474
475// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
476
477 //template < typename ET >
478 //Vector<T> &
479 //operator+=( const ET & aExpression )
480 //{
481 // mVector += aExpression;
482 // return *this;
483 //}
484
485// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
486
487 template < typename ET >
488 Vector<T> &
489 operator+=( const blaze::DMatScalarMultExpr< ET, T, false> & aExpression )
490 {
491 mVector += blaze::column( aExpression, 0UL );
492 return *this;
493 }
494
495//------------------------------------------------------------------------------
496// SUBTRACT OPERATORS
497//------------------------------------------------------------------------------
498
499 Vector<T> &
500 operator-=( const T & aValue )
501 {
502 std::for_each( mVector.data(), mVector.data() + this->length(),
503 [ aValue ]( T & tVal )
504 { tVal -= aValue; } );
505 return *this;
506 }
507
508// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
509
510 Vector<T> &
511 operator-=( const Vector<T> & aVector )
512 {
513 mVector -= aVector.vector_data();
514 return *this;
515 }
516
517// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
518
519 //Vector<T> &
520 //operator-=( const VectorType & aExpression )
521 // {
522 // mVector -= aExpression;
523 // return *this;
524 // }
525
526// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
527
528 //template < typename ET >
529 //Vector<T> &
530 //operator-=( const ET & aExpression )
531 //{
532 // mVector -= aExpression;
533 // return *this;
534 //}
535
536// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
537
538 template < typename ET >
539 Vector<T> &
540 operator-=( const blaze::DMatScalarMultExpr< ET, T, false> & aExpression )
541 {
542 mVector -= blaze::column( aExpression, 0UL );
543 return *this;
544 }
545
546//------------------------------------------------------------------------------
547// MULTIPLY OPERATORS
548//------------------------------------------------------------------------------
549
550 Vector<T> &
551 operator*=( const T & aValue )
552 {
553 mVector *= aValue;
554 return *this;
555 }
556
557// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
558
559 Vector<T> &
560 operator*=( const Vector<T> & aVector )
561 {
562 mVector *= aVector.vector_data();
563 return *this;
564 }
565
566// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
567
568 Vector<T> &
569 operator*=( const VectorType & aExpression )
570 {
571 mVector *= aExpression;
572 return *this;
573 }
574
575// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
576
578 operator%=( const Vector< T > & aVector )
579 {
580 size_t tN = this->length();
581
582 BELFEM_ASSERT( aVector.length() == tN,
583 "Vector sizes do not match ( %lu and %lu ).",
584 ( long unsigned int ) tN,
585 ( long unsigned int ) aVector.length() );
586
587 for( size_t k=0; k<tN; ++k )
588 {
589 mVector[ k ] *= aVector( k );
590 }
591 return *this;
592 }
593
594// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
595
596 template < typename ET >
597 Vector<T> &
598 operator*=( const ET & aExpression )
599 {
600 mVector *= aExpression;
601 return *this;
602 }
603
604//------------------------------------------------------------------------------
605// DEVIDE OPERATORS
606//------------------------------------------------------------------------------
607
608 Vector<T> &
609 operator/=( const T & aValue )
610 {
611 mVector /= aValue;
612 return *this;
613 }
614
615//------------------------------------------------------------------------------
616// PRINT OPERATION
617//------------------------------------------------------------------------------
618
619 void
620 print( const std::string aLabel="Vector" ) const;
621
622//------------------------------------------------------------------------------
623 };
624
625// turn off annoying waning
626#ifdef BELFEM_GCC
627#pragma GCC diagnostic push
628#pragma GCC diagnostic ignored "-Wformat"
629#endif
630
631 template < typename T > void
632 Vector< T >::print( const std::string aLabel ) const
633 {
634 FILE * tOutFile = stdout;
635
636 fprintf( tOutFile, "\n%s = [ ... \n", aLabel.c_str() );
637
638 // get a ref of this
639 const Vector< T > & tThis = *this;
640
641 uint tLength = tThis.length();
642
643 for( uint i=0; i< tLength; ++i )
644 {
645
646 fprintf( tOutFile, "%d; ", ( int ) tThis( i ) );
647
648
649 if( i < tLength-1 )
650 {
651 fprintf( tOutFile, "...\n" );
652 }
653 else
654 {
655 fprintf( tOutFile, "];\n" );
656 }
657 }
658 }
659
660// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
661
662 template <>
663 inline void
664 Vector< real >::print( const std::string aLabel ) const
665 {
666 FILE * tOutFile = stdout;
667
668 fprintf( tOutFile, "%s = [ ... \n", aLabel.c_str() );
669
670 const Vector< real > & tThis = *this;
671
672 uint tLength = tThis.length();
673
674 for( uint i=0; i< tLength; ++i )
675 {
676
677 fprintf( tOutFile, "%+.15e; ", ( double ) tThis( i ) );
678
679
680 if( i < tLength-1 )
681 {
682 fprintf( tOutFile, "...\n" );
683 }
684 else
685 {
686 fprintf( tOutFile, "];\n" );
687 }
688 }
689 }
690
691#ifdef BELFEM_GCC
692#pragma GCC diagnostic pop
693#endif
694}
695
696#ifdef BELFEM_INTEL
697#pragma warning pop
698#endif
699
700#endif //BELFEM_CL_BZ_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_BZ_Vector.hpp:298
void print(const std::string aLabel="Vector") const
Vector< T > & operator+=(const Vector< T > &aVector)
Definition cl_BZ_Vector.hpp:469
Vector(const std::vector< T > &aVector)
Definition cl_BZ_Vector.hpp:123
void fill(const T &aValue)
write value into all entries of the vector
Definition cl_BZ_Vector.hpp:279
T & operator()(const size_t aIndex)
Definition cl_BZ_Vector.hpp:347
T * data()
expose the underlying raw pointer ( writable version )
Definition cl_BZ_Vector.hpp:233
Vector< T > & operator=(const T &aValue)
Definition cl_BZ_Vector.hpp:404
const T * begin() const
Definition cl_BZ_Vector.hpp:387
Vector< T > & operator*=(const VectorType &aExpression)
Definition cl_BZ_Vector.hpp:569
Vector(const blaze::DenseVector< VT, true > &aExpression)
Constructor from row vector expression (TF=true), transposes to column.
Definition cl_BZ_Vector.hpp:143
Vector< T > & operator*=(const ET &aExpression)
Definition cl_BZ_Vector.hpp:598
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_BZ_Vector.hpp:290
Vector< T > & operator=(Vector< T > &&aVector) noexcept
move assignment operator
Definition cl_BZ_Vector.hpp:323
Vector(const size_t aNumRows)
Constructor without fill value.
Definition cl_BZ_Vector.hpp:69
Vector< T > & operator=(const ET &aExpression)
Definition cl_BZ_Vector.hpp:437
arma::Mat< T > VectorType
Definition cl_AR_Vector.hpp:38
Vector< T > & operator+=(const blaze::DMatScalarMultExpr< ET, T, false > &aExpression)
Definition cl_BZ_Vector.hpp:489
Vector(const blaze::Columns< blaze::DynamicMatrix< T, true >, true, true, false > &aColumn)
Constructor from column.
Definition cl_BZ_Vector.hpp:158
size_t length() const
get the length of the vector
Definition cl_BZ_Vector.hpp:310
VectorType & vector_data()
expose the underlying matrix implementation ( writable version )
Definition cl_BZ_Vector.hpp:255
const T & operator()(const size_t aIndex) const
Definition cl_BZ_Vector.hpp:360
Vector< T > & operator/=(const T &aValue)
Definition cl_BZ_Vector.hpp:609
auto end() const
Definition cl_BZ_Vector.hpp:393
Vector< T > & operator%=(const Vector< T > &aVector)
Definition cl_BZ_Vector.hpp:578
T * begin()
Definition cl_BZ_Vector.hpp:375
Vector< T > & operator-=(const T &aValue)
Definition cl_BZ_Vector.hpp:500
Vector(const size_t aNumRows, const T &aValue)
Constructor with fill value.
Definition cl_BZ_Vector.hpp:77
Vector< T > & operator-=(const blaze::DMatScalarMultExpr< ET, T, false > &aExpression)
Definition cl_BZ_Vector.hpp:540
Vector()=default
empty constructor
Vector(Vector< T > &&aVector) noexcept
move constructor
Definition cl_BZ_Vector.hpp:214
const VectorType & vector_data() const
expose the underlying matrix implementation ( const version )
Definition cl_BZ_Vector.hpp:266
Vector< T > & operator=(const Vector< T > &aVector)
copy assignment operator
Definition cl_BZ_Vector.hpp:336
Vector(const blaze::DenseVector< VT, false > &aExpression)
Constructor from column vector expression (TF=false).
Definition cl_BZ_Vector.hpp:136
virtual ~Vector()=default
empty destructor
Vector< T > & operator+=(const T &aValue)
Definition cl_BZ_Vector.hpp:448
Vector(const blaze::Rows< blaze::DynamicMatrix< T, true >, false, true, false > &aRow)
Constructor from row.
Definition cl_BZ_Vector.hpp:179
Vector< T > & operator*=(const T &aValue)
Definition cl_BZ_Vector.hpp:551
Vector< T > & operator-=(const Vector< T > &aVector)
Definition cl_BZ_Vector.hpp:511
const T * data() const
expose the underlying raw pointer ( const version )
Definition cl_BZ_Vector.hpp:244
Vector< T > & operator*=(const Vector< T > &aVector)
Definition cl_BZ_Vector.hpp:560
Vector(const Cell< T > &aCell)
Definition cl_BZ_Vector.hpp:115
Vector(const VectorType &aExpression)
Constructor from expression.
Definition cl_BZ_Vector.hpp:110
Vector(std::initializer_list< T > aInitList)
Constructor with initializer list.
Definition cl_BZ_Vector.hpp:85
T * end()
Definition cl_BZ_Vector.hpp:381
Vector(const Vector< T > &aVector)
copy constructor
Definition cl_BZ_Vector.hpp:207
Vector< T > & operator=(std::initializer_list< T > aInitList)
Definition cl_BZ_Vector.hpp:413
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30