File:  [CENS] / python / pyGiNaC / wrappers3 / symbol_py.cpp
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Sun May 20 08:58:02 2001 UTC (16 years, 6 months ago) by pearu
Branches: MAIN
CVS tags: HEAD
Fixed get_precedence, cleaned up code, requires current CVS verion of GiNaC

/*
# This file is part of the PyGiNaC package.
# http://cens.ioc.ee/projects/pyginac/
#
# $Revision: 1.5 $
# $Id: symbol_py.cpp,v 1.5 2001-05-20 08:58:02 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
  Symbol
  ------

  >>> from ginac import symbol,ex,is_symbol

  To create a dummy symbol (it will have a name of the form `symbol\d+'):
  >>> print repr(symbol())[:14]
  symbol('symbol

  To create a named symbol:
  >>> symbol('name')
  symbol('name')
  
  Note that symbol applied to a symbol does nothing:
  >>> symbol(symbol('aa'))
  symbol('aa')
  
  But when applied to a any other object than string or symbol,
  an exception is raised:
  >>> symbol(2)
  Traceback (most recent call last):
  ...
  TypeError: symbol from_python() argument must be (string|ex(symbol)) (got <type 'int'>)
  
  Use .is_symbol() method to check whether an object is symbol or not:
  >>> a = symbol()
  >>> b = ex()
  >>> a.is_symbol(),b.is_symbol()
  (1, 0)

  There is also a function for that:
  >>> is_symbol(a), is_symbol([])
  (1, 0)
  
  To get the name of a symbol:
  >>> a = symbol('abc')
  >>> a.get_name()
  'abc'

  To set a new name to an existing symbol:
  >>> a.set_name('foo')
  >>> a
  symbol('foo')
  >>> a.get_name()
  'foo'

*/


#ifdef PYGINAC_DEFS

if ((symbol_dict=PyDict_New())==NULL)
  throw py::error_already_set();
this_module.def(&symbol_0, "symbol");
this_module.def(&symbol_1, "symbol");
this_module.def(&symbol_2, "symbol");
this_module.def(&ex::is_symbol, "is_symbol");
this_module.def(&return_false, "is_symbol");
ex_class.def(&ex::is_symbol, "is_symbol");
ex_class.def(&ex::get_name, "get_name");
ex_class.def(&ex::set_name, "set_name");
#else

#ifdef PYGINAC_EX_PROTOS
#define PYGINAC_PROTOS
#endif

#ifdef PYGINAC_PROTOS

#ifdef PYGINAC_EX_PROTOS
bool is_symbol(void) const;
std::string get_name(void) const;
void set_name(const std::string & n);

#else // PYGINAC_EX_PROTOS
class wrap_symbol: public GiNaC::symbol {
public:
  void set_TeX_name(const std::string & n) { TeX_name = n; }
};
GiNaC::ex symbol_0(void);
GiNaC::ex symbol_1(const GiNaC::symbol & sym);
GiNaC::ex symbol_2(const GiNaC::symbol & sym, const std::string & texname);
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
GiNaC::symbol from_python (PyObject* o, py::type<const GiNaC::symbol &>);
BOOST_PYTHON_END_CONVERSION_NAMESPACE
PyObject* symbol_dict = NULL;
#endif // !PYGINAC_EX_PROTOS

#else  // PYGINAC_PROTOS
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
GiNaC::symbol from_python (PyObject* o, py::type<const GiNaC::symbol &>) {
  //cerr<<"symbol_dict="<<PyString_AsString(PyObject_Repr(symbol_dict))<<endl;
  if (ExInstance_Check(o)) {
    GiNaC::ex e = from_python(o, py::type<const GiNaC::ex &>());
    if (is_ex_exactly_of_type(e, symbol))
      return ex_to_symbol(e);
  }
  if (PyString_Check(o)) {
    PyObject * r = NULL;
    if ((r = PyDict_GetItem(symbol_dict, o))==NULL) {
      GiNaC::symbol s(PyString_AS_STRING(o));
      PyDict_SetItem(symbol_dict,o,to_python(s));
      return s;
    }
    return ex_to_symbol(from_python(r, py::type<const GiNaC::ex &>()));
  }
  PYGINAC_FROMPYTHON_TYPEERROR(symbol,(string|ex(symbol)));
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE

/*F_DT is_symbol(obj)
  Check if `obj' is symbol.
*/
/*M_DT is_symbol(self)
  Check if object is symbol.
*/
bool ex::is_symbol(void) const { return is_ex_exactly_of_type(*this, symbol); }

/*M_DT get_name(self)
  Return the name of a symbol|function.
 */
std::string ex::get_name(void) const
{
  if (is_symbol())
    return ex_to_symbol(*this).get_name();
  if (is_function())
    return ex_to_function(*this).get_name();
  PyErr_SetString(PyExc_NotImplementedError, "ex.get_name() can be used only for ex(symbol)|ex(function)");
  throw py::error_already_set();
}

/*M_DT set_name(self, s)
  Set the name of a symbol to `s'.
 */
void ex::set_name(const std::string & n) {
  if (is_symbol())
    return ((GiNaC::symbol &)(*(this->bp))).set_name(n);
  PyErr_SetString(PyExc_NotImplementedError, "ex.set_name() can be used only for ex(symbol)");
  throw py::error_already_set();
}

/*F_DT symbol(name='symbol\d+',latex_name='name')
  symbol() - create symbol with a name of the form `symbol\d+'.
  symbol('name') - create symbol with a name 'name'.
  symbol('name','latex_name')
  symbol(symbol(...)) -> symbol(...)
 */
GiNaC::ex symbol_0(void) {
  return GiNaC::symbol();
}
GiNaC::ex symbol_1(const GiNaC::symbol & s) {
  return s;
}
GiNaC::ex symbol_2(const GiNaC::symbol & s, const std::string & texname) {
  ((wrap_symbol &)s).set_TeX_name(texname);
  return s;
}
#endif // !PYGINAC_PROTOS
#endif // !PYGINAC_DEFS

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