File: toeplitz.inl

package info (click to toggle)
linbox 1.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,640 kB
  • sloc: cpp: 106,991; lisp: 5,469; makefile: 1,293; sh: 1,049; csh: 95; python: 74; perl: 2
file content (416 lines) | stat: -rw-r--r-- 13,727 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *    toeplitz.inl     NTL_Toeplitz.cpp file
 *
 *    Copyright (C) 2002 Austin Lobo, B. David Saunders
 *    Author: Austin Lobo
 *    LinBox version 2001 and 2002
 * ========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========

 *    This file is included in the template description of ntl-Toeplitz.h
 *    it contains the implementations of templatized member functions in the
 *    partial template  specialization for toeplitz matrices that
 *    are manipulated in fields and rings according to the arithmetic
 *    in the ntl package from V. Shoup
 *-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*/

#ifndef __LINBOX_bb_toeplitz_INL
#define __LINBOX_bb_toeplitz_INL

#include <iostream>
#include <fstream>
// #include <clinbox_check> // JGD 26.09.2003
#include "linbox/algorithms/toeplitz-det.h"

namespace LinBox
{
	/*-----------------------------------------------------------------
	 *----    Destructor
	 *----------------------------------------------------------------*/
	template <class _CField, class _PRing>
	inline ToeplitzBase<_CField,_PRing>::~ToeplitzBase()
	{
#ifdef DBGMSGS
		std::cout << "Toeplitz::~Toeplitz():\tDestroyed a " << rowdim() << "x"<< this->coldim()<<
		" Toeplitz matrix "<< std::endl;
#endif
	}//---- Destructor ---- [Tested 6/14/02 -- Works]



	/*-----------------------------------------------------------------
	 *----    Field-only Constructor
	 *----------------------------------------------------------------*/
	template <class _CField, class _PRing>
	ToeplitzBase<_CField, _PRing>::ToeplitzBase(const Field& F) :
		P(F), field_(&F)
	{
		sysDim =               // Default dimension is 0
		rowDim =               // Default row dim is 0
		this->colDim = 0;            // Default col dim is 0
		shape.shape(BlackboxSpecifier::TOEPLITZ);
#ifdef DBGMSGS
		std::cout << "Toeplitz::Toeplitz():\tCreated a " << rowDim << "x"<< this->colDim<<
		" Toeplitz matrix "<< std::endl;
#endif

	}//----- Field-only Constructor


#if 0
	/*-----------------------------------------------------------------
	 *----    Zero Parameter Constructor
	 *----------------------------------------------------------------*/
	template <class _CField, class _PRing>
	ToeplitzBase<_CField, _PRing>::ToeplitzBase() :
		P(0), field_(0)
	{
		sysDim =               // Default dimension is 0
		rowDim =               // Default row dim is 0
		this->colDim = 0;            // Default col dim is 0
		shape.shape(BlackboxSpecifier::TOEPLITZ);
#ifdef DBGMSGS
		std::cout << "Toeplitz::Toeplitz():\tCreated a " << rowDim << "x"<< this->colDim<<
		" Toeplitz matrix "<< std::endl;
#endif

	}//----- Zero Param Constructor ---- [Tested 6/14/02 -- Works]
#endif

	template <class _CField, class _PRing>
	ToeplitzBase<_CField, _PRing>::ToeplitzBase(const _PRing& PF) :
		P(PF), field_(&(PF.getCoeffField()))
	{
		sysDim = rowDim = this->colDim = 0;
		shape.shape(BlackboxSpecifier::TOEPLITZ);

	}//------ Polynomial Field constructor

	/*-----------------------------------------------------------------
	 *------ Polynomial constructor
	 *-----------------------------------------------------------------*/
	template< class _CField, class _PRing >
	ToeplitzBase<_CField,_PRing>::ToeplitzBase ( const PRing& PF
						     , const Poly& p
						     , size_t m
						     , size_t n ) :
		P(PF), field_(&(PF.getCoeffField())), rowDim(m), colDim(n), pdata(p)
	{
		shape.shape(BlackboxSpecifier::TOEPLITZ);
		if( n == 0 ) this->colDim = rowDim;
		if( rowDim >= this->colDim ) sysDim = rowDim;
		else sysDim = this->colDim;

		linbox_check( P.deg(p) <= rowDim + this->colDim - 2 );

		P.rev(rpdata, pdata);

		// Account for possible trailing zeroes
		if( P.deg(pdata) < rowDim + this->colDim - 2 ) {
			Poly x;
			P.assign(x,P.zero);
			P.setCoeff(x, (rowDim + this->colDim - 2 - P.deg(pdata)), field().one);
			P.mulin( rpdata, x );
		}


	}//------ Polynomial constructor

	/*-----------------------------------------------------------------
	 *----- Constructor With User-Supplied First Row And Column
	 *----------------------------------------------------------------*/
	template <class _PRing>
	void Toeplitz<typename _PRing::CoeffField, _PRing>::init_vector( const std::vector<Element>&v)
	{
		if ( (1 & v.size()) == 0)
		{
			std::cout << "There must be an ODD number of entries in the input vector " <<
			"The length given is " << v.size();
		}
		linbox_check( (1 & v.size()) == 1);

		this->P.init(this->pdata, v);
		this->P.rev(this->rpdata, this->pdata);

		// Account for possible trailing zeroes
		if( this->P.deg(this->pdata) < v.size() - 1 ) {
			Poly x;
			this->P.assign(x,this->P.zero);
			this->P.setCoeff(x, (v.size() - 1 - this->P.deg(this->pdata)), this->field().one);
			this->P.mulin( this->rpdata, x );
		}

		this->rowDim = this->colDim = this->sysDim = (v.size()+1)/2;

		//data = v;

#ifdef DBGMSGS
		std::cout << "Toeplitz::Toeplitz(F,V):\tCreated a " << rowDim << "x"<< this->colDim<<
		" Toeplitz matrix "<< std::endl;
#endif

	}//----- Constructor given a vector---- [Tested 6/14/02 -- Works]



	/*-----------------------------------------------------------------
	 *-----    Print The Matrix To Screen
	 *----------------------------------------------------------------*/
	template <class _PRing>
	std::ostream & Toeplitz<typename _PRing::CoeffField,_PRing>::write(std::ostream& os) const
	{

		int N;
		Element temp;

		os<< this->rowdim() << " " << this->coldim() << " " << this->shape.shape() << std::endl;
		N = (int) (this->rowdim() + this->coldim()) -1;

		if ( N < 20 )             // Print small matrices in dense format
		{
			int i;
			unsigned int j;
			for (i = (int)this->coldim()-1; i < N; i++)
			{
				for ( j = 0; j < this->coldim() ; j++)
					os << " " ;
				this->field().write(os,this->P.getCoeff(temp,this->pdata,static_cast<size_t>((unsigned int)i-j))) ;
				os << std::endl;
			}
		}
		else
		{                    // Print large matrices' first row and col
			os << "[";
			for (size_t ii = this->rowdim() + this->coldim() - 2; ii> 0;ii--)
				this->field().write(os, this->P.getCoeff(temp,this->pdata,ii) ) << " ";
			this->field().write(os,this->P.getCoeff(temp,this->pdata,0)) << "]\n";
			this->P.write(os, this->pdata) << std::endl;
		} //[v(2n-2),....,v(0)]; where v(0) is the top right entry of the matrix

		return os;
	} //---- write()----- [Tested 6/14/02 -- Works]


#if 0
	// 	/*-----------------------------------------------------------------
	// 	 *----    The infamous clone has been created here
	// 	 *----------------------------------------------------------------*/
	template <class Field, class Vector>
	BlackboxArchetype<Vector>* Toeplitz<Field, Vector>::clone() const
	{
		return new Toeplitz(*this);
	}// ------ This is not tested.
#endif

	/*-----------------------------------------------------------------
	 *----    Save To File, Given Destination Filename
	 *----------------------------------------------------------------*/
	template <class _PRing>
	void Toeplitz<typename _PRing::CoeffField, _PRing>::write( char *outFileName) const
	{
		Element temp;

		std::cout << "Printing toeplitz matrix to " << outFileName << std::endl;

		if ( outFileName == NULL )
			write();    // Print to stdout if no file is specified
		else
		{
			std::ofstream o_fp(outFileName, std::ios::out);
			o_fp << this->rowdim() << " " << this->coldim() << " " << this->shape.shape() << std::endl ;
			o_fp << "[";
			for (size_t i = this->rowdim() + this->coldim() - 1 ; i-- ; )
				this->field().write(o_fp,this->P.getCoeff(temp,this->pdata,i))
				<< " ";
			o_fp << "]\n";

			o_fp.close();
		}
		return;
	} // write(char *) [Tested 6/14/02 -- Works]


#if 0 //dated material with no known use -bds 2012Jul
	/*-----------------------------------------------------------------
	 *    Make the matrix upper triangular with determinant 1.
	 *    i.e. clear the last N-1 elements in the data vector
	 *----------------------------------------------------------------*/
	template <class _CField, class _PRing>
	void ToeplitzBase<_CField, _PRing>::setToUniModUT()
	{
		const PRing & PF = P.getCoeffField() ;

		for( size_t i = sysdim(); i <= P.deg(pdata); ++i )
			P.setCoeff(pdata,i,PF.zero);

		for( size_t i = 0; i < sysDim - 1; ++i )
			P.setCoeff(rpdata,i,PF.zero);

		P.setCoeff(pdata,sysDim-1,PF.one);
		P.setCoeff(rpdata,sysDim-1,PF.one);

		shape.shape(BlackboxSpecifier::UNIMOD_UT);
		return;
	}// [UNCOMMENTED PART Tested 6/14/02 -- Works]



	/*-----------------------------------------------------------------
	 *    Make matrix a unimodular Lower Triangular with det 1
	 *    i.e. clear the first N-1 elements in the data vector
	 *----------------------------------------------------------------*/
	template <class _CField, class _PRing>
	void ToeplitzBase<_CField, _PRing>::setToUniModLT()
	{
		const PRing & PF = P.getCoeffField() ;

		for( size_t i = sysDim; i <= P.deg(rpdata); ++i )
			P.setCoeff(rpdata,i,PF.zero);

		for( size_t i = 0; i < sysDim - 1; ++i )
			P.setCoeff(pdata,i,PF.zero);

		P.setCoeff(pdata,sysDim-1,PF.one);
		P.setCoeff(rpdata,sysDim-1,PF.one);

		shape.shape(BlackboxSpecifier::UNIMOD_LT);
		return;
	}// [UNCOMMENTED PART Tested 6/14/02 -- Works]

#endif

	/*-----------------------------------------------------------------
	 *     Compute the determinant of the matrix
	 *-----------------------------------------------------------------*/
	template<class _PRing>
	typename Toeplitz<typename _PRing::CoeffField,_PRing>::Element&
	Toeplitz<typename _PRing::CoeffField,_PRing>::det
	( Element& res ) const
	{
		return toeplitz_determinant( this->P, res, this->pdata, this->sysDim );
	}

	/*-----------------------------------------------------------------
	 *     Compute the trace of the matrix
	 *-----------------------------------------------------------------*/
	/*-----------------------------------------------------------------
	 *     Compute the trace of the matrix
	 *-----------------------------------------------------------------*/
	/*-----------------------------------------------------------------
	 *     Compute the trace of the matrix
	 *-----------------------------------------------------------------*/
	template<class _PRing>
	typename Toeplitz<typename _PRing::CoeffField,_PRing>::Element&
	Toeplitz<typename _PRing::CoeffField,_PRing>::trace
	( Element& res ) const
	{
		Element x; this->field().init(x, min(rowdim(), coldim()));
		this->P.getCoeff(res, pdata, rowdim()-1);
		return this->field().mulin(res, x);
	}



	/*-----------------------------------------------------------------
	 *    Apply the matrix to a vector
	 *----------------------------------------------------------------*/
	template <class _PRing>
	template <class OutVector, class InVector>
	OutVector& Toeplitz<typename _PRing::CoeffField,_PRing>::apply( OutVector &v_out,
									const InVector& v_in) const
	{

		linbox_check((v_out.size() == this->rowdim()) &&
			     (v_in.size() == this->coldim()))  ;

		Poly pOut, pIn;
		this->P.init( pIn, v_in );

#ifdef DBGMSGS
		std::cout << "\npX in is " << pxIn << std::endl;
		std::cout << "multiplied by " << this->pdata << std::endl;
#endif

		this->P.mul(pOut, pIn, this->pdata);

#ifdef DBGMSGS
		std::cout <<"pxOut is " << pxOut << std::endl;
#endif

		size_t N = this->rowdim();
		for( size_t i = 0; i < N; ++i )
			this->P.getCoeff(v_out[i], pOut, N-1+i);

		return v_out;

	}




	/*-----------------------------------------------------------------
	 *    Apply the transposed matrix to a vector
	 *----------------------------------------------------------------*/
	template <class _PRing>
	template<class OutVector, class InVector>
	OutVector& Toeplitz<typename _PRing::CoeffField,_PRing>::applyTranspose( OutVector &v_out,
										 const InVector& v_in) const
	{

		if (v_out.size() != this->coldim())
			std::cout << "\tToeplitz::apply()\t output vector not correct size, at "
			<< v_out.size() << ". System rowDim is" <<  this->coldim() << std::endl;
		if ( v_in.size() != this->rowdim() )
			std::cout << "\tToeplitz::apply()\t input vector not correct size at "
			<< v_in.size() << ". System colDim is" <<  this->rowdim() << std::endl;
		linbox_check((v_out.size() == this->coldim()) &&
			     (v_in.size() == this->rowdim()))  ;

		Poly pOut, pIn;
		this->P.init( pIn, v_in );

#ifdef DBGMSGS
		std::cout << "\npX in is " << pxIn << std::endl;
		std::cout << "multiplied by " << this->rpdata << std::endl;
#endif

		this->P.mul(pOut, pIn, this->rpdata);

#ifdef DBGMSGS
		std::cout <<"pxOut is " << pxOut << std::endl;
#endif

		size_t N = this->coldim();
		for( size_t i = 0; i < N; ++i )
			this->P.getCoeff(v_out[i], pOut, N-1+i);

		return v_out;

	}

} // namespace LinBox

#endif //__LINBOX_bb_toeplitz_INL

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