optional arguments

Messages in english

Modérateur : xcasadmin

compsystems
Messages : 562
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

optional arguments

Message par compsystems » sam. avr. 13, 2019 8:09 pm

Hello

There is a way in which the arguments of a function are optional, that is, that the following code will take only the first argument

Code : Tout sélectionner

def legendre_( n, evalX ):
    local px, x; purge(x)
    px := 1/(2^n*n!)*diff((x^2-1)^n,x,n)
    if getType(evalX)=="NUM" or getType(evalX)=="EXPR" :
        px:=subst(px,x=evalX)
    elif getType(evalX)=="STR" and evalX=="poly1":
        px:=e2r(px)   
    return px;
legendre_(2) > "[2] size() != [n,evalX] Error: Bad Argument Value"

legendre_(2,""),legendre_(2,"poly1"),legendre_(2,10),legendre_(2,cos(t)) >
(3*x**2-1)/2, poly1[3/2,0,-1/2], 299/2, (3*cos(t)**2-1)/2

parisse
Messages : 5739
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: optional arguments

Message par parisse » dim. avr. 14, 2019 5:55 am

def legendre_( n, evalX=x ): ...

compsystems
Messages : 562
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

Re: optional arguments

Message par compsystems » mer. avr. 17, 2019 8:00 pm

ok ^

Code : Tout sélectionner

def legendre_( n, evalX="" ):
    local px, x; purge(x)
    px := 1/(2^n*n!)*diff((x^2-1)^n,x,n)
    if getType(evalX)=="NUM" or getType(evalX)=="EXPR" or getType(evalX)=="VAR":
        px:=subst(px,x=evalX)
    elif getType(evalX)=="STR" and evalX=="list":
        px:=e2r(px)   
    return px;
legendre_(2),legendre_(2,y),legendre_(2,"list"),legendre_(2,10),legendre_(2,cos(t)) enter

1/8*(12*x**2-4), 1/8*(12*y**2-4), poly1[3/2,0,-1/2],2 99/2, 1/8*(12*cos(t)**2-4)

Répondre