My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
qf_poly.h
Go to the documentation of this file.
1 /***************************************************************************
2  qf_poly.h
3  ----------------
4  begin : Mon Jan 02 2006
5  copyright : (C) 2006 by Vincent Habchi, F5RCS
6  email : 10.50@free.fr
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef _QF_POLY_H
19 #define _QF_POLY_H
20 
21 /* Headers for R[X] arithmetic */
22 
23 #define qf_double_t long double
24 
25 #include "qf_matrix.h"
26 
27 // A polynom can be described either by a product of monoms equal to
28 // (x - r[i]) where r[i] is the ith root and a constant factor, or by
29 // the classical series of coefficient a[0]...a[n], or both.
30 
32 {
33  NONE = 0, // Not initialized
34  ROOTS = 1, // P(X) = k * prod (x - r[i])
35  COEFF = 2, // P(X) = sum (a[i] * x^i)
36  BOTH = 3 // Both have been computed
37 };
38 
39 typedef enum qf_poly_rep qpr;
40 
41 class qf_poly
42 {
43  private:
44  qpr rep; // Type of representation
45  unsigned d; // Current degree
46  qf_double_t krts; // Constant k
47  qf_double_t * p; // Table of coefficients
48  qf_double_t * rts; // Table of complex roots
49 
50  // Functions used by solve
51  void qf_bcm (qf_matrix &);
52  int qf_qrc (qf_matrix &, qf_double_t *);
53  void qf_scm (qf_matrix &);
54 
55  public:
56  qf_poly ();
57  qf_poly (unsigned); // id with d°
58  qf_poly (qf_double_t, qf_double_t, qf_double_t, unsigned); // Up to d°=2
59  qf_poly (int, const qf_double_t[]); // Id, with inst.
60  qf_poly (int, qf_double_t, const qf_double_t[]);
61  qf_poly (const qf_poly &); // Copy
62  ~qf_poly ();
63 
64  // access operators
65  qf_poly & operator = (const qf_poly &); // P = Q
66  qf_double_t & operator [] (int i); // Access to element
67 
68  // arithmetic operators
69  qf_poly operator - (void); // Unary -
70 
74  friend qf_poly operator * (qf_poly, const qf_double_t);
75 
78  qf_poly operator *= (qf_poly); // P(X) = P(X)*Q(X)
80 
81  qf_poly operator << (unsigned); // Basic div by X^n
82  qf_poly operator >> (unsigned); // Multiply by X^n
83 
84  bool operator == (qf_poly); // Test
85  bool operator != (qf_poly); // Test
86  bool is_null (void);
87 
88  unsigned deg (void); // Degree of poly
89  void spl (void); // Simplify
90  qf_poly odd (void); // Odd part
91  qf_poly even (void); // Even part
92  qf_poly mnx (void); // P(X) -> P(-X)
93  qf_poly hsq (void); // P(X)*P(-X)
94  qf_poly sqr (void); // Q(X) = P(X^2)
95  qf_double_t eval (qf_double_t); // P(X = a)
96  qf_double_t evalX2 (qf_double_t); // P(X^2 = a)
97 
98  void to_roots (void); // Solves
99  qf_double_t k (void); // Return krts factor
100  void to_coeff (void); // Calculate normal form
101  void div (qf_double_t, qf_double_t); // Simple division
102  void hurw (void); // "Hurwitzes" polynom
103 
104  void disp (const char *); // Prints P(X)
105  void disp_c (void);
106  void disp_r (void);
107 
108  friend void smpf (qf_poly &, qf_poly &); // Simplify
109 };
110 
111 // For solve, we need some gibber
112 
113 // Save complex value elements
114 #define SET_COMPLEX_PACKED(zp,n,r,i) \
115  *((zp)+2*(n)+0)=(r); *((zp)+2*(n)+1)=(i);
116 
117 // Some constants
118 
119 // IEEE long precision 2^{-52}
120 // # define EPSILON 2.2204460492503131e-16
121 // IEEE double long precision 2^{-63}
122 #define EPSILON 1.0842021724855044e-19
123 #define ROOT_PREC 1e-9
124 #define ROOT_TOL 1e-7
125 
127  if (k > 0)
128  return floor (k / ROOT_PREC) * ROOT_PREC;
129  else
130  return ceil (k / ROOT_PREC) * ROOT_PREC;
131 }
132 
133 #define RADIX 2
134 #define RADIX2 (RADIX*RADIX)
135 #define MAX_ITERATIONS 60
136 
137 #endif // _QF_POLY_H