summate.c: sum_base: check equality constraints in Param_Polyhedron
[barvinok.git] / zsolve / vector.c
blob06c9b08fb774bdd3c70b7cd6c654d79537029f42
1 /*
2 4ti2 -- A software package for algebraic, geometric and combinatorial
3 problems on linear spaces.
5 Copyright (C) 2006 4ti2 team.
6 Main author(s): Matthias Walter.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "vector.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include "defs.h"
29 // //
31 Vector createVector(int size)
33 Vector vector = (Vector)malloc(size*sizeof(vector_t));
35 if (vector==NULL)
37 fprintf(stderr, "Fatal Error (%s/%d): Could not allocate memory for Vector!\n", __FILE__, __LINE__);
38 exit(1);
41 return vector;
43 // //
45 Vector createZeroVector(int size)
47 Vector vector = (Vector)calloc(size, sizeof(vector_t));
49 if (vector==NULL)
51 fprintf(stderr, "Fatal Error (%s/%d): Could not allocate memory for Vector!\n", __FILE__, __LINE__);
52 exit(1);
55 return vector;
58 // //
60 inline void deleteVector(Vector vector)
62 free(vector);
65 // //
67 Vector copyVector(Vector old, int size)
69 Vector new = (Vector)malloc(size*sizeof(vector_t));
71 if (new==NULL)
73 fprintf(stderr, "Fatal Error (%s/%d): Could not allocate memory for Vector!\n", __FILE__, __LINE__);
74 exit(1);
77 while (size--)
78 new[size] = old[size];
80 return new;
83 // //
85 void fprintVector(FILE *stream, Vector vector, int size)
87 int i;
89 assert(stream);
90 assert(vector);
92 fprintf(stream, "%2d", vector[0]);
93 for (i=1; i<size; i++)
94 fprintf(stream, " %2d", vector[i]);
97 // //
99 inline void printVector(Vector vector, int size)
101 assert(vector);
103 fprintVector(stdout, vector, size);
106 // //
108 Vector readVector(FILE *stream, int size)
110 int i;
111 Vector vector;
113 assert(stream);
115 vector = createVector(size);
116 assert(vector);
118 for (i=0; i<size; i++)
120 if (fscanf(stream, "%d", &(vector[i]))<1)
122 deleteVector(vector);
123 return NULL;
127 return vector;
130 // //
132 int normVector(Vector vector, int size)
134 int norm = 0;
136 assert(vector);
138 if (size>=0)
139 while (size--)
140 norm += abs(vector[size]);
142 return norm;
145 // //
147 void swapVector(Vector vector, int a, int b)
149 int tmp;
151 assert(vector);
153 tmp = vector[a];
154 vector[a] = vector[b];
155 vector[b] = tmp;
158 // //
160 int gcdVector(Vector vector, int size)
162 int i,result;
164 if (size==0)
165 return 0;
167 result = vector[0];
168 for (i=1; i<size; i++)
169 result = gcd(result, vector[i]);
171 return result;
174 // //
176 int lexCompareInverseVector(Vector vector, int size) {
177 int i = 0;
178 while (i<size && vector[i]==0)
179 i++;
180 return i==size ? 0 : vector[i];
183 // //