File:  [CENS] / python / pyGiNaC / wrappers / power.py
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Tue Apr 17 22:39:25 2001 UTC (16 years, 7 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Fixed bugs. Exposed/checked functions. Started testing framework.

# This file is part of the PyGiNaC package.
# http://cens.ioc.ee/projects/pyginac/
#
# $Revision: 1.4 $
# $Id: power.py,v 1.4 2001-04-17 22:39:25 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.
#

depends = ['basic']
uses = []

#STARTPROTO

class power(basic):
    """A two-component object, a basis and and exponent representing
    exponentiation."""
    def __init__(self,basis,exponent):
        """power(b,e) - construct b ** e
    power(ex(power(...))) - returns power(...)
    """
def pow(b,e):
    """Symbolic exponentiation.
    Returns a power-object as a new expression.
    b - the basis expression
    e - the exponent expression
    """
def sqrt(x):
    """Square root expression.
    Returns a power-object with exponent 1/2."""

#ENDPROTO

wrapperclass = '''
class power_w : public GiNaC::power {
  PyObject * self;
public:
  power_w(python::ref r) {
    DEBUG_C("power_w(raw:ref)");
    PyErr_SetString(PyExc_NotImplementedError, "power(raw:ref)");
    throw python::error_already_set();
  }

  power_w(PyObject * self_, const GiNaC::power & p)
  : GiNaC::power(p), self(self_) {
    DEBUG_C("power_w(power)");
  }
  power_w(PyObject * self_, const GiNaC::ex & p)
  : GiNaC::power(ex_to_power_w(p)), self(self_) {
    DEBUG_C("power_w(ex)");
  }
  power_w(PyObject * self_, const GiNaC::ex & base, const GiNaC::ex & exponent)
  : GiNaC::power(base,exponent), self(self_) {
    DEBUG_C("power_w(ex,ex)");
  }
#ifdef PYGINAC_numeric
  power_w(PyObject * self_, const GiNaC::ex & base, const GiNaC::numeric & exponent)
  : GiNaC::power(base,exponent), self(self_) {
    DEBUG_C("power_w(ex,numeric)");
  }
#endif
  power_w(PyObject * self_, python::ref base, python::ref exponent)
  : GiNaC::power(ex_w(base),ex_w(exponent)), self(self_) {
    DEBUG_C("power_w(ref,ref)");
  }
  ~power_w() {
    DEBUG_C("~power_w()");
  }
};

UNEX_RET pow_w (python::ref b, python::ref e) {
  return UNEX(GiNaC::pow(ex_w(b),ex_w(e)));
}

UNEX_RET sqrt_w (python::ref x) {
  GiNaC::ex a = ex_w(x);
#ifdef PYGINAC_numeric
  if (is_ex_exactly_of_type(a, numeric))
    return UNEX(GiNaC::ex(GiNaC::sqrt(ex_to_numeric(a))));
#endif
  return UNEX(GiNaC::sqrt(a));
}

'''

builder = '''
python::class_builder<power_w> power_w_class(this_module, "_power_w");
python::class_builder<GiNaC::power, power_w> power_class(this_module, "power");
power_py_class = python::as_object(power_class.get_extension_class());
power_class.declare_base(power_w_class);
power_class.declare_base(basic_class);
'''

constructors = '''
power_class.def(python::constructor<const GiNaC::power &>());
power_class.def(python::constructor<const GiNaC::ex &>());
power_class.def(python::constructor<const GiNaC::ex &,const GiNaC::ex &>());
power_class.def(python::constructor<python::ref, python::ref>());
#ifdef PYGINAC_numeric
power_class.def(python::constructor<const GiNaC::ex &,const GiNaC::numeric &>());
#endif
'''

defs = '''
power_class.def(&basic_w::python_str, "__str__");
power_class.def(&basic_w::python_repr, "__repr__");
power_class.def(&basic_w::coerce, "__coerce__");
BASIC_OPS(power)

this_module.def(pow_w, "pow");
this_module.def(sqrt_w, "sqrt");
'''

implementation = '''
EX_TO_BASIC(power)
'''


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