BELFEM 0.9.3
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
59 Vector() = default;
60
61// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62
63 Vector( const size_t aNumRows ) :
64 mVector( aNumRows ) {}
65
66// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
67
68 Vector( const size_t aNumRows, const T & aValue ) :
69 mVector( aNumRows, aValue ) {}
70
71// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72
73 Vector( std::initializer_list<T> aInitList )
74 {
75 if( aInitList.size() == 1 )
76 {
77 // Single element: interpret as Vector(1, value)
78 mVector.resize( 1, false );
79 mVector[ 0 ] = *aInitList.begin();
80 }
81 else
82 {
83 // Multiple elements: interpret as list of values
84 mVector.resize( aInitList.size(), false );
85 size_t i = 0;
86 for( const T & val : aInitList )
87 {
88 mVector[ i++ ] = val;
89 }
90 }
91 }
92
93// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94
95 Vector( const VectorType & aExpression ) :
96 mVector( aExpression ) {}
97
98// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99
100 Vector( const Cell< T > & aCell )
101 {
102 mVector.resize( aCell.size(), false );
103 std::copy( aCell.begin(), aCell.end(), mVector.begin() );
104 }
105
106// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107
108 Vector( const std::vector< T > & aVector )
109 {
110 mVector.resize( aVector.size(), false );
111 std::copy( aVector.begin(), aVector.end(), mVector.begin() );
112 }
113
114
115// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116
120 template< typename VT >
121 Vector( const blaze::DenseVector<VT, false> & aExpression )
122 : mVector( static_cast< const VT& >( aExpression ) ) {}
123
127 template< typename VT >
128 Vector( const blaze::DenseVector<VT, true> & aExpression )
129 {
130 const VT & tRow = static_cast< const VT& >( aExpression );
131 mVector.resize( tRow.size(), false );
132 for( size_t k = 0; k < tRow.size(); ++k )
133 {
134 mVector[ k ] = tRow[ k ];
135 }
136 }
137
138// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139
143 Vector( const blaze::Columns<blaze::DynamicMatrix<T, true>, true, true, false> & aColumn )
144 {
145 // get length of vector
146 std::size_t tLength = aColumn.rows();
147
148 // make sure that this is a column matrix
149 BELFEM_ASSERT( aColumn.columns() == 1,
150 "aColumn must be a single column, but has %lu columns.",
151 ( long unsigned int ) aColumn.columns() );
152
153 // create data
154 mVector.resize( tLength , false );
155
156 std::copy( aColumn.data(), aColumn.data() + tLength, mVector.data() );
157 }
158
159// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160
164 Vector( const blaze::Rows<blaze::DynamicMatrix<T, true>, false, true, false> & aRow )
165 {
166 // get length of vector
167 std::size_t tLength = aRow.columns();
168
169 // make sure that this is a row matrix
170 BELFEM_ASSERT( aRow.rows() == 1,
171 "aColumn must be a single row, but has %lu rows.",
172 ( long unsigned int ) aRow.rows());
173
174 // create data
175 mVector.resize( tLength, false );
176
177 // get pointer to data
178 T * tTarget = mVector.data();
179
180 // manual copy of data
181 for ( std::size_t k = 0; k < tLength; ++k )
182 {
183 tTarget[ k ] = aRow.at( 0, k );
184 }
185 }
186
187//------------------------------------------------------------------------------
188
192 Vector( const Vector< T > & aVector ) :
193 mVector( aVector.mVector )
194 {}
195
199 Vector( Vector< T > && aVector ) noexcept :
200 mVector( std::move( aVector.mVector ) )
201 {}
202
203//------------------------------------------------------------------------------
204
208 virtual ~Vector() = default;
209
210//------------------------------------------------------------------------------
211// MEMORY
212//------------------------------------------------------------------------------
213
216 T *
218 {
219 return mVector.data();
220 }
221
222// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
223
224 const T *
225 data() const
226 {
227 return mVector.data();
228 }
229
230//------------------------------------------------------------------------------
231
235 VectorType &
237 {
238 return mVector;
239 }
240
241// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
242
246 const VectorType &
248 {
249 return mVector;
250 }
251
252//------------------------------------------------------------------------------
253// UTILITIES
254//------------------------------------------------------------------------------
255
259 void
260 fill( const T & aValue )
261 {
262 mVector = aValue;
263 }
264
265//------------------------------------------------------------------------------
266
270 void
271 set_size( const size_t aNumRows )
272 {
273 mVector.resize( aNumRows, false );
274 }
275
276// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
277
278 void
279 set_size( const size_t aNumRows, const T & aValue )
280 {
281 this->set_size( aNumRows );
282 this->fill( aValue );
283 }
284
285//------------------------------------------------------------------------------
286
290 size_t
291 length() const
292 {
293 return mVector.size();
294 }
295
296//------------------------------------------------------------------------------
297// ACCESS OPERATORS
298//------------------------------------------------------------------------------
299
304 operator=( Vector< T > && aVector ) noexcept
305 {
306 if ( this != & aVector )
307 {
308 mVector = std::move( aVector.mVector );
309 }
310 return *this;
311 }
312
317 operator=( const Vector< T > & aVector )
318 {
319 if ( this != &aVector )
320 {
321 mVector = aVector.mVector;
322 }
323 return *this;
324 }
325
326//------------------------------------------------------------------------------
327 T &
328 operator()( const size_t aIndex )
329 {
330 BELFEM_ASSERT( aIndex < this->length(),
331 "Index %lu out of bounds, which must be smaller than %lu.",
332 ( long unsigned int ) aIndex,
333 ( long unsigned int ) this->length());
334
335 return mVector[ aIndex ];
336 }
337
338// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
339
340 const T &
341 operator()( const size_t aIndex ) const
342 {
343 BELFEM_ASSERT( aIndex < this->length(),
344 "Index %lu out of bounds, which must be smaller than %lu.",
345 ( long unsigned int ) aIndex,
346 ( long unsigned int ) this->length());
347
348 return mVector[ aIndex ];
349 }
350
351//------------------------------------------------------------------------------
352// ITERATORS
353//------------------------------------------------------------------------------
354
355 T *
357 {
358 return mVector.data();
359 }
360
361 T *
363 {
364 return mVector.data() + this->length();
365 }
366
367 const T *
368 begin() const
369 {
370 return mVector.data();
371 }
372
373 auto
374 end() const
375 {
376 return mVector.data() + this->length();
377 }
378
379
380//------------------------------------------------------------------------------
381// EQUAL OPERATORS
382//------------------------------------------------------------------------------
383
384 Vector<T> &
385 operator=( const T & aValue )
386 {
387 this->fill( aValue );
388 return *this;
389 }
390
391// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
392
393 Vector<T> &
394 operator=( std::initializer_list<T> aInitList )
395 {
396 if( aInitList.size() == 1 )
397 {
398 // Single element: interpret as set_size(1, value)
399 this->set_size( 1, *aInitList.begin() );
400 }
401 else
402 {
403 // Multiple elements: interpret as list of values
404 this->set_size( aInitList.size() );
405 size_t i = 0;
406 for( const T & val : aInitList )
407 {
408 mVector[ i++ ] = val;
409 }
410 }
411 return *this;
412 }
413
414// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
415
416 template < typename ET >
417 Vector<T> &
418 operator=( const ET & aExpression )
419 {
420 mVector = aExpression;
421 return *this;
422 }
423
424//------------------------------------------------------------------------------
425// ADD OPERATORS
426//------------------------------------------------------------------------------
427
428 Vector<T> &
429 operator+=( const T & aValue )
430 {
431 std::for_each( mVector.data(), mVector.data() + this->length(),
432 [ aValue ]( T & tVal )
433 { tVal += aValue; } );
434 return *this;
435 }
436
437
438// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
439
440 //Vector<T> &
441 //operator+=( const VectorType & aExpression )
442 // {
443 // mVector += aExpression;
444 // return *this;
445 // }
446
447// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
448
449 Vector<T> &
450 operator+=( const Vector<T> & aVector )
451 {
452 mVector += aVector.vector_data();
453 return *this;
454 }
455
456// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
457
458 //template < typename ET >
459 //Vector<T> &
460 //operator+=( const ET & aExpression )
461 //{
462 // mVector += aExpression;
463 // return *this;
464 //}
465
466// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
467
468 template < typename ET >
469 Vector<T> &
470 operator+=( const blaze::DMatScalarMultExpr< ET, T, false> & aExpression )
471 {
472 mVector += blaze::column( aExpression, 0UL );
473 return *this;
474 }
475
476//------------------------------------------------------------------------------
477// SUBTRACT OPERATORS
478//------------------------------------------------------------------------------
479
480 Vector<T> &
481 operator-=( const T & aValue )
482 {
483 std::for_each( mVector.data(), mVector.data() + this->length(),
484 [ aValue ]( T & tVal )
485 { tVal -= aValue; } );
486 return *this;
487 }
488
489// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
490
491 Vector<T> &
492 operator-=( const Vector<T> & aVector )
493 {
494 mVector -= aVector.vector_data();
495 return *this;
496 }
497
498// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
499
500 //Vector<T> &
501 //operator-=( const VectorType & aExpression )
502 // {
503 // mVector -= aExpression;
504 // return *this;
505 // }
506
507// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
508
509 //template < typename ET >
510 //Vector<T> &
511 //operator-=( const ET & aExpression )
512 //{
513 // mVector -= aExpression;
514 // return *this;
515 //}
516
517// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
518
519 template < typename ET >
520 Vector<T> &
521 operator-=( const blaze::DMatScalarMultExpr< ET, T, false> & aExpression )
522 {
523 mVector -= blaze::column( aExpression, 0UL );
524 return *this;
525 }
526
527//------------------------------------------------------------------------------
528// MULTIPLY OPERATORS
529//------------------------------------------------------------------------------
530
531 Vector<T> &
532 operator*=( const T & aValue )
533 {
534 mVector *= aValue;
535 return *this;
536 }
537
538// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
539
540 Vector<T> &
541 operator*=( const Vector<T> & aVector )
542 {
543 mVector *= aVector.vector_data();
544 return *this;
545 }
546
547// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
548
549 Vector<T> &
550 operator*=( const VectorType & aExpression )
551 {
552 mVector *= aExpression;
553 return *this;
554 }
555
556// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
557
559 operator%=( const Vector< T > & aVector )
560 {
561 size_t tN = this->length();
562
563 BELFEM_ASSERT( aVector.length() == tN,
564 "Vector sizes do not match ( %lu and %lu ).",
565 ( long unsigned int ) tN,
566 ( long unsigned int ) aVector.length() );
567
568 for( size_t k=0; k<tN; ++k )
569 {
570 mVector[ k ] *= aVector( k );
571 }
572 return *this;
573 }
574
575// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
576
577 template < typename ET >
578 Vector<T> &
579 operator*=( const ET & aExpression )
580 {
581 mVector *= aExpression;
582 return *this;
583 }
584
585//------------------------------------------------------------------------------
586// DEVIDE OPERATORS
587//------------------------------------------------------------------------------
588
589 Vector<T> &
590 operator/=( const T & aValue )
591 {
592 mVector /= aValue;
593 return *this;
594 }
595
596//------------------------------------------------------------------------------
597// PRINT OPERATION
598//------------------------------------------------------------------------------
599
600 void
601 print( const std::string aLabel="Vector" ) const;
602
603//------------------------------------------------------------------------------
604 };
605
606// turn off annoying waning
607#ifdef BELFEM_GCC
608#pragma GCC diagnostic push
609#pragma GCC diagnostic ignored "-Wformat"
610#endif
611
612 template < typename T > void
613 Vector< T >::print( const std::string aLabel ) const
614 {
615 FILE * tOutFile = stdout;
616
617 fprintf( tOutFile, "\n%s = [ ... \n", aLabel.c_str() );
618
619 // get a ref of this
620 const Vector< T > & tThis = *this;
621
622 uint tLength = tThis.length();
623
624 for( uint i=0; i< tLength; ++i )
625 {
626
627 fprintf( tOutFile, "%d; ", ( int ) tThis( i ) );
628
629
630 if( i < tLength-1 )
631 {
632 fprintf( tOutFile, "...\n" );
633 }
634 else
635 {
636 fprintf( tOutFile, "];\n" );
637 }
638 }
639 }
640
641// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
642
643 template <>
644 inline void
645 Vector< real >::print( const std::string aLabel ) const
646 {
647 FILE * tOutFile = stdout;
648
649 fprintf( tOutFile, "%s = [ ... \n", aLabel.c_str() );
650
651 const Vector< real > & tThis = *this;
652
653 uint tLength = tThis.length();
654
655 for( uint i=0; i< tLength; ++i )
656 {
657
658 fprintf( tOutFile, "%+.15e; ", ( double ) tThis( i ) );
659
660
661 if( i < tLength-1 )
662 {
663 fprintf( tOutFile, "...\n" );
664 }
665 else
666 {
667 fprintf( tOutFile, "];\n" );
668 }
669 }
670 }
671
672#ifdef BELFEM_GCC
673#pragma GCC diagnostic pop
674#endif
675}
676
677#ifdef BELFEM_INTEL
678#pragma warning pop
679#endif
680
681#endif //BELFEM_CL_BZ_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_BZ_Vector.hpp:279
void print(const std::string aLabel="Vector") const
Vector< T > & operator+=(const Vector< T > &aVector)
Definition cl_BZ_Vector.hpp:450
Vector(const std::vector< T > &aVector)
Definition cl_BZ_Vector.hpp:108
void fill(const T &aValue)
write value into all entries of the vector
Definition cl_BZ_Vector.hpp:260
T & operator()(const size_t aIndex)
Definition cl_BZ_Vector.hpp:328
T * data()
Returns a borrowed raw buffer under the layout contract stated in cl_Vector.hpp; may be nullptr when ...
Definition cl_BZ_Vector.hpp:217
Vector< T > & operator=(const T &aValue)
Definition cl_BZ_Vector.hpp:385
const T * begin() const
Definition cl_BZ_Vector.hpp:368
Vector< T > & operator*=(const VectorType &aExpression)
Definition cl_BZ_Vector.hpp:550
Vector(const blaze::DenseVector< VT, true > &aExpression)
Constructor from row vector expression (TF=true), transposes to column.
Definition cl_BZ_Vector.hpp:128
Vector< T > & operator*=(const ET &aExpression)
Definition cl_BZ_Vector.hpp:579
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_BZ_Vector.hpp:271
Vector< T > & operator=(Vector< T > &&aVector) noexcept
move assignment operator
Definition cl_BZ_Vector.hpp:304
Vector(const size_t aNumRows)
Definition cl_BZ_Vector.hpp:63
Vector< T > & operator=(const ET &aExpression)
Definition cl_BZ_Vector.hpp:418
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:470
Vector(const blaze::Columns< blaze::DynamicMatrix< T, true >, true, true, false > &aColumn)
Constructor from column.
Definition cl_BZ_Vector.hpp:143
size_t length() const
get the length of the vector
Definition cl_BZ_Vector.hpp:291
VectorType & vector_data()
expose the underlying matrix implementation ( writable version )
Definition cl_BZ_Vector.hpp:236
const T & operator()(const size_t aIndex) const
Definition cl_BZ_Vector.hpp:341
Vector< T > & operator/=(const T &aValue)
Definition cl_BZ_Vector.hpp:590
auto end() const
Definition cl_BZ_Vector.hpp:374
Vector< T > & operator%=(const Vector< T > &aVector)
Definition cl_BZ_Vector.hpp:559
T * begin()
Definition cl_BZ_Vector.hpp:356
Vector< T > & operator-=(const T &aValue)
Definition cl_BZ_Vector.hpp:481
Vector(const size_t aNumRows, const T &aValue)
Definition cl_BZ_Vector.hpp:68
Vector< T > & operator-=(const blaze::DMatScalarMultExpr< ET, T, false > &aExpression)
Definition cl_BZ_Vector.hpp:521
Vector()=default
Vector(Vector< T > &&aVector) noexcept
move constructor
Definition cl_BZ_Vector.hpp:199
const VectorType & vector_data() const
expose the underlying matrix implementation ( const version )
Definition cl_BZ_Vector.hpp:247
Vector< T > & operator=(const Vector< T > &aVector)
copy assignment operator
Definition cl_BZ_Vector.hpp:317
Vector(const blaze::DenseVector< VT, false > &aExpression)
Constructor from column vector expression (TF=false).
Definition cl_BZ_Vector.hpp:121
virtual ~Vector()=default
empty destructor
Vector< T > & operator+=(const T &aValue)
Definition cl_BZ_Vector.hpp:429
Vector(const blaze::Rows< blaze::DynamicMatrix< T, true >, false, true, false > &aRow)
Constructor from row.
Definition cl_BZ_Vector.hpp:164
Vector< T > & operator*=(const T &aValue)
Definition cl_BZ_Vector.hpp:532
Vector< T > & operator-=(const Vector< T > &aVector)
Definition cl_BZ_Vector.hpp:492
const T * data() const
Definition cl_BZ_Vector.hpp:225
Vector< T > & operator*=(const Vector< T > &aVector)
Definition cl_BZ_Vector.hpp:541
Vector(const Cell< T > &aCell)
Definition cl_BZ_Vector.hpp:100
Vector(const VectorType &aExpression)
Definition cl_BZ_Vector.hpp:95
Vector(std::initializer_list< T > aInitList)
Definition cl_BZ_Vector.hpp:73
T * end()
Definition cl_BZ_Vector.hpp:362
Vector(const Vector< T > &aVector)
copy constructor
Definition cl_BZ_Vector.hpp:192
Vector< T > & operator=(std::initializer_list< T > aInitList)
Definition cl_BZ_Vector.hpp:394
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30