File:  [CENS] / python / pyGiNaC / wrappers4 / ginac_enhancements.cpp
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Mon Nov 26 22:37:44 2001 UTC (16 years ago) by pearu
Branches: MAIN
CVS tags: HEAD
See NEWS.txt (impl. matrix)

#include <Python.h>
#include <ginac/ginac.h>
#include <boost/python/class_builder.hpp>

namespace py = boost::python;

namespace pyginac {

  GiNaC::ex constant_dummy(void);
  PyObject* constant_cache_get(unsigned h);
//   void constant_cache_erase(unsigned h);
  const GiNaC::ex & ex_from_py(PyObject* o);

}

namespace GiNaC {
  
//   void constant::destroy(bool call_parent)
//   {
//     delete number;
//     pyginac::constant_cache_erase(gethash());
//     if (call_parent) {
//       inherited::destroy(call_parent);
//     }
//   }

  ex constant::evalf(int level) const {
    if (ef==pyginac::constant_dummy) {
      PyObject * ret = pyginac::constant_cache_get(gethash());
      if (ret==NULL) {
	PyErr_SetString(PyExc_RuntimeError,"constant() internal error (constant_cache_get failed)");
	throw py::error_already_set();
      }
      if (PyCallable_Check(ret))
	ret = PyObject_CallFunction(ret,NULL);
      if (ret==NULL)
	throw py::error_already_set();
      if (ret==Py_None)
	return *this;
      ex r = pyginac::ex_from_py(ret);
      return r.evalf();
    }
    if (ef!=0) {
      return ef();
    } else if (number != 0) {
      return number->evalf();
    }
    return *this;
  }

  void constant::print(const print_context & c, unsigned level) const
  {
    if (is_a<print_tree>(c)) {
      c.s << std::string(level, ' ') << name << " (" << class_name() << ")"
	  << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
	  << std::endl;
    } else if (is_a<print_latex>(c))
      c.s << TeX_name;
    else if (is_a<print_python_repr>(c)) {
      c.s << class_name() << "('" << name << "'";
      if (ef==pyginac::constant_dummy) {
	PyObject * ret = pyginac::constant_cache_get(gethash());
	if (ret==NULL) {
	  PyErr_SetString(PyExc_RuntimeError,"constant() internal error (constant_cache_get failed)");
	  throw py::error_already_set();
	}
	if (ret!=Py_None)
	  c.s << "," << PyString_AsString(PyObject_Repr(ret));
      }
      if (TeX_name != "\\mbox{" + name + "}")
	c.s << ",TeX_name='" << TeX_name << "'";
      c.s << ')';
    } else
      c.s << name;
  } 
}

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