Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

r""" 

Constant functions 

""" 

  

#***************************************************************************** 

# Copyright (C) 2009 Nicolas M. Thiery <nthiery at users.sf.net> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

  

from sage.structure.richcmp cimport richcmp 

from sage.structure.sage_object cimport SageObject 

  

  

cdef class ConstantFunction(SageObject): 

""" 

A class for function objects implementing constant functions. 

  

EXAMPLES:: 

  

sage: f = ConstantFunction(3) 

sage: f 

The constant function (...) -> 3 

sage: f() 

3 

sage: f(5) 

3 

  

Such a function could be implemented as a lambda expression, but 

this is not (currently) picklable:: 

  

sage: g = lambda x: 3 

sage: g == loads(dumps(g)) 

Traceback (most recent call last): 

... 

PicklingError: Can't pickle ...: attribute lookup ... failed 

sage: f == loads(dumps(f)) 

True 

  

Also, in the long run, the information that this function is 

constant could be used by some algorithms. 

  

.. TODO:: 

  

- Should constant functions have unique representation? 

- Should the number of arguments be specified in the input? 

- Should this go into ``sage.categories.maps``? 

Then what should be the parent (e.g. for ``lambda x: True``)? 

  

TESTS: 

  

These tests do fail if we try to use ``UniqueRepresentation``:: 

  

sage: f = ConstantFunction(True) 

sage: g = ConstantFunction(1) 

sage: f(), g() 

(True, 1) 

  

That's because ``1`` and ``True`` cannot be distinguished as keys 

in a dictionary (argl!):: 

  

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

{1: 'b'} 

""" 

cdef object _value 

  

def __init__(self, value): 

""" 

EXAMPLES:: 

  

sage: ConstantFunction(1)() 

1 

""" 

self._value = value 

  

def __reduce__(self): 

""" 

TESTS:: 

  

sage: loads(dumps(ConstantFunction(5))) == ConstantFunction(5) # indirect doctest 

True 

  

""" 

return ConstantFunction, (self._value,) 

  

def _repr_(self): 

""" 

EXAMPLES:: 

  

sage: ConstantFunction(1) 

The constant function (...) -> 1 

""" 

return "The constant function (...) -> %s"%self._value 

  

def __call__(self, *args): 

""" 

EXAMPLES:: 

  

sage: ConstantFunction(1)() 

1 

sage: ConstantFunction(1)(5,3) 

1 

sage: ConstantFunction(True)() 

True 

""" 

return self._value 

  

def __richcmp__(self, other, op): 

""" 

EXAMPLES:: 

  

sage: ConstantFunction(1) == ConstantFunction(1) 

True 

sage: ConstantFunction(1) == ConstantFunction(3) 

False 

sage: ConstantFunction(1) == 1 

False 

sage: ConstantFunction(True) == ConstantFunction(1) # argl! 

True 

""" 

if not isinstance(other, ConstantFunction): 

return NotImplemented 

return richcmp((<ConstantFunction>self)._value, 

(<ConstantFunction>other)._value, op)