Opened 12 years ago

Closed 5 years ago

#10035 closed enhancement (fixed)

Hold context

Reported by: Karl-Dieter Crisman Owned by: Burcin Erocal
Priority: major Milestone: sage-8.1
Component: symbolics Keywords:
Cc: Burcin Erocal, Eviatar Bach Merged in:
Authors: Ralf Stephan Reviewers: Emmanuel Charpentier
Report Upstream: N/A Work issues:
Branch: 3ad332c (Commits, GitHub, GitLab) Commit: 3ad332c9baddebc3c438409d3abec69d3b1f118a
Dependencies: #23820 Stopgaps:

Status badges

Description (last modified by Ralf Stephan)

Now that Pynac will support 'hold' of symbolic expressions, like

sage: tan(pi/12,hold=True)
tan(1/12*pi)

we might want a 'context' for this. The ticket enables the following:

        sage: tan(1/12*pi)    
        -sqrt(3) + 2
        sage: with hold:
        ....:     tan(1/12*pi)
        ....:     
        tan(1/12*pi)
        
        sage: with hold:
        ....:     sin(0)
        ....:     cos(0)
        ....:     
        sin(0)
        cos(0)
        sage: hold.start()
        sage: sin(0)
        sin(0)
        sage: cos(0)
        cos(0)
        sage: hold.stop()
        sage: sin(0)
        0

Change History (29)

comment:1 Changed 12 years ago by Jason Grout

Can you give a code sample of what you mean?

comment:2 in reply to:  1 Changed 12 years ago by Karl-Dieter Crisman

Cc: Burcin Erocal added

Replying to jason:

Can you give a code sample of what you mean?

No, because this is really a followup to another ticket. I'll cc: Burcin in, though he probably already gets it given the component. I imagine something like

sage: context('hold') # or whatever the usual syntax is for such things - Sage doesn't have any yet
sage: a = tan(pi/12)
sage: a
tan(pi/12)

as opposed to evaluating it.

comment:3 Changed 12 years ago by Burcin Erocal

I was thinking of something like:

sage: with hold:
....:     t = tan(pi/2)
....:
sage: t
tan(pi/12)
sage: tan(pi/12)
-sqrt(3) + 2

comment:4 Changed 12 years ago by Jason Grout

That would be awesome!

comment:5 in reply to:  4 ; Changed 12 years ago by Jean-Pierre Flori

Replying to jason:

That would be awesome!

But with such a construction it is not obvious how to simplify 't' without going through Maxima as suggested in #10034

sage: with hold: 
....:     t = tan(pi/2) 
....: 
sage: t tan(pi/12) 
sage: t.eval() #or t.unhold() or whatever 
-sqrt(3) + 2

comment:6 Changed 11 years ago by Karl-Dieter Crisman

#11776 seems to be asking for something very similar.

comment:7 Changed 9 years ago by Eviatar Bach

Cc: Eviatar Bach added

comment:8 Changed 9 years ago by Jeroen Demeyer

Milestone: sage-5.11sage-5.12

comment:9 Changed 9 years ago by For batch modifications

Milestone: sage-6.1sage-6.2

comment:10 Changed 9 years ago by Karl-Dieter Crisman

An interesting use case for this showed up at this tex.SX question.

comment:11 Changed 8 years ago by For batch modifications

Milestone: sage-6.2sage-6.3

comment:12 Changed 8 years ago by For batch modifications

Milestone: sage-6.3sage-6.4

comment:13 in reply to:  5 Changed 8 years ago by Ralf Stephan

Replying to jpflori:

Replying to jason:

That would be awesome!

But with such a construction it is not obvious how to simplify 't' without going through Maxima as suggested in #10034

sage: with hold: 
....:     t = tan(pi/2) 
....: 
sage: t tan(pi/12) 
sage: t.eval() #or t.unhold() or whatever 
-sqrt(3) + 2

You are going through Maxima because the simplify... functions do this. So improve these by replacing Maxima. It has nothing to do with the question of a global hold context.

comment:15 Changed 7 years ago by Ralf Stephan

Milestone: sage-6.4sage-6.8
Priority: minormajor

comment:16 Changed 5 years ago by Ralf Stephan

The construction would look like:

sage: class hold_class:
....:     def __enter__(self): print('entered')
....:     def __exit__(self, *args): print('exited')
....:     
sage: hold = hold_class()
sage: with hold:
....:     x^2
....:     
entered
x^2
exited

comment:17 Changed 5 years ago by Ralf Stephan

So we can actually use a global state (e.g. a boolean hold state inside Pynac) and via the Python with ensure that it is always set locally.

comment:18 Changed 5 years ago by Ralf Stephan

Branch: u/rws/create_hold_context

comment:19 Changed 5 years ago by Ralf Stephan

Authors: Ralf Stephan
Commit: 18b6c7ad1b34bca2d52834f19ce6863c477ee690
Dependencies: pynac-0.7.11
Milestone: sage-6.8sage-8.2

New commits:

18b6c7a10035: hold context

comment:20 Changed 5 years ago by Karl-Dieter Crisman

Ordinarily I'd just say well done and leave it at that. But what to do with the "old" hold stuff? Keep, deprecate, side-by-side - should we transform old examples into this one, not, leave that to another ticket that may or may not get attention ... I assume you have thoughts on this so this is just for the purposes of hearing them, I don't have a strong opinion other than that a decision would have to be made, this patch alone wouldn't suffice. THANK YOU for this.

comment:21 Changed 5 years ago by Ralf Stephan

I think the best strategy is to keep the old functionality but replace doctests in tutorials and introductory docstrings (another ticket). As there will be more such contexts a syntax for multiple settings like with context(hold, eval_fp, simplify): seems best.

Please review this whenever pynac-0.7.11 is merged.

comment:22 Changed 5 years ago by Ralf Stephan

Or even with context(x > 0, simplify):

comment:23 Changed 5 years ago by Ralf Stephan

Note however that the Python directive with interactively acts on its content all at once. That is,

sage: with hold:
....:     sin(0)
....:     cos(0)
....:     
sin(0)
cos(0)

Maybe we should provide start()/stop() so people can do

sage: mycontext1 = context(...)
sage: mycontext2 = context(...)
sage: with mycontext1:
........
sage: mycontext2.start()
...
sage: mycontext2.stop()

comment:24 Changed 5 years ago by git

Commit: 18b6c7ad1b34bca2d52834f19ce6863c477ee6903ad332c9baddebc3c438409d3abec69d3b1f118a

Branch pushed to git repo; I updated commit sha1. New commits:

3ad332c10035: hold.start/stop

comment:25 Changed 5 years ago by Ralf Stephan

Dependencies: pynac-0.7.11#23820

comment:26 Changed 5 years ago by Ralf Stephan

Description: modified (diff)
Milestone: sage-8.2sage-8.1
Status: newneeds_review
Summary: Create hold contextHold context

comment:27 Changed 5 years ago by Emmanuel Charpentier

Reviewers: Emmanuel Charpentier
Status: needs_reviewpositive_review

On top of 8.1beta9 + #24026 + #23990 + #23923 + #24088 + their dependencies running on Debian Testing on core i7 + 16 GB RAM, passes ptestlong with no error whatsoever.

==>positive_review

comment:28 Changed 5 years ago by Ralf Stephan

Thanks.

comment:29 Changed 5 years ago by Volker Braun

Branch: u/rws/create_hold_context3ad332c9baddebc3c438409d3abec69d3b1f118a
Resolution: fixed
Status: positive_reviewclosed
Note: See TracTickets for help on using tickets.