File:  [CENS] / python / pyGiNaC / wrappers3 / pseries_py.cpp
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Thu Apr 26 19:44:48 2001 UTC (16 years, 7 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Impl. pseries,ncmul. Preparing for a release.

/*
# This file is part of the PyGiNaC package.
# http://cens.ioc.ee/projects/pyginac/
#
# $Revision: 1.1 $
# $Id: pseries_py.cpp,v 1.1 2001-04-26 19:44:48 pearu Exp $
#
# Copyright 2001 Pearu Peterson all rights reserved,
# Pearu Peterson <pearu@cens.ioc.ee>
# Permission to use, modify, and distribute this software is given under the
# terms of the LGPL.  See http://www.fsf.org
#
# NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
#
*/
/*DT
  Power Series
  ------

  >>> from ginac import is_pseries,pseries
  >>> from ginac import is_terminating, is_compatible_to
  >>> from ginac import symbol,series
  >>> x = symbol('x')
  >>> e = x**5 + 2*x**4 + 3*x**3 + 4*x**2 + 5 * x + 6
  >>> p1 = series(e,(x,0),3)
  >>> print p1
  6 + 5 * x + 4 * x ** 2 + Order(x ** 3)
  >>> p2 = series(e,(x,1),3)
  >>> print p2
  21 + 35 * (-1 + x) + 35 * (-1 + x) ** 2 + Order((-1 + x) ** 3)
  >>> from ginac import sin
  >>> p = series(sin(x),(x,0),5)
  >>> p.is_pseries(), is_pseries(p), x.is_pseries(), is_pseries({})
  (1, 1, 0, 0)
  >>> p.is_compatible_to(p1), is_compatible_to(p, p1)
  (1, 1)
  >>> print p
  1 * x + (-1/6) * x ** 3 + Order(x ** 5)
  >>> print p + p1
  6 + 6 * x + 4 * x ** 2 + Order(x ** 3)
  >>> print p1 - p
  6 + 4 * x + 4 * x ** 2 + Order(x ** 3)
  >>> print p * p1
  6 * x + 5 * x ** 2 + 3 * x ** 3 + Order(x ** 4)
  >>> from ginac import Order
  >>> r = pseries((x,0),[(3,-1),(2,2),(Order(1),3)])
  >>> print r
  3 * x ** (-1) + 2 * x ** 2 + Order(x ** 3)
  >>> print r * p
  3 + (-1/2) * x ** 2 + 2 * x ** 3 + Order(x ** 4)
  >>> from ginac import series_to_poly
  >>> print p.to_poly()
  -1/6 * x ** 3 + Order(x ** 5) + x
  >>> print p.to_poly(1)
  -1/6 * x ** 3 + x
  >>> print series_to_poly(p)
  -1/6 * x ** 3 + Order(x ** 5) + x
  >>> print r << 5   # shift powers by degree = 5
  3 * x ** 4 + 2 * x ** 7 + Order(x ** 8)

  >>> p=series(x,(x,1),2)
  >>> print p
  1 + 1 * (-1 + x)
  >>> print p*x
  1 + 2 * (-1 + x) + 1 * (-1 + x) ** 2
  >>> print p ** 4
  1 + 4 * (-1 + x) + 6 * (-1 + x) ** 2 + 4 * (-1 + x) ** 3 + Order((-1 + x) ** 4)
  >>> print pow(p,4,2)
  1 + 4 * (-1 + x) + Order((-1 + x) ** 2)

*/


#ifdef PYGINAC_DEFS
this_module.def(&ex::is_pseries, "is_pseries");
this_module.def(&return_false, "is_pseries");
ex_class.def(&ex::is_pseries, "is_pseries");
this_module.def(&pseries_2, "pseries");
this_module.def(&pseries_1, "pseries");
ex_class.def(&ex::is_compatible_to, "is_compatible_to");
this_module.def(&ex::is_compatible_to, "is_compatible_to");
ex_class.def(&ex::is_terminating, "is_terminating");
this_module.def(&ex::is_terminating, "is_terminating");
this_module.def(&return_false, "is_terminating");
ex_class.def(&ex::convert_to_poly_0, "to_poly");
ex_class.def(&ex::convert_to_poly_1, "to_poly");
this_module.def(&series_to_poly_0, "series_to_poly");
this_module.def(&series_to_poly_1, "series_to_poly");
ex_class.def(&ex::get_var, "get_var");
ex_class.def(&ex::get_point, "get_point");
#else
#ifdef PYGINAC_EX_PROTOS
#define PYGINAC_PROTOS
#endif
#ifdef PYGINAC_PROTOS
#ifdef PYGINAC_EX_PROTOS
bool is_pseries(void) const;
bool is_terminating(void) const;
bool is_compatible_to(py::ref p) const;
GiNaC::ex convert_to_poly_0(void) const;
GiNaC::ex convert_to_poly_1(bool no_order) const;
GiNaC::ex get_var(void) const;
GiNaC::ex get_point(void) const;
#else // PYGINAC_EX_PROTOS
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
GiNaC::pseries from_python (PyObject* o, py::type<const GiNaC::pseries &>);
BOOST_PYTHON_END_CONVERSION_NAMESPACE
GiNaC::ex pseries_2(const GiNaC::relational & rel, const GiNaC::epvector & ops);
GiNaC::ex pseries_1(const GiNaC::pseries & ps);
GiNaC::ex series_to_poly_0(const GiNaC::pseries & expr);
GiNaC::ex series_to_poly_1(const GiNaC::pseries & expr, bool no_order);
#endif // !PYGINAC_EX_PROTOS
#else  // PYGINAC_PROTOS
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
GiNaC::pseries from_python (PyObject* o, py::type<const GiNaC::pseries &>) {
  if (ExInstance_Check(o)) {
    GiNaC::ex e = from_python(o, py::type<const GiNaC::ex &>());
    if (is_ex_exactly_of_type(e, pseries))
      return ex_to_pseries(e);
  }
  PYGINAC_FROMPYTHON_TYPEERROR(pseries,(ex(pseries)|ex(relational),(2-sequence)-sequence));
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
/*F_DT is_pseries(obj)
  Check if `obj' is pseries.
*/
/*M_DT is_pseries(self)
  Check if object is pseries.
*/
bool ex::is_pseries(void) const { return is_ex_exactly_of_type(*this, pseries); }
/*F_DT is_terminating(obj)
  Check if `obj' is terminating pseries (i.e., without Order term).
*/
/*M_DT is_terminating(self)
  Check if object is terminating pseries (i.e., without Order term).
*/
bool ex::is_terminating(void) const {
  return is_ex_exactly_of_type(*this, pseries)
    && ex_to_pseries(*this).is_terminating();
}
/*F_DT is_compatible_to(obj, other)
  Check if `obj' is compatible pseries to pseries `other'
  (i.e., expansion variable and point are the same).
*/
/*M_DT is_compatible_to(self, other)
  Check if object is compatible pseries to pseries `other'
  (i.e., expansion variable and point are the same).
*/
/*TODO allow arbitrary arguments for is_compatiple_to() */
bool ex::is_compatible_to(py::ref p) const {
  return is_ex_exactly_of_type(*this, pseries)
    && ex_to_pseries(*this).is_compatible_to(BOOST_PYTHON_CONVERSION::from_python(p.get(), py::type<const GiNaC::pseries &>()));
}

/*F_DT pseries(rel,seq)
  Return power series.
  rel - holds an expansion variable and an expansion point
        (as a 2-sequence|ex(relational)).
  seq - holds pairs of coefficients and exponents.
  For example,
    pseries((x,x0),[(a,-1),(b,2),(Order(1),5)])
  corresponds to power series
    a / (x-x0) + b * (x-x0) ** 2 + Order((x-x0) ** 5)

  pseries(pseries(...)) -> pseries(...)
*/
GiNaC::ex pseries_2(const GiNaC::relational & rel, const GiNaC::epvector & ops) {
  return GiNaC::pseries(rel, ops);
}
GiNaC::ex pseries_1(const GiNaC::pseries & ps) {
  return ps;
}
/*M_DT to_poly(self, no_order = 0)
  Convert the pseries object to an ordinary polynomial.
  If no_order, then discard higher order terms.
  `self' must be ex(pseries).
*/
GiNaC::ex ex::convert_to_poly_0(void) const {
  if (is_pseries())
    return ex_to_pseries(*this).convert_to_poly();
  PyErr_SetString(PyExc_NotImplementedError, "ex.to_poly() can be used only for ex(pseries)");
  throw py::error_already_set();
}
GiNaC::ex ex::convert_to_poly_1(bool no_order) const {
  if (is_pseries())
    return ex_to_pseries(*this).convert_to_poly(no_order);
  PyErr_SetString(PyExc_NotImplementedError, "ex.to_poly() can be used only for ex(pseries)");
  throw py::error_already_set();
}
/*F_DT series_to_poly(expr, no_order = 1)
  Convert the pseries object to an ordinary polynomial.
  If no_order, then discard higher order terms.
  `expr' must be ex(pseries).
*/
GiNaC::ex series_to_poly_0(const GiNaC::pseries & expr) {
  return expr.convert_to_poly();
}
GiNaC::ex series_to_poly_1(const GiNaC::pseries & expr, bool no_order) {
  return expr.convert_to_poly(no_order);
}
/*M_DT get_var(self)
  Get the expansion variable.
  `self' must be ex(pseries).
*/
/*M_DT get_point(self)
  Get the expansion point.
  `self' must be ex(pseries).
 */
GiNaC::ex ex::get_var(void) const {
  if (is_pseries())
    return ex_to_pseries(*this).get_var();
  PyErr_SetString(PyExc_NotImplementedError, "ex.get_var() can be used only for ex(pseries)");
  throw py::error_already_set();
}
GiNaC::ex ex::get_point(void) const {
  if (is_pseries())
    return ex_to_pseries(*this).get_point();
  PyErr_SetString(PyExc_NotImplementedError, "ex.get_point() can be used only for ex(pseries)");
  throw py::error_already_set();
}
#endif // !PYGINAC_PROTOS
#endif // !PYGINAC_DEFS




FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>