Skip to content
Snippets Groups Projects
Commit be71f56a authored by Bernhard Manfred Gruber's avatar Bernhard Manfred Gruber
Browse files

Avoid NVector copies in arithmetic operators and use std::inner_product for sqnorm()

parent 3673bfcf
No related branches found
No related tags found
1 merge request!18A few NVector improvements
#include <ostream> #include <ostream>
#include <assert.h> #include <assert.h>
#include <algorithm> #include <algorithm>
#include <numeric>
template <int N, class T=float> template <int N, class T=float>
class NVector { class NVector {
...@@ -114,10 +115,7 @@ NVector<N,T>& NVector<N,T>::operator+=(const NVector<N,T> other) { ...@@ -114,10 +115,7 @@ NVector<N,T>& NVector<N,T>::operator+=(const NVector<N,T> other) {
template <int N, class T> template <int N, class T>
T NVector<N,T>::sqnorm() const { T NVector<N,T>::sqnorm() const {
T norm; return std::inner_product(m_data, m_data + N, m_data, T());
std::for_each(m_data, m_data+N,
[&norm](T a) { norm += a*a; });
return norm;
} }
template <int N, class T> template <int N, class T>
......
#include <ostream> #include <ostream>
#include <assert.h> #include <assert.h>
#include <algorithm> #include <algorithm>
#include <numeric>
template <int N, class T=float> template <int N, class T=float>
class NVector { class NVector {
...@@ -14,15 +15,15 @@ public: ...@@ -14,15 +15,15 @@ public:
~NVector(); ~NVector();
NVector& operator-() const; NVector& operator-() const;
NVector& operator+(const NVector other) const; NVector& operator+(const NVector& other) const;
NVector& operator-(const NVector other) const; NVector& operator-(const NVector& other) const;
NVector& operator*(const NVector other) const; NVector& operator*(const NVector& other) const;
NVector& operator*(const T factor) const; NVector& operator*(const T factor) const;
NVector& operator/(const T dividend) const; NVector& operator/(const T dividend) const;
NVector& operator=(NVector other); NVector& operator=(NVector other);
NVector& operator+=(const NVector other); NVector& operator+=(const NVector& other);
T sqnorm() const; T sqnorm() const;
bool operator<(const NVector a) const; bool operator<(const NVector& a) const;
void print(std::ostream& os) const; void print(std::ostream& os) const;
template<int M,class R> friend void swap(NVector<M,R> &a, NVector<M,R> &b); template<int M,class R> friend void swap(NVector<M,R> &a, NVector<M,R> &b);
...@@ -71,7 +72,7 @@ NVector<N,T>& NVector<N,T>::operator-() const { ...@@ -71,7 +72,7 @@ NVector<N,T>& NVector<N,T>::operator-() const {
} }
template <int N, class T> template <int N, class T>
NVector<N,T>& NVector<N,T>::operator+(const NVector<N,T> other) const { NVector<N,T>& NVector<N,T>::operator+(const NVector<N,T>& other) const {
NVector res; NVector res;
std::transform(m_data, m_data+N, std::transform(m_data, m_data+N,
other.m_data, res.m_data, other.m_data, res.m_data,
...@@ -80,7 +81,7 @@ NVector<N,T>& NVector<N,T>::operator+(const NVector<N,T> other) const { ...@@ -80,7 +81,7 @@ NVector<N,T>& NVector<N,T>::operator+(const NVector<N,T> other) const {
} }
template <int N, class T> template <int N, class T>
NVector<N,T>& NVector<N,T>::operator-(const NVector<N,T> other) const { NVector<N,T>& NVector<N,T>::operator-(const NVector<N,T>& other) const {
NVector res; NVector res;
std::transform(m_data, m_data+N, std::transform(m_data, m_data+N,
other.m_data, res.m_data, other.m_data, res.m_data,
...@@ -111,7 +112,7 @@ NVector<N,T>& NVector<N,T>::operator=(NVector<N,T> other) { ...@@ -111,7 +112,7 @@ NVector<N,T>& NVector<N,T>::operator=(NVector<N,T> other) {
} }
template <int N, class T> template <int N, class T>
NVector<N,T>& NVector<N,T>::operator+=(const NVector<N,T> other) { NVector<N,T>& NVector<N,T>::operator+=(const NVector<N,T>& other) {
std::transform(m_data, m_data+N, std::transform(m_data, m_data+N,
other.m_data, m_data, other.m_data, m_data,
[](T a, T b) {return a + b;}); [](T a, T b) {return a + b;});
...@@ -120,14 +121,11 @@ NVector<N,T>& NVector<N,T>::operator+=(const NVector<N,T> other) { ...@@ -120,14 +121,11 @@ NVector<N,T>& NVector<N,T>::operator+=(const NVector<N,T> other) {
template <int N, class T> template <int N, class T>
T NVector<N,T>::sqnorm() const { T NVector<N,T>::sqnorm() const {
T norm; return std::inner_product(m_data, m_data+N, m_data, T());
std::for_each(m_data, m_data+N,
[&norm](T a) { norm += a*a; });
return norm;
} }
template <int N, class T> template <int N, class T>
bool NVector<N,T>::operator<(const NVector<N,T> other) const { bool NVector<N,T>::operator<(const NVector<N,T>& other) const {
return this->sqnorm() < other.sqnorm(); return this->sqnorm() < other.sqnorm();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment