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 {
GINAC_DECLARE_REGISTERED_CLASS(pseries,basic)37 	GINAC_DECLARE_REGISTERED_CLASS(pseries, basic)
38 
39         friend class ex;
40 	// other constructors
41 public:
42 	pseries(const ex &rel_, epvector ops_);
43         static ex unarchive(const archive_node &n, lst &sym_lst)
44         {
45                 return (new pseries(n, sym_lst))->
46                         setflag(status_flags::dynallocated);
47         }
48 
49 	// functions overriding virtual functions from base classes
50 public:
precedence()51 	unsigned precedence() const override {return 38;} // for clarity just below add::precedence
52 	size_t nops() const override;
53 	const ex op(size_t i) const override;
54 	numeric degree(const ex &s) const override;
55 	numeric ldegree(const ex &s) const override;
56 	ex coeff(const ex &s, const ex & n) const override;
57 	ex collect(const ex &s, bool distributed = false) const override;
58 	ex eval(int level=0) const override;
59 	ex evalf(int level=0, PyObject* parent=nullptr) const override;
60 	ex series(const relational & r, int order, unsigned options = 0) const override;
61 	ex subs(const exmap & m, unsigned options = 0) const override;
62 	ex normal(exmap & repl, exmap & rev_lookup, int level = 0, unsigned options = 0) const override;
63 	ex expand(unsigned options = 0) const override;
64 	ex conjugate() const override;
65 	ex real_part() const override;
66 	ex imag_part() const override;
67 protected:
68 	ex derivative(const symbol & s) const override;
69 
70 	// non-virtual functions in this class
71 public:
72 	/** Get the expansion variable. */
get_var()73 	ex get_var() const {return var;}
74 
75 	/** Get the expansion point. */
get_point()76 	ex get_point() const {return point;}
77 
78 	/** Convert the pseries object to an ordinary polynomial.
79 	 *
80 	 *  @param no_order flag: discard higher order terms */
81 	ex convert_to_poly(bool no_order = false) const;
82 
83 	/** Check whether series is compatible to another series (expansion
84 	 *  variable and point are the same. */
is_compatible_to(const pseries & other)85 	bool is_compatible_to(const pseries &other) const {return var.is_equal(other.var) && point.is_equal(other.point);}
86 
87 	/** Check whether series has the value zero. */
is_zero()88 	bool is_zero() const {return seq.empty();}
89 
90 	/** Returns true if there is no order term, i.e. the series terminates and
91 	 *  false otherwise. */
92 	bool is_terminating() const;
93 
94 	/** Get coefficients and exponents. */
95 	ex coeffop(size_t i) const;
96 	ex exponop(size_t i) const;
97 
98 	ex add_series(const pseries &other) const;
99 	ex mul_const(const numeric &other) const;
100 	ex mul_series(const pseries &other) const;
101 	ex power_const(const numeric &p, int deg) const;
102 	pseries shift_exponents(int deg) const;
103 
104 protected:
105 	void print_series(const print_context & c, const char *openbrace, const char *closebrace, const char *mul_sym, const char *pow_sym, unsigned level) const;
106 	void do_print(const print_context & c, unsigned level) const override;
107 	void do_print_latex(const print_latex & c, unsigned level) const;
108 	void do_print_tree(const print_tree & c, unsigned level) const override;
109 	void do_print_python(const print_python & c, unsigned level) const;
110 	void do_print_python_repr(const print_python_repr & c, unsigned level) const override;
111 
112 protected:
113 	/** Vector of {coefficient, power} pairs */
114 	epvector seq;
115 
116 	/** Series variable (holds a symbol) */
117 	ex var;
118 
119 	/** Expansion point */
120 	ex point;
121 };
122 
123 
124 // utility functions
125 
126 /** Convert the pseries object embedded in an expression to an ordinary
127  *  polynomial in the expansion variable. The result is undefined if the
128  *  expression does not contain a pseries object at its top level.
129  *
130  *  @param e expression
131  *  @return polynomial expression
132  *  @see is_a<>
133  *  @see pseries::convert_to_poly */
series_to_poly(const ex & e)134 inline ex series_to_poly(const ex &e)
135 {
136 	return (ex_to<pseries>(e).convert_to_poly(true));
137 }
138 
is_terminating(const pseries & s)139 inline bool is_terminating(const pseries & s)
140 {
141 	return s.is_terminating();
142 }
143 
144 } // namespace GiNaC
145 
146 #endif // ndef __GINAC_SERIES_H__
147