Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/misc/constant_function.pyx
8814 views
1
r"""
2
Constant functions
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2009 Nicolas M. Thiery <nthiery at users.sf.net>
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# http://www.gnu.org/licenses/
9
#******************************************************************************
10
11
from sage.structure.sage_object cimport SageObject
12
cdef class ConstantFunction(SageObject):
13
"""
14
A class for function objects implementing constant functions.
15
16
EXAMPLES::
17
18
sage: f = ConstantFunction(3)
19
sage: f
20
The constant function (...) -> 3
21
sage: f()
22
3
23
sage: f(5)
24
3
25
26
Such a function could be implemented as a lambda expression, but
27
this is not (currently) picklable::
28
29
sage: g = lambda x: 3
30
sage: g == loads(dumps(g))
31
Traceback (most recent call last):
32
...
33
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
34
sage: f == loads(dumps(f))
35
True
36
37
Also, in the long run, the information that this function is
38
constant could be used by some algorithms.
39
40
TODO:
41
42
- Should constant functions have unique representation?
43
- Should the number of arguments be specified in the input?
44
- Should this go into ``sage.categories.maps``?
45
Then what should be the parent (e.g. for ``lambda x: True``)?
46
47
TESTS:
48
49
These tests do fail if we try to use ``UniqueRepresentation``::
50
51
sage: f = ConstantFunction(True)
52
sage: g = ConstantFunction(1)
53
sage: f(), g()
54
(True, 1)
55
56
That's because ``1`` and ``True`` cannot be distinguished as keys
57
in a dictionary (argl!)::
58
59
sage: { 1: 'a', True: 'b' }
60
{1: 'b'}
61
"""
62
cdef object _value
63
64
def __init__(self, value):
65
"""
66
EXAMPLES::
67
68
sage: ConstantFunction(1)()
69
1
70
"""
71
self._value = value
72
73
def __reduce__(self):
74
"""
75
TESTS::
76
77
sage: loads(dumps(ConstantFunction(5))) == ConstantFunction(5) # indirect doctest
78
True
79
80
"""
81
return ConstantFunction, (self._value,)
82
83
def _repr_(self):
84
"""
85
EXAMPLES::
86
87
sage: ConstantFunction(1)
88
The constant function (...) -> 1
89
"""
90
return "The constant function (...) -> %s"%self._value
91
92
def __call__(self, *args):
93
"""
94
EXAMPLES::
95
96
sage: ConstantFunction(1)()
97
1
98
sage: ConstantFunction(1)(5,3)
99
1
100
sage: ConstantFunction(True)()
101
True
102
"""
103
return self._value
104
105
def __cmp__(self, other):
106
"""
107
EXAMPLES::
108
109
sage: ConstantFunction(1) == ConstantFunction(1)
110
True
111
sage: ConstantFunction(1) == ConstantFunction(3)
112
False
113
sage: ConstantFunction(1) == 1
114
False
115
sage: ConstantFunction(True) == ConstantFunction(1) # argl!
116
True
117
"""
118
cdef int c = cmp(self.__class__, other.__class__)
119
if c:
120
return c
121
return cmp(self._value, (<ConstantFunction>other)._value)
122
123