Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
1/*
2* Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3*/
4#ifndef _Stroika_Foundation_Math_LinearAlgebra_Vector_h_
5#define _Stroika_Foundation_Math_LinearAlgebra_Vector_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
10#include "Stroika/Foundation/Containers/Sequence.h"
13
14/**
15 * \file
16 *
17 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
18 */
19
20#define Stroika_Foundation_Math_LinearAlgebra_Vector_ALLOW_MUTATION 1
21#ifndef Stroika_Foundation_Math_LinearAlgebra_Vector_ALLOW_MUTATION
22#define Stroika_Foundation_Math_LinearAlgebra_Vector_ALLOW_MUTATION 0
23#endif
24
25namespace Stroika::Foundation::Math::LinearAlgebra {
26
27 /**
28 */
29 template <typename T>
30 class Vector {
31 public:
32 /**
33 */
34 Vector (size_t dimension);
35 Vector (size_t dimension, Common::ArgByValueType<T> fillValue);
36 Vector (size_t dimension, const function<T ()>& filler);
37 template <Traversal::IIterableOfTo<T> CONTAINER_OF_T>
38 Vector (const CONTAINER_OF_T& c);
39
40#if Stroika_Foundation_Math_LinearAlgebra_Vector_ALLOW_MUTATION
41 public:
42 /**
43 * \note - Armadillo calls the Fill overload with a function argument 'imbue'
44 */
45 nonvirtual void Fill (T value);
46 nonvirtual void Fill (function<T ()> filler);
47#endif
48
49 public:
50 /**
51 */
52 nonvirtual size_t GetDimension () const;
53
54 public:
55 /**
56 * construct a new vector by applying the argument function to each element and collecting the results.
57 */
58 nonvirtual Vector<T> Transform (const function<T (T)>& f) const;
59
60 public:
61 /**
62 * Euclidian norm = sqrt (sum (xi^2))
63 */
64 nonvirtual T Norm () const;
65
66 public:
67 /**
68 */
69 nonvirtual Containers::Sequence<T> GetItems () const;
70
71#if Stroika_Foundation_Math_LinearAlgebra_Vector_ALLOW_MUTATION
72 public:
73 /**
74 */
75 template <typename CONTAINER>
76 nonvirtual void SetItems (const CONTAINER& s);
77#endif
78
79 public:
80 /**
81 */
82 nonvirtual T GetAt (size_t i) const;
83
84#if Stroika_Foundation_Math_LinearAlgebra_Vector_ALLOW_MUTATION
85 public:
86 /**
87 */
88 nonvirtual void SetAt (size_t i, Common::ArgByValueType<T> v);
89#endif
90
91 public:
92 /**
93 */
94 nonvirtual T operator[] (size_t i) const;
95
96#if Stroika_Foundation_Math_LinearAlgebra_Vector_ALLOW_MUTATION
97 private:
98 struct TMP_ {
99 Vector<T>& fV;
100 size_t fIndex;
101 T fValue;
102 ~TMP_ ()
103 {
104 fV.SetAt (fIndex, fValue);
105 }
106 operator T& ()
107 {
108 return fValue;
109 }
110 };
111
112 public:
113 /**
114 */
115 nonvirtual TMP_ operator[] (size_t i)
116 {
117 return TMP_{*this, i, GetAt (i)};
118 }
119#endif
120
121 public:
122 nonvirtual Characters::String ToString () const;
123
124 private:
125 class IRep_;
126
127 private:
128 Memory::SharedByValue<IRep_> fRep_;
129 };
130
131 template <typename T>
132 Vector<T> operator* (T lhs, const Vector<T>& rhs)
133 {
134 vector<T> tmp;
135 for (const auto& i : rhs.GetItems ()) {
136 tmp.push_back (lhs * i);
137 }
138 return tmp;
139 }
140 template <typename T>
141 Vector<T> operator* (const Vector<T>& lhs, T rhs)
142 {
143 vector<T> tmp;
144 for (const auto& i : lhs.GetItems ()) {
145 tmp.push_back (i * rhs);
146 }
147 return tmp;
148 }
149
150 template <typename T>
151 Vector<T> operator+ (const Vector<T>& lhs, const Vector<T>& rhs)
152 {
153 Require (lhs.GetDimension () == rhs.GetDimension ());
154 vector<T> tmp;
155 for (size_t i = 0; i < lhs.GetDimension (); ++i) {
156 tmp.push_back (lhs[i] + rhs[i]);
157 }
158 return tmp;
159 }
160 template <typename T>
161 Vector<T> operator+ (T lhs, const Vector<T>& rhs)
162 {
163 vector<T> tmp;
164 for (const auto& i : rhs.GetItems ()) {
165 tmp.push_back (lhs + i);
166 }
167 return tmp;
168 }
169 template <typename T>
170 Vector<T> operator+ (const Vector<T>& lhs, T rhs)
171 {
172 vector<T> tmp;
173 for (const auto& i : lhs.GetItems ()) {
174 tmp.push_back (i + rhs);
175 }
176 return tmp;
177 }
178
179 template <typename T>
180 Vector<T> operator- (const Vector<T>& lhs, const Vector<T>& rhs)
181 {
182 Require (lhs.GetDimension () == rhs.GetDimension ());
183 vector<T> tmp;
184 for (size_t i = 0; i < lhs.GetDimension (); ++i) {
185 tmp.push_back (lhs[i] - rhs[i]);
186 }
187 return tmp;
188 }
189 template <typename T>
190 Vector<T> operator- (T lhs, const Vector<T>& rhs)
191 {
192 vector<T> tmp;
193 for (const T& i : rhs.GetItems ()) {
194 tmp.push_back (lhs - i);
195 }
196 return tmp;
197 }
198 template <typename T>
199 Vector<T> operator- (const Vector<T>& lhs, T rhs)
200 {
201 vector<T> tmp;
202 for (const auto& i : lhs.GetItems ()) {
203 tmp.push_back (i - rhs);
204 }
205 return tmp;
206 }
207
208}
209
210/*
211 ********************************************************************************
212 ***************************** Implementation Details ***************************
213 ********************************************************************************
214 */
215#include "Vector.inl"
216
217#endif /*_Stroika_Foundation_Math_LinearAlgebra_Vector_h_*/
constexpr auto Transform(const optional< T > &o, F &&f)
Definition Optional.h:241
STRING_TYPE ToString(FLOAT_TYPE f, const ToStringOptions &options={})