-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorc.cpp
211 lines (179 loc) · 4.51 KB
/
vectorc.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// vectorc.cpp
// linear_alg_objects
//
// Created by Jon Lederman on 7/17/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include "vectorc.h"
#include "complex_util.h"
#include "gsl/gsl_complex_math.h"
//Need to fix ostream operator
std::ostream& operator<<(std::ostream & os, const vectorc::ComplexProxy& cp)
{
// os<<"("<<GSL_REAL(gsl_vector_complex_get(cp.theVector.v_m, cp.ComplexIndex))<<","<<GSL_IMAG(gsl_vector_complex_get(cp.theVector.v_m, cp.ComplexIndex))<<")";
return os;
}
vectorc::ComplexProxy::ComplexProxy(vectorc& vec, int index) : theVector(vec), ComplexIndex(index)
{}
vectorc::ComplexProxy::operator gsl_complex() const
{
return gsl_vector_complex_get(theVector.v_m, ComplexIndex);
}
vectorc::ComplexProxy& vectorc::ComplexProxy::operator=(const ComplexProxy& rhs)
{
gsl_vector_complex_set(theVector.v_m, ComplexIndex, gsl_vector_complex_get(rhs.theVector.v_m, ComplexIndex));
return *this;
}
vectorc::ComplexProxy& vectorc::ComplexProxy::operator=(gsl_complex c)
{
gsl_vector_complex_set(theVector.v_m, ComplexIndex, c);
return *this;
}
vectorc::vectorc(int size) : size_m(size), v_m(gsl_vector_complex_calloc(size))
{}
vectorc::~vectorc()
{
gsl_vector_complex_free(v_m);
};
vectorc::vectorc(const double values[], const int size) : size_m(size), v_m(gsl_vector_complex_alloc(size))
{
gsl_complex z;
for (int i=0; i<size_m; i++)
{
GSL_SET_COMPLEX(&z, values[2*i], values[2*i+1]);
gsl_vector_complex_set(v_m, i, z);
}
}
vectorc::vectorc(const vectorc& vec) : size_m(vec.size_m), v_m(gsl_vector_complex_alloc(vec.size_m))
{
for (int i=0; i<size_m; i++)
gsl_vector_complex_set(v_m, i, gsl_vector_complex_get(vec.v_m, i));
}
vectorc vectorc::operator=(const vectorc& vec)
{
if (this==&vec)
return *this;
gsl_vector_complex_free (v_m);
size_m=vec.size_m;
v_m=gsl_vector_complex_alloc(size_m);
for (int i=0; i<size_m; i++)
gsl_vector_complex_set(v_m, i, gsl_vector_complex_get(vec.v_m, i));
return *this;
}
bool vectorc::operator==(const vectorc& vec) const
{
bool equal=true;
for (int i=0; i<size_m; i++)
{
if (GSL_COMPLEX_EQ(gsl_vector_complex_get(v_m, i), gsl_vector_complex_get(vec.v_m, i)))
{
equal=false;
break;
}
}
return equal;
}
const vectorc::ComplexProxy vectorc::operator[](int index) const
{
return ComplexProxy(const_cast<vectorc&>(*this), index);
}
vectorc::ComplexProxy vectorc::operator[](int index)
{
return ComplexProxy(*this, index);
}
int vectorc::get_size() const
{
return size_m;
}
void vectorc::get_values_copy(double *vector) const
{
for (int i=0; i<size_m; i++)
{
vector[2*i]=GSL_REAL(gsl_vector_complex_get(v_m, i));
vector[2*i+1]=GSL_IMAG(gsl_vector_complex_get(v_m, i));
}
}
vectorc vectorc::operator+(const vectorc & vec) const
{
try
{
if (size_m!=vec.size_m)
throw("\nCan't add vectors of different sizes.");
}
catch(char* str)
{
std::cout << "Exception raised: " << str << '\n';
}
vectorc tot(*this);
gsl_vector_complex_add(tot.v_m, vec.v_m);
return tot;
}
void vectorc::set_values(double *vals) const
{
gsl_complex z;
for (int i=0; i<size_m; i++)
{
GSL_SET_COMPLEX(&z, vals[2*i], vals[2*i+1]);
gsl_vector_complex_set(v_m, i, z);
}
}
vectorc vectorc::operator-(const vectorc& vec) const
{
try
{
if (size_m!=vec.size_m)
throw("\nCan't add vectors of different sizes.");
}
catch(char* str)
{
std::cout << "Exception raised: " << str << '\n';
}
vectorc tot(*this);
gsl_vector_complex_sub(tot.v_m, vec.v_m);
return tot;
}
vectorc vectorc::operator-() const
{
vectorc tot(size_m);
gsl_vector_complex_sub(tot.v_m, v_m);
return tot;
}
vectorc vectorc::operator*(const gsl_complex n) const
{
vectorc result(size_m);
for (int i=0; i<size_m; i++)
gsl_vector_complex_set(result.v_m, i, gsl_complex_mul(n,gsl_vector_complex_get(v_m,i)));
return result;
}
gsl_complex vectorc::operator*(const vectorc& vec) const
{
gsl_complex result;
for (int i=0; i<size_m; i++)
gsl_complex_add(result, gsl_complex_mul(gsl_vector_complex_get(v_m, i), gsl_vector_complex_get(vec.v_m, i)));
return result;
}
vectorc operator*(const gsl_complex n, const vectorc & vec)
{
vectorc result(vec.size_m);
for (int i=0; i<vec.size_m; i++)
gsl_vector_complex_set(result.v_m, i, gsl_complex_mul(n,gsl_vector_complex_get(vec.v_m,i)));
return result;
}
std::ostream& operator<<(std::ostream &os, const vectorc& vec)
{
os<<"[";
for (int i=0; i<vec.size_m; i++)
{
os<<vec[i];
if (i<vec.size_m-1)
os<<",";
}
os<<"]";
return os;
}
void vectorc::clear()
{
gsl_vector_complex_set_zero(v_m);
}