-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.cpp
121 lines (92 loc) · 3.57 KB
/
vector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef vector_CPP // © 2020, Alois Pichler
#define vector_CPP
//#include "matrix.h" // header file
#include <iostream>
template<typename T> // copy constructor
Vector<T>::Vector(const Vector<T>& src)
{ vLen= src.vLen;
vData= new T[vLen]; // deep copy
std::memcpy(vData, src.vData, vLen*sizeof(T));}
template<> // construct a Vector from Matrix
Vector<double>::Vector<double>(const Matrix& src)
{ vLen= src.rows()* src.cols();
vData= new double[vLen]; // deep copy from Vector source
std::memcpy(vData, src[0], vLen* sizeof(double));}
template<typename T> // assignment operator=
Vector<T>& Vector<T>::operator=(const Vector<T>& src)
{ if (this != &src) // protect against invalid self-assignment
{ if (vLen != src.vLen)
{ delete[] vData; // destroy old stack?
vLen= src.vLen;
vData = new T[src.vLen];} // deep copy
std::memcpy(vData, src.vData, vLen*sizeof(T));}
return *this;}
template<typename T> // element access operator
T& Vector<T>::operator[](const unsigned _index) const
{ return vData[_index];}
template<typename T> // element access operator
T &Vector<T>::operator()(const unsigned index) const
{ return vData[index- 1];}// the indexes are one-based, not zero based.
template<typename T>
Vector<T> Vector<T>::Zeros()// set all entries to 0
{ for(unsigned i= 0; i< vLen; i++)
(*this)[i]= (T) 0;
return *this;}
template<typename T> // find the max [] position in the vector
unsigned Vector<T>::maxPosition()
{ unsigned j= 0;
for (unsigned i= 1; i< vLen; i++)
if (vData[i]> vData[j]) j= i;
return j;}
template<typename T>
Vector<T> Vector<T>::operator+ (T lambda) // add a scalar to this vector
{ Vector<T> c(vLen);
for (unsigned i= 0; i< vLen; i++)
c.vData[i]= this->vData[i]+ lambda;
return c;}
template<typename T>
Vector<T> Vector<T>::operator+ (const Vector<T>& other) // add Vectors
{ assert((vLen== other.Length()) && "Vector length mismatch. ");
Vector c(vLen);
for (unsigned i= 0; i< vLen; i++)
c.vData[i]= this->vData[i] + other.vData[i];
return c;}
template<typename T>
Vector<T> Vector<T>::operator- (const Vector<T>& other) // subtract Vectors
{ assert((vLen== other.Length()) && "Vector length mismatch. ");
Vector c(vLen);
for(unsigned i= 0; i< vLen; i++)
c.vData[i]= (*this).vData[i] - other.vData[i];
return c;}
template<typename T>
Vector<T> Vector<T>::operator* (T lambda) // multiply this vector by scalar
{ Vector c(vLen);
for(unsigned i= 0; i< vLen; i++)
c.vData[i]= lambda* this->vData[i];
return c;}
double Norm(const Vector<double> vec, const double p= 2) // p-norm of a Vector
{ if (p < 1) std::cout << "Norm: 1 > p= " << p << std::endl;
double tmpNorm= 0;
if (p == 1) // 1-norm, weighted
{ for (unsigned i= 0; i< vec.Length(); i++)
tmpNorm+= fabs(vec[i]);
return tmpNorm/ vec.Length();}
else if (p == 2) // 2-norm, weighted
{ for (unsigned i= 0; i< vec.Length(); i++)
tmpNorm+= Square(vec[i]);
return sqrt(tmpNorm/ vec.Length());}
else if (isinf(p)) // ∞-norm
{ for (unsigned i= 0; i< vec.Length(); i++)
tmpNorm= std::max(tmpNorm, fabs(vec[i]));
return tmpNorm;}
else // all other weighted p-norms
{ for (unsigned i= 0; i< vec.Length(); i++)
if (vec[i] != 0.) tmpNorm+= pow(fabs(vec[i]), p);
return pow(tmpNorm/ vec.Length(), 1/ p);}}
template<typename T> // print
std::ostream& operator<< (std::ostream& os, const Vector<T> &src)
{ os << "(" << src.Length() << "-vector):";
for(unsigned i= 0; i< src.Length(); i++)
os << " " << src[i];
return os;}
#endif // vector_CPP