File: test-sparse.C

package info (click to toggle)
linbox 1.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,940 kB
  • sloc: cpp: 108,392; lisp: 5,469; makefile: 1,345; sh: 1,244; csh: 131; python: 74; perl: 2
file content (222 lines) | stat: -rw-r--r-- 6,207 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* tests/test-sparse.C
 * Copyright (C) 2014 the LinBox group
 *
 * Written by bds <saunders@udel.edu>
 *            Brice Boyer (briceboyer) <boyer.brice@gmail.com>
 *
 * --------------------------------------------------------
 *
 *
 * ========LICENCE========
 * This file is part of the library LinBox.
 *
 * LinBox is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 *
 */

/*! @file  tests/test-sparse.C
 * @ingroup tests
 *
 * @brief no doc
 *
 * @test no doc.
 */

#include "linbox/linbox-config.h"

#include <iostream>
#include <fstream>
#include <sstream>


#include "linbox/util/commentator.h"
#include "linbox/ring/modular.h"
#include "linbox/matrix/sparse-matrix.h"


#include "test-blackbox.h"

using namespace LinBox;

template <class SM, class SM2>
bool buildBySetGetEntry(SM & A, const SM2 &B);

template <class Field, class SMF>
bool testSparseFormat(string format, const SparseMatrix<Field> & S1)
{
	typedef SparseMatrix<Field, SMF> SM;
	bool pass=true;
	string msg = "SparseMatrix<Field, SparseMatrixFormat::" + format + ">";
	commentator().start(msg.c_str(), format.c_str());
	const typename SM::Field& F = S1.field();
	MatrixDomain<typename SM::Field> MD(F);
	SM S2(F,S1.rowdim(),S1.coldim());
	SM S3(F,S1.rowdim(),S1.coldim());
	buildBySetGetEntry(S2, S1);
	buildBySetGetEntry(S3, S2);
	// behaves as a blackbox should
	if ( ! testBlackbox(S2,true) ) {
		msg = format + " FAIL(testBlackbox)";
		commentator().stop(msg.c_str());
		pass = false;
	}
	// equal behaviour when representing same mat
	else if ( ! MD.areEqual(S1,S2) ) { 
		msg = format + " FAIL(areEqual)";
		commentator().stop(msg.c_str());
		pass = false;
	}
	// just a check of S2 getEntry
	else if ( ! MD.areEqual(S3,S2) ) { 
		msg = format + " FAIL(getEntry)";
		commentator().stop(msg.c_str());
		pass = false;
	}
	else {
		msg = format + " pass";
		commentator().stop(msg.c_str());
	}
	return pass;
}

template <class SM, class SM2>
bool buildBySetGetEntry(SM & A, const SM2 &B)
{

	A.resize(B.rowdim(),B.coldim());

	for (size_t i = 0; i < B.rowdim(); ++i)
		for (size_t j = 0; j < B.coldim(); ++j)
		{
			typename SM::Field::Element x = B.getEntry(i,j);
			if (not A.field().isZero(x))
			A.setEntry(i,j,x);
		}
	A.finalize();

	MatrixDomain<typename SM::Field> MD(B.field()) ;

	return MD.areEqual(A,B);
}

int main (int argc, char **argv)
{
	bool pass = true;

	static size_t n = 10;
	static size_t m = 10;
	static integer q = 11;
	static size_t N = m+n;

	static Argument args[] = {
		{ 'm', "-m m", "Set row dimension of test matrices to m.", TYPE_INT,     &m },
		{ 'n', "-n n", "Set column dimension of test matrices to n.", TYPE_INT,     &n },
		{ 'N', "-N N", "Set number of nonzeros in test matrices.", TYPE_INT,     &N },
		{ 'q', "-q Q", "Operate over the \"field\" GF(Q) [1].", TYPE_INTEGER, &q },
		END_OF_ARGUMENTS
	};
	parseArguments (argc, argv, args);

	//typedef	Givaro::Modular<uint32_t> Field;
	typedef	Givaro::Modular<double> Field;

	Field F (q);

	commentator().getMessageClass (INTERNAL_DESCRIPTION).setMaxDepth (5);
	commentator().getMessageClass (INTERNAL_DESCRIPTION).setMaxDetailLevel (Commentator::LEVEL_UNIMPORTANT);

	commentator().start("Sparse matrix black box test suite", "Sparse");
	MatrixDomain<Field> MD(F) ;
	typename Field::RandIter r(F,1);
	srand(0);

	 /*  default case */
	commentator().start("SparseMatrix<Field>", "Field");

	SparseMatrix<Field> S1(F, m, n);
	typename Field::Element x;
	for (size_t k = 0; k < N; ++k)
	{
		size_t i = rand() % m;
		size_t j = rand() % n;
		while (S1.field().isZero(r.random(x)));
		S1.setEntry(i,j,x);
	}
	S1.finalize();

	//if ( testBlackbox(S1, true, N != 0))
	if ( testBlackbox(S1, false, N != 0))
		commentator().stop("SparseMatrix<Field> pass");
	else {
		commentator().stop("SparseMatrix<Field> FAIL");
		pass = false;
	}

	/* other formats */
	pass = pass and 
		testSparseFormat<Field, SparseMatrixFormat::COO>("COO",S1);
	pass = pass and 
		testSparseFormat<Field, SparseMatrixFormat::CSR>("CSR",S1);
	pass = pass and 
		testSparseFormat<Field, SparseMatrixFormat::ELL>("ELL",S1);
	pass = pass and 
		testSparseFormat<Field, SparseMatrixFormat::ELL_R>("ELL_R",S1);
	pass = pass and 
		testSparseFormat<Field, SparseMatrixFormat::TPL>("TPL",S1);
	pass = pass and 
		testSparseFormat<Field, SparseMatrixFormat::SparseSeq>("SparseSeq",S1);
	pass = pass and 
		testSparseFormat<Field, SparseMatrixFormat::SparsePar>("SparsePar",S1);
	pass = pass and 
		testSparseFormat<Field, SparseMatrixFormat::SparseMap>("SparseMap",S1);
#if 0 // doesn't compile
	commentator().start("SparseMatrix<Field, SparseMatrixFormat::HYB>", "HYB");
	SparseMatrix<Field, SparseMatrixFormat::HYB> S6(F, m, n);
	buildBySetGetEntry(S6, S1);
	S6.optimise();
	if ( testBlackbox(S6,false)  && MD.areEqual(S1,S6))
		commentator().stop("Format HYB pass");
	else {
		commentator().stop("Format HYB FAIL");
		pass = false;
	}
#endif

	{ /*  Default OLD */
		commentator().start("SparseMatrix<Field>", "Field");
		Protected::SparseMatrixGeneric<Field> S11(F, m, n);
		buildBySetGetEntry(S11, S1);
		if ( testBlackbox(S11,true)  && MD.areEqual(S1,S11))
			commentator().stop("SparseMatrix<Field> pass");
		else {
			commentator().stop("SparseMatrix<Field> FAIL");
			pass = false;
		}
	}

	commentator().stop( MSG_STATUS(pass), "Sparse matrix black box test suite pass");


	return pass ? 0 : -1;
}

// Local Variables:
// mode: C++
// tab-width: 4
// indent-tabs-mode: nil
// c-basic-offset: 4
// End:
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s