File:  [CENS] / python / pyGiNaC / wrappers3 / relational_py.cpp
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Thu Apr 26 19:44:48 2001 UTC (16 years, 6 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.3 $
# $Id: relational_py.cpp,v 1.3 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
  Relational
  ------

  >>> from ginac import relational
  >>> print relational(2,3)
  2 == 3
  >>> print relational(2,3,'==')
  2 == 3
  >>> print relational(2,3,'!=')
  2 != 3
  >>> print relational(2,3,'<')
  2 < 3
  >>> print relational(2,3,'<=')
  2 <= 3
  >>> print relational(2,3,'>')
  2 > 3
  >>> print relational(2,3,'>=')
  2 >= 3
  >>> relational(2,3,'aaaa')
  Traceback (most recent call last):
  ...
  TypeError: relational::operators from_python() argument must be (string: == | != | < | <= | > | >=) (got <type 'string'>)
  >>> relational(2,3,'==')
  relational(numeric('2'), numeric('3'))
  >>> relational(2,3,'!=')
  relational(numeric('2'), numeric('3'), '!=')
  >>> relational(2,3,'<')
  relational(numeric('2'), numeric('3'), '<')
  >>> relational(2,3,'<=')
  relational(numeric('2'), numeric('3'), '<=')
  >>> relational(2,3,'>')
  relational(numeric('2'), numeric('3'), '>')
  >>> relational(2,3,'>=')
  relational(numeric('2'), numeric('3'), '>=')
  >>> relational(relational(2,3))
  relational(numeric('2'), numeric('3'))
  >>> print relational((2,3))
  2 == 3
  >>> print relational((2,3,'>'))
  2 > 3
  >>> from ginac import rhs,lhs,symbol
  >>> print lhs(relational ((2,3)))
  2
  >>> print rhs(relational ((2,3)))
  3
  >>> print relational ((2,3)).lhs()
  2
  >>> print relational ((2,3)).rhs()
  3
  >>> lhs(symbol())
  Traceback (most recent call last):
  ...
  TypeError: ex.lhs() can be used only for ex(relational).
  >>> not relational(2,3)
  1
  >>> not relational(2,2)
  0
  >>> from ginac import is_relational,numeric
  >>> is_relational(2), is_relational(relational(2,3))
  (0, 1)
  >>> numeric().is_relational(), relational(2,3).is_relational()
  (0, 1)

  >>> a = relational(2,3)
  >>> print a
  2 == 3
  >>> a[0] = 4
  >>> print a
  4 == 3
*/


#ifdef PYGINAC_DEFS
this_module.def(&ex::is_relational, "is_relational");
this_module.def(&return_false, "is_relational");
ex_class.def(&ex::is_relational, "is_relational");
this_module.def(&relational_1, "relational");
this_module.def(&relational_2, "relational");
this_module.def(&relational_3, "relational");
ex_class.def(&ex::nonzero, "__nonzero__");
if ((relational_op_map = PyDict_New())==NULL)
  throw py::error_already_set();
PyDict_SetItemString(relational_op_map,"==",PyInt_FromLong(0));
PyDict_SetItemString(relational_op_map,"!=",PyInt_FromLong(1));
PyDict_SetItemString(relational_op_map,"<",PyInt_FromLong(2));
PyDict_SetItemString(relational_op_map,"<=",PyInt_FromLong(3));
PyDict_SetItemString(relational_op_map,">",PyInt_FromLong(4));
PyDict_SetItemString(relational_op_map,">=",PyInt_FromLong(5));
//cerr<<"relational_op_map="<<PyString_AsString(PyObject_Repr(relational_op_map))<<endl;

#else
#ifdef PYGINAC_EX_PROTOS
#define PYGINAC_PROTOS
#endif
#ifdef PYGINAC_PROTOS
#ifdef PYGINAC_EX_PROTOS
bool nonzero(void) const;
bool is_relational(void) const;
#else // PYGINAC_EX_PROTOS
GiNaC::ex relational_1(const GiNaC::relational &);
GiNaC::ex relational_2(py::ref, py::ref);
GiNaC::ex relational_3(py::ref, py::ref, GiNaC::relational::operators);
PyObject * relational_op_map = NULL;
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
GiNaC::relational from_python (PyObject* o, py::type<const GiNaC::relational &>);
GiNaC::relational::operators from_python (PyObject* o, py::type<GiNaC::relational::operators>);
BOOST_PYTHON_END_CONVERSION_NAMESPACE
#endif // !PYGINAC_EX_PROTOS
#else  // PYGINAC_PROTOS
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
GiNaC::relational from_python (PyObject* o, py::type<const GiNaC::relational &>) {
  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, relational))
      return ex_to_relational(e);
  }
  if (PySequence_Check(o)) {
    std::size_t l = PySequence_Length(o);
    switch (l) {
    case 2: return GiNaC::relational(ex_from_ref(PySequence_GetItem(o,0)),
				     ex_from_ref(PySequence_GetItem(o,1)));
    case 3: return GiNaC::relational(ex_from_ref(PySequence_GetItem(o,0)),
				     ex_from_ref(PySequence_GetItem(o,1)),
				     from_python(PySequence_GetItem(o,2),py::type<GiNaC::relational::operators>()));
    }
  }
  PYGINAC_FROMPYTHON_TYPEERROR(relational,(2-sequence|3-sequence|ex(relational)));
}
GiNaC::relational::operators from_python (PyObject* o, py::type<GiNaC::relational::operators>) {
  if (PyString_Check(o)) {
    PyObject *v = PyDict_GetItem(relational_op_map,o);
    if (v!=NULL && PyInt_Check(v))
      switch (PyInt_AS_LONG(v)) {
      case 0: return GiNaC::relational::equal;
      case 1: return GiNaC::relational::not_equal;
      case 2: return GiNaC::relational::less;
      case 3: return GiNaC::relational::less_or_equal;
      case 4: return GiNaC::relational::greater;
      case 5: return GiNaC::relational::greater_or_equal;
      }
  }
  PYGINAC_FROMPYTHON_TYPEERROR(relational::operators,(string: == | != | < | <= | > | >=));
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
/*F_DT relational(*args)
  relational(l,r) - logical relation `l == r'.
  relational(l,r,'!=') - logical relation `l != r'.
  relational(l,r,'<') - logical relation `l < r'.
  relational(l,r,'<=') - logical relation `l <= r'.
  relational(l,r,'>') - logical relation `l > r'.
  relational(l,r,'>=') - logical relation `l >= r'.
  relational(relational(...)) -> relational(...)
  relational(2-seq) - logical relation `seq[0] == seq[1]'.
  relational(3-seq) - logical relation `seq[0] seq[2] seq[1]'.
 */
GiNaC::ex relational_1(const GiNaC::relational & r) {
  return r;
}
GiNaC::ex relational_2(py::ref lh, py::ref rh) {
  return GiNaC::relational(ex_from_ref(lh), ex_from_ref(rh));
}
GiNaC::ex relational_3(py::ref lh, py::ref rh, GiNaC::relational::operators op) {
  return GiNaC::relational(ex_from_ref(lh), ex_from_ref(rh), op);
}
/*M_DT __nonzero__(self)
  `self' must be ex(relational)|ex(numeric)
 */
bool ex::nonzero(void) const {
  if (is_ex_exactly_of_type(*this, relational))
    return ex_to_relational(*this);
  if (is_ex_exactly_of_type(*this, numeric))
    return ex_to_numeric(*this) != 0;
  PyErr_SetString(PyExc_NotImplementedError, "ex.__nonzero__() can be used only for ex(relational)|ex(numeric)");
  throw py::error_already_set();
}
/*F_DT is_relational(obj)
  Check if `obj' is relational.
*/
/*M_DT is_relational(self)
  Check if object is relational.
*/
bool ex::is_relational(void) const { return is_ex_exactly_of_type(*this, relational); }

#endif // !PYGINAC_PROTOS
#endif // !PYGINAC_DEFS




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