File:  [CENS] / python / pyGiNaC / wrappers3 / power_py.cpp
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Tue Apr 24 09:01:59 2001 UTC (16 years, 7 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Impl. function, etc. for 3rd way. It looks promising.

/*
# This file is part of the PyGiNaC package.
# http://cens.ioc.ee/projects/pyginac/
#
# $Revision: 1.2 $
# $Id: power_py.cpp,v 1.2 2001-04-24 09:01:59 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
  ------

  >>> from ginac import power,symbol,is_power
  >>> power(2,3)
  numeric('8')
  >>> power(symbol('a'),2)
  power(symbol('a'), numeric('2'))
  >>> power(symbol('a'),0)
  numeric('1')
  >>> p = power(symbol('a'),symbol('b'))
  >>> p.is_power(), symbol().is_power()
  (1, 0)
  >>> is_power(p), is_power(1)
  (1, 0)
  >>> from ginac import sqrt
  >>> print sqrt(-1)
  I
  >>> print sqrt(symbol('aa'))
  sqrt(aa)
*/


#ifdef PYGINAC_DEFS
this_module.def(&power_1, "power");
this_module.def(&power_2, "power");
this_module.def(&ex::is_power, "is_power");
this_module.def(&return_false, "is_power");
ex_class.def(&ex::is_power, "is_power");
this_module.def(&sqrt_1, "sqrt");
#else

#ifdef PYGINAC_EX_PROTOS
#define PYGINAC_PROTOS
#endif
#ifdef PYGINAC_PROTOS
#ifdef PYGINAC_EX_PROTOS
bool is_power(void) const;
#else // PYGINAC_EX_PROTOS
GiNaC::ex sqrt_1(py::ref obj);
GiNaC::ex power_1(py::ref obj);
GiNaC::ex power_2(py::ref base, py::ref exponent);
#endif // !PYGINAC_EX_PROTOS
#else  // PYGINAC_PROTOS

/*F_DT is_power(obj)
  Check if `obj' is power.
*/
/*M_DT is_power(self)
  Check if object is power.
*/
bool ex::is_power(void) const { return is_ex_exactly_of_type(*this, power); }

/*F_DT power(base, exponent)
  Power base ** exponent.
  power(power(...)) -> power(...)
 */

GiNaC::ex power_1(py::ref obj)
{
  PyObject * o = obj.get();
  if (ExInstance_Check(o)) {
    GiNaC::ex e = BOOST_PYTHON_CONVERSION::from_python(o, py::type<const GiNaC::ex &>());
    if (is_ex_exactly_of_type(e, power))
      return e;
  }
  PyErr_SetString(PyExc_TypeError, "power() argument must be (ex,ex|power)");
  throw py::error_already_set();
}
GiNaC::ex power_2(py::ref base, py::ref exponent) {
  return GiNaC::power(ex_from_ref(base), ex_from_ref(exponent));
}
/*F_DT sqrt(x)
  Square root.
 */
GiNaC::ex sqrt_1(py::ref obj) {
  return GiNaC::power(ex_from_ref(obj), GiNaC::numeric(1)/2);
}
  
#endif // !PYGINAC_PROTOS
#endif // !PYGINAC_DEFS

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