Library of Assembled Shared Sources
polynomial_quotient.inl
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_POLYNOMIAL_QUOTIENT_INL
44#define LASS_GUARDIAN_OF_INCLUSION_NUM_POLYNOMIAL_QUOTIENT_INL
45
46#include "num_common.h"
47#include "polynomial.h"
48
49namespace lass
50{
51namespace num
52{
53
54// --- public --------------------------------------------------------------------------------------
55
56template <typename T>
57PolynomialQuotient<T>::PolynomialQuotient():
58 numerator_(),
59 denominator_(TNumTraits::one)
60{
61}
62
63
64template <typename T>
65PolynomialQuotient<T>::PolynomialQuotient(TParam scalar):
66 numerator_(scalar),
67 denominator_(TNumTraits::one)
68{
69}
70
71
72
73template <typename T>
74PolynomialQuotient<T>::PolynomialQuotient(const TCoefficients& numerator):
75 numerator_(numerator),
76 denominator_(TNumTraits::one)
77{
78}
79
80
81
82template <typename T>
83PolynomialQuotient<T>::PolynomialQuotient(const TPolynomial& numerator):
84 numerator_(numerator),
85 denominator_(TNumTraits::one)
86{
87}
88
89
90
91template <typename T>
92PolynomialQuotient<T>::PolynomialQuotient(const TCoefficientsPair& numeratorDenominator):
93 numerator_(numeratorDenominator.first),
94 denominator_(numeratorDenominator.second)
95{
96}
97
98
99
100template <typename T>
101PolynomialQuotient<T>::PolynomialQuotient(TParam scalar, const TCoefficients& denominator):
102 numerator_(scalar),
103 denominator_(denominator)
104{
105}
106
107
108
109template <typename T>
110PolynomialQuotient<T>::PolynomialQuotient(TParam scalar, const TPolynomial& denominator):
111 numerator_(scalar),
112 denominator_(denominator)
113{
114}
115
116
117
118template <typename T>
119PolynomialQuotient<T>::PolynomialQuotient(const TCoefficients& numerator, const TCoefficients& denominator):
120 numerator_(numerator),
121 denominator_(denominator)
122{
123}
124
125
126
127template <typename T>
128PolynomialQuotient<T>::PolynomialQuotient(const TPolynomial& numerator, const TPolynomial& denominator):
129 numerator_(numerator),
130 denominator_(denominator)
131{
132}
133
134
135template <typename T>
136template <typename InputIterator>
137PolynomialQuotient<T>::PolynomialQuotient(InputIterator numFirst, InputIterator numLast):
138 numerator_(numFirst, numLast),
139 denominator_(TNumTraits::one)
140{
141}
142
143
144template <typename T>
145template <typename InputIterator>
146PolynomialQuotient<T>::PolynomialQuotient(
147 InputIterator numFirst, InputIterator numLast, InputIterator denFirst, InputIterator denLast):
148 numerator_(numFirst, numLast),
149 denominator_(denFirst, denLast)
150{
151}
152
153
154
155template <typename T> inline
156const typename PolynomialQuotient<T>::TPolynomial&
157PolynomialQuotient<T>::numerator() const
158{
159 return numerator_;
160}
161
162
163
164template <typename T> inline
165const typename PolynomialQuotient<T>::TPolynomial&
166PolynomialQuotient<T>::denominator() const
167{
168 return denominator_;
169}
170
171
172
173template <typename T>
174const typename PolynomialQuotient<T>::TValue
175PolynomialQuotient<T>::operator()(TParam x) const
176{
177 return numerator_(x) / denominator_(x);
178}
179
180
181
182template <typename T>
183const PolynomialQuotient<T>& PolynomialQuotient<T>::operator+() const
184{
185 return *this;
186}
187
188
189
190template <typename T>
191const PolynomialQuotient<T> PolynomialQuotient<T>::operator-() const
192{
193 return TSelf(-numerator_, denominator_);
194}
195
196
197
198template <typename T>
199PolynomialQuotient<T>& PolynomialQuotient<T>::operator+=(const TSelf& other)
200{
201 numerator_ = numerator_ * other.denominator_ + other.numerator_ * other.denominator_;
202 denominator_ *= other.denominator_;
203 return *this;
204}
205
206
207
208template <typename T>
209PolynomialQuotient<T>& PolynomialQuotient<T>::operator-=(const TSelf& other)
210{
211 numerator_ = numerator_ * other.denominator_ - other.numerator_ * other.denominator_;
212 denominator_ *= other.denominator_;
213 return *this;
214}
215
216
217
218template <typename T>
219PolynomialQuotient<T>& PolynomialQuotient<T>::operator*=(const TSelf& other)
220{
221 numerator_ *= other.numerator_;
222 denominator_ *= other.denominator_;
223 return *this;
224}
225
226
227
228template <typename T>
229PolynomialQuotient<T>& PolynomialQuotient<T>::operator/=(const TSelf& other)
230{
231 numerator_ *= other.denominator_;
232 denominator_ *= other.numerator_;
233 return *this;
234}
235
236
237
238template <typename T>
239PolynomialQuotient<T>& PolynomialQuotient<T>::operator+=(const Polynomial<T>& other)
240{
241 numerator_ += other * denominator_;
242 return *this;
243}
244
245
246
247template <typename T>
248PolynomialQuotient<T>& PolynomialQuotient<T>::operator-=(const Polynomial<T>& other)
249{
250 numerator_ -= other * denominator_;
251 return *this;
252}
253
254
255
256template <typename T>
257PolynomialQuotient<T>& PolynomialQuotient<T>::operator*=(const Polynomial<T>& other)
258{
259 numerator_ *= other;
260 return *this;
261}
262
263
264
265template <typename T>
266PolynomialQuotient<T>& PolynomialQuotient<T>::operator/=(const Polynomial<T>& other)
267{
268 denominator_ *= other;
269 return *this;
270}
271
272
273
274template <typename T>
275PolynomialQuotient<T>& PolynomialQuotient<T>::operator+=(TParam scalar)
276{
277 numerator_ += scalar * denominator_;
278 return *this;
279}
280
281
282
283template <typename T>
284PolynomialQuotient<T>& PolynomialQuotient<T>::operator-=(TParam scalar)
285{
286 numerator_ -= scalar * denominator_;
287 return *this;
288}
289
290
291
292template <typename T>
293PolynomialQuotient<T>& PolynomialQuotient<T>::operator*=(TParam scalar)
294{
295 numerator_ *= scalar;
296 return *this;
297}
298
299
300
301template <typename T>
302PolynomialQuotient<T>& PolynomialQuotient<T>::operator/=(TParam scalar)
303{
304 numerator_ /= scalar;
305 return *this;
306}
307
308
309
310template <typename T>
311PolynomialQuotient<T> PolynomialQuotient<T>::derivative() const
312{
313 const TPolynomial num = denominator_ * numerator_.derivative() - denominator_.derivative() * numerator_;
314 const TPolynomial den = denominator_ * denominator_;
315 return TSelf(num, den);
316}
317
318
319
320template <typename T>
321PolynomialQuotient<T> PolynomialQuotient<T>::pow(unsigned power) const
322{
323 return PolynomialQuotient<T>(numerator_.pow(power), denominator_.pow(power));
324}
325
326
327
328template <typename T>
329PolynomialQuotient<T> PolynomialQuotient<T>::one()
330{
331 static PolynomialQuotient result(1);
332 return result;
333}
334
335
336
337template <typename T>
338PolynomialQuotient<T> PolynomialQuotient<T>::x()
339{
340 static TValue coefficients[2] = { 0, 1 };
341 static PolynomialQuotient result(coefficients, coefficients + 2);
342 return result;
343}
344
345
346
347// --- free ----------------------------------------------------------------------------------------
348
349template <typename T>
350bool operator==(const PolynomialQuotient<T>& a, const PolynomialQuotient<T>& b)
351{
352 return a.numerator() == b.numerator() && a.denominator() == b.denominator();
353}
354
355
356
357template <typename T> inline
358bool operator!=(const PolynomialQuotient<T>& a, const PolynomialQuotient<T>& b)
359{
360 return !(a == b);
361}
362
363
364
365template <typename T> inline
366PolynomialQuotient<T> operator+(const PolynomialQuotient<T>& a, const PolynomialQuotient<T>& b)
367{
368 PolynomialQuotient<T> result(a);
369 result += b;
370 return result;
371}
372
373
374
375template <typename T> inline
376PolynomialQuotient<T> operator-(const PolynomialQuotient<T>& a, const PolynomialQuotient<T>& b)
377{
378 PolynomialQuotient<T> result(a);
379 result -= b;
380 return result;
381}
382
383
384
385template <typename T> inline
386PolynomialQuotient<T> operator*(const PolynomialQuotient<T>& a, const PolynomialQuotient<T>& b)
387{
388 PolynomialQuotient<T> result(a);
389 result *= b;
390 return result;
391}
392
393
394
395template <typename T> inline
396PolynomialQuotient<T> operator/(const PolynomialQuotient<T>& a, const PolynomialQuotient<T>& b)
397{
398 PolynomialQuotient<T> result(a);
399 result /= b;
400 return result;
401}
402
403
404
405template <typename T> inline
406PolynomialQuotient<T> operator+(const PolynomialQuotient<T>& a, const Polynomial<T>& b)
407{
408 PolynomialQuotient<T> result(a);
409 result += b;
410 return result;
411}
412
413
414
415template <typename T> inline
416PolynomialQuotient<T> operator-(const PolynomialQuotient<T>& a, const Polynomial<T>& b)
417{
418 PolynomialQuotient<T> result(a);
419 result -= b;
420 return result;
421}
422
423
424
425template <typename T> inline
426PolynomialQuotient<T> operator*(const PolynomialQuotient<T>& a, const Polynomial<T>& b)
427{
428 PolynomialQuotient<T> result(a);
429 result *= b;
430 return result;
431}
432
433
434
435template <typename T> inline
436PolynomialQuotient<T> operator/(const PolynomialQuotient<T>& a, const Polynomial<T>& b)
437{
438 PolynomialQuotient<T> result(a);
439 result /= b;
440 return result;
441}
442
443
444
445template <typename T> inline
446PolynomialQuotient<T> operator+(const Polynomial<T>& a, const PolynomialQuotient<T>& b)
447{
448 PolynomialQuotient<T> result(b);
449 result += a;
450 return result;
451}
452
453
454
455template <typename T> inline
456PolynomialQuotient<T> operator-(const Polynomial<T>& a, const PolynomialQuotient<T>& b)
457{
458 PolynomialQuotient<T> result(a);
459 result -= b;
460 return result;
461}
462
463
464
465template <typename T> inline
466PolynomialQuotient<T> operator*(const Polynomial<T>& a, const PolynomialQuotient<T>& b)
467{
468 PolynomialQuotient<T> result(b);
469 result *= a;
470 return result;
471}
472
473
474
475template <typename T> inline
476PolynomialQuotient<T> operator/(const Polynomial<T>& a, const PolynomialQuotient<T>& b)
477{
478 PolynomialQuotient<T> result(a);
479 result /= b;
480 return result;
481}
482
483
484
485template <typename T> inline
486PolynomialQuotient<T> operator+(const T& a, const PolynomialQuotient<T>& b)
487{
488 PolynomialQuotient<T> result(b);
489 result += a;
490 return result;
491}
492
493
494
495template <typename T> inline
496PolynomialQuotient<T> operator-(const T& a, const PolynomialQuotient<T>& b)
497{
498 PolynomialQuotient<T> result(-b);
499 result += a;
500 return result;
501}
502
503
504
505template <typename T> inline
506PolynomialQuotient<T> operator*(const T& a, const PolynomialQuotient<T>& b)
507{
508 PolynomialQuotient<T> result(b);
509 result *= a;
510 return result;
511}
512
513
514
515template <typename T> inline
516PolynomialQuotient<T> operator/(const T& a, const PolynomialQuotient<T>& b)
517{
518 PolynomialQuotient<T> result(a);
519 result /= b;
520 return result;
521}
522
523
524
525template <typename T> inline
526PolynomialQuotient<T> operator+(const PolynomialQuotient<T>& a, const T& b)
527{
528 PolynomialQuotient<T> result(a);
529 result += b;
530 return result;
531}
532
533
534
535template <typename T> inline
536PolynomialQuotient<T> operator-(const PolynomialQuotient<T>& a, const T& b)
537{
538 PolynomialQuotient<T> result(a);
539 result -= b;
540 return result;
541}
542
543
544
545template <typename T> inline
546PolynomialQuotient<T> operator*(const PolynomialQuotient<T>& a, const T& b)
547{
548 PolynomialQuotient<T> result(a);
549 result *= b;
550 return result;
551}
552
553
554
555template <typename T> inline
556PolynomialQuotient<T> operator/(const PolynomialQuotient<T>& a, const T& b)
557{
558 PolynomialQuotient<T> result(a);
559 result /= b;
560 return result;
561}
562
563
564
565template <typename T> inline
566PolynomialQuotient<T> operator/(const Polynomial<T>& a, const Polynomial<T>& b)
567{
568 return PolynomialQuotient<T>(a, b);
569}
570
571
572
573template <typename T, typename Char, typename Traits>
574std::basic_ostream<Char, Traits>&
575operator<<(std::basic_ostream<Char, Traits>& s, const PolynomialQuotient<T>& a)
576{
577 s << "(" << a.numerator() << ")/(" << a.denominator() << ")";
578 return s;
579}
580
581
582
583}
584
585}
586
587#endif
588
589// EOF
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53