Unpacking doesn't work for defining a function

asked 2023-04-27 22:51:40 +0100

Cyrille gravatar image

I want to define a fonction of n parmeters who gives $a R/(a+b)p_x$.

If I construct the tuple of variable :

vard = var('a, b, p_x,p_y, R')

I was expecting that

Dx(*vard) = a R/(a+b)p_x

would be understud by Sagemath since *vard unpacks the tuple. But obviously it's not the case. Where am I wrong ?

edit retag flag offensive close merge delete

Comments

1

See this. Bur remember :

  • Python has no macroes. Whatsoever.

  • The preparser is a Sage-ONLY facility, easing the creation of so-called "callable sybolic expresion", another Sage-ONLY facility.

  • Do not confuse syntactic sugar and structural works.

BTW, remember that "Syntactic sugar causes cancer of the semicolon" (Alan Perlis).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-04-27 23:15:08 +0100 )edit

BTW, something somewhat related to what you seem to aim to achieve is doable by this crock :

sage: exec(preparse("Dx%s= a*R/(a+b)*p_x"%str(vard)))
sage: Dx
(a, b, p_x, p_y, R) |--> R*a*p_x/(a + b)

But remember that playing with string representations is not guiaranteed to work :

Python has no guarantee that eval(read(print(x))==x (which is a central requirement of Lisp...).

Fidgeting with strings representation may sometimes be useful, but is usually a swell way to shoot yourself (and not in the foot...). You'd bette work with the methods allowing to modify the internal structure of your objects (and reinvent Lisp in passing...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-04-27 23:26:14 +0100 )edit