comparison src/c_sources/Vector.c @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1
2 #include "Vector.h"
3
4 /*
5 *
6 *
7 * Ingo Diepholz 08-06-2011
8 *
9 * $Id: Vector.c,v 1.1 2011/06/14 10:40:52 ingo Exp $
10 */
11
12 /*****************************************************************************/
13 /*** PLUS ***/
14 /*****************************************************************************/
15
16 Vector *vec_plus_s(Vector *v, double s) {
17 plus_internal(v, s);
18 return v;
19 }
20
21 Vector *s_plus_vec(double s, Vector *v) {
22 plus_internal(v, s);
23 return v;
24 }
25
26 Vector *vec_plus_vec(int num, Vector *v, ...) {
27
28 int i;
29 Vector *v1 = v;
30 va_list args;
31 va_start (args, v);
32
33 /* get first and second vector */
34
35 for (i=1; i<num; i++)
36 {
37 Vector *v2 = va_arg(args, Vector * );
38 plus_internal_v(v1, v2);
39 }
40
41 va_end (args);
42 return v1;
43 }
44
45 Vector *plus_internal_v(Vector *v1, Vector *v2) {
46
47 long int ii;
48 if (v1==NULL) {
49 v1 = ALLOCATE(Vector);
50 }
51 if (v2==NULL) {
52 return v1;
53 }
54
55 if ((v1->data == NULL) && (v2->data == NULL)) {
56 /* nothing to do */
57 }
58 else if ((v1->data != NULL) && (v2->data == NULL)) {
59 /* nothing to do */
60 }
61 else if ((v1->data == NULL) && (v2->data != NULL)) {
62 /* Copy v2 to v1 */
63 v1 = copyVector(v2);
64 }
65 else {
66 for (ii = v1->size-1; ii >= 0; ii--) v1->data[ii] += v2->data[ii];
67 }
68 return v1;
69 }
70
71 Vector *plus_internal(Vector *v, double s) {
72
73 long int ii;
74 if (v==NULL) {
75 v = ALLOCATE(Vector);
76 }
77 else {
78
79 if (v->data == NULL) {
80 /* nothing to do */
81 }
82 else {
83 for (ii = v->size-1; ii >= 0; ii--) v->data[ii] += s;
84 }
85 }
86 return v;
87 }
88
89
90
91 /*****************************************************************************/
92 /*** MINUS ***/
93 /*****************************************************************************/
94
95 Vector *vec_minus_s(Vector *v, double s) {
96 plus_internal(v, -s);
97 return v;
98 }
99
100 Vector *s_minus_vec(double s, Vector *v) {
101 plus_internal(times_internal(v, -1), s);
102 return v;
103 }
104
105 /*****************************************************************************/
106 /*** TIMES ***/
107 /*****************************************************************************/
108
109 Vector *vec_times_s(Vector *v, double s) {
110 times_internal(v, s);
111 return v;
112 }
113
114 Vector *s_times_vec(double s, Vector *v) {
115 times_internal(v, s);
116 return v;
117 }
118
119 Vector *vec_times_vec(int num, Vector *v, ...) {
120
121 int i;
122 va_list args;
123 /* get first and second vector */
124 Vector *v1 = v;
125 va_start (args, v);
126
127 for (i=1; i<num; i++)
128 {
129 Vector *v2 = va_arg(args, Vector * );
130 times_internal_v(v1, v2);
131 }
132
133 va_end (args);
134 return v1;
135 }
136
137 Vector *times_internal_v(Vector *v1, Vector *v2) {
138
139 long int ii;
140 if (v1==NULL) {
141 v1 = ALLOCATE(Vector);
142 }
143 if (v2==NULL) {
144 return v1;
145 }
146
147 if ((v1->data == NULL) && (v2->data == NULL)) {
148 /* nothing to do */
149 }
150 else if ((v1->data != NULL) && (v2->data == NULL)) {
151 /* nothing to do */
152 }
153 else if ((v1->data == NULL) && (v2->data != NULL)) {
154 /* Copy v2 to v1 */
155 v1 = copyVector(v2);
156 }
157 else {
158 for (ii = v1->size-1; ii >= 0; ii--) v1->data[ii] *= v2->data[ii];
159 }
160 return v1;
161 }
162
163 Vector *times_internal(Vector *v, double s) {
164
165 long int ii;
166 if (v==NULL) {
167 v = ALLOCATE(Vector);
168 }
169 else {
170
171 if (v->data == NULL) {
172 /* nothing to do */
173 }
174 else {
175 for (ii = v->size-1; ii >= 0; ii--) {
176 v->data[ii] *= s;
177 }
178 }
179 }
180 return v;
181 }
182
183 /*****************************************************************************/
184 /*** DIVIDE ***/
185 /*****************************************************************************/
186
187 Vector *vec_div_s(Vector *v, double s) {
188 times_internal(v, 1/s);
189 return v;
190 }
191
192
193 /*****************************************************************************/
194 /*** Arithmetic Operators ***/
195 /*****************************************************************************/
196
197 Vector *sin_of_vec(Vector *v) {
198 long int ii;
199 if (v==NULL) {
200 v = ALLOCATE(Vector);
201 }
202 else {
203
204 if (v->data == NULL) {
205 /* nothing to do */
206 }
207 else {
208 for (ii = v->size-1; ii >= 0; ii--) {
209 v->data[ii] = sin(v->data[ii]);
210 }
211 }
212 }
213 return v;
214 }
215
216 Vector *cos_of_vec(Vector *v) {
217 long int ii;
218 if (v==NULL) {
219 v = ALLOCATE(Vector);
220 }
221 else {
222
223 if (v->data == NULL) {
224 /* nothing to do */
225 }
226 else {
227 for (ii = v->size-1; ii >= 0; ii--) {
228 v->data[ii] = cos(v->data[ii]);
229 }
230 }
231 }
232 return v;
233 }
234
235
236
237 /*****************************************************************************/
238 /*** MISC ***/
239 /*****************************************************************************/
240
241 Vector *copyVector(Vector *in) {
242
243 Vector *out = ALLOCATE(Vector);
244 double *dout = ALLOCATE_ARRAY(double, in->size);
245 double *din = in->data;
246 long int i;
247
248 out->data = dout;
249 out->size = in->size;
250
251 for ( i = out->size-1; i >= 0; i-- )
252 *dout++ = *din++;
253
254 return out;
255 }
256
257 Vector *initVector(double *data, long int size) {
258 Vector *v = ALLOCATE(Vector);
259 v->data = data;
260 v->size = size;
261 return v;
262 }
263
264 char *toString(Vector *t) {
265
266 static char retbuf[300] = "";
267 long int ll;
268
269 if (t==NULL) {
270 return "NULL";
271 }
272
273 retbuf[0] = '\0';
274 strcpy(retbuf, "[");
275 for(ll=0; ll<myMin(10,t->size); ll++) {
276 sprintf(retbuf, "%s%16.15e, ", retbuf, t->data[ll]);
277 }
278 /* Remove last blank */
279 retbuf[strlen(retbuf)-2] = ']';
280 retbuf[strlen(retbuf)-1] = '\0';
281
282 return retbuf;
283 }
284
285 int myMin(long int a, long int b) {
286 return ((a<b) ? a : b);
287 }
288
289 void freeVector(Vector *v) {
290 if (v != NULL) {
291 if (v->data && v->data != NULL)
292 free(v->data);
293 free(v);
294 printMsg(DEBUG, "----------- Free !!!\n");
295 }
296 }
297
298 void printMsg(int level, char *format, ...) {
299 char buffer[400];
300 char lvlMsg[10];
301 va_list args;
302 va_start (args, format);
303 vsprintf (buffer,format, args);
304
305 if ((level!=NONE) && (VERBOSE_LEVEL>=level)) {
306 switch(level){
307 case DEBUG: strcpy(lvlMsg, "DEBUG"); break;
308 case INFO: strcpy(lvlMsg, "INFO"); break;
309 case WARNING: strcpy(lvlMsg, "WARNING"); break;
310 }
311
312 printf("%-9s %s\n", lvlMsg, buffer);
313 }
314 va_end (args);
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334