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