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

uses = ['exvector','epvector']

#STARTPROTO

class add(expairseq):
    """Sum of expressions."""
    def __init__(self,*args):
        """add() - construct empty sum (equivalent to 0)
    add(e1,e2) - construct a sum e1+e2
    add(seq)   - construct a sum seq[0]+seq[1]+...+seq[-1]
    add(vp,coeff) - construct a sum from epvector vp and coeff.
    add(ex(add(...))) - returns add(...)
    """


#ENDPROTO

#STARTDOCTEST
def test_03_add():
    """
>>> from ginac import add, symbol
>>> x=symbol('x')
>>> y=symbol('y')
>>> add(x,y)
add([symbol('x'), symbol('y')])
>>> add([x,y,3])
add([symbol('x'), symbol('y'), numeric('3')])
>>> add()
numeric('0')
>>> add(1)
numeric('1')
>>> add(3,4)
numeric('7')
>>> add(add([x,y]))
add([symbol('x'), symbol('y')])
>>> from ginac import ex
>>> add(ex(add([x,y])))
add([symbol('x'), symbol('y')])
>>> add(ex(x*y))
Traceback (most recent call last):
...
TypeError: ex_to_add() argument must be ex(add)
>>> add({})
Traceback (most recent call last):
...
TypeError: ex() argument must be int|float|complex|basic|ex|string,sequence
>>> add(x,y)+7
add([symbol('x'), symbol('y'), numeric('7')])
>>> 7+add(x,y)
add([symbol('x'), symbol('y'), numeric('7')])
>>> add(x,y)*7
add([mul([symbol('x'), numeric('7')]), mul([symbol('y'), numeric('7')])])
>>> 7*add(x,y)
add([mul([symbol('x'), numeric('7')]), mul([symbol('y'), numeric('7')])])
>>> add(x,y)/7
add([mul([symbol('x'), numeric('1/7')]), mul([symbol('y'), numeric('1/7')])])
>>> 7/add(x,y)
mul([power(add([symbol('x'), symbol('y')]), numeric('-1')), numeric('7')])
>>> add(x,y)**3
power(add([symbol('x'), symbol('y')]), numeric('3'))
>>> add(x,y)+add(x,y)
add([mul([symbol('x'), numeric('2')]), mul([symbol('y'), numeric('2')])])

>>> from ginac import exvector,epvector
>>> add(exvector([x,y]))
add([symbol('x'), symbol('y')])
>>> add(epvector([[y,3],[x,3]]),4)
add([mul([symbol('x'), numeric('3')]), mul([symbol('y'), numeric('3')]), numeric('4')])

>>> a = add(x,y)
>>> a.nops()
2
>>> a.op(0), a.op(1)
(symbol('x'), symbol('y'))
>>> a.has(x)
1
>>> a.degree(x)
1
>>> a.ldegree(x)
0
>>> add(2*x,y).diff(x)
numeric('2')
>>> add(2*x,y*x)
add([mul([symbol('x'), numeric('2')]), mul([symbol('x'), symbol('y')])])
>>> add(2*x,y*x).collect(x)
mul([add([symbol('y'), numeric('2')]), symbol('x')])
>>> add(x,y).is_equal(x+y)
1
>>> add(x,y).is_equal(y+x)
1
>>> add(x*x,x*y).series(x,2,3)
>>> add(x,y).subs(x,y)
mul([symbol('y'), numeric('2')])
>>> add(x,y).normal([],[])
lst([add([symbol('x'), symbol('y')]), numeric('1')])
>>> ex(add(x,y)).normal()
add([symbol('x'), symbol('y')])
>>> x+y
add([symbol('x'), symbol('y')])
>>> y+x
add([symbol('x'), symbol('y')])
>>> add(y,x)
add([symbol('x'), symbol('y')])
"""
#ENDDOCTEST

wrapperclass = '''
class add_w : public GiNaC::add {
  PyObject * self;
public:
  //add_w(const GiNaC::add & a): GiNaC::add(a) {}
  add_w(python::ref v)
  : GiNaC::add(exvector_w(v)) {
    DEBUG_C("add_w(raw:ref)");
  }
  add_w(PyObject * self_)
  : GiNaC::add(), self(self_) {
    DEBUG_C("add_w()");
  }
  add_w(PyObject * self_, const GiNaC::add & a)
  : GiNaC::add(a), self(self_) {
    DEBUG_C("add_w(add)");
  }
  add_w(PyObject * self_, const GiNaC::ex & a)
  : GiNaC::add(ex_to_add_w(a)), self(self_) {
    DEBUG_C("add_w(ex)");
  }
  add_w(PyObject * self_, const GiNaC::ex & lh, const GiNaC::ex & rh)
  : GiNaC::add(lh,rh), self(self_) {
    DEBUG_C("add_w(ex,ex)");
  }
  add_w(PyObject * self_, const GiNaC::epvector & v, const GiNaC::ex & oc)
  : GiNaC::add(v,oc), self(self_) {
    DEBUG_C("add_w(epvector,ex)");
  }
  add_w(PyObject * self_, const GiNaC::epvector & v, python::ref oc)
  : GiNaC::add(v,ex_w(oc)), self(self_) {
    DEBUG_C("add_w(epvector,ref)");
  }
  add_w(PyObject * self_, const GiNaC::exvector & v)
  : GiNaC::add(v), self(self_) {
    DEBUG_C("add_w(exvector)");
  }
  add_w(PyObject * self_, python::ref v)
  : GiNaC::add(exvector_w(v)), self(self_) {
    DEBUG_C("add_w(ref)");
  }
  add_w(PyObject * self_, python::ref lh, python::ref rh)
  : GiNaC::add(ex_w(lh),ex_w(rh)), self(self_) {
    DEBUG_C("add_w(ref,ref)");
  }
  ~add_w() {
    DEBUG_C("~add_w()");
  }
};

'''

builder = '''
python::class_builder<add_w> add_w_class(this_module, "_add_w");
python::class_builder<GiNaC::add, add_w> add_class(this_module, "add");
add_py_class = python::as_object(add_class.get_extension_class());
add_class.declare_base(add_w_class);
add_class.declare_base(basic_class);
'''

constructors = '''
add_class.def(python::constructor<>());
add_class.def(python::constructor<const GiNaC::add &>());
add_class.def(python::constructor<const GiNaC::ex &>());
add_class.def(python::constructor<const GiNaC::ex &, const GiNaC::ex>());
add_class.def(python::constructor<const GiNaC::epvector &, const GiNaC::ex>());
add_class.def(python::constructor<const GiNaC::epvector &, python::ref>());
add_class.def(python::constructor<python::ref, python::ref>());
add_class.def(python::constructor<const GiNaC::exvector &>());
add_class.def(python::constructor<python::ref>());
'''

defs = '''
add_class.def(&basic_w::python_str, "__str__");
add_class.def(&basic_w::python_repr, "__repr__");
add_class.def(&basic_w::coerce, "__coerce__");
BASIC_OPS(add)

'''

implementation = '''
EX_TO_BASIC(add)
'''

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