Add openscop_vector_mul_scalar function
[openscop.git] / source / vector.c
blob78da98e828ab7b5b1f8c87c8fde494c9ded36a5c
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** vector.c **
6 **-----------------------------------------------------------------**
7 ** First version: 30/04/2008 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
35 * are met: *
36 * *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
44 * *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55 * *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
64 # include <stdlib.h>
65 # include <stdio.h>
66 # include <ctype.h>
67 # include <openscop/vector.h>
70 /*+***************************************************************************
71 * Structure display function *
72 *****************************************************************************/
75 /**
76 * openscop_vector_print_structure function:
77 * Displays a openscop_vector_t structure (*vector) into a file (file, possibly
78 * stdout) in a way that trends to be understandable without falling in a deep
79 * depression or, for the lucky ones, getting a headache... It includes an
80 * indentation level (level) in order to work with others print_structure
81 * functions.
82 * \param file File where informations are printed.
83 * \param vector The vector whose information have to be printed.
84 * \param level Number of spaces before printing, for each line.
86 void
87 openscop_vector_print_structure(FILE * file, openscop_vector_p vector,
88 int level)
90 int j;
92 if (vector != NULL)
94 // Go to the right level.
95 for (j = 0; j < level; j++)
96 fprintf(file,"|\t");
97 fprintf(file,"+-- openscop_vector_t\n");
99 for (j = 0; j <= level; j++)
100 fprintf(file,"|\t");
101 fprintf(file,"%d\n",vector->size);
103 // Display the vector.
104 for (j = 0; j <= level; j++)
105 fprintf(file,"|\t");
107 fprintf(file,"[ ");
109 for (j = 0; j < vector->size; j++)
111 SCOPINT_print(file,OPENSCOP_FMT,vector->v[j]);
112 fprintf(file," ");
115 fprintf(file,"]\n");
117 else
119 // Go to the right level.
120 for (j = 0; j < level; j++)
121 fprintf(file,"|\t");
122 fprintf(file,"+-- NULL vector\n");
125 // The last line.
126 for (j = 0; j <= level; j++)
127 fprintf(file,"|\t");
128 fprintf(file,"\n");
133 * openscop_vector_print function:
134 * This function prints the content of a openscop_vector_t structure
135 * (*vector) into a file (file, possibly stdout).
136 * \param file File where informations are printed.
137 * \param vector The vector whose information have to be printed.
139 void
140 openscop_vector_print(FILE * file, openscop_vector_p vector)
142 openscop_vector_print_structure(file,vector,0);
146 /*+***************************************************************************
147 * Memory allocation/deallocation function *
148 *****************************************************************************/
152 * openscop_vector_malloc function:
153 * This function allocates the memory space for a openscop_vector_t structure
154 * and sets its fields with default values. Then it returns a pointer to the
155 * allocated space.
156 * \param size The number of entries of the vector to allocate.
157 * \return A pointer to the newly allocated openscop_vector_t structure.
159 openscop_vector_p
160 openscop_vector_malloc(unsigned size)
162 openscop_vector_p vector;
163 openscop_int_t * p;
164 int i ;
166 vector = (openscop_vector_p)malloc(sizeof(openscop_vector_t));
167 if (vector == NULL)
169 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
170 exit(1);
172 vector->size = size;
173 if (size == 0)
174 vector->v = NULL;
175 else
177 p = (openscop_int_t *)malloc(size * sizeof(openscop_int_t));
178 if (p == NULL)
180 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
181 exit(1);
183 vector->v = p;
184 for (i = 0; i < size; i++)
185 SCOPINT_init_set_si(vector->v[i],0);
187 return vector;
192 * openscop_vector_free function:
193 * This function frees the allocated memory for a openscop_vector_t structure.
194 * \param vector The pointer to the vector we want to free.
196 void
197 openscop_vector_free(openscop_vector_p vector)
199 int i;
200 openscop_int_t * p;
202 if (vector != NULL)
204 p = vector->v;
205 for (i = 0; i < vector->size; i++)
206 SCOPINT_clear(*p++);
208 free(vector->v);
209 free(vector);
214 /*+***************************************************************************
215 * Processing functions *
216 *****************************************************************************/
220 * openscop_vector_add_scalar function:
221 * This function adds a scalar to the vector representation of an affine
222 * expression (this means we add the scalar only to the very last entry of the
223 * vector). It returns a new vector resulting from this addition.
224 * \param vector The basis vector.
225 * \param scalar The scalar to add to the vector.
226 * \return A pointer to a new vector, copy of the basis one plus the scalar.
228 openscop_vector_p
229 openscop_vector_add_scalar(openscop_vector_p vector, int scalar)
231 int i;
232 openscop_vector_p result;
234 if ((vector == NULL) || (vector->size < 2))
236 fprintf(stderr,"[OpenScop] Error: incompatible vector for addition.\n");
237 exit(1);
240 result = openscop_vector_malloc(vector->size);
241 for (i = 0; i < vector->size; i++)
242 SCOPINT_assign(result->v[i],vector->v[i]);
243 SCOPINT_add_int(result->v[vector->size - 1],
244 vector->v[vector->size - 1],scalar);
246 return result;
251 * openscop_vector_add function:
252 * This function achieves the addition of two vectors and returns the
253 * result as a new vector (the addition means the ith entry of the new vector
254 * is equal to the ith entry of vector v1 plus the ith entry of vector v2).
255 * \param v1 The first vector for the addition.
256 * \param v2 The second vector for the addition.
257 * \return A pointer to a new vector, corresponding to v1 + v2.
259 openscop_vector_p
260 openscop_vector_add(openscop_vector_p v1, openscop_vector_p v2)
262 int i;
263 openscop_vector_p v3;
265 if ((v1 == NULL) || (v2 == NULL) || (v1->size != v2->size))
267 fprintf(stderr,"[OpenScop] Error: incompatible vectors for addition.\n");
268 exit(1);
271 v3 = openscop_vector_malloc(v1->size);
272 for (i = 0; i < v1->size; i++)
273 SCOPINT_addto(v3->v[i],v1->v[i],v2->v[i]);
275 return v3;
280 * openscop_vector_sub function:
281 * This function achieves the subtraction of two vectors and returns the
282 * result as a new vector (the addition means the ith entry of the new vector
283 * is equal to the ith entry of vector v1 minus the ith entry of vector v2).
284 * \param v1 The first vector for the subtraction.
285 * \param v2 The second vector for the subtraction (result is v1-v2).
286 * \return A pointer to a new vector, corresponding to v1 - v2.
288 openscop_vector_p
289 openscop_vector_sub(openscop_vector_p v1, openscop_vector_p v2)
291 int i;
292 openscop_vector_p v3;
294 if ((v1 == NULL) || (v2 == NULL) || (v1->size != v2->size))
296 fprintf(stderr,"[OpenScop] Error: "
297 "incompatible vectors for subtraction.\n");
298 exit(1);
301 v3 = openscop_vector_malloc(v1->size);
302 for (i = 0; i < v1->size; i++)
303 SCOPINT_subtract(v3->v[i],v1->v[i],v2->v[i]);
305 return v3;
310 * openscop_vector_tag_inequality function:
311 * This function tags a vector representation of a contraint as being an
312 * inequality >=0. This means in the PolyLib format, to set to 1 the very
313 * first entry of the vector. It modifies directly the vector provided as
314 * an argument.
315 * \param vector The vector to be tagged.
317 void
318 openscop_vector_tag_inequality(openscop_vector_p vector)
320 if ((vector == NULL) || (vector->size < 1))
322 fprintf(stderr,"[OpenScop] Error: vector cannot be tagged.\n");
323 exit(1);
325 SCOPINT_set_si(vector->v[0],1);
330 * openscop_vector_tag_equality function:
331 * This function tags a vector representation of a contraint as being an
332 * equality ==0. This means in the PolyLib format, to set to 0 the very
333 * first entry of the vector. It modifies directly the vector provided as
334 * an argument.
335 * \param vector The vector to be tagged.
337 void
338 openscop_vector_tag_equality(openscop_vector_p vector)
340 if ((vector == NULL) || (vector->size < 1))
342 fprintf(stderr,"[OpenScop] Error: vector cannot be tagged.\n");
343 exit(1);
345 SCOPINT_set_si(vector->v[0],0);
350 * openscop_vector_equal function:
351 * this function returns true if the two vectors are the same, false
352 * otherwise.
353 * \param v1 The first vector.
354 * \param v2 The second vector.
355 * \return 1 if v1 and v2 are the same (content-wise), 0 otherwise.
358 openscop_vector_equal(openscop_vector_p v1, openscop_vector_p v2)
360 int i;
362 if (v1->size != v2->size)
363 return 0;
365 for (i = 0; i < v1->size; i++)
366 if (SCOPINT_ne(v1->v[i], v2->v[i]))
367 return 0;
369 return 1;
374 * openscop_vector_mul_scalar function:
375 * this function returns a new vector corresponding to the one provided
376 * as parameter with each entry multiplied by a scalar.
377 * \param v The vector to multiply.
378 * \param scalar The scalar coefficient.
379 * \return A new vector corresponding to scalar * v.
381 openscop_vector_p openscop_vector_mul_scalar(openscop_vector_p v,
382 int scalar)
384 int i;
385 openscop_vector_p result = openscop_vector_malloc(v->size);
387 for(i = 0; i < v->size; i++)
388 SCOPINT_multo(result->v[i], scalar, v->v[i]);
390 return result;