1 //==============================================================================================
2 //
3 //	This file is part of LiDIA --- a library for computational number theory
4 //
5 //	Copyright (c) 1994--2001 the LiDIA Group.  All rights reserved.
6 //
7 //	See http://www.informatik.tu-darmstadt.de/TI/LiDIA/
8 //
9 //----------------------------------------------------------------------------------------------
10 //
11 //	$Id$
12 //
13 //	Author	: Patrick Theobald (PT)
14 //	Changes	: See CVS log
15 //
16 //==============================================================================================
17 
18 
19 #include	<fstream>
20 #include	"LiDIA/base_matrix.h"
21 
22 #ifndef MATRIX
23 # if defined SPARSE
24 #  define MATRIX sparse_base_matrix
25 # elif defined DENSE
26 #  define MATRIX dense_base_matrix
27 # else
28 #  define MATRIX base_matrix
29 # endif
30 #endif
31 
32 #ifndef TYPE
33 # define TYPE bigint
34 # define INCLUDE "LiDIA/bigint.h"
35 #endif
36 
37 #define IN_NAME argv[1]
38 #define OUT_NAME argv[2]
39 
40 
41 #include	INCLUDE
42 
43 
44 
45 #ifdef LIDIA_NAMESPACE
46 using namespace LiDIA;
47 #endif
48 
49 
50 
main_LiDIA(int argc,char ** argv)51 int main_LiDIA(int argc, char** argv)
52 {
53 	if (argc != 3) {
54 		std::cerr << "usage: " << argv[0] << " input-file output-file" << std::endl;
55 		return 4;
56 	}
57 
58 	std::ifstream in(IN_NAME);
59 	std::ofstream dz(OUT_NAME);
60 
61 	register int i, j;
62 
63 	// pointer
64 	TYPE *pointer_1 = NULL;
65 	TYPE *pointer_2 = NULL;
66 	TYPE *pointer_3 = new TYPE[4];
67 	TYPE *pointer_4 = new TYPE[6];
68 
69 	// vectors
70 	base_vector< TYPE > v1, v2;
71 	base_vector< TYPE > v3(4, 4);
72 	base_vector< TYPE > v4(6, 6);
73 
74 	// scalar
75 	TYPE s1, s2, s3, skalar_1;
76 	in >> s2 >> s3;
77 	lidia_size_t c, r;
78 
79 	// array
80 	TYPE **wertearray = new TYPE *[2];
81 	for (i = 0; i < 2; i++) {
82 		wertearray[i] = new TYPE[3];
83 		for (j = 0; j < 3; j++) {
84 			in >> wertearray[i][j];
85 			//std::cout << wertearray[i][j] << std::flush;
86 		}
87 	}
88 
89 	std::cout << "**********************************************************" << std::endl;
90 	std::cout << "***             Test for class base_matrix             ***" << std::endl;
91 	std::cout << "***                     Version 3.0                    ***" << std::endl;
92 	std::cout << "**********************************************************" << std::endl;
93 	std::cout.flush();
94 
95 	std::cout << std::endl << "testing constructors" << std::flush;
96 	dz << "testing constructor" << std::endl << std::endl;
97 
98 	MATRIX< TYPE > A;
99 	A.set_zero_element(0);
100 	MATRIX< TYPE > B(2, 3);
101 	B.set_zero_element(0);
102 	MATRIX< TYPE > C(2, 3, const_cast<const TYPE **>(wertearray));
103 	C.set_zero_element(0);
104 	MATRIX< TYPE > D(C);
105 	D.set_zero_element(0);
106 	MATRIX< TYPE > E = C;
107 	E.set_zero_element(0);
108 	MATRIX< TYPE > F = C;
109 	F.set_zero_element(0);
110 	MATRIX< TYPE > G(v3);
111 	G.set_zero_element(0);
112 
113 	dz << A << B << C << D << E << F << G << std::flush;
114 	std::cout << ".............................completed" << std::endl;
115 
116 	A.read_from_stream(in);
117 	B.read_from_stream(in);
118 	C.read_from_stream(in);
119 
120 	std::cout << "testing set_print_mode" << std::flush;
121 	dz << "testing set_print_mode" << std::endl;
122 	dz << " set A BEAUTY_MODE" << std::endl;
123 	A.set_print_mode(BEAUTY_MODE);
124 	dz << " set B LIDIA_MODE" << std::endl;
125 	B.set_print_mode(LIDIA_MODE);
126 	dz << " set C GP_MODE" << std::endl;
127 	C.set_print_mode(GP_MODE);
128 	dz << " set D MAPLE_MODE" << std::endl;
129 	D.set_print_mode(MAPLE_MODE);
130 	dz << " set E MATHEMATICA_MODE" << std::endl;
131 	E.set_print_mode(MATHEMATICA_MODE);
132 	dz << " set F KASH_MODE" << std::endl;
133 	F.set_print_mode(KASH_MODE);
134 	dz << " set G LATEX_MODE" << std::endl;
135 	G.set_print_mode(LATEX_MODE);
136 	std::cout << "...........................completed" << std::endl;
137 
138 	std::cout << "testing get_print_mode" << std::flush;
139 	dz << "testing get_print_mode" << std::endl;
140 	dz << A.get_print_mode() << std::endl;
141 	dz << B.get_print_mode() << std::endl;
142 	dz << C.get_print_mode() << std::endl;
143 	dz << D.get_print_mode() << std::endl;
144 	dz << E.get_print_mode() << std::endl;
145 	dz << F.get_print_mode() << std::endl;
146 	dz << G.get_print_mode() << std::endl;
147 	std::cout << "...........................completed" << std::endl;
148 
149 	dz << D << std::endl;
150 	std::cout << "testing write_to_lidia" << std::flush;
151 	dz << "testing write_to_lidia" << std::endl;
152 	std::ofstream out0("LIDIA_I");
153 	D.write_to_stream(out0);
154 	out0.close();
155 	std::cout << "...........................completed" << std::endl;
156 
157 	std::cout << "testing write_to_stream" << std::flush;
158 	dz << "testing write_to_stream" << std::endl;
159 	std::ofstream out1("LIDIA_II");
160 	D.write_to_stream(out1);
161 	out1.close();
162 	std::cout << "..........................completed" << std::endl;
163 
164 	std::cout << "testing write_to_gp" << std::flush;
165 	dz << "testing write_to_gp" << std::endl;
166 	std::ofstream out2("GP");
167 	D.write_to_gp(out2);
168 	out2.close();
169 	std::cout << "..............................completed" << std::endl;
170 
171 	std::cout << "testing write_to_maple" << std::flush;
172 	dz << "testing write_to_maple" << std::endl;
173 	std::ofstream out3("MAPLE");
174 	D.write_to_maple(out3);
175 	out3.close();
176 	std::cout << "...........................completed" << std::endl;
177 
178 	std::cout << "testing write_to_mathematica" << std::flush;
179 	dz << "testing write_to_mathematica" << std::endl;
180 	std::ofstream out4("MATHEMATICA");
181 	D.write_to_mathematica(out4);
182 	out4.close();
183 	std::cout << ".....................completed" << std::endl;
184 
185 	std::cout << "testing write_to_kash" << std::flush;
186 	dz << "testing write_to_kash" << std::endl;
187 	std::ofstream out5("KASH");
188 	D.write_to_kash(out5);
189 	out5.close();
190 	std::cout << "............................completed" << std::endl;
191 
192 	A.set_print_mode(BEAUTY_MODE);
193 	B.set_print_mode(BEAUTY_MODE);
194 	C.set_print_mode(BEAUTY_MODE);
195 	D.set_print_mode(BEAUTY_MODE);
196 	E.set_print_mode(BEAUTY_MODE);
197 	F.set_print_mode(BEAUTY_MODE);
198 	G.set_print_mode(BEAUTY_MODE);
199 
200 	std::cout << "testing read_from_lidia" << std::flush;
201 	dz << "testing read_from_lidia" << std::endl;
202 	std::ifstream in0("LIDIA_I");
203 	D.read_from_lidia(in0);
204 	dz << D << std::flush;
205 	in0.close();
206 	std::cout << "..........................completed" << std::endl;
207 
208 	std::cout << "testing read_from_stream" << std::flush;
209 	dz << "testing read_from_stream" << std::endl;
210 	std::ifstream in1("LIDIA_II");
211 	D.read_from_stream(in1);
212 	dz << D << std::flush;
213 	in1.close();
214 	std::cout << ".........................completed" << std::endl;
215 
216 	std::cout << "testing read_from_gp" << std::flush;
217 	dz << "testing read_from_gp" << std::endl;
218 	std::ifstream in2("GP");
219 	D.read_from_gp(in2);
220 	dz << D << std::flush;
221 	in2.close();
222 	std::cout << ".............................completed" << std::endl;
223 
224 	std::cout << "testing read_from_maple" << std::flush;
225 	dz << "testing read_from_maple" << std::endl;
226 	std::ifstream in3("MAPLE");
227 	D.read_from_maple(in3);
228 	dz << D << std::flush;
229 	in3.close();
230 	std::cout << "..........................completed" << std::endl;
231 
232 	std::cout << "testing read_from_mathematica" << std::flush;
233 	dz << "testing read_from_mathematica" << std::endl;
234 	std::ifstream in4("MATHEMATICA");
235 	D.read_from_mathematica(in4);
236 	dz << D << std::flush;
237 	in4.close();
238 	std::cout << "....................completed" << std::endl;
239 
240 	std::cout << "testing read_from_kash" << std::flush;
241 	dz << "testing read_from_kash" << std::endl;
242 	std::ifstream in5("KASH");
243 	D.read_from_kash(in5);
244 	dz << D << std::flush;
245 	in5.close();
246 	std::cout << "...........................completed" << std::endl;
247 
248 	dz << A << std::endl;
249 
250 	std::cout << "testing member" << std::flush;
251 	dz << "testing member" << std::endl;
252 	skalar_1 = A.member(2, 1);
253 	dz << skalar_1 << std::endl;
254 	skalar_1 = A(2, 1);
255 	dz << skalar_1 << std::endl;
256 	std::cout << "...................................completed" << std::endl;
257 
258 	std::cout << "testing column" << std::flush;
259 	dz << "testing column" << std::endl;
260 	v1 = A(0);
261 	dz << v1 << std::endl;
262 	pointer_1 = A.get_column(1);
263 	for (i = 0; i < 4; i++)
264 		dz << pointer_1[i] << " " << std::flush;
265 	dz << std::endl;
266 	A.get_column(pointer_1, 2);
267 	for (i = 0; i < 4; i++)
268 		dz << pointer_1[i] << " " << std::flush;
269 	dz << std::endl;
270 	A.get_column_vector(v1, 2);
271 	dz << v1 << std::endl;
272 	v1 = A.get_column_vector(1);
273 	dz << v1 << std::endl;
274 	std::cout << "...................................completed" << std::endl;
275 
276 	std::cout << "testing row" << std::flush;
277 	dz << "testing row" << std::endl;
278 	v2 = A[0];
279 	dz << v2 << std::endl;
280 	dz << std::endl;
281 	pointer_2 = A.get_row(1);
282 	for (i = 0; i < 6; i++)
283 		dz << pointer_2[i] << " " << std::flush;
284 	dz << std::endl;
285 	A.get_row(pointer_2, 2);
286 	for (i = 0; i < 6; i++)
287 		dz << pointer_2[i] << " " << std::flush;
288 	dz << std::endl;
289 	A.get_row_vector(v2, 2);
290 	dz << v2 << std::endl;
291 	v2 = A.get_row_vector(1);
292 	dz << v2 << std::endl;
293 	std::cout << "......................................completed" << std::endl;
294 
295 	std::cout << "testing sto" << std::flush;
296 	E = A;
297 	dz << "testing sto" << std::endl;
298 	E.sto(2, 0, s2);
299 	dz << E << std::flush;
300 	std::cout << "......................................completed" << std::endl;
301 
302 	std::cout << "testing sto_column" << std::flush;
303 	dz << "testing sto_column" << std::endl;
304 	E = A;
305 	E.sto_column(pointer_1, 3, 0);
306 	dz << E << std::flush;
307 	E.sto_column(pointer_1, 2, 1, 1);
308 	dz << E << std::flush;
309 	E.sto_column_vector(v1, 3, 2);
310 	dz << E << std::flush;
311 	E.sto_column_vector(v1, 2, 3, 1);
312 	dz << E << std::flush;
313 	std::cout << "...............................completed" << std::endl;
314 
315 	std::cout << "testing sto_row" << std::flush;
316 	dz << "testing sto_row" << std::endl;
317 	E = A;
318 	E.sto_row(pointer_2, 4, 0);
319 	dz << E << std::flush;
320 	E.sto_row(pointer_2, 3, 1, 1);
321 	dz << E << std::flush;
322 	E.sto_row_vector(v2, 4, 2);
323 	dz << E << std::flush;
324 	E.sto_row_vector(v2, 3, 3, 1);
325 	dz << E << std::flush;
326 	std::cout << "..................................completed" << std::endl;
327 
328 	std::cout << "testing get_data" << std::flush;
329 	dz << "testing get_data" << std::endl;
330 	r = E.get_no_of_rows();
331 	c = E.get_no_of_columns();
332 	TYPE **value = E.get_data();
333 	for (i = 0; i < r; i++)
334 		for (j = 0; j < c; j++)
335 			dz << value[i][j] << " ";
336 	dz << std::endl;
337 	std::cout << ".................................completed" << std::endl;
338 
339 	std::cout << "testing set_data" << std::flush;
340 	dz << "testing set_data" << std::endl;
341 	E = A;
342 	E.set_data(const_cast<const TYPE **>(value), r, c);
343 	dz << E << std::endl;
344 	for (i = 0; i < r; i++)
345 		delete[] value[i];
346 	delete[] value;
347 	std::cout << ".................................completed" << std::endl;
348 
349 	std::cout << "testing swap" << std::flush;
350 	dz << "testing swap" << std::endl;
351 	dz << E << A << std::flush;
352 	swap(E, A);
353 	dz << E << A << std::flush;
354 	swap(E, A);
355 	std::cout << ".....................................completed" << std::endl;
356 
357 	std::cout << "testing swap_columns" << std::flush;
358 	dz << "testing swap_columns" << std::endl;
359 	E = A;
360 	E.swap_columns(0, 5);
361 	dz << E << std::flush;
362 	std::cout << ".............................completed" << std::endl;
363 
364 	std::cout << "testing swap_rows" << std::flush;
365 	dz << "testing swap_rows" << std::endl;
366 	E = A;
367 	E.swap_rows(3, 0);
368 	dz << E << std::flush;
369 	std::cout << "................................completed" << std::endl;
370 
371 	std::cout << "testing split_t" << std::flush;
372 	dz << "testing split_t" << std::endl;
373 	MATRIX< TYPE > Part1(2, 4), Part2(3, 2), Part3(2, 4), Part4(1, 2);
374 	A.split_t(Part1, Part2, Part3, Part4);
375 	dz << Part1 << Part2 << Part3 << Part4 << std::flush;
376 	std::cout << "..................................completed" << std::endl;
377 
378 	std::cout << "testing split_h" << std::flush;
379 	dz << "testing split_h" << std::endl;
380 	MATRIX< TYPE > Part5(4, 4), Part6(4, 2);
381 	MATRIX< TYPE > PartA(4, 5), PartB(4, 5), PartC(4, 5), PartD(4, 5);
382 	A.split_h(Part5, Part6);
383 	dz << Part5 << Part6 << std::flush;
384 
385 	A.split_h(pointer_1, PartA);
386 	dz << PartA;
387 	for (i = 0; i < 4; i++)
388 		dz << pointer_1[i] << " ";
389 	dz << std::endl;
390 	A.split_h(v1, PartB);
391 	dz << PartB << v1 << std::endl;
392 
393 	A.split_h(PartC, pointer_3);
394 	dz << PartC;
395 	for (i = 0; i < 4; i++)
396 		dz << pointer_3[i] << " ";
397 	dz << std::endl;
398 	A.split_h(PartD, v3);
399 	dz << PartD << v3 << std::endl;
400 	std::cout << "..................................completed" << std::endl;
401 
402 	std::cout << "testing split_v" << std::flush;
403 	dz << "testing split_v" << std::endl;
404 	MATRIX< TYPE > Part7(1, 6), Part8(3, 6);
405 	MATRIX< TYPE > PartE(3, 6), PartF(3, 6), PartG(3, 6), PartH(3, 6);
406 	A.split_v(Part7, Part8);
407 	dz << Part7 << Part8 << std::flush;
408 
409 	A.split_v(pointer_2, PartE);
410 	dz << PartE;
411 	for (i = 0; i < 6; i++)
412 		dz << pointer_2[i] << " ";
413 	dz << std::endl;
414 	A.split_v(v2, PartF);
415 	dz << PartF << v2 << std::endl;
416 
417 	A.split_v(PartG, pointer_4);
418 	dz << PartG;
419 	for (i = 0; i < 6; i++)
420 		dz << pointer_4[i] << " ";
421 	dz << std::endl;
422 	A.split_v(PartH, v4);
423 	dz << PartH << v4 << std::endl;
424 	std::cout << "..................................completed" << std::endl;
425 
426 	std::cout << "testing compose_t" << std::flush;
427 	dz << "testing compose_t" << std::endl;
428 	{
429 		MATRIX< TYPE > TMP(4, 6);
430 		TMP.compose_t(Part1, Part2, Part3, Part4);
431 		dz << TMP << std::flush;
432 	}
433 	std::cout << "................................completed" << std::endl;
434 
435 	std::cout << "testing compose_h" << std::flush;
436 	dz << "testing compose_h" << std::endl;
437 	{
438 		MATRIX< TYPE > TMP(4, 6);
439 		TMP.compose_h(Part5, Part6);
440 		dz << TMP << std::flush;
441 
442 		TMP.compose_h(pointer_1, PartA);
443 		dz << TMP << std::flush;
444 
445 		TMP.compose_h(v1, PartB);
446 		dz << TMP << std::flush;
447 
448 		TMP.compose_h(PartC, pointer_3);
449 		dz << TMP << std::flush;
450 
451 		TMP.compose_h(PartD, v3);
452 		dz << TMP << std::flush;
453 	}
454 	std::cout << "................................completed" << std::endl;
455 
456 	std::cout << "testing compose_v" << std::flush;
457 	dz << "testing compose_v" << std::endl;
458 	{
459 		MATRIX< TYPE > TMP(4, 6);
460 		TMP.compose_v(Part7, Part8);
461 		dz << TMP << std::flush;
462 
463 		TMP.compose_v(pointer_2, PartE);
464 		dz << TMP << std::flush;
465 
466 		TMP.compose_v(v2, PartF);
467 		dz << TMP << std::flush;
468 
469 		TMP.compose_v(PartG, pointer_4);
470 		dz << TMP << std::flush;
471 
472 		TMP.compose_v(PartH, v4);
473 		dz << TMP << std::flush;
474 	}
475 	std::cout << "................................completed" << std::endl;
476 
477 	std::cout << "testing get_no_of_columns" << std::flush;
478 	dz << "testing get_no_of_columns" << std::endl;
479 	skalar_1 = A.get_no_of_columns();
480 	dz << skalar_1 << std::endl;
481 	std::cout << "........................completed" << std::endl;
482 
483 	std::cout << "testing get_no_of_rows" << std::flush;
484 	dz << "testing get_no_of_rows" << std::endl;
485 	skalar_1 = A.get_no_of_rows();
486 	dz << skalar_1 << std::endl;
487 	std::cout << "...........................completed" << std::endl;
488 
489 	std::cout << "testing set_no_of_columns" << std::flush;
490 	dz << "set_no_of_columns" << std::endl << std::flush;
491 	E = A;
492 	E.set_no_of_columns(3);
493 	dz << E << std::flush;
494 	std::cout << "........................completed" << std::endl;
495 
496 	std::cout << "testing set_no_of_rows" << std::flush;
497 	dz << "set_no_of_rows" << std::endl;
498 	E.set_no_of_rows(3);
499 	dz << E << std::flush;
500 	std::cout << "...........................completed" << std::endl;
501 
502 	std::cout << "testing resize" << std::flush;
503 	dz << "resize" << std::endl;
504 	E = A;
505 	E.resize(3, 3);
506 	dz << E << std::flush;
507 	std::cout << "...................................completed" << std::endl;
508 
509 	std::cout << "testing kill / reset" << std::flush;
510 	dz << "kill / reset" << std::endl;
511 	E = A;
512 	E.kill();
513 	dz << E << std::flush;
514 	std::cout << ".............................completed" << std::endl;
515 
516 	std::cout << "testing assign" << std::flush;
517 	dz << "testing assign" << std::endl;
518 	E = A;
519 	dz << E << std::flush;
520 	E.kill();
521 	assign(E, A);
522 	dz << E << std::flush;
523 	std::cout << "...................................completed" << std::endl;
524 
525 	std::cout << "testing trans" << std::flush;
526 	dz << "testing trans" << std::endl;
527 	E = trans(A);
528 	dz << E << std::flush;
529 	E = E.trans();
530 	dz << E << std::flush;
531 	E.trans(E);
532 	dz << E << std::flush;
533 	trans(E, E);
534 	dz << E << std::flush;
535 	std::cout << "....................................completed" << std::endl;
536 
537 	std::cout << "testing diag" << std::flush;
538 	dz << "diag" << std::endl;
539 	E.diag(s1, s2);
540 	dz << E << std::flush;
541 	diag(E, s2, s3);
542 	dz << E << std::flush;
543 	std::cout << ".....................................completed" << std::endl;
544 	G.diag(0, 0);
545 	dz << E << G << std::endl;
546 
547 	std::cout << "testing is_column_zero" << std::flush;
548 	dz << "is_column_zero" << std::endl;
549 	dz << E.is_column_zero(1) << std::endl;
550 	dz << G.is_column_zero(0) << std::endl;
551 	std::cout << "...........................completed" << std::endl;
552 
553 	std::cout << "testing is_row_zero" << std::flush;
554 	dz << "is_row_zero" << std::endl;
555 	dz << E.is_row_zero(1) << std::endl;
556 	dz << G.is_row_zero(0) << std::endl;
557 	std::cout << "..............................completed" << std::endl;
558 
559 	std::cout << "testing is_matrix_zero" << std::flush;
560 	dz << "is_matrix_zero" << std::endl;
561 	dz << E.is_matrix_zero() << std::endl;
562 	dz << G.is_matrix_zero() << std::endl;
563 	std::cout << "...........................completed" << std::endl;
564 
565 	dz.close();
566 	std::cout << "\nPlease use now the 'diff' or 'cmp' command to verify" << std::endl;
567 	std::cout << "the equality of {dense_, sparse_}base_matrix_appl_< type > .dat";
568 	std::cout << " and {dense_, sparse_}base_matrix_appl_< type > .out." << std::endl;
569 
570 	return 0;
571 }
572 
573 
main(int argc,char ** argv)574 int main(int argc, char** argv) {
575 
576 #if defined(LIDIA_EXCEPTIONS)
577     try {
578 #endif
579 
580 	main_LiDIA(argc, argv);
581 
582 #if defined(LIDIA_EXCEPTIONS)
583     }
584     catch(basic_error const& ex) {
585 	ex.traditional_error_handler();
586 	return 1;
587     }
588     catch(std::exception const& ex) {
589 	std::cerr << "unexpected exception: " << ex.what() << "\n";
590 	return 1;
591     }
592 #endif
593 
594 }
595