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