Opened 8 years ago

Closed 11 months ago

#10035 closed enhancement (fixed)

Hold context

Reported by: kcrisman Owned by: burcin
Priority: major Milestone: sage-8.1
Component: symbolics Keywords:
Cc: burcin, eviatarbach Merged in:
Authors: Ralf Stephan Reviewers: Emmanuel Charpentier
Report Upstream: N/A Work issues:
Branch: 3ad332c (Commits) Commit: 3ad332c9baddebc3c438409d3abec69d3b1f118a
Dependencies: #23820 Stopgaps:

Description (last modified by rws)

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 follow-up: Changed 8 years ago by jason

Can you give a code sample of what you mean?

comment:2 in reply to: ↑ 1 Changed 8 years ago by kcrisman

  • Cc burcin 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 8 years ago by burcin

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 follow-up: Changed 8 years ago by jason

That would be awesome!

comment:5 in reply to: ↑ 4 ; follow-up: Changed 8 years ago by 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

comment:6 Changed 7 years ago by kcrisman

#11776 seems to be asking for something very similar.

comment:7 Changed 5 years ago by eviatarbach

  • Cc eviatarbach added

comment:8 Changed 5 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:9 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:10 Changed 5 years ago by kcrisman

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

comment:11 Changed 4 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:12 Changed 4 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:13 in reply to: ↑ 5 Changed 4 years ago by rws

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 3 years ago by rws

  • Milestone changed from sage-6.4 to sage-6.8
  • Priority changed from minor to major

comment:16 Changed 13 months ago by rws

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 13 months ago by rws

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 13 months ago by rws

  • Branch set to u/rws/create_hold_context

comment:19 Changed 13 months ago by rws

  • Authors set to Ralf Stephan
  • Commit set to 18b6c7ad1b34bca2d52834f19ce6863c477ee690
  • Dependencies set to pynac-0.7.11
  • Milestone changed from sage-6.8 to sage-8.2

New commits:

18b6c7a10035: hold context

comment:20 Changed 13 months ago by kcrisman

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 13 months ago by rws

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 13 months ago by rws

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

comment:23 Changed 13 months ago by rws

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 13 months ago by git

  • Commit changed from 18b6c7ad1b34bca2d52834f19ce6863c477ee690 to 3ad332c9baddebc3c438409d3abec69d3b1f118a

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

3ad332c10035: hold.start/stop

comment:25 Changed 13 months ago by rws

  • Dependencies changed from pynac-0.7.11 to #23820

comment:26 Changed 12 months ago by rws

  • Description modified (diff)
  • Milestone changed from sage-8.2 to sage-8.1
  • Status changed from new to needs_review
  • Summary changed from Create hold context to Hold context

comment:27 Changed 11 months ago by charpent

  • Reviewers set to Emmanuel Charpentier
  • Status changed from needs_review to positive_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 11 months ago by rws

Thanks.

comment:29 Changed 11 months ago by vbraun

  • Branch changed from u/rws/create_hold_context to 3ad332c9baddebc3c438409d3abec69d3b1f118a
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.