Opened 10 years ago

Closed 10 years ago

#6253 closed enhancement (fixed)

[with patch, positive review] Constant functions

Reported by: nthiery Owned by: nthiery
Priority: major Milestone: sage-4.1.2
Component: misc Keywords: constant functions
Cc: sage-combinat, mhansen, jason Merged in: Sage 4.1.2.alpha0
Authors: Nicolas M. Thiéry Reviewers: Florent Hivert, Minh Van Nguyen
Report Upstream: Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by nthiery)

This trivial patch adds basic support for constant functions

Such a function could be written as lambda x: constant, but that's not picklable. Besides, this should eventually be cythoned for speed.

Prerequisite for #4326.

Attachments (3)

constant_function-6253-nt.patch (3.9 KB) - added by nthiery 10 years ago.
trac_6253-constant_function-nt.patch (3.8 KB) - added by nthiery 10 years ago.
trac_6253-reviewer.patch (2.5 KB) - added by mvngu 10 years ago.
reviewer patch

Download all attachments as: .zip

Change History (13)

comment:1 Changed 10 years ago by nthiery

Oh, I forgot to mention: let me know if this readily exists somewhere and I missed it.

comment:2 follow-up: Changed 10 years ago by mhansen

  • Cc mhansen added

How is this intended to be used? What are your typical constants? Are there instances where you'd want a non-constant function to be used in the same place?

comment:3 Changed 10 years ago by jason

  • Cc jason added

comment:4 in reply to: ↑ 2 Changed 10 years ago by nthiery

  • Description modified (diff)
  • Status changed from new to assigned

Replying to mhansen:

How is this intended to be used?

One of my use case looks like:

def my_objects(<some parameters>, predicate = ConstantFunction?(True)):

""" Returns all the objects blah blah blah (as an EnumeratedSet?) Optionally, a predicate can be specified to select only those objects satisfying the predicate

Another one looks like:

def generating_series(..., weight = ConstantFunction?(1)):

...

What are your typical constants?

So far, True, 1, Integer(1)

Btw: with UniqueRepresentation?, the two first yield the same constant function with the current implementation, thanks to this horror:

sage: { 1: 'a', True: 'b' } {1: 'b'}

Fixed patch in a couple minutes.

Are there instances where you'd want a non-constant function to be used in the same place?

Yes. Actually, that's the case in all the situations I encountered so far

Changed 10 years ago by nthiery

Changed 10 years ago by nthiery

comment:5 Changed 10 years ago by nthiery

  • Authors changed from nthiery to Nicolas M. Thiéry
  • Reviewers set to Florent Hivert

The updated patch removes two unused imports spotted by Florent. Apply only this one.

comment:6 Changed 10 years ago by hivert

  • Summary changed from [with patch, needs review] Constant functions to [with patch, positive review] Constant functions

The patch looks good ! Positive review !

Florent

Changed 10 years ago by mvngu

reviewer patch

comment:7 follow-up: Changed 10 years ago by mvngu

This is great stuff! So let's put it in the reference manual. The reviewer patch trac_6253-reviewer.patch adds the module sage/misc/constant_function.py to the reference manual. It also fixes some typos so that the reference manual builds without any warnings. If people are happy with my changes, then patches should be merged in this order:

  1. trac_6253-constant_function-nt.patch
  2. trac_6253-reviewer.patch

comment:8 Changed 10 years ago by mvngu

  • Summary changed from [with patch, positive review] Constant functions to [with patch, needs review] Constant functions

comment:9 in reply to: ↑ 7 Changed 10 years ago by nthiery

Replying to mvngu:

This is great stuff! So let's put it in the reference manual. The reviewer patch trac_6253-reviewer.patch adds the module sage/misc/constant_function.py to the reference manual. It also fixes some typos so that the reference manual builds without any warnings. If people are happy with my changes, then patches should be merged in this order:

  1. trac_6253-constant_function-nt.patch
  2. trac_6253-reviewer.patch

Thanks Minh! (again)

Positive review on your reviewer patch.

comment:10 Changed 10 years ago by mvngu

  • Merged in set to Sage 4.1.2.alpha0
  • Resolution set to fixed
  • Reviewers changed from Florent Hivert to Florent Hivert, Minh Van Nguyen
  • Status changed from assigned to closed
  • Summary changed from [with patch, needs review] Constant functions to [with patch, positive review] Constant functions

Merged patches in this order:

  1. trac_6253-constant_function-nt.patch
  2. trac_6253-reviewer.patch
Note: See TracTickets for help on using tickets.