SPI
SLEPc PETSc Interface is used for rapid development and intuitive matrix operations similar to MatLab or NumPy style of operations
SPIVec.hpp
Go to the documentation of this file.
1 #ifndef SPIVEC_H
2 #define SPIVEC_H
3 #include <iostream>
4 #include <petscksp.h>
5 #include <string>
6 #include <fstream>
7 #include <vector>
8 #include <petscviewerhdf5.h>
9 #include <math.h>
10 #include "SPIprint.hpp"
11 
12 namespace SPI{
13  struct SPIVec{
14  PetscInt rows=0;
15 
16  // Constructors
17  SPIVec( std::string _name="SPIVec" );// constructor with no arguments (no initialization)
18  SPIVec( const SPIVec& A, std::string _name="SPIVec"); // constructor using another SPIVec
19  SPIVec( const PetscInt _rows, const std::string _name="SPIVec" ); // constructor with one arguement to make vector of length rows
20 
21  Vec vec;
22  PetscErrorCode ierr;
23 
24  // flags
25  PetscBool flag_init=PETSC_FALSE;
26  std::string name;
27 
28  PetscInt Init( const PetscInt _rows, const std::string name="SPIVec"); // initialize the vector of size _rows
29  PetscInt set(const PetscInt _row, const PetscScalar v); // set a scalar value at position row
30  PetscInt set(const PetscScalar v); // set a scalar value at all positions
31  PetscInt add(PetscInt _row, const PetscScalar v); // add a scalar value at position row
32  // get info operators
33  PetscInt size(); // get the size of the vector using VecGetSize
34  // () operators
35  PetscScalar operator()(PetscInt _row, PetscBool global=PETSC_FALSE); // get value at row
36  PetscScalar operator()(PetscInt _row, PetscBool global=PETSC_FALSE) const; // get value at row
37  PetscInt operator()(PetscInt _row, const PetscScalar v); // set operator the same as set function
38  PetscInt operator()(PetscInt _row, const double v); // set operator the same as set function
39  PetscInt operator()(PetscInt _row, const int v); // set operator the same as set function
40  SPIVec& operator()(); // assemble the vector
41  // +- operators
42  SPIVec& operator+=(const SPIVec &X); // VecAXPY, Y = 1.*X + Y operation
43  SPIVec& axpy(const PetscScalar a, const SPIVec &X); // VecAXPY function call to add a*X to the current vec
44  SPIVec operator+(const SPIVec &X); // Y + X operation
45  SPIVec operator+(const PetscScalar a); // Y + a operation
46  SPIVec operator+(const double a); // Y + a operation
47  SPIVec operator-(const PetscScalar a); // Y - a operation
48  SPIVec operator-(const PetscInt a); // Y - a operation
49  SPIVec& operator-=(const SPIVec &X); // Y = -1.*X + Y operation
50  SPIVec operator-(const SPIVec &X); // Y - X operation
51  SPIVec operator-() const; // -X operation
52  // * operators
53  SPIVec operator*(const PetscScalar a); // Y*a operation
54  SPIVec operator*(const double a); // Y*a operation
55  SPIVec& operator*=(const PetscScalar a); // Y = Y*a operation
56  SPIVec& operator*=(const double a); // Y = Y*a operation
57  SPIVec& operator*=(const SPIVec &a); // Y = Y*a operation
58  SPIVec operator*(const SPIVec &X); // Y*X operation
59  // / operators
60  SPIVec operator/(const PetscScalar a); // Y/a operation
61  SPIVec operator/(const double a); // Y*a operation
62  SPIVec operator/(const SPIVec X); // Y/X operation
63  SPIVec& operator/=(const PetscScalar a); // Y = Y/a operation
64  // ^ operators
65  SPIVec operator^(const PetscScalar p); // Y^p operation
66  SPIVec operator^(const double p); // Y^p operation
67  SPIVec operator^(const int p); // Y^p operation
68  SPIVec operator^(SPIVec p); // elementwise Y^p operation
69  // = operator
70  SPIVec& operator=(const SPIVec &X); // Y=X with initialization of Y
71  // == vecequal operator
72  PetscBool operator==(const SPIVec &x2); // check if this==x2
73  // overload % for element wise multiplication
74  //SPIVec operator%(SPIVec A);
75  // Transpose functions
76  // conjugate
77  SPIVec& conj(); // elemenwise conjugate current vector
78  PetscScalar max(); // return maximum value of vector
79  PetscInt argmax(); // return maximum value of vector
80  SPIVec& real(); // real part of current vector
81  SPIVec& imag(); // real part of current vector
82  PetscScalar dot(SPIVec y); // take inner dot product (this,y) or y^H this, where H is the complex conjugate transpose
83  PetscInt print(); // print vec to screen using PETSC_VIEWER_STDOUT_WORLD
84 
85  ~SPIVec(); // destructor to delete memory
86 
87  };
88  SPIVec operator/(const PetscScalar a, const SPIVec &A); // a/A operation
89  SPIVec operator*(const PetscScalar a, const SPIVec &A); // a*A operation to be equivalent to A*a
90  SPIVec operator+(const PetscScalar a, const SPIVec &A); // a+A operation to be equivalent to A+a
91  SPIVec operator-(const PetscScalar a, const SPIVec &A); // a-A operation to be equivalent to A-a
92  PetscInt save(const SPIVec &A, std::string filename); // save A to hdf5 to filename as variable A.name
93  PetscInt save(std::vector<PetscScalar> A, std::string variablename, std::string filename); // save A to hdf5 to filename as variable
94  PetscInt save(std::vector<SPIVec> A, std::string variablename, std::string filename); // save A vectors to hdf5 to filename as variable indexed 0 to A.size()-1
95  PetscInt load( SPIVec &A, const std::string filename); // load A from hdf5 filename using variable A.name, be sure it has the right size first before loading
96  SPIVec ones(const PetscInt rows); // return a vector of size rows full of ones
97  SPIVec zeros(const PetscInt rows); // return a vector of size rows full of zeros
98  SPIVec conj(const SPIVec &A); // return the conjugate vector
99  SPIVec real(const SPIVec &A); // return real part of vector
100  SPIVec imag(const SPIVec &A); // return imaginary part of vector
101  SPIVec linspace(const PetscScalar begin, const PetscScalar end, const PetscInt rows); // return linspace of number of rows equally spaced points between begin and end
102  SPIVec arange(const PetscScalar begin, const PetscScalar end, const PetscScalar stepsize=1); // return a range of number of rows equally spaced points between begin and end of step stepsize
103  SPIVec arange(const PetscScalar end); // return a range of number of rows equally spaced by 1 points between 0 and end of step 1
104  template <class T>
105  SPIVec _Function_on_each_element(T (*f)(T const&), const SPIVec &A); // take the function of element in vector
106  SPIVec sin(const SPIVec &A); // take the sin of element
107  SPIVec cos(const SPIVec &A); // take the cos of element
108  SPIVec tan(const SPIVec &A); // take the tan of element
109  SPIVec exp(const SPIVec &A); // take the exp of element
110  SPIVec log(const SPIVec &A); // take the log (natural log) of element
111  SPIVec log10(const SPIVec &A); // take the log10 of element
112  SPIVec sinh(const SPIVec &A); // take the sinh of element
113  SPIVec cosh(const SPIVec &A); // take the cosh of element
114  SPIVec tanh(const SPIVec &A); // take the tanh of element
115  SPIVec asin(const SPIVec &A); // take the asin of element
116  SPIVec acos(const SPIVec &A); // take the acos of element
117  SPIVec atan(const SPIVec &A); // take the atan of element
118  SPIVec asinh(const SPIVec &A); // take the asinh of element
119  SPIVec acosh(const SPIVec &A); // take the acosh of element
120  SPIVec atanh(const SPIVec &A); // take the atanh of element
121  SPIVec sqrt(const SPIVec &A); // take the atanh of element
122  SPIVec erf(const SPIVec &A); // take the erf of element
123  SPIVec erfc(const SPIVec &A); // take the erf of element
124  // template for scalar function on each element
125  template <class T>
126  SPIVec _Function_on_each_element(T (*f)(T const&,T const&), const SPIVec &A, SPIVec &B); // take the function of elements in vectors e.g. (*f)(A(i),B(i))
127  SPIVec pow(const SPIVec &A, SPIVec &B); // take the pow(A(i),B(i)) of element
128  SPIVec pow(const SPIVec &A, PetscScalar b); // take the pow(A(i),b) of element
129  SPIVec abs(const SPIVec &A); // take absolute value of vector
130  PetscScalar sum(SPIVec x); // sum of vector
131  PetscScalar integrate_coeffs(const SPIVec &a); // integrate a vector of Chebyshev Coefficients on grid from -1 to 1
132  PetscScalar integrate_coeffs(const SPIVec &a, const SPIVec &y); // integrate a vector of Chebyshev Coefficients on stretched grid y
133  PetscScalar dot(SPIVec x, SPIVec y); // inner dot product of the two vectors (i.e. y^H x)
134  PetscReal L2(SPIVec x1, const SPIVec x2, NormType type=NORM_2);
135  PetscReal L2(const SPIVec x1, NormType type=NORM_2);
136  SPIVec diff(SPIVec x1); // diff of vector (will be size x1.rows-1)
137  PetscScalar trapz(const SPIVec y); // trapezoidal integration of y with unity spacing \int y dx
138  PetscScalar trapz(const SPIVec y, const SPIVec x); // trapezoidal integration of y with x coordinates \int y dx
139  PetscInt draw(const SPIVec &x); // draw nonzero structure and wait at command line input
140 }
141 
142 
143 
144 #endif // SPIVEC_H
SPIprint.hpp
SPI::SPIVec::SPIVec
SPIVec(std::string _name="SPIVec")
constructor with no arguments (no initialization)
Definition: SPIVec.cpp:9
SPI::sin
SPIMat sin(const SPIMat &A)
take the sin of each element in a matrix
Definition: SPIMat.cpp:2025
SPI::real
SPIVec real(const SPIVec &A)
return the real part of the vector
Definition: SPIVec.cpp:631
SPI::SPIVec::operator()
SPIVec & operator()()
Definition: SPIVec.cpp:170
SPI::atanh
SPIVec atanh(const SPIVec &A)
take the atanh of each element in a vector
Definition: SPIVec.cpp:739
SPI::SPIVec::real
SPIVec & real()
take the real part of the vector
Definition: SPIVec.cpp:449
SPI::operator-
SPIVec operator-(const PetscScalar a, const SPIVec &A)
Definition: SPIVec.cpp:532
SPI::erf
SPIVec erf(const SPIVec &A)
take the erf of each element in a vector
Definition: SPIVec.cpp:743
SPI::SPIVec::operator==
PetscBool operator==(const SPIVec &x2)
== VecEqual test if this==x2
Definition: SPIVec.cpp:404
SPI::acosh
SPIVec acosh(const SPIVec &A)
take the acosh of each element in a vector
Definition: SPIVec.cpp:737
SPI::arange
SPIVec arange(const PetscScalar begin, const PetscScalar end, const PetscScalar stepsize=1)
return a range of number of equally spaced points between begin and end by step size step
Definition: SPIVec.cpp:667
SPI::SPIVec::operator=
SPIVec & operator=(const SPIVec &X)
Definition: SPIVec.cpp:382
SPI::SPIVec
Definition: SPIVec.hpp:13
SPI::SPIVec::flag_init
PetscBool flag_init
flag if it has been initialized
Definition: SPIVec.hpp:25
SPI
Definition: SPIbaseflow.hpp:16
SPI::sum
PetscScalar sum(SPIVec x)
take the sum of a vector
Definition: SPIVec.cpp:805
SPI::SPIVec::operator*
SPIVec operator*(const PetscScalar a)
Definition: SPIVec.cpp:271
SPI::conj
SPIVec conj(const SPIVec &A)
Definition: SPIVec.cpp:622
SPI::SPIVec::operator*=
SPIVec & operator*=(const PetscScalar a)
Definition: SPIVec.cpp:289
SPI::ones
SPIVec ones(const PetscInt rows)
Definition: SPIVec.cpp:605
SPI::save
PetscInt save(const SPIMat &A, const std::string filename)
save matrix to filename in binary format (see Petsc documentation for format ) Format is (from Petsc ...
Definition: SPIMat.cpp:1924
SPI::cosh
SPIVec cosh(const SPIVec &A)
take the cosh of each element in a vector
Definition: SPIVec.cpp:725
SPI::L2
PetscReal L2(SPIVec x1, const SPIVec x2, NormType type=NORM_2)
calculate the norm of the difference between and vectors.
Definition: SPIVec.cpp:859
SPI::diff
SPIVec diff(SPIVec x1)
diff of the vector (see numpy.diff)
Definition: SPIVec.cpp:881
SPI::SPIVec::operator^
SPIVec operator^(const PetscScalar p)
pow operation pow(this,p)
Definition: SPIVec.cpp:357
SPI::SPIVec::operator/=
SPIVec & operator/=(const PetscScalar a)
Definition: SPIVec.cpp:349
SPI::log
SPIVec log(const SPIVec &A)
take the log (natural log) of each element in a vector
Definition: SPIVec.cpp:719
SPI::SPIVec::print
PetscInt print()
Definition: SPIVec.cpp:470
SPI::asinh
SPIVec asinh(const SPIVec &A)
take the asinh of each element in a vector
Definition: SPIVec.cpp:735
SPI::SPIVec::operator-
SPIVec operator-() const
Definition: SPIVec.cpp:266
SPI::linspace
SPIVec linspace(const PetscScalar begin, const PetscScalar end, const PetscInt rows)
return linspace of number of rows equally spaced points between begin and end
Definition: SPIVec.cpp:645
SPI::_Function_on_each_element
SPIMat _Function_on_each_element(T(*f)(T const &), const SPIMat &A)
take the function of each element in a matrix, e.g. (*f)(A(i,j)) for each i,j
Definition: SPIMat.cpp:2011
SPI::SPIVec::name
std::string name
Vec name (important for hdf5 i/o)
Definition: SPIVec.hpp:26
SPI::log10
SPIVec log10(const SPIVec &A)
take the log10 of each element in a vector
Definition: SPIVec.cpp:721
SPI::draw
PetscInt draw(const SPIMat &A)
draw nonzero structure of matrix
Definition: SPIMat.cpp:1994
SPI::imag
SPIVec imag(const SPIVec &A)
return the imaginary part of the vector
Definition: SPIVec.cpp:638
SPI::dot
PetscScalar dot(SPIVec x, SPIVec y)
take the inner product of the two vectors (i.e. y^H x) where ^H is the complex conjugate transpose
Definition: SPIVec.cpp:788
SPI::exp
SPIVec exp(const SPIVec &A)
take the exp of each element in a vector
Definition: SPIVec.cpp:712
SPI::tan
SPIMat tan(const SPIMat &A)
take the tan of each element in a matrix
Definition: SPIMat.cpp:2031
SPI::SPIVec::set
PetscInt set(const PetscInt _row, const PetscScalar v)
Definition: SPIVec.cpp:64
SPI::trapz
PetscScalar trapz(const SPIVec y)
trapezoidal integration of y with unity coordinate spacing,
Definition: SPIVec.cpp:898
SPI::SPIVec::operator+
SPIVec operator+(const SPIVec &X)
Definition: SPIVec.cpp:193
SPI::SPIVec::size
PetscInt size()
Definition: SPIVec.cpp:96
SPI::abs
SPIMat abs(const SPIMat &A)
take the abs of each element in a matrix
Definition: SPIMat.cpp:2045
SPI::SPIVec::operator-=
SPIVec & operator-=(const SPIVec &X)
Definition: SPIVec.cpp:244
SPI::integrate_coeffs
PetscScalar integrate_coeffs(const SPIVec &a)
integrate a vector of chebyshev Coefficients on a grid
Definition: SPIVec.cpp:813
SPI::SPIVec::ierr
PetscErrorCode ierr
ierr for various routines and operators
Definition: SPIVec.hpp:22
SPI::SPIVec::operator/
SPIVec operator/(const PetscScalar a)
Definition: SPIVec.cpp:323
SPI::SPIVec::axpy
SPIVec & axpy(const PetscScalar a, const SPIVec &X)
Definition: SPIVec.cpp:184
SPI::atan
SPIVec atan(const SPIVec &A)
take the atan of each element in a vector
Definition: SPIVec.cpp:733
SPI::sqrt
SPIVec sqrt(const SPIVec &A)
take the atanh of each element in a vector
Definition: SPIVec.cpp:741
SPI::SPIVec::~SPIVec
~SPIVec()
Definition: SPIVec.cpp:483
SPI::erfc
SPIVec erfc(const SPIVec &A)
take the erfc(z) = 1-erf(z) of each element in a vector
Definition: SPIVec.cpp:752
SPI::load
PetscInt load(SPIMat &A, const std::string filename)
load matrix from filename from binary format (works with save(SPIMat,std::string) function
Definition: SPIMat.cpp:1966
SPI::asin
SPIVec asin(const SPIVec &A)
take the asin of each element in a vector
Definition: SPIVec.cpp:729
SPI::pow
SPIVec pow(const SPIVec &A, SPIVec &B)
take the pow of each element in the vectors
Definition: SPIVec.cpp:775
SPI::SPIVec::operator+=
SPIVec & operator+=(const SPIVec &X)
Definition: SPIVec.cpp:177
SPI::tanh
SPIVec tanh(const SPIVec &A)
take the tanh of each element in a vector
Definition: SPIVec.cpp:727
SPI::SPIVec::argmax
PetscInt argmax()
Definition: SPIVec.cpp:437
SPI::sinh
SPIVec sinh(const SPIVec &A)
take the sinh of each element in a vector
Definition: SPIVec.cpp:723
SPI::acos
SPIMat acos(const SPIMat &A)
take the arccos of each element in a matrix
Definition: SPIMat.cpp:2029
SPI::SPIVec::conj
SPIVec & conj()
Definition: SPIVec.cpp:419
SPI::SPIVec::Init
PetscInt Init(const PetscInt _rows, const std::string name="SPIVec")
initialize the vector of size _rows
Definition: SPIVec.cpp:44
SPI::SPIVec::rows
PetscInt rows
number of rows in vec
Definition: SPIVec.hpp:14
SPI::SPIVec::dot
PetscScalar dot(SPIVec y)
take the inner product of two vectors
Definition: SPIVec.cpp:459
SPI::operator*
SPIMat operator*(const PetscScalar a, const SPIMat A)
a*A operation to be equivalent to A*a
Definition: SPIMat.cpp:517
SPI::SPIVec::imag
SPIVec & imag()
take the imaginary part of the vector
Definition: SPIVec.cpp:454
SPI::operator+
SPIVec operator+(const PetscScalar a, const SPIVec &A)
Definition: SPIVec.cpp:521
SPI::cos
SPIMat cos(const SPIMat &A)
take the cos of each element in a matrix
Definition: SPIMat.cpp:2027
SPI::SPIVec::add
PetscInt add(PetscInt _row, const PetscScalar v)
Definition: SPIVec.cpp:83
SPI::operator/
SPIVec operator/(const SPIVec &b, const SPIMat &A)
Solve linear system, Ax=b using b/A notation.
Definition: SPIMat.cpp:535
SPI::SPIVec::vec
Vec vec
petsc Vec data
Definition: SPIVec.hpp:21
SPI::SPIVec::max
PetscScalar max()
Definition: SPIVec.cpp:425
SPI::zeros
SPIMat zeros(const PetscInt m, const PetscInt n)
create, form, and return zeros matrix of size mxn
Definition: SPIMat.cpp:708