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 "vectorarray.h"
30 VectorArray
createVectorArray(int var
)
32 VectorArray array
= (VectorArray
)malloc(sizeof(vectorarray_t
));
36 fprintf(stderr
, "Fatal Error (%s/%d): Could not allocate memory for VectorArray!\n", __FILE__
, __LINE__
);
40 array
->Variables
= var
;
44 array
->Properties
= createVariableProperties(var
);
51 void deleteVectorArray(VectorArray array
)
57 for (i
=0; i
<array
->Size
; i
++)
58 deleteVector(array
->Data
[i
]);
60 deleteVariableProperties(array
->Properties
);
69 void fprintVectorArray(FILE *stream
, VectorArray array
, bool header
)
78 fprintf(stream
, "%d %d\n\n", array
->Size
, array
->Variables
);
79 fprintVariableProperties(stream
, array
->Properties
, array
->Variables
);
83 for (i
=0; i
<array
->Size
; i
++)
85 fprintVector(stream
, array
->Data
[i
], array
->Variables
);
92 inline void printVectorArray(VectorArray array
, bool header
)
96 fprintVectorArray(stdout
, array
, header
);
101 void appendToVectorArray(VectorArray array
, Vector vector
)
107 if (array
->Size
>array
->Memory
)
109 array
->Memory
= 2*(array
->Memory
+1);
110 array
->Data
= (Vector
*)realloc(array
->Data
, array
->Memory
* sizeof(Vector
));
112 array
->Data
[array
->Size
-1] = vector
;
117 void swapVectorArrayRows(VectorArray array
, int a
, int b
)
122 assert(a
>=0 && a
<array
->Size
);
123 assert(b
>=0 && b
<array
->Size
);
125 vector
= array
->Data
[a
];
126 array
->Data
[a
] = array
->Data
[b
];
127 array
->Data
[b
] = vector
;
132 void swapVectorArrayColumns(VectorArray array
, int a
, int b
)
137 assert(a
>=0 && a
<array
->Variables
);
138 assert(b
>=0 && b
<array
->Variables
);
140 for (i
=0; i
<array
->Size
; i
++)
141 swapVector(array
->Data
[i
], a
, b
);
142 swapVariableProperties(array
->Properties
, a
, b
);
147 void appendVectorArrayNegatives(VectorArray array
)
154 vec
= createVector(array
->Variables
);
155 for (i
=array
->Size
-1; i
>=0; i
--)
157 for (j
=0; j
<array
->Variables
; j
++)
158 vec
[j
] = -array
->Data
[i
][j
];
159 appendToVectorArray(array
, copyVector(vec
, array
->Variables
));
166 void sortVectorArrayColumns(VectorArray array
, ColumnCompare cmp
)
169 int bestvalue
, bestindex
;
174 for (i
=0; i
<array
->Variables
-1; i
++)
177 bestvalue
= array
->Properties
[i
].Column
;
178 for (j
=i
+1; j
<array
->Variables
; j
++)
180 if (cmp(array
->Properties
[j
].Column
, bestvalue
) < 0)
183 bestvalue
= array
->Properties
[j
].Column
;
186 swapVectorArrayColumns(array
, i
, bestindex
);
192 VectorArray
readVectorArray(FILE *stream
, bool header
)
198 if (fscanf(stream
, "%d %d", &size
, &vars
)!=2)
201 array
= createVectorArray(vars
);
204 readVariableProperties(stream
, array
->Properties
, vars
);
208 vector
= readVector(stream
, vars
);
210 appendToVectorArray(array
, vector
);
213 deleteVectorArray(array
);