CppNoddy  0.92
Loading...
Searching...
No Matches
Utils_Fill.h
Go to the documentation of this file.
1#pragma once
2
3#include <SparseVector.h>
4#include <DenseVector.h>
6#include <BandedMatrix.h>
7#include <DenseMatrix.h>
8#include <ctime>
9
10namespace Utils_Fill {
11
12
13 /// initialise RNG
14 void time_seed() {
15 srand((unsigned) std::time(0));
16 }
17
18
19 /// Fill diagonal with unit values
20 /// \param A The dense matrix to be filled
21 template <typename _Type>
23 for (std::size_t row = 0; row < A.nrows(); ++row) {
24 A(row, row) = 1.0;
25 }
26 }
27
28 /// Fill a diagonal band of a matrix
29 /// \param A The matrix to be used
30 /// \param offset The offset of the band from the main diagonal
31 /// e.g. 0 = main diagional, -1 = first sub-diagonal
32 /// \param value The value to be written to the band elements
33 template <typename _Type>
34 void fill_band(CppNoddy::Sequential_Matrix_base<_Type>& A, const int& offset, const _Type& value) {
35 for (std::size_t row = 0; row < A.nrows(); ++row) {
36 if ((row + offset < A.ncols()) && (row + offset >= 0)) {
37 A(row, row + offset) = value;
38 }
39 }
40 }
41
42 /// Set all elements of a DENSE vector
43 /// \param X The DENSE vector to be filled
44 /// \param value The value to be placed in each element of the vector
45 template <typename _Type>
46 void fill(CppNoddy::DenseVector<_Type>& X, const _Type& value) {
47 for(std::size_t i = 0; i < X.size(); ++i) {
48 X[ i ] = value;
49 }
50 }
51
52
53 void fill_random(CppNoddy::SparseVector<double>& V, const unsigned& num_of_elts) {
54 do {
55 double index = (double) rand() /
56 ((double) RAND_MAX + (double) 1) ;
57 index *= V.size();
58 double x = (double) rand() /
59 ((double) RAND_MAX + (double) 1) ;
60 V[(unsigned) index ] = x;
61 } while(V.nelts() < num_of_elts);
62 }
63
64 void fill_random(CppNoddy::SparseVector<std::complex<double> >& V, const unsigned& num_of_elts) {
65 do {
66 double index = (double) rand() /
67 ((double) RAND_MAX + (double) 1) ;
68 index *= V.size();
69 double x = (double) rand() /
70 ((double) RAND_MAX + (double) 1) ;
71 double y = (double) rand() /
72 ((double) RAND_MAX + (double) 1) ;
73 V[(unsigned) index ] = std::complex<double>(x, y);
74 } while(V.nelts() < num_of_elts);
75 }
76
78 for(unsigned i = 0; i < V.size(); ++i) {
79 double x = (double) rand() /
80 ((double) RAND_MAX + (double) 1) ;
81 V[ i ] = x;
82 }
83 }
84
85 void fill_random(CppNoddy::DenseVector<std::complex<double> >& V) {
86 for(unsigned i = 0; i < V.size(); ++i) {
87 double x = (double) rand() /
88 ((double) RAND_MAX + (double) 1) ;
89 double y = (double) rand() /
90 ((double) RAND_MAX + (double) 1) ;
91 V[ i ] = std::complex<double>(x, y);
92 }
93 }
94
96 CppNoddy::DenseVector<double> temp(A.ncols(), 0.0);
97 for(std::size_t row = 0; row < A.nrows(); ++row) {
98 fill_random(temp);
99 A[ row ] = temp;
100 }
101 }
102
104 for(std::size_t row = 0; row < A.nrows(); ++row) {
105 for(std::size_t col = std::max((int)(row - A.noffdiag()), 0);
106 (int) col <= std::min((int)(row + A.noffdiag()), (int) A.ncols()); ++col) {
107 double x = (double) rand() / ((double) RAND_MAX + (double) 1) ;
108 A(row, col) = x;
109 }
110 }
111 }
112
113}
@ V
Definition: BVPKarman.cpp:20
A matrix class that constructs a BANDED matrix.
A matrix class that constructs a DENSE matrix as an STL Vector of DenseVectors.
Specification for a templated DenseVector class – a dense, dynamic, vector object.
A base matrix class to ensure a consistent interface between the inheriting dense/banded matrix class...
A templated SparseVector class – a sparse, variable size, vector object.
A matrix class that constructs a BANDED matrix.
Definition: BandedMatrix.h:16
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
std::size_t size() const
A pass-thru definition to get the size of the vector.
Definition: DenseVector.h:330
A base matrix class for sequential matrices.
An SparseVector class – a sparse vector object.
Definition: SparseVector.h:20
void time_seed()
initialise RNG
Definition: Utils_Fill.h:14
void fill_random(CppNoddy::SparseVector< double > &V, const unsigned &num_of_elts)
Definition: Utils_Fill.h:53
void fill(CppNoddy::DenseVector< _Type > &X, const _Type &value)
Set all elements of a DENSE vector.
Definition: Utils_Fill.h:46
void fill_identity(CppNoddy::Sequential_Matrix_base< _Type > &A)
Fill diagonal with unit values.
Definition: Utils_Fill.h:22
void fill_band(CppNoddy::Sequential_Matrix_base< _Type > &A, const int &offset, const _Type &value)
Fill a diagonal band of a matrix.
Definition: Utils_Fill.h:34

© 2012

R.E. Hewitt