Chi-Tech
chi_math_03_vector_operations.cc
Go to the documentation of this file.
1#include "chi_math.h"
2#include <assert.h>
3
4//######################################################### Print Vector
5/** Prints the Vector.*/
7{
8 for (auto& xi : x)
9 std::cout << xi << ' ';
10 std::cout << std::endl;
11}
12
13//######################################################### Scale
14/** Scales a vector in place by constant.*/
15void chi_math::Scale(VecDbl &x, const double &val)
16{
17 for (double& xi : x)
18 xi *= val;
19}
20
21//######################################################### Scale
22/** Sets a constant value to a vector.*/
23void chi_math::Set(VecDbl &x, const double &val)
24{
25 for (double& xi : x)
26 xi = val;
27}
28
29//######################################################### Dot Product
30/** Computes the dot product of two vectors.
31 *
32 * \f[
33 * \mathrm{a} \cdot \mathrm{b}=\sum_{i=1}^{n} a_{i} b_{i}
34 * \f]
35 * */
36double chi_math::Dot(const VecDbl &x, const VecDbl &y)
37{
38 // Error Checks
39 assert(x.size() > 0);
40 assert(y.size() > 0);
41 assert(x.size() == y.size());
42 // Local Variables
43 size_t n = x.size();
44 double val = 0.0;
45
46 for(size_t i = 0; i != n; i++)
47 val += x[i] * y[i];
48
49 return val;
50}
51
52//######################################################### Multiply with const
53/** Multiplies the vector with a constant and returns result.*/
54VecDbl chi_math::VecMul(const VecDbl &x, const double &val)
55{
56 size_t n = x.size();
57 VecDbl y(n);
58
59 for (size_t i = 0; i != n; ++i)
60 y[i] = val * x[i];
61
62 return y;
63}
64
65//######################################################### Norms
66/** Returns the 1-norm. Also known as the Taxicab or Manhattan norm.
67 *
68 * \f[
69 * \|\boldsymbol{x}\|_{1}=\sum_{i=1}^{n}\left|x_{i}\right|
70 * \f]
71 *
72 * */
73 double chi_math::Vec1Norm(const VecDbl &x)
74 {
75 // Local Variables
76 size_t n = x.size();
77 double val = 0.;
78
79 for(size_t i = 0; i != n; i++)
80 val += std::fabs(x[i]);
81
82 return val;
83 }
84
85
86/** Returns the 2-norm. Also known as the Euclidian or Frobenius norm.
87 *
88 * \f[
89 * \|\boldsymbol{x}\|_{2}=\sqrt{x_{1}^{2}+\cdots+x_{n}^{2}}
90 * \f]
91 *
92 * */
93double chi_math::Vec2Norm(const VecDbl &x)
94{
95 // Local Variables
96 size_t n = x.size();
97 double val = 0.;
98
99 for(size_t i = 0; i != n; i++)
100 val += x[i]*x[i];
101
102 return std::sqrt(val);
103}
104
105/** Returns the infinity-norm.
106 *
107 * \f[
108 * \|\mathbf{x}\|_{\infty}=\max \left(\left|x_{1}\right|, \ldots,\left|x_{n}\right|\right)
109 * \f]
110 *
111 * */
113{
114 // Local Variables
115 size_t n = x.size();
116 double val = 0.0;
117
118 for(size_t i = 0; i != n; i++)
119 val += std::max(std::fabs(x[i]), val);
120
121 return val;
122}
123
124/** Returns the p-norm.
125 *
126 * \f[
127 * \|\mathbf{x}\|_{p}=\left(\sum_{i=1}^{n}\left|x_{i}\right|^{p}\right)^{1 / p}
128 * \f]
129 *
130 * */
131double chi_math::VecPNorm(const VecDbl &x, const double& p)
132{
133 // Local Variables
134 size_t n = x.size();
135 double val = 0.;
136
137 for(size_t i = 0; i != n; i++)
138 val += std::pow(std::fabs(x[i]), p);
139
140 return std::pow(val, 1./p);
141}
142
143/**Adds two vectors component-wise.*/
145{
146 assert(a.size() == b.size());
147 VecDbl result(a.size(), 0.0);
148
149 for (size_t i=0; i<a.size(); ++i)
150 result[i] = a[i] + b[i];
151
152 return result;
153}
154
155/**Subtracts two vectors component-wise.*/
157{
158 assert(a.size() == b.size());
159 VecDbl result(a.size(), 0.0);
160
161 for (size_t i=0; i<a.size(); ++i)
162 result[i] = a[i] - b[i];
163
164 return result;
165}
std::vector< double > VecDbl
Definition: chi_math.h:12
void PrintVector(const VecDbl &x)
double Vec1Norm(const VecDbl &x)
VecDbl operator-(const VecDbl &a, const VecDbl &b)
void Scale(VecDbl &x, const double &val)
VecDbl VecMul(const VecDbl &x, const double &val)
void Set(VecDbl &x, const double &val)
double VecInfinityNorm(const VecDbl &x)
VecDbl operator+(const VecDbl &a, const VecDbl &b)
double Dot(const VecDbl &x, const VecDbl &y)
double VecPNorm(const VecDbl &x, const double &p)
double Vec2Norm(const VecDbl &x)