File:  [CENS] / python / pyGiNaC / wrappers / exvector.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: exvector.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 = []
uses = ['ex']

#STARTPROTO
class exvector:
    pass
#ENDPROTO

wrapperclass = '''
class exvector_w : public GiNaC::exvector {
  PyObject * self;
public:
  exvector_w(python::ref obj);
  
  exvector_w(PyObject * self_): GiNaC::exvector(), self(self_) {
    DEBUG_C("exvector_w::exvector_w()");
  }
  exvector_w(PyObject* self_, const std::size_t n)
  : GiNaC::exvector(n), self(self_) {
    DEBUG_C("exvector_w::exvector_w(size_t)");
  }
  exvector_w(PyObject* self_, const GiNaC::exvector & obj)
  : GiNaC::exvector(obj), self(self_) {
    DEBUG_C("exvector_w::exvector_w(GiNaC::exvector)");
  }
  exvector_w(PyObject * self_, python::ref obj)
  : GiNaC::exvector(exvector_w(obj)), self(self_) {
    DEBUG_C("exvector_w::exvector_w(ref)");
  }

  ~exvector_w() {
    DEBUG_C("exvector_w::~exvector_w()");
  }
  std::string python_str(void) const {
    std::ostrstream os;
    GiNaC::python_str(os, *this);
    os << std::ends;
    return os.str();
  }
  std::string python_repr(void) const {
    std::ostrstream os;
    os << "exvector(";
    GiNaC::python_repr(os, *this);
    os << ")" << std::ends;
    return os.str();
  }
  void delitem(const std::size_t);

  PyObject * getitem(python::ref);
  void setitem(python::ref, python::ref);
  
  static void throw_index_error_if(const GiNaC::exvector& v, const std::size_t key) {
    if ((key >= v.size()) || (key < 0)) {
        PyErr_SetObject(PyExc_IndexError, BOOST_PYTHON_CONVERSION::to_python(key));
        throw python::error_already_set();
    }
  }
  
};

'''
protos = '''
'''

builder = '''
python::class_builder<exvector_w> exvector_w_class(this_module, "_exvector_w");
python::class_builder<GiNaC::exvector, exvector_w> exvector_class(this_module, "exvector");
exvector_class.declare_base(exvector_w_class);
exvector_py_class = python::as_object(exvector_class.get_extension_class());
'''

constructors = '''
exvector_class.def(python::constructor<>());
exvector_class.def(python::constructor<std::size_t>());
exvector_class.def(python::constructor<const GiNaC::exvector &>());
exvector_class.def(python::constructor<python::ref>());
'''

defs = '''
exvector_class.def(&exvector_w::python_str, "__str__");
exvector_class.def(&exvector_w::python_repr, "__repr__");

exvector_class.def(&exvector_w::size, "__len__");
exvector_class.def(&exvector_w::getitem, "__getitem__");
exvector_class.def(&exvector_w::setitem, "__setitem__");
exvector_class.def(&exvector_w::delitem, "__delitem__");
'''

implementation = '''
GiNaC::exvector as_exvector_w(python::ref obj) {
  return exvector_w(obj);
}

exvector_w::exvector_w(python::ref obj)
: GiNaC::exvector() {
  PyObject * o = obj.get();
  DEBUG_C("exvector_w::exvector_w(raw:ref)");
  if (PySequence_Check(o)) {
    std::size_t l = PySequence_Length(o);
    for(std::size_t i = 0; i < l; ++i)
      this->push_back(ex_w(python::ref(PySequence_GetItem(o,i))));
  } else
    this->push_back(ex_w(python::ref(o)));
}

void exvector_w::delitem(const std::size_t key) {
  GiNaC::exvector::iterator viter = (*this).begin();
  DEBUG_M("exvector::detitem");
  exvector_w::throw_index_error_if(*this, key);
  (*this).erase(&viter[key]);
}

PyObject * exvector_w::getitem (python::ref index) {
  PyObject * o = index.get();
  int l = this->size();
  int start, step_size, n_steps;
  DEBUG_M("exvector::getitem(ref)");
  start = parse_subindex(o, &step_size, &n_steps, l);
  DEBUG_M("start="<<start<<",step_size="<<step_size<<",n_steps="<<n_steps)
  if (start < 0)
    throw python::error_already_set();
  if (n_steps==PseudoIndex || n_steps==RubberIndex) {
    PyErr_SetString(PyExc_NotImplementedError, "exvector() getitem PseudoIndex|RubberIndex");
    throw python::error_already_set();
  }
  if (n_steps==SingleIndex)
    return BOOST_PYTHON_CONVERSION::to_python((*this)[start]);
  GiNaC::exvector v(0);
  int k = start;
  for (int i=0;i<n_steps;++i,k += step_size)
    v.push_back((*this)[(k==l)?0:k]);
  return BOOST_PYTHON_CONVERSION::to_python(v);
}

void exvector_w::setitem (python::ref index, python::ref obj) {
  PyObject * o = index.get();
  int l = this->size();
  int start, step_size, n_steps;
  DEBUG_M("exvector::setitem(ref,ref)");

  start = parse_subindex(o, &step_size, &n_steps, l);
  DEBUG_M("start="<<start<<",step_size="<<step_size<<",n_steps="<<n_steps)

  if (start == -1)
    throw python::error_already_set();
  if (n_steps==PseudoIndex || n_steps==RubberIndex) {
    PyErr_SetString(PyExc_NotImplementedError, "exvector() setitem PseudoIndex|RubberIndex");
    throw python::error_already_set();
  }
  if (n_steps==SingleIndex) {
    GiNaC::exvector::iterator viter = (*this).begin();
    viter[start] = ex_w(obj);
  } else {
    exvector_w v(obj);
    if (n_steps != int(v.size())) {
       PyErr_SetString(PyExc_IndexError, "exvector() not matching index vectors");
       throw python::error_already_set();
    }
    int k = start;
    GiNaC::exvector::iterator viter = (*this).begin();
    GiNaC::exvector::iterator viter2 = (v).begin();
    for (int i=0;i<n_steps;++i,k += step_size)
      viter[(k==l)?0:k] = viter2[i];
  }
}


'''

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