]> www.ginac.de Git - ginac.git/blob - ginac/pseries.h
gcd_pf_pow: get rid of duplicate code.
[ginac.git] / ginac / pseries.h
1 /** @file pseries.h
2  *
3  *  Interface to class for extended truncated power series. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef __GINAC_SERIES_H__
24 #define __GINAC_SERIES_H__
25
26 #include "basic.h"
27 #include "expairseq.h"
28
29 namespace GiNaC {
30
31 /** This class holds a extended truncated power series (positive and negative
32  *  integer powers). It consists of expression coefficients (only non-zero
33  *  coefficients are stored), an expansion variable and an expansion point.
34  *  Other classes must provide members to convert into this type. */
35 class pseries : public basic
36 {
37         GINAC_DECLARE_REGISTERED_CLASS(pseries, basic)
38
39         // other constructors
40 public:
41         pseries(const ex &rel_, const epvector &ops_);
42
43         // functions overriding virtual functions from base classes
44 public:
45         unsigned precedence() const {return 38;} // for clarity just below add::precedence
46         size_t nops() const;
47         ex op(size_t i) const;
48         int degree(const ex &s) const;
49         int ldegree(const ex &s) const;
50         ex coeff(const ex &s, int n = 1) const;
51         ex collect(const ex &s, bool distributed = false) const;
52         ex eval(int level=0) const;
53         ex evalf(int level=0) const;
54         ex series(const relational & r, int order, unsigned options = 0) const;
55         ex subs(const exmap & m, unsigned options = 0) const;
56         ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
57         ex expand(unsigned options = 0) const;
58         ex conjugate() const;
59         ex real_part() const;
60         ex imag_part() const;
61         ex eval_integ() const;
62         ex evalm() const;
63 protected:
64         ex derivative(const symbol & s) const;
65
66         // non-virtual functions in this class
67 public:
68         /** Get the expansion variable. */
69         ex get_var() const {return var;}
70
71         /** Get the expansion point. */
72         ex get_point() const {return point;}
73
74         /** Convert the pseries object to an ordinary polynomial.
75          *
76          *  @param no_order flag: discard higher order terms */
77         ex convert_to_poly(bool no_order = false) const;
78
79         /** Check whether series is compatible to another series (expansion
80          *  variable and point are the same. */
81         bool is_compatible_to(const pseries &other) const {return var.is_equal(other.var) && point.is_equal(other.point);}
82
83         /** Check whether series has the value zero. */
84         bool is_zero() const {return seq.size() == 0;}
85
86         /** Returns true if there is no order term, i.e. the series terminates and
87          *  false otherwise. */
88         bool is_terminating() const;
89
90         /** Get coefficients and exponents. */
91         ex coeffop(size_t i) const;
92         ex exponop(size_t i) const;
93
94         ex add_series(const pseries &other) const;
95         ex mul_const(const numeric &other) const;
96         ex mul_series(const pseries &other) const;
97         ex power_const(const numeric &p, int deg) const;
98         pseries shift_exponents(int deg) const;
99
100 protected:
101         void print_series(const print_context & c, const char *openbrace, const char *closebrace, const char *mul_sym, const char *pow_sym, unsigned level) const;
102         void do_print(const print_context & c, unsigned level) const;
103         void do_print_latex(const print_latex & c, unsigned level) const;
104         void do_print_tree(const print_tree & c, unsigned level) const;
105         void do_print_python(const print_python & c, unsigned level) const;
106         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
107
108 protected:
109         /** Vector of {coefficient, power} pairs */
110         epvector seq;
111
112         /** Series variable (holds a symbol) */
113         ex var;
114
115         /** Expansion point */
116         ex point;
117 };
118
119
120 // utility functions
121
122 /** Convert the pseries object embedded in an expression to an ordinary
123  *  polynomial in the expansion variable. The result is undefined if the
124  *  expression does not contain a pseries object at its top level.
125  *
126  *  @param e expression
127  *  @return polynomial expression
128  *  @see is_a<>
129  *  @see pseries::convert_to_poly */
130 inline ex series_to_poly(const ex &e)
131 {
132         return (ex_to<pseries>(e).convert_to_poly(true));
133 }
134
135 inline bool is_terminating(const pseries & s)
136 {
137         return s.is_terminating();
138 }
139
140 } // namespace GiNaC
141
142 #endif // ndef __GINAC_SERIES_H__