msl 1.1.0
 
Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
1// Copyright 2023-2025 Julien Lamy, ICube, Université de Strasbourg-CNRS.
2// Part of msl, distributed under the terms of the MIT license.
3
4#ifndef _da006854_5a52_4f29_8aa1_24be101f8935
5#define _da006854_5a52_4f29_8aa1_24be101f8935
6
7#include <array>
8#include <initializer_list>
9#include <ostream>
10
11namespace msl
12{
13
15template<std::size_t N, typename T>
16class Vector
17{
18public:
19 using Container = std::array<T, N>;
20 using value_type = typename Container::value_type;
21 using const_iterator = typename Container::const_iterator;
22 using iterator = typename Container::iterator;
23
25 Vector(std::initializer_list<T> && initializer);
26 ~Vector() = default;
27 Vector(Vector<N, T> const &) = default;
28 Vector(Vector<N, T> &&) = default;
29 Vector<N, T> & operator=(Vector<N, T> const &) = default;
31
33 bool operator==(Vector<N, T> const & other) const;
34
36 bool operator!=(Vector<N, T> const & other) const;
37
39 constexpr std::size_t size() const { return N; }
40
42 T const & operator[](std::size_t i) const;
43
45 T & operator[](std::size_t i);
46
49
52
55
58
61
64
66 Vector<N, T> const & operator+() const;
67
70
72 T norm() const;
73
76
79
82
85
88
91
93 template<typename Dest>
95
96private:
97 Container _data;
98};
99
102
105
108
111
114
117
119template<std::size_t N, typename T>
121
123template<std::size_t N, typename T>
125
127template<std::size_t N, typename T>
129
131template<std::size_t N, typename T>
133
135template<std::size_t N, typename T>
137
139template<std::size_t N, typename T>
141
143template<std::size_t N, typename T>
145
147template<std::size_t N, typename T>
148std::ostream & operator<<(std::ostream & stream, Vector<N,T> const & v);
149
150}
151
152namespace std
153{
154
156template<std::size_t N, typename T>
158
159}
160
161#include "Vector.txx"
162
163#endif // _da006854_5a52_4f29_8aa1_24be101f8935
A vector of arithmetic types.
Definition Vector.h:17
Vector< N, T > & operator%=(T right)
In-place remainder of division by a scalar.
Vector(std::initializer_list< T > &&initializer)
Vector< N, Dest > cast() const
Element-wise cast.
Vector< N, T > & operator=(Vector< N, T > const &)=default
const_iterator cbegin() const
Constant iterator to the first element.
std::array< T, N > Container
Definition Vector.h:19
Vector< N, T > & operator%=(Vector< N, T > const &right)
Elementwise in-place remainder of division.
~Vector()=default
const_iterator cend() const
Constant iterator after the last element.
Vector< N, T > operator-() const
Opposite operator.
Vector(Vector< N, T > const &)=default
typename Container::iterator iterator
Definition Vector.h:22
Vector< N, T > & operator+=(Vector< N, T > const &right)
Element-wise in-place addition.
Vector< N, T > const & operator+() const
Identity operator.
bool operator==(Vector< N, T > const &other) const
Test equality of all elements.
iterator end()
Iterator after the last element.
const_iterator begin() const
Constant iterator to the first element.
Vector< N, T > & operator/=(T right)
In-place division by a scalar.
Vector(Vector< N, T > &&)=default
T & operator[](std::size_t i)
Access an element.
iterator begin()
Iterator to the first element.
typename Container::const_iterator const_iterator
Definition Vector.h:21
const_iterator end() const
Constant iterator after the last element.
Vector< N, T > & operator=(Vector< N, T > &&)=default
T norm() const
Euclidean norm (i.e. L2 norm)
Vector< N, T > & operator*=(T scalar)
In-place multiplication by a scalar.
Vector< N, T > & operator-=(Vector< N, T > const &right)
Element-wise in-place subtraction.
bool operator!=(Vector< N, T > const &other) const
Test difference of any element.
T const & operator[](std::size_t i) const
Access an element.
constexpr std::size_t size() const
Return the number of elements.
Definition Vector.h:39
typename Container::value_type value_type
Definition Vector.h:20
Definition acceleration.h:17
Vector< N, T > operator*(Vector< N, T > left, T right)
Multiplication by a scalar.
Vector< N, T > operator+(Vector< N, T > left, Vector< N, T > const &right)
Element-wise addition.
std::ostream & operator<<(std::ostream &stream, Vector< N, T > const &v)
Textual representation.
Vector< N, T > operator/(Vector< N, T > left, T right)
Division by a scalar.
Vector< 2, long > Vector2l
2D vector of longs
Definition Vector.h:104
Vector< 3, double > Vector3d
3D vector of doubles
Definition Vector.h:110
Vector< 3, long > Vector3l
3D vector of longs
Definition Vector.h:113
Vector< 2, bool > Vector2b
2D vector of booleans
Definition Vector.h:107
Vector< 3, bool > Vector3b
3D vector of booleans
Definition Vector.h:116
Vector< N, T > operator%(Vector< N, T > left, Vector< N, T > const &right)
Element-wise remainder of division.
Vector< 2, double > Vector2d
2D vector of doubles
Definition Vector.h:101
Vector< N, T > operator-(Vector< N, T > left, Vector< N, T > const &right)
Element-wise subtraction.
Definition Vector.h:153
msl::Vector< N, T > abs(msl::Vector< N, T > v)
Element-wise absolute value.