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

  >>> from ginac import constant, is_constant,numeric
  >>> a=constant('a')
  >>> a.is_constant(),numeric(2).is_constant()
  (1, 0)
  >>> a
  constant('a')
  >>> print a
  a
  >>> b = constant('b',1.5)
  >>> print b, b.evalf()
  b 1.5
  >>> def c_eval():
  ...     return numeric(2)/3
  >>> c = constant('c',c_eval)
  >>> print c, c.evalf()
  c 0.6666666666666666667
  >>> from ginac import Pi, Euler, Catalan
  >>> Pi
  constant('Pi')
  >>> print Pi
  Pi
  >>> print Pi.evalf(), Euler.evalf(), Catalan.evalf()
  3.1415926535897932385 0.5772156649015328606 0.91596559417721901505
  
 */


#ifdef PYGINAC_DEFS
this_module.def(&constant_1, "constant");
this_module.def(&constant_1c, "constant");
this_module.def(&constant_2, "constant");
this_module.def(&ex::is_constant, "is_constant");
this_module.def(&return_false, "is_constant");
ex_class.def(&ex::is_constant, "is_constant");
this_module.add(BOOST_PYTHON_CONVERSION::to_python(GiNaC::Pi), "Pi");
this_module.add(BOOST_PYTHON_CONVERSION::to_python(GiNaC::Catalan), "Catalan");
this_module.add(BOOST_PYTHON_CONVERSION::to_python(GiNaC::Euler), "Euler");

//this_module.def(&fun, "fun");
//ex_class.def(&ex::mth, "mth");
#else
#ifdef PYGINAC_EX_PROTOS
#define PYGINAC_PROTOS
#endif
#ifdef PYGINAC_PROTOS
#ifdef PYGINAC_EX_PROTOS
bool is_constant(void) const;
#else // PYGINAC_EX_PROTOS
GiNaC::ex constant_1c(const GiNaC::constant & c);
GiNaC::ex constant_1(const std::string & initname);
GiNaC::ex constant_2(const std::string & initname, py::ref value);
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
GiNaC::constant from_python (PyObject* o, py::type<const GiNaC::constant &>);
BOOST_PYTHON_END_CONVERSION_NAMESPACE
#endif // !PYGINAC_EX_PROTOS
#else  // PYGINAC_PROTOS
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
GiNaC::constant from_python (PyObject* o, py::type<const GiNaC::constant &>) {
  if (ExInstance_Check(o)) {
    GiNaC::ex e = from_python(o, py::type<const GiNaC::ex &>());
    if (is_ex_exactly_of_type(e, constant))
      return ex_to_constant(e);
  }
  PYGINAC_FROMPYTHON_TYPEERROR(constant,(string|string,number|string,callable|ex(constant)));
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
/*F_DT constant(name, value=None)
  constant(name) - return new constant with a `name'.
  constant(name, value) - return new constant with a `name' and `value'.
    `value' must be number.
  constant(name, func) - return new constant with a `name' and evaluation
    function `func'. `func' must be callable with 0 arguments and return
    the value of the constant.
  constant(constant(...)) -> constant(...)
 */
GiNaC::ex constant_1(const std::string & initname) {
  return GiNaC::constant(initname);
}
GiNaC::ex pyginac::constant_dummy(void) {
    PyErr_SetString(PyExc_RuntimeError,"constant() internal error (constant_dummy accessed)");
    throw py::error_already_set();
}
GiNaC::ex constant_1c(const GiNaC::constant & c) {
  return c;
}
GiNaC::ex constant_2(const std::string & initname, py::ref value) {
  PyObject * v = value.get();
  if (PyCallable_Check(v)) {
	GiNaC::constant c(initname, pyginac::constant_dummy);
	Py_INCREF(v);
	GiNaC::constant_w_cache()[c.gethash()] = v;
	return c;
  }
  GiNaC::constant c(initname, BOOST_PYTHON_CONVERSION::from_python(v, py::type<const GiNaC::numeric &>()));
  Py_INCREF(v);
  GiNaC::constant_w_cache()[c.gethash()] = v;
  return c;
}

/*F_DT is_constant(obj)
  Check if `obj' is constant.
*/
/*M_DT is_constant(self)
  Check if object is constant.
*/
bool ex::is_constant(void) const { return is_ex_exactly_of_type(*this, constant); }
#endif // !PYGINAC_PROTOS
#endif // !PYGINAC_DEFS




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