Skip to content
Snippets Groups Projects
Commit f27c74e3 authored by Sebastien Ponce's avatar Sebastien Ponce
Browse files

Merge branch 'nvector' into 'master'

A few NVector improvements

See merge request !18
parents 3673bfcf d326f756
No related branches found
No related tags found
1 merge request!18A few NVector improvements
#include <ostream>
#include <assert.h>
#include <algorithm>
#include <numeric>
template <int N, class T=float>
class NVector {
......@@ -114,10 +115,7 @@ NVector<N,T>& NVector<N,T>::operator+=(const NVector<N,T> other) {
template <int N, class T>
T NVector<N,T>::sqnorm() const {
T norm;
std::for_each(m_data, m_data+N,
[&norm](T a) { norm += a*a; });
return norm;
return std::inner_product(m_data, m_data + N, m_data, T());
}
template <int N, class T>
......
#include <ostream>
#include <assert.h>
#include <algorithm>
#include <numeric>
template <int N, class T=float>
class NVector {
......@@ -14,15 +15,15 @@ public:
~NVector();
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 dividend) const;
NVector& operator=(NVector other);
NVector& operator+=(const NVector other);
NVector& operator+=(const NVector& other);
T sqnorm() const;
bool operator<(const NVector a) const;
bool operator<(const NVector& a) const;
void print(std::ostream& os) const;
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 {
}
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;
std::transform(m_data, m_data+N,
other.m_data, res.m_data,
......@@ -80,7 +81,7 @@ NVector<N,T>& NVector<N,T>::operator+(const NVector<N,T> other) const {
}
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;
std::transform(m_data, m_data+N,
other.m_data, res.m_data,
......@@ -111,7 +112,7 @@ NVector<N,T>& NVector<N,T>::operator=(NVector<N,T> other) {
}
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,
other.m_data, m_data,
[](T a, T b) {return a + b;});
......@@ -120,14 +121,11 @@ NVector<N,T>& NVector<N,T>::operator+=(const NVector<N,T> other) {
template <int N, class T>
T NVector<N,T>::sqnorm() const {
T norm;
std::for_each(m_data, m_data+N,
[&norm](T a) { norm += a*a; });
return norm;
return std::inner_product(m_data, m_data+N, m_data, 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();
}
......
......@@ -26,8 +26,7 @@ template<class T>
std::vector<T> getRandomVector(int len) {
// allocate vectors
std::vector<T> v(len);
// fill and randomize v
for (int i = 0; i < len; i++) v[i] = T();
// randomize v
randomize(v);
return v;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment