File:  [CENS] / python / pyGiNaC / wrappers / inifcns.py
Revision 1.5: 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.5 $
# $Id: inifcns.py,v 1.5 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.
#
# TODO:
# Expose is_order_function
# Expose sqrt(symbolic)
depends = ['ex']

uses = []

#STARTPROTO

def abs(x): 'Absolute value.'
def csgn(x): 'Complex sign.'
def eta(a,b): 'Eta function: log(a*b) == log(a) + log(b) + eta(a, b).'
def sin(x): 'Sine.'
def cos(x): 'Cosine.'
def tan(x): 'Tangent.'
def exp(x): 'Exponential function.'
def log(x): 'Natural logarithm.'
def asin(x): 'Inverse sine (arc sine).'
def acos(x): 'Inverse cosine (arc cosine).'
def atan(x): 'Inverse tangent (arc tangent).'
def atan2(x,y): 'Inverse tangent with two arguments.'
def sinh(x): 'Hyperbolic Sine.'
def cosh(x): 'Hyperbolic Cosine.'
def tanh(x): 'Hyperbolic Tangent.'
def asinh(x): 'Inverse hyperbolic Sine (area hyperbolic sine).'
def acosh(x): 'Inverse hyperbolic Cosine (area hyperbolic cosine).'
def atanh(x): 'Inverse hyperbolic Tangent (area hyperbolic tangent).'
def Li2(x): 'Dilogarithm.'
def Li3(x): 'Trilogarithm.'
def zeta(p1,p2=None):
    """Riemann's Zeta-function (if p2 is None).
    Derivatives of Riemann's Zeta-function.
    """
def lgamma(x): 'Gamma-function.'
def tgamma(x): 'Gamma-function.'
def beta(x,y): 'Beta-function.'
def psi(p1,p2=None):
    """Psi-function (aka digamma-function) (if p2 is None).
    Derivatives of Psi-function (aka polygamma-functions).
    """
def factorial(x): 'Factorial function.'
def binomial(x,y): 'Binomial function.'
def Order(x): 'Order term function (for truncated power series).'
def Derivative(x,i): 'Inert partial differentiation operator.'
def lsolve(x,y): ''

def doublefactorial(x): ''
def bernoulli(x): ''
def fibonacci(x): ''
def isqrt(x): ''
def mod(a,b): ''
def smod(a,b): ''
def irem(a,b,q=None): ''
def iquo(a,b,q=None): ''
def gcd(a,b): ''
def lcm(a,b): ''
def pow(x,y): ''
def is_zero(x): ''
def is_positive(x): ''
def is_negative(x): ''
def is_integer(x): ''
def is_pos_integer(x): ''
def is_nonneg_integer(x): ''
def is_even(x): ''
def is_odd(x): ''
def is_prime(x): ''
def is_rational(x): ''
def is_real(x): ''
def is_cinteger(x): ''
def is_crational(x): ''
def real(x): ''
def imag(x): ''
def numer(x): ''
def denom(x): ''

#ENDPROTO


funcs_1p = ['abs','csgn','tan','exp','log',
            'sin','cos','asin','acos','atan',
            'sinh','cosh','tanh','asinh','acosh','atanh',
            'Li2','Li3','lgamma','tgamma','factorial','Order']
num_funcs_1p = ['exp','log','sin','cos','tan','asin','acos','atan',
                'sinh','cosh','tanh','asinh','acosh','atanh',
                'Li2','zeta','lgamma','tgamma','psi','factorial',
                'doublefactorial','bernoulli','fibonacci',
                'abs','isqrt',
                #'sqrt', # power
                #'inverse', # matrix
                'real',
                'imag',
                #'numer', # ex
                #'denom' # ex
                ]

num2_funcs_1p = [#'csgn',
                 #'is_zero', # ex
                 'is_positive','is_integer','is_pos_integer','is_negative',
                 'is_nonneg_integer','is_even','is_odd','is_prime',
                 'is_rational','is_real','is_cinteger','is_crational',
                 ]

funcs_2p = ['eta','atan2','beta','binomial','Derivative']
num_funcs_2p = ['atan','psi','binomial','mod','smod','irem','iquo',
                'gcd','lcm','pow']

num_funcs_3p = ['irem','iquo']

GiNaC_protos = '''
#ifdef GINAC_VERSION_0_8_1_OR_EARLIER
bool is_negative(const numeric &);
#endif
'''

wrapperclass = '''

UNEX_RET zeta1_w(python::ref p1) {
  GiNaC::ex a = ex_w(p1);
#ifdef PYGINAC_numeric
  if (is_ex_exactly_of_type(a, numeric))
    return BOOST_PYTHON_CONVERSION::to_python(GiNaC::zeta(ex_to_numeric(a)));
#endif
  return UNEX(GiNaC::ex(GiNaC::function(GiNaC::function_index_zeta1, ex_w(p1))));
}
UNEX_RET zeta2_w(python::ref p1,python::ref p2) {
  return UNEX(GiNaC::ex(GiNaC::function(GiNaC::function_index_zeta2, ex_w(p1), ex_w(p2))));
}

UNEX_RET psi1_w(python::ref p1) {
  GiNaC::ex a = ex_w(p1);
#ifdef PYGINAC_numeric
  if (is_ex_exactly_of_type(a, numeric))
    return BOOST_PYTHON_CONVERSION::to_python(GiNaC::psi(ex_to_numeric(a)));
#endif
  return UNEX(GiNaC::ex(GiNaC::function(GiNaC::function_index_psi1, ex_w(p1))));
}

UNEX_RET psi2_w(python::ref p1,python::ref p2) {
  GiNaC::ex a1 = ex_w(p1);
  GiNaC::ex a2 = ex_w(p2);
#ifdef PYGINAC_numeric
  if (is_ex_exactly_of_type(a1, numeric) && is_ex_exactly_of_type(a2, numeric))
    return BOOST_PYTHON_CONVERSION::to_python(GiNaC::psi(ex_to_numeric(a1),ex_to_numeric(a2)));
#endif
  return UNEX(GiNaC::ex(GiNaC::function(GiNaC::function_index_psi2, ex_w(p1), ex_w(p2))));
}
UNEX_RET lsolve_w(python::ref p1, python::ref p2) {
  return UNEX(GiNaC::lsolve(ex_w(p1), ex_w(p2)));
}
#ifdef PYGINAC_numeric
int csgn_w (python::ref p1) { return GiNaC::csgn(as_numeric_w(p1)); }
#endif
/*
UNEX_RET sqrt_w(python::ref p1) {
  GiNaC::ex a = ex_w(p1);
#ifdef PYGINAC_numeric
  if (is_ex_exactly_of_type(a, numeric))
    return UNEX(GiNaC::ex(GiNaC::sqrt(ex_to_numeric(a))));
#endif
  return UNEX(GiNaC::ex(GiNaC::power(a,GiNaC::numeric(1)/2)));
}
*/
'''
defs = '''
this_module.def(zeta1_w, "zeta");
this_module.def(zeta2_w, "zeta");
this_module.def(psi1_w, "psi");
this_module.def(psi2_w, "psi");
this_module.def(lsolve_w, "lsolve");
this_module.def(csgn_w, "csgn");
//this_module.def(sqrt_w, "sqrt");

'''

for f in num2_funcs_1p:
    wrapperclass += '''
#ifdef PYGINAC_numeric
 bool #name#1_w(python::ref p1) {
  GiNaC::ex a = ex_w(p1);
  if (is_ex_exactly_of_type(a, numeric))
    return GiNaC::#name#(ex_to_numeric(a));
  return false;
}
#endif
'''.replace('#name#',f)
    defs += '''
#ifdef PYGINAC_numeric
  this_module.def(%s1_w, "%s");
#endif
'''%(f,f)

for f in num_funcs_1p:
    if f in funcs_1p+['zeta','psi','sqrt']: continue
    wrapperclass += '''
#ifdef PYGINAC_numeric
 GiNaC::numeric #name#1_w(python::ref p1) {
  return GiNaC::#name#(as_numeric_w(p1));
}
#endif
'''.replace('#name#',f)
    defs += '''
#ifdef PYGINAC_numeric
  this_module.def(%s1_w, "%s");
#endif
'''%(f,f)

for f in funcs_1p:
    if f in num_funcs_1p:
        wrapperclass += '''
UNEX_RET #name#1_w(python::ref p1) {
  GiNaC::ex a = ex_w(p1);
#ifdef PYGINAC_numeric
  if (is_ex_exactly_of_type(a, numeric))
    return BOOST_PYTHON_CONVERSION::to_python(GiNaC::#name#(ex_to_numeric(a)));
#endif
  return UNEX(GiNaC::ex(GiNaC::function(GiNaC::function_index_#name#, a)));
}
'''.replace('#name#',f)
        
    else:
        wrapperclass += '''
 UNEX_RET %s1_w(python::ref p1) {
  return UNEX(GiNaC::ex(GiNaC::function(GiNaC::function_index_%s, ex_w(p1))));
}
'''%(f,f)
    defs += '''
this_module.def(%s1_w, "%s");
'''%(f,f)

for f in num_funcs_2p:
    if f in funcs_2p+['psi']: continue
    wrapperclass += '''
#ifdef PYGINAC_numeric
 GiNaC::numeric #name#2_w(python::ref p1, python::ref p2) {
  return GiNaC::#name#(as_numeric_w(p1),as_numeric_w(p2));
}
#endif
'''.replace('#name#',f)
    defs += '''
#ifdef PYGINAC_numeric
  this_module.def(%s2_w, "%s");
#endif
'''%(f,f)

for f in funcs_2p:
    if f in num_funcs_2p:
        wrapperclass += '''
 UNEX_RET #name#2_w(python::ref p1, python::ref p2) {
  GiNaC::ex a1 = ex_w(p1);
  GiNaC::ex a2 = ex_w(p2);
#ifdef PYGINAC_numeric
  if (is_ex_exactly_of_type(a1, numeric) && is_ex_exactly_of_type(a2, numeric))
    return BOOST_PYTHON_CONVERSION::to_python(GiNaC::#name#(ex_to_numeric(a1),ex_to_numeric(a2)));
#endif
  return UNEX(GiNaC::ex(GiNaC::function(GiNaC::function_index_#name#, ex_w(p1), ex_w(p2))));
}
'''.replace('#name#',f)
    else:
        wrapperclass += '''
 UNEX_RET %s2_w(python::ref p1, python::ref p2) {
  return UNEX(GiNaC::ex(GiNaC::function(GiNaC::function_index_%s, ex_w(p1), ex_w(p2))));
}
'''%(f,f)
    defs += '''
this_module.def(%s2_w, "%s");
'''%(f,f)

for f in num_funcs_3p:
    wrapperclass += '''
#ifdef PYGINAC_numeric
 GiNaC::numeric #name#3_w(python::ref p1, python::ref p2, python::ref p3) {
  GiNaC::numeric a3 = as_numeric_w(p3);
  return GiNaC::#name#(as_numeric_w(p1),as_numeric_w(p2), a3);
}
#endif
'''.replace('#name#',f)
    defs += '''
#ifdef PYGINAC_numeric
  this_module.def(%s3_w, "%s");
#endif
'''%(f,f)






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