summate.c: sum_base: check equality constraints in Param_Polyhedron
[barvinok.git] / zsolve / matrix.c
blob395fdf4336d89d7b3e70a024085c621138b7be53
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 "matrix.h"
25 #include <assert.h>
26 #include <stdlib.h>
28 // //
30 Matrix createMatrix(int w, int h)
32 Matrix matrix = (Matrix)malloc(sizeof(matrix_t));
34 if (matrix==NULL)
36 fprintf(stderr, "Fatal Error (%s/%d): Could not allocate memory for Matrix!\n", __FILE__, __LINE__);
37 exit(1);
40 matrix->Width = w;
41 matrix->Height = h;
42 matrix->Data = (int *)malloc(w*h*sizeof(int));
44 return matrix;
46 // //
48 Matrix createIdentityMatrix(int size)
50 int i;
51 Matrix matrix;
53 assert(size>0);
55 matrix = createMatrix(size, size);
57 i = size * size;
58 while (i--)
59 matrix->Data[i] = (i / size == i % size) ? 1 : 0;
61 return matrix;
64 // //
66 void deleteMatrix(Matrix matrix)
68 if (matrix)
70 if (matrix->Data)
71 free(matrix->Data);
72 free(matrix);
76 // //
78 Matrix readMatrix(FILE *stream)
80 Matrix matrix = NULL;
81 int i,w,h;
83 assert(stream);
85 if (fscanf(stream, "%d %d", &h, &w)<2)
86 return NULL;
88 matrix = createMatrix(w,h);
89 w *= h;
90 for (i=0; i<w; i++)
92 if (fscanf(stream, "%d", &(matrix->Data[i]))<1)
94 deleteMatrix(matrix);
95 return NULL;
99 return matrix;
102 // //
104 void fprintMatrix(FILE *stream, Matrix matrix)
106 int i,j;
108 assert(stream);
109 assert(matrix);
111 fprintf(stream, "%d %d\n\n", matrix->Height, matrix->Width);
113 for (i=0; i<matrix->Height; i++)
115 for (j=0; j<matrix->Width; j++)
116 fprintf(stream, "%3d ", matrix->Data[j+matrix->Width*i]);
117 fprintf(stream, "\n");
121 // //
123 inline void printMatrix(Matrix matrix)
125 assert(matrix);
127 fprintMatrix(stdout, matrix);
130 // //
132 void swapMatrixRows(Matrix matrix, int a, int b)
134 int i, tmp,w;
136 assert(matrix);
137 assert(a>=0 && a<matrix->Height);
138 assert(b>=0 && b<matrix->Height);
140 if (a==b)
141 return;
143 w = matrix->Width;
144 for (i=0; i<w; i++)
146 tmp = matrix->Data[a*w+i];
147 matrix->Data[a*w+i] = matrix->Data[b*w+i];
148 matrix->Data[b*w+i] = tmp;
152 // //
154 void swapMatrixColumns(Matrix matrix, int a, int b)
156 int i, tmp,w;
158 assert(matrix);
159 assert(a>=0 && a<matrix->Width);
160 assert(b>=0 && b<matrix->Width);
162 if (a==b)
163 return;
165 w = matrix->Width;
166 for (i=0; i<matrix->Height; i++)
168 tmp = matrix->Data[i*w+a];
169 matrix->Data[i*w+a] = matrix->Data[i*w+b];
170 matrix->Data[i*w+b] = tmp;
174 // //
176 void negateMatrixColumn(Matrix matrix, int col)
178 int i,w;
180 assert(matrix);
181 assert(col>=0 && col<matrix->Width);
183 w = matrix->Width;
184 for (i=0; i<matrix->Height; i++)
185 matrix->Data[i*w+col] *= -1;
188 // //
190 void combineMatrixColumns(Matrix matrix, int dest, int factor, int src)
192 int i,w;
194 assert(matrix);
195 assert(dest>=0 && dest<matrix->Width);
196 assert(src>=0 && src<matrix->Width);
197 assert(dest!=src);
199 w = matrix->Width;
200 for (i=0; i<matrix->Height; i++)
201 matrix->Data[i*w+dest] += factor*matrix->Data[i*w+src];
204 // //
206 Matrix copyMatrix(Matrix old)
208 Matrix new;
209 int i;
211 assert(old);
213 new = createMatrix(old->Width, old->Height);
215 assert(new);
217 i = old->Width*old->Height;
218 while (i--)
219 new->Data[i] = old->Data[i];
221 return new;
224 // //