| Karim Belabas on Sun, 17 Jun 2018 11:17:16 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: Rational functions equal almost everywhere, but not for "subst" |
* Jacques Gélinas [2018-06-17 06:49]:
> A perplexing (bad) use of global variables.
>
> g1(r,R) = R / (2*r+1 - R);
> g2(r,R) = a + r;
> F(r,a,b,R) = g1(r,a*b*R) / g1(r-1,a*R) * g2(r,a*b*R) / g2(r-1,a*b*R) ;
> G(r,a,b,R) = b * (2*r-1-a*R) / (2*r+1-a*b*R) * g2(r,a*b*R) / g2(r-1,a*b*R) ;
> G(r,a,b,R) == F(r,a,b,R) \\ identical !
> 1
> G(r,0,b,R) == subst(F(r,a,b,R),a,0) \\ not equal ??? (:-(
> 0
Please have a look at '?? my'. Then instrument g2 as follows:
g2(r,R) = print(a); a + r;
and observe
(10:57) gp > G(r,a,b,R) == F(r,a,b,R)
a
a
a
a
%6 = 1
(10:57) gp > G(r,0,b,R) == subst(F(r,a,b,R),a,0)
a
a
a
a
%7 = 0
When you write G(r,0,b,R),
- the variable 'a' which is a parameter of the function G, lexically scoped to
the body of G, is set to 0,
- but the *other* variable 'a' with global scope is unaffected (it happens to
share the same name in your program but is otherwise unrelated to the
one set to 0) and this is the one seen by g2.
The behaviour is indeed different in 2.3-stable (and in the first development
releases 2.4.*). It changed in 2.5-stable when lexically scoped variables
and the my() operator were introduced:
- in stable versions up to 2.3.0 function parameters were *dynamically*
scoped (as per local()) and were indeed 'semi-global': later
references from anywhere in the program accessed the new value until
the function returned; [ this is what you expected ]
- from 2.5.0 on they are lexically scoped (as per my()).
This change of behaviour was highlighted in the release notes for pari-2.5
https://pari.math.u-bordeaux.fr/archives/pari-announce-11/msg00001.html
[...]
MAIN BACKWARD COMPATIBILITY ISSUES: see the 'COMPAT' file for the full list.
===================================
- The main issue with existing GP scripts has to do with the scope of
private variables (my vs. local), see section 2.6 in User's Manual.
Indeed, variables implicitly scoped to loop or function bodies are now
lexically scoped. From GP-2.5 on, in constructs like
for(i = 1, 10, g())
f(i) = g()
the index i is truly local to the loop/function body. It is no longer seen
by the function g(), as used to be the case in GP-2.3.
[...]
Cheers,
K.B.
--
Karim Belabas, IMB (UMR 5251) Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux Fax: (+33) (0)5 40 00 21 23
351, cours de la Liberation http://www.math.u-bordeaux.fr/~kbelabas/
F-33405 Talence (France) http://pari.math.u-bordeaux.fr/ [PARI/GP]
`