File:  [CENS] / python / pyGiNaC / wrappers4 / _ginac.cpp
Revision 1.16: download - view: text, annotated - select for diffs - revision graph
Mon Dec 9 12:14:35 2002 UTC (14 years, 11 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

/*
  This file is part of the PyGiNaC package.
  http://cens.ioc.ee/projects/pyginac/

  $Revision: 1.16 $
  $Id: _ginac.cpp,v 1.16 2002-12-09 12:14:35 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.
*/

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

#include "cln_py.hpp"
#include "slice.h"

namespace py = boost::python;

namespace GiNaC {

  template <class T> inline T &ex_to_nonconst(const ex &e)
  {  return const_cast<T&>(ex_to<T>(e));  }

}

namespace pyginac {

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

}

using namespace pyginac;

namespace {

PyObject* ex_py_class = NULL;

  /* prototypes */

#include "converters.cpp"
#include "aux.cpp"
#include "ex.cpp"
#include "symbol.cpp"
#include "numeric.cpp"
#include "power.cpp"
#include "add.cpp"
#include "mul.cpp"
#include "lst.cpp"
#include "relational.cpp"
#include "constant.cpp"
#include "function.cpp"
#include "fderivative.cpp"
#include "ncmul.cpp"
#include "matrix.cpp"
#include "wildcard.cpp"
#include "pseries.cpp"
#include "funcs.cpp"

}

BOOST_PYTHON_MODULE_INIT(_ginac)
{

      py::module_builder this_module("_ginac");
      py::class_builder<GiNaC::ex> ex_class(this_module, "ex");
      ex_class.def(py::constructor<>());
      ex_class.def(py::constructor<const GiNaC::ex &>());
      
      ex_py_class = py::as_object(ex_class.get_extension_class());
      
      ex_class.def(py::operators<(py::op_neg | py::op_add | py::op_sub | py::op_mul | py::op_div)>());
      
      /* definitions */
      
#include "aux.cpp"
#include "ex.cpp"
#include "symbol.cpp"
#include "numeric.cpp"
#include "power.cpp"
#include "add.cpp"
#include "mul.cpp"
#include "lst.cpp"
#include "relational.cpp"
#include "constant.cpp"
#include "function.cpp"
#include "fderivative.cpp"
#include "ncmul.cpp"
#include "matrix.cpp"
#include "wildcard.cpp"
#include "pseries.cpp"
#include "funcs.cpp"

}
  /* implementation */

namespace {

#include "converters.cpp"
#include "aux.cpp"
#include "ex.cpp"
#include "symbol.cpp"
#include "numeric.cpp"
#include "power.cpp"
#include "add.cpp"
#include "mul.cpp"
#include "lst.cpp"
#include "relational.cpp"
#include "constant.cpp"
#include "function.cpp"
#include "fderivative.cpp"
#include "ncmul.cpp"
#include "matrix.cpp"
#include "wildcard.cpp"
#include "pseries.cpp"
#include "funcs.cpp"

}

namespace pyginac {
  
  const GiNaC::ex & ex_from_py(PyObject* o) {
    return ex_nonconst_from_py(o);
  }

  GiNaC::ex pyginac::constant_dummy(void) {
    PyErr_SetString(PyExc_RuntimeError,"constant() internal error (constant_dummy accessed)");
    throw py::error_already_set();
  }

  std::map<unsigned, py::ref> constant_cache;

  PyObject* constant_cache_get(unsigned h) {
    const std::map<unsigned, py::ref>::const_iterator viter = constant_cache.find(h);
    if (viter==constant_cache.end())
      return NULL;
    return viter->second.get();
  }

  void constant_cache_set(unsigned h, PyObject* init) {
    Py_INCREF(init);
    constant_cache[h] = init;
  }

  void constant_cache_erase(void) {
    std::map<unsigned, py::ref>::iterator viter = constant_cache.begin();
    while (viter!=constant_cache.end()) {
//       std::cout << "Clearing constant(hash="<<viter->first<<")" << std::endl;
      Py_DECREF(viter->second.get());
      constant_cache.erase(viter->first);
      viter++;
    }
  }

//   void constant_cache_erase(unsigned h) {
//    std::map<unsigned, py::ref>::iterator viter = constant_cache.find(h);
//    std::cout << "Erasing constant(hash="<<h<<") from cache." << std::endl;
//    if (viter!=constant_cache.end()) {
//      PyObject* obj = viter->second.get();
//      Py_DECREF(obj);
//      constant_cache.erase(h);
//    }
//   }

}

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