-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxvector.c
More file actions
308 lines (225 loc) · 6.39 KB
/
xvector.c
File metadata and controls
308 lines (225 loc) · 6.39 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "xvector.h"
double vector_angle(const double* const A, const double* const B, const size_t length) {
if (A == NULL || B == NULL) {
return 0;
}
if (length == 0) {
return 0;
}
return acos(vector_dotProduct(A,B,length) / (vector_length(A,length)*vector_length(B,length)));
}
int vector_abs(double* const A, const size_t length) {
size_t i = 0;
if (A == NULL) return ERR_NUL;
for (; i < length; i++){
if(A[i] < 0) A[i] = -A[i];
}
return 0;
}
int vector_negate(double* const A, const size_t length) {
size_t i = 0;
if (A == NULL) return ERR_NUL;
for (; i < length; i++) {
A[i] = -A[i];
}
return 0;
}
int vector_add(const double* const A, const size_t Alength, const double* const B, const size_t Blength, double** R, size_t* Rsize) {
size_t i = 0;
if (A == NULL || B == NULL || R == NULL || Rsize == NULL) return ERR_NUL;
if (Alength != Blength) return ERR_DIM;
REALLOCATE(*R, *Rsize, Alength);
for (; i < Alength; i++) {
(*R)[i] = A[i] + B[i];
}
return 0;
}
int vector_subtract(const double* const A, const size_t Alength, const double* const B, const size_t Blength, double** R, size_t* Rsize) {
size_t i = 0;
if (A == NULL || B == NULL || R == NULL || Rsize == NULL) return ERR_NUL;
if (Alength != Blength) return ERR_DIM;
REALLOCATE(*R, *Rsize, Alength);
for (; i < Alength; i++) {
(*R)[i] = A[i] - B[i];
}
return 0;
}
int vector_multiply_s(double* const A, const size_t length, const double scalar, const size_t incr) {
size_t i = 0;
if (A == NULL) return ERR_NUL;
if (incr == 0) return ERR_ZERO;
for(;i < length;i+=incr){
A[i] *= scalar;
}
return 0;
}
int vector3_cross(const double* const A, const double* const B, double** const R, size_t* const length) {
if (A == NULL || B == NULL || R == NULL || length == NULL) return ERR_NUL;
REALLOCATE(*R, *length, 3);
(*R)[0] = A[1] * B[2] - A[2] * B[1];
(*R)[1] = -(A[0] * B[2] - A[2] * B[0]);
(*R)[2] = A[0] * B[1] - A[1] * B[0];
return 0;
}
int vector_normalize(double* const A, const size_t length){
size_t i = 0;
double magnitude = 0;
if (A == NULL) return ERR_NUL;
magnitude = vector_length(A,length);
for (;i<length;i++){
A[i] /= magnitude;
}
return 0;
}
double vector_dotProduct(const double* const A, const double* const B, const size_t length) {
size_t i = 0;
double result = 0;
if (A == NULL || B == NULL) {
return 0;
}
for (; i < length; i++) {
result += A[i] * B[i];
}
return result;
}
double vector_distance(const double* const A, const double* const B, const size_t length) {
size_t i = 0;
double result = 0;
if (A == NULL || B == NULL) {
return 0;
}
for (; i < length; i++) {
result += (A[i] - B[i]) * (A[i] - B[i]);
}
return sqrt(result);
}
int vector_swapEntrys(double* const A, const size_t Alength, const size_t x, const size_t y, const size_t length, const size_t incr) {
size_t i = 0;
double temp = 0;
if (A == NULL) return ERR_NUL;
if (incr == 0) return ERR_ZERO;
if (x == y || x + length > Alength || y + length > Alength) return ERR_PARA;
for (; i < length; i+=incr) {
temp = A[i+x];
A[i+x] = A[i+y];
A[i+x] = temp;
}
return 0;
}
int vector_setList(double* const A, const size_t Alength, const char* const list) {
size_t i = 0;
size_t j = 0;
double num = 0;
bool neg = 0;
if (A == NULL || list == NULL) return ERR_NUL;
while (list[i] != '\0') {
if (!((list[i] >= '0' && list[i] <= '9') || list[i] == ',' || list[i] == '-')) return ERR_COMP;
if (list[i] == '-') {
neg = true;
i++;
}
if (list[i] == ',') {
if (j > Alength) return ERR_COMP;
if (neg == true) A[j] = -num;
else A[j] = num;
num = 0;
neg = false;
j++;
i++;
continue;
}
num = num * 10;
num += (double)list[i] - (double)'0';
i++;
}
return 0;
}
int vector_setEqualArray(double** const A, size_t* const Alength, const double* const B, const size_t Blength, const size_t length) {
size_t i = 0;
if (A == NULL || B == NULL) return ERR_NUL;
if (length > Blength) return ERR_PARA;
REALLOCATE(*A, *Alength, Blength);
for (; i < length; i++) {
(*A)[i] = B[i];
}
return 0;
}
int vector_setEntrysEqual(const double* const A, const size_t Alength, double* const R, const size_t Rlength, const size_t length, const size_t incr) {
size_t i = 0;
if (A == NULL || R == NULL) return ERR_NUL;
if (length > Alength || length > Rlength) return ERR_PARA;
if (incr == 0) return ERR_ZERO;
for (; i < length; i+=incr) {
R[i] = A[i];
}
return 0;
}
int vector_setAll(double* const A, const size_t length, const double val, const size_t incr) {
size_t i = 0;
if (A == NULL) return ERR_NUL;
if (incr == 0) return ERR_ZERO;
for (; i < length; i+=incr) {
A[i] = val;
}
return 0;
}
int vector_getEntrys(const double* const A, const size_t Alength, double** R, size_t* const Rsize, const size_t length, const size_t incr) {
size_t i = 0;
size_t j = 0;
if (A == NULL || R == NULL || Rsize == NULL) return ERR_NUL;
if (length > Alength) return ERR_PARA;
if (incr == 0) return ERR_ZERO;
REALLOCATE(*R, *Rsize, length);
for (; i < length; i+=incr, j++) {
(*R)[j] = A[i];
}
return 0;
}
double vector_retMax(const double* const A, const size_t length, const size_t incr) {
size_t i = 0;
double max = 0;
if (A == NULL) {
return 0;
}
if (incr == 0) {
return 0;
}
max = A[0];
for (; i < length; i+=incr) {
if (max < A[i]) max = A[i];
}
return max;
}
double vector_retMin(const double* const A, const size_t length, const size_t incr) {
size_t i = 0;
double min = 0;
if (A == NULL) {
return 0;
}
if (incr == 0) {
return 0;
}
min = A[0];
for (; i < length; i+=incr) {
if (min > A[i]) min = A[i];
}
return min;
}
bool vector_isEqual(const double* const A, const size_t Alength, const double* const B, const size_t Blength) {
size_t i = 0;
if (A == NULL || B == NULL) return -ERR_NUL;
if (Alength != Blength) return false;
for (; i < Alength; i++) {
if (A[i] != B[i]) return false;
}
return true;
}
bool vector_isZero(const double* const A, const size_t length) {
size_t i = 0;
if (A == NULL) return -ERR_NUL;
if (length == 0) return -ERR_ZERO;
for (; i < length; i++) {
if (A[i] != 0) return false;
}
return true;
}