File: lb-polynomial.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 (188 lines) | stat: -rw-r--r-- 5,557 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
/* lb-polynomial.C
 * Copyright (C) 2005 Pascal Giorgi
 *
 * Written by Pascal Giorgi <pgiorgi@uwaterloo.ca>
 *
 * ========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========
 */

#ifndef __LINBOX_lb_polynomial_C
#define __LINBOX_lb_polynomial_C

#include <lb-domain-collection.h>
#include <lb-domain-function.h>
#include <lb-vector-collection.h>
#include <lb-vector-function.h>
#include <lb-polynomial.h>

/***********************************************
 * Polynomial are handled through dense vector *
 ***********************************************/

// nothing to do at this stage


/*************************************************************
 * API to write a polynomial over the standard output stream *
 *************************************************************/

template<class Element, class VElement>
class PrintPoly{
public:
	template< class Domain, class Poly>
	void operator()(Domain *D, Poly *poly, std::ostream &os){
		throw lb_runtime_error("LinBox ERROR: incompatible type (polynomial writing impossible)\n");
	}
};

template<class Element>
class PrintPoly<Element, Element> {
public:
	template< class Domain, class Poly, class Out>
	void operator()(Domain *D, Poly *poly, Out &os){
		size_t deg=poly->size()-1;
		while  (D->isZero((*poly)[deg])) --deg;
		D->write(os,(*poly)[deg])<<".x^"<<deg;
		for (int i= (int)deg-1; i > 1 ; --i) {
			if (!D->isZero((*poly)[i])){os<<" + ";D->write(os, (*poly)[i])<<".x^"<<i;}
		}
		if (deg > 1)
			if (!D->isZero((*poly)[1])){os<<" + ";D->write(os,(*poly)[1])<<".x";}
		if (deg > 0)
			if (!D->isZero((*poly)[0])){os<<" + ";D->write(os,(*poly)[0]);}
		os<<"\n";
	}
};

template<class Polynomial>
class WritePolynomialSpecFunctor{
protected:
	std::ostream     &os;
	Polynomial     *poly;
public:
	WritePolynomialSpecFunctor(std::ostream &o, Polynomial *P) : os(o), poly(P){}

	template<class Domain>
	void operator()(void *&, Domain *D) const {
		PrintPoly<typename Domain::Element, typename Polynomial::value_type> ()(D, poly, os);
	}
};


class WritePolynomialFunctor{
protected:
	std::ostream     &os;
	const PolynomialKey &key;
public:
	WritePolynomialFunctor(std::ostream &o, const PolynomialKey &k) : os(o), key(k) {}

	template<class Polynomial>
	void operator() (void*, Polynomial *P) const {

		VectorTable::iterator it = vector_hashtable.find(key);
		if ( it == vector_hashtable.end())
			throw lb_runtime_error("LinBox ERROR: invalid polynomial (writing impossible)");

		WritePolynomialSpecFunctor<Polynomial> Fct(os,P);
		DomainFunction::call(it->second->getDomainKey(), Fct);
	}
};


void writePolynomial (const PolynomialKey &key, std::ostream &os){
	WritePolynomialFunctor Fct(os, key);
	VectorFunction::call(key, Fct);
}


/**********************************
 * API to serialize a  polynomial *
 **********************************/
template <class PElement, class DElement>
class ToSerialPolynomial {
public:
	template<class Domain, class Polynomial>
	inline void operator()(SerialPolynomial &s, Domain *D, Polynomial *poly){
		throw lb_runtime_error("LinBox ERROR: incompatible type (polynomial writing impossible)\n");
	}
};

template<class Element>
class ToSerialPolynomial<Element, Element>{
public:
	template<class Domain, class Polynomial>
	inline void operator()(SerialPolynomial &s, Domain *D, Polynomial *poly){
		s.type = "integer";
		s.list.resize(poly->size());
		typename Polynomial::const_iterator    it_P= poly->begin();
		std::vector<LinBox::integer>::iterator it_s= s.list.begin();
		for (; it_P != poly->end(); ++it_P, ++it_s)
			D->convert(*it_s, *it_P);
	}
};


template<class Polynomial>
class SerializePolynomialSpecFunctor{
protected:
	Polynomial     *poly;
public:
	SerializePolynomialSpecFunctor(Polynomial *P) :  poly(P){}

	template<class Domain>
	void operator()(SerialPolynomial &s, Domain *D) const {
		ToSerialPolynomial<typename Polynomial::value_type, typename Domain::Element>()(s, D, poly);
	}
};


class SerializePolynomialFunctor{
protected:
	const PolynomialKey &key;
public:
	SerializePolynomialFunctor(const PolynomialKey &k) :  key(k) {}

	template<class Polynomial>
	void operator() (SerialPolynomial& s, Polynomial *P) const {

		VectorTable::iterator it = vector_hashtable.find(key);
		if ( it == vector_hashtable.end())
			throw lb_runtime_error("LinBox ERROR: invalid polynomial (serializing impossible)");

		SerializePolynomialSpecFunctor<Polynomial> Fct(P);
		DomainFunction::call(s, it->second->getDomainKey(), Fct);
	}
};


void  SerializePolynomial (SerialPolynomial &s, const PolynomialKey &key) {
	SerializePolynomialFunctor Fct(key);
	VectorFunction::call(s, key, Fct);
}


#endif

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