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.
30 Matrix
createMatrix(int w
, int h
)
32 Matrix matrix
= (Matrix
)malloc(sizeof(matrix_t
));
36 fprintf(stderr
, "Fatal Error (%s/%d): Could not allocate memory for Matrix!\n", __FILE__
, __LINE__
);
42 matrix
->Data
= (int *)malloc(w
*h
*sizeof(int));
48 Matrix
createIdentityMatrix(int size
)
55 matrix
= createMatrix(size
, size
);
59 matrix
->Data
[i
] = (i
/ size
== i
% size
) ? 1 : 0;
66 void deleteMatrix(Matrix matrix
)
78 Matrix
readMatrix(FILE *stream
)
85 if (fscanf(stream
, "%d %d", &h
, &w
)<2)
88 matrix
= createMatrix(w
,h
);
92 if (fscanf(stream
, "%d", &(matrix
->Data
[i
]))<1)
104 void fprintMatrix(FILE *stream
, Matrix 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");
123 inline void printMatrix(Matrix matrix
)
127 fprintMatrix(stdout
, matrix
);
132 void swapMatrixRows(Matrix matrix
, int a
, int b
)
137 assert(a
>=0 && a
<matrix
->Height
);
138 assert(b
>=0 && b
<matrix
->Height
);
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
;
154 void swapMatrixColumns(Matrix matrix
, int a
, int b
)
159 assert(a
>=0 && a
<matrix
->Width
);
160 assert(b
>=0 && b
<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
;
176 void negateMatrixColumn(Matrix matrix
, int col
)
181 assert(col
>=0 && col
<matrix
->Width
);
184 for (i
=0; i
<matrix
->Height
; i
++)
185 matrix
->Data
[i
*w
+col
] *= -1;
190 void combineMatrixColumns(Matrix matrix
, int dest
, int factor
, int src
)
195 assert(dest
>=0 && dest
<matrix
->Width
);
196 assert(src
>=0 && src
<matrix
->Width
);
200 for (i
=0; i
<matrix
->Height
; i
++)
201 matrix
->Data
[i
*w
+dest
] += factor
*matrix
->Data
[i
*w
+src
];
206 Matrix
copyMatrix(Matrix old
)
213 new = createMatrix(old
->Width
, old
->Height
);
217 i
= old
->Width
*old
->Height
;
219 new->Data
[i
] = old
->Data
[i
];