Mantid
Loading...
Searching...
No Matches
EigenComplexVector.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11
12#include <cmath>
13#include <iomanip>
14#include <sstream>
15#include <stdexcept>
16
17namespace Mantid::CurveFitting {
18
20ComplexVector::ComplexVector() : m_vector(Eigen::VectorXcd(1)) { zero(); }
21
24ComplexVector::ComplexVector(const size_t n) : m_vector(Eigen::VectorXcd(n)) {}
25
28ComplexVector::ComplexVector(const ComplexVector &v) : m_vector(v.m_vector) {}
29
32ComplexVector::ComplexVector(ComplexVector &&v) : m_vector(std::move(v.m_vector)) {}
33
36ComplexVector::ComplexVector(const Eigen::VectorXcd v) : m_vector(v) {}
37
40ComplexVector::ComplexVector(Eigen::VectorXcd &&v) : m_vector(std::move(v)) {}
41
46 return *this;
47}
48
51ComplexVector &ComplexVector::operator=(const Eigen::VectorXcd &v) {
52 m_vector = v;
53 return *this;
54}
55
59 m_vector = std::move(v.m_vector);
60 return *this;
61}
62
64Eigen::VectorXcd &ComplexVector::eigen() { return m_vector; }
65
67const Eigen::VectorXcd ComplexVector::eigen() const { return m_vector; }
68
71void ComplexVector::resize(const size_t n) {
72 auto newVector = Eigen::VectorXcd(n);
73 size_t m = size() < n ? size() : n;
74 for (size_t i = 0; i < m; ++i) {
75 newVector(i) = m_vector(i);
76 }
77 for (size_t i = m; i < n; ++i) {
78 newVector(i) = ComplexType(0, 0);
79 }
80 m_vector = newVector;
81}
82
84size_t ComplexVector::size() const { return m_vector.size(); }
85
89void ComplexVector::set(const size_t i, const ComplexType &value) {
90 if (i < size()) {
91 m_vector(i) = value;
92 } else {
93 std::stringstream errmsg;
94 errmsg << "ComplexVector index = " << i << " is out of range = " << m_vector.size() << " in ComplexVector.set()";
95 throw std::out_of_range(errmsg.str());
96 }
97}
100ComplexType ComplexVector::get(const size_t i) const {
101 if (i < size()) {
102 return m_vector(i);
103 }
104 std::stringstream errmsg;
105 errmsg << "ComplexVector index = " << i << " is out of range = " << m_vector.size() << " in ComplexVector.get()";
106 throw std::out_of_range(errmsg.str());
107}
108
109// Set all elements to zero
110void ComplexVector::zero() { m_vector.setZero(); }
111
115 if (size() != v.size()) {
116 throw std::runtime_error("ComplexVectors have different sizes.");
117 }
118 m_vector += v.eigen();
119 return *this;
120}
121
125 if (size() != v.size()) {
126 throw std::runtime_error("ComplexVectors have different sizes.");
127 }
128 m_vector -= v.eigen();
129 return *this;
130}
131
135 m_vector *= d;
136 return *this;
137}
138
142 m_vector.array() += d;
143 return *this;
144}
145
149
151std::ostream &operator<<(std::ostream &ostr, const ComplexVector &v) {
152 std::ios::fmtflags fflags(ostr.flags());
153 ostr << std::scientific << std::setprecision(6);
154 for (size_t j = 0; j < v.size(); ++j) {
155 auto value = v.get(j);
156 ostr << std::setw(28) << value.real() << "+" << value.imag() << "j";
157 }
158 ostr.flags(fflags);
159 return ostr;
160}
161
164void ComplexVector::sort(const std::vector<size_t> &indices) {
165 std::vector<ComplexType> temp_data(size());
166 std::transform(indices.cbegin(), indices.cend(), temp_data.begin(),
167 [&m_vector = m_vector](size_t i) { return m_vector(i); });
168 resize(indices.size());
169 m_vector = complex_vec_map_type(temp_data.data(), temp_data.size(), dynamic_stride(0, 1));
170}
171
172} // namespace Mantid::CurveFitting
double value
The value of the point.
Definition: FitMW.cpp:51
A complex-valued vector for linear algebra computations.
ComplexType get(const size_t i) const
Get an element.
ComplexVector & operator=(const ComplexVector &v)
Copy assignment operator.
void sort(const std::vector< size_t > &indices)
Sort Vector by indicies provided.
ComplexVector & operator*=(const ComplexType d)
Multiply by a number.
ComplexVector & operator-=(const ComplexVector &v)
Subtract a vector.
size_t size() const
Size of the vector.
Eigen::VectorXcd & eigen()
Get the pointer to the GSL vector.
ComplexVector move()
Get index of the minimum element.
void set(const size_t i, const ComplexType &value)
Set an element.
Eigen::VectorXcd m_vector
The Eigen vector.
ComplexVector & operator+=(const ComplexVector &v)
Add a vector.
void resize(const size_t n)
Resize the vector.
MANTID_API_DLL std::ostream & operator<<(std::ostream &, const AlgorithmHistory &)
Prints a text representation.
std::complex< double > ComplexType
Eigen::Stride< Eigen::Dynamic, Eigen::Dynamic > dynamic_stride
Eigen::Map< Eigen::VectorXcd, 0, dynamic_stride > complex_vec_map_type
STL namespace.