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