File:  [CENS] / python / pyGiNaC / wrappers / relational.py
Revision 1.6: 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.6 $
# $Id: relational.py,v 1.6 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 = ['ex']

#STARTPROTO
class relational:
    eq = 0

class relational(basic):
    """A relation consisting of two expressions and a logical
    relation between them."""
    global relational
    eq = 0
    def __init__(self,lhs,rhs,oper=relational.eq):
        """relational(lhs,rhs) - construct a relation `lhs==rhs'
    relation(lhs,rhs,oper) - construct a relation `lhs oper rhs'
         where `oper' is relational.[eq,ne,lt,le,gt,ge].
    relation(ex(relation(...))) - returns relation(...)
    """
    def __nonzero__(self):
        """Returns 1 if relation holds.
    Note that (a<b) == false does not imply (a>=b) == true.  A false
    result means the comparison is either false or undecidable (except
    for !=, where true means unequal or undecidable).
    """
    def lhs(self):
        """Left hand side of relational."""
    def rhs(self):
        """Right hand side of relational."""

#ENDPROTO

wrapperclass = '''

GiNaC::relational relational_from_ref(python::ref);

class relational_w : public GiNaC::relational {
  PyObject * self;
public:
  relational_w(const GiNaC::relational & r): GiNaC::relational(r) {
    DEBUG_C("relational_w::relational_w(raw:relational)");
  }
  relational_w(const GiNaC::ex &lhs, const GiNaC::ex &rhs)
  : GiNaC::relational(lhs,rhs) {
    DEBUG_C("relational_w::relational_w(raw:ex,ex)");
  }
  relational_w(python::ref r): GiNaC::relational(relational_from_ref(r)) {
    DEBUG_C("relational_w::relational_w(raw:ref)");
  }

  relational_w(PyObject * self_, const GiNaC::ex & r)
  : GiNaC::relational(ex_to_relational_w(r)), self(self_) {
    DEBUG_C("relational_w::relational_w(ex)");
  }
  relational_w(PyObject * self_, const GiNaC::relational & r)
  : GiNaC::relational(r), self(self_) {
    DEBUG_C("relational_w::relational_w(relational)");
  }
  relational_w(PyObject * self_, const GiNaC::ex &lhs, const GiNaC::ex &rhs)
  : GiNaC::relational(lhs,rhs), self(self_) {
    DEBUG_C("relational_w::relational_w(ex,ex)");
  }
  
  relational_w(PyObject * self_, const GiNaC::ex &lhs, const GiNaC::ex &rhs, GiNaC::relational::operators oper)
  : GiNaC::relational(lhs,rhs,oper), self(self_) {
    DEBUG_C("relational_w::relational_w(ex,ex,operator)");
  }
  relational_w(PyObject * self_, python::ref lhs, python::ref rhs)
  : GiNaC::relational(ex_w(lhs),ex_w(rhs)), self(self_) {
    DEBUG_C("relational_w::relational_w(ref,ref)");
  }
  relational_w(PyObject * self_, python::ref lhs, python::ref rhs, GiNaC::relational::operators oper)
  : GiNaC::relational(ex_w(lhs),ex_w(rhs),oper), self(self_) {
    DEBUG_C("relational_w::relational_w(ref,ref,operator)");
  }

  ~relational_w() {
    DEBUG_C("relational_w::~relational_w()");
  }
   bool nonzero(void) const {
    DEBUG_M("relational_w::nonzero()");
    return *this;
  }
   UNEX_RET lhs_w(void) const { return UNEX(this->lhs()); }
   UNEX_RET rhs_w(void) const { return UNEX(this->rhs()); }
};

template class python::enum_as_int_converters<GiNaC::relational::operators>;

'''

protos = '''
'''

builder = '''
python::class_builder<relational_w> relational_w_class(this_module, "_relational_w");
python::class_builder<GiNaC::relational, relational_w> relational_class(this_module, "relational");
relational_py_class = python::as_object(relational_class.get_extension_class());
relational_class.declare_base(relational_w_class);
relational_class.declare_base(basic_class);
'''

constructors = '''
relational_class.def(python::constructor<const GiNaC::relational &>());
relational_class.def(python::constructor<const GiNaC::ex &>());
relational_class.def(python::constructor<const GiNaC::ex &, const GiNaC::ex &>());
relational_class.def(python::constructor<python::ref, python::ref>());
relational_class.def(python::constructor<const GiNaC::ex &, const GiNaC::ex &, GiNaC::relational::operators>());
relational_class.def(python::constructor<python::ref, python::ref, GiNaC::relational::operators>());
'''
defs = '''
relational_class.def(&basic_w::python_str, "__str__");
relational_class.def(&basic_w::python_repr, "__repr__");

relational_class.def(&relational_w::nonzero, "__nonzero__");
relational_class.def(&relational_w::lhs_w, "lhs");
relational_class.def(&relational_w::rhs_w, "rhs");
relational_class.add(PyInt_FromLong(relational_w::equal), "eq");
relational_class.add(PyInt_FromLong(relational_w::not_equal), "ne");
relational_class.add(PyInt_FromLong(relational_w::less), "lt");
relational_class.add(PyInt_FromLong(relational_w::less_or_equal), "le");
relational_class.add(PyInt_FromLong(relational_w::greater), "gt");
relational_class.add(PyInt_FromLong(relational_w::greater_or_equal), "ge");
'''

implementation = '''
EX_TO_BASIC(relational)

GiNaC::relational relational_from_ref(python::ref r) {
  PyObject * o = r.get();
  if (RelationalInstance_Check(o)) {
    Py_INCREF(o);
    return BOOST_PYTHON_CONVERSION::from_python(o, python::type<const GiNaC::relational &>());
  }
  if (PySequence_Check(o) && PySequence_Length(o)==2) {
     return GiNaC::relational(ex_w(PySequence_GetItem(o,0)), ex_w(PySequence_GetItem(o,1)));
  }
  PyErr_SetString(PyExc_TypeError, "relational_from_ref() argument must be relational|2-seq");
  throw python::error_already_set();
}

'''

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