2 questions on var

Yametazamwa mara 60
Ruka hadi kwenye ujumbe wa kwanza ambao haujasomwa

G. M.-S.

hayajasomwa,
6 Mac 2022, 12:13:4206/03/2022
kwa sage-s...@googlegroups.com

Beginner's questions, I guess.

Some time ago, Emmanuel Charpentier wrote

var("v", n=2)

which gives

(v0, v1)

Where is this documented?  I have been unable to find keywords for var other than domain and latex_name.  Are there any others?

Another question:
I have learnt to write
x,y,z=var('x,y,z')

Is it possible to do the same for
v0,v1,v2=var('v', n=3)
without having to write explicitly the LHS?

Guillermo

Nils Bruin

hayajasomwa,
6 Mac 2022, 13:26:3306/03/2022
kwa sage-support
On Sunday, 6 March 2022 at 09:13:42 UTC-8 list...@gmail.com wrote:

Beginner's questions, I guess.

Some time ago, Emmanuel Charpentier wrote

var("v", n=2)

which gives

(v0, v1)

Where is this documented?  I have been unable to find keywords for var other than domain and latex_name.  Are there any others?

This is documented on SR.var , which is slightly different from the top-level "var" you are referring to. It looks like the documentation on the top-level one wasn't updated when the feature was added to SR.var. Since top-level var just wraps SR.var, it automatically got the feature too.
 
Another question:
I have learnt to write
x,y,z=var('x,y,z')

You can do that, but with the top-level var it's not necessary (this is exactly where it differs from SR.var) :
it will inject bindings for the created symbols in the current name space. This is a hack that is convenient for interactive use. In library code, you must use SR.var instead, in which case you have to write

x,y,z = SR.var('x,y,z')

( with just "SR.var('x,y,z')" you'd create the symbols but you wouldn't bind them to anything).

Is it possible to do the same for
v0,v1,v2=var('v', n=3)
without having to write explicitly the LHS?

Yes, that already works. If you use

var('v', n=3)

the names v0,v1,v2 in your current scope will be bound to the newly created symbols.

There's been discussion about this when it was implemented:


G. M.-S.

hayajasomwa,
6 Mac 2022, 17:36:0906/03/2022
kwa sage-s...@googlegroups.com

Thanks, Nils.

A further question is thus how to do something like
v0,...,vk=SR.var('v', n=k)
when k varies.

Should I look at the implementation of SR.var?

Guillermo

G. M.-S.

hayajasomwa,
6 Mac 2022, 17:37:4806/03/2022
kwa sage-s...@googlegroups.com

I mean, the implementation of top-level var, of course.

slelievre

hayajasomwa,
8 Mac 2022, 02:20:0708/03/2022
kwa sage-support
Even more practical, I find, is to name the tuple of indexed variables:
```
sage: v = SR.var('v', n=8)
sage: v
(v0, v1, v2, v3, v4, v5, v6, v7)
```
and to use index notation `v[k]` instead of `vk` to use the variables.
```
sage: v[0]
v0
sage: v[7]
v7
```

That does not assign the variables to the names `v0` to `v7`:
```
sage: v2
Traceback (most recent call last)=
...
NameError: name 'v2' is not defined
```

If you really want to use `v0` to `v7` instead of `v[0]` to `v[7]`,
follow the implementation in `var` (accessed with `var??`),
which simply amounts to:
```
G = globals()
for vk in v:
    G[repr(vk)] = vk
```

After that:
```
sage: v2
v2
```

Emmanuel Charpentier

hayajasomwa,
8 Mac 2022, 13:19:3608/03/2022
kwa sage-support

even more simpler :-) :

sage: V=var("v", n=8)
sage: V
(v0, v1, v2, v3, v4, v5, v6, v7)
sage: v2
v2
sage: V[2]
v2

“Who could ask for anything mooooore ?”

G. M.-S.

hayajasomwa,
16 Apr 2022, 08:37:1616/04/2022
kwa sage-s...@googlegroups.com

Thanks Samuel and Emmanuel.

Follow up question:  Why does
var('lambda',n=1)
fail?

I know that the name "lambda" is not a valid Python identifier, but I am asking for "lambda0", "lambda1".

Guillermo

Nils Bruin

hayajasomwa,
16 Apr 2022, 16:56:3016/04/2022
kwa sage-support
On Saturday, 16 April 2022 at 05:37:16 UTC-7 list...@gmail.com wrote:

Thanks Samuel and Emmanuel.

Follow up question:  Why does
var('lambda',n=1)
fail?

Because the code in question tests the string actually passed in for whether it's a valid python identifier.

Probably this is because the "n" optional argument was only added later. However, there's been quite a discussion on what the appropriate binding behaviour should be. One option would be to bind the actual returned result (a tuple of the constructed symbols) to a name in the current namespace. i.e., the result of

x = SR.var('x',n=3)

For that behaviour, the passed-in name would have to be a valid identifier. So to future-proof sage, perhaps it's better to have this stricter check.

You can create symbols without the check: SR.symbol('lambda') works just fine. To create the symbols you'd like, you can do:

L = (SR.symbol("lambda{}".format(i)) for i in range(3))

binding them to corresponding symbols would require a little more work . It's basically

for c in L:
    globals()[str(c)] = c

which works when typed in as-is, but requires some more trickery if it is wrapped in a function in some module, due to the semantics of the actual "globals()" call in python.

[in general, for anything other than direct interactive use, you should probably not rely on binding injections]

G. M.-S.

hayajasomwa,
16 Apr 2022, 17:06:2616/04/2022
kwa sage-s...@googlegroups.com

Thanks for the explanation, Nils (even if I am not sure to have understood everything).

Guillermo
Jibu wote
Mjibu mchapishaji
Sambaza
Ujumbe 0 mpya