File: samplebb.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 (275 lines) | stat: -rw-r--r-- 6,960 bytes parent folder | download | duplicates (4)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * examples/samplebb.C
 *
 * Copyright (C) 2005, 2010 D Saunders
 * ========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 examples/samplebb.C
 * @example  examples/samplebb.C
 * \ingroup examples
 * \brief generate an example matrix with specified frobenius form.
 *
 * samplebb takes options and any number of argument triples denoting companion
 * matrix blocks.
 * For example, the call "samplebb -r 7 2 3 a3 1 1" generates a sparsely
 * randomized matrix (because of the '-r' option) matrix which is similar
 * (because of the two triples '7 2 3' and 'a2 1 1') to a direct sum of 3
 * companion matrices for (x-7)^2 plus one companion matrix for x^3 + x + 2, the
 * polynomial denoted by 'a3'.
 *
 *  In general, in the first position of each triple 'aK' denotes the polynomial
 *  x^k + x + K-1 and a number n denotes the polynomial x-n.  The second number
 *  in the triple specifies a power of the polynomial and the third specifies how
 *  many companion matrix blocks for that power of that polynomial.
 *
 *  Possible options are
 *  -r lightly randomized similarity transform, matrix remains sparse.
 *  -R fully randomized similarity transform, matrix becomes dense.
 *
 *  The matrix is written to standard out in SMS format (triples).
 *
 *  For some other examples:
 *  "samplebb  1 1 2  2 1 2  4 1 2  12 1 1  0 1 1" is a 8 by 8 diagonal matrix in smith form,
 *  diag(1,1,2,2,4,4,12,0)
 *
 */

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <linbox/blackbox/direct-sum.h>
#include <linbox/blackbox/companion.h>
#include <linbox/algorithms/matrix-hom.h>
#include <linbox/ring/ntl.h>
#include <NTL/ZZX.h>
#include <linbox/vector/blas-vector.h>

using std::string;
using std::list;
using LinBox::Companion;
using LinBox::DirectSum;
using LinBox::DenseMatrix;
using LinBox::NTL_ZZ;
using NTL::ZZX;


void stripOptions(int& acp, char* avp[], string& opts, const int ac, char** av)
{
	acp = 0;
	for (int i = 1; i < ac; ++i)
	{
		//std::cout << av[i] << " ";
		if (av[i][0] == '-') {
			for (const char* j = av[i]+1; *j != 0; ++j)
				opts.push_back(*j);
		}

		else {
			avp[acp] = av[i];
			++acp;
		}
	}
}

template <class List, class Ring>
void augmentBB(List& L, char* code, int e, int k, const Ring& R)
{
	typedef typename Ring::Element Int;
	Int a;
	ZZX p;

	// build poly p

	if ( *code != 'a')  // build linear poly
	{
		R.init(a, -atoi(code));
		p += ZZX(0, a);
		p += ZZX(1, R.one);
	}
	else // build long poly
	{
		int n = atoi(code+1);
		R.init(a, n-1);
		p += ZZX(n, R.one);
		p += ZZX(1, R.one);
		p += ZZX(0, a);
	}

	//std::cout << "(code, e, k) =(" << code << ", " << e << ", " << k << ")" << std::endl;
	//std::cout << "Correspoding poly: " << p << std::endl;
	// compute q =  p^e
	ZZX q(0, R.one);
	for(int i = 0; i < e; ++i) q *= p;
	//std::cout <<"Polynomial: " << q << std::endl;

	LinBox::DenseVector<Ring>  v(R,deg(q)+1);
	for (int i = 0; i < v.size(); ++i) v[i] = coeff(q, i);

	// companion matrix of q
	Companion<Ring>* C = new Companion<Ring>(R, v);
	for(int i = 0; i < k; ++i) L.push_back(C);

}

template < class Ring >
void scramble(DenseMatrix<Ring>& M)
{

	Ring R = M.field();

	int N,n = M.rowdim(); // number of random basic row and col ops.
	N = 2*n;

	for (int k = 0; k < N; ++k) {

		int i = rand()%M.rowdim();

		int j = rand()%M.coldim();

		if (i == j) continue;

		// M*i += alpha M*j and Mj* -= alpha Mi*

		typename Ring::Element alpha, beta, x;
		R.init(alpha, rand()%5 - 2);
		R.neg(beta, alpha);

		for (size_t l = 0; l < M.rowdim(); ++l) if (!R.isZero(alpha)) {
			R.mul(x, alpha, M[l][j]);
			R.addin(M[l][i], x);
		}


		for (size_t l = 0; l < M.rowdim(); ++l) if (!R.isZero(alpha)) {
			R.mul(x, beta, M[i][l]);
			R.addin(M[j][l], x);
		}
	}

	/*
	   std::ofstream out("matrix", std::ios::out);

	//M. write(std::cout);

	out << n << " " << n << "\n";

	for (int i = 0; i < n; ++ i) {

	for ( int j = 0; j < n; ++ j) {

	R. write(out, M[i][j]);

	out << " ";
	}

	out << "\n";

	}
	*/

}

template <class Matrix>
void printMatrix (const Matrix& A)
{
	int m = A. rowdim();
	int n = A. coldim();
	typedef typename Matrix::Field Ring;
	typedef typename Ring::Element Element;
	const Ring &r = A.field();
	LinBox::DenseVector<Ring> x(r,m), y(r,n);

	std::cout << m << " " << n <<  " M" << std::endl;
	typename LinBox::DenseVector<Ring>::iterator y_p;
	for (int i = 0; i < m; ++ i) {
		r. assign (x[i], r.one);
		A. applyTranspose(y, x);
		for(y_p = y. begin(); y_p != y. end(); ++ y_p)
			if (! r.isZero(*y_p))
				std::cout << i+1 << " " << y_p - y.begin() + 1 << " " << *y_p << std::endl;
		r. assign (x[i], r.zero);
	}
	std::cout << "0 0 0" << std::endl;
}


int main(int ac, char* av[])
{
	if (ac < 2)
	{	std::cout << "usage: " << av[0] <<
		" options block-groups." << std::endl;
		std::cout << av[0] << " -r 1 2 3 a4 1 1" << std::endl;
		std::cout <<
		"for lightly randomized matrix similar to direct sum of 3 copies of companion " << std::endl
		<< "matrix of (x-1)^2 and one copy of companion matrix of (x^4 + x + 3)^1." << std::endl;
	}

	typedef NTL_ZZ Ring;
	Ring Z;
	typedef Companion<Ring> BB;
	int acp; char* avp[ac];
	string opts;
	stripOptions(acp, avp, opts, ac, av);
	//std::cout << "number of triples: " << acp << std::endl;
	//for (int i = 0; i < acp; ++ i)
	//	std::cout << avp[i];
	//std::cout << std::endl;
	//std::cout << "Begin to ....\n";
	list<BB*> L;

	for (int i = 0; i < acp; i += 3)
		augmentBB(L, avp[i], atoi(avp[i+1]), atoi(avp[i+2]), Z);

	DirectSum<BB> A(L);
	//std::cout <<"Option: " << opts.c_str() << std::endl;

	if (opts.size() >= 1)
	{	if (opts[0] == 'r')
		{
			// into sparse matrix, then 3n row ops with corresponding col ops
			DenseMatrix<Ring> B(Z,A.rowdim(), A.coldim());
			//MatrixDomain<Ring> MD(Z);
			LinBox::MatrixHom::map (B, A);

			scramble(B);
			printMatrix(B);
			// delete B;
		}

		if (opts[0] == 'R') { throw LinBox::NotImplementedYet(); }
		// into dense matrix, then many row ops
		//...

	}
	else {
		printMatrix (A);
	}

	return 0 ;
}

// 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