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

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

r""" 

Symbolic Minimum and Maximum 

 

Sage provides a symbolic maximum and minimum due to the fact that the Python 

builtin max and min are not able to deal with variables as users might expect. 

These functions wait to evaluate if there are variables. 

 

Here you can see some differences:: 

 

sage: max(x,x^2) 

x 

sage: max_symbolic(x,x^2) 

max(x, x^2) 

sage: f(x) = max_symbolic(x,x^2); f(1/2) 

1/2 

 

This works as expected for more than two entries:: 

 

sage: max(3,5,x) 

5 

sage: min(3,5,x) 

3 

sage: max_symbolic(3,5,x) 

max(x, 5) 

sage: min_symbolic(3,5,x) 

min(x, 3) 

 

""" 

############################################################################### 

# Sage: Open Source Mathematical Software 

# Copyright (C) 2010 Burcin Erocal <burcin@erocal.org> 

# Distributed under the terms of the GNU General Public License (GPL), 

# version 2 or any later version. The full text of the GPL is available at: 

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

############################################################################### 

from __future__ import absolute_import 

 

from sage.symbolic.function import BuiltinFunction 

from sage.symbolic.expression import Expression 

from sage.symbolic.ring import SR 

 

from six.moves.builtins import max as builtin_max, min as builtin_min 

 

class MinMax_base(BuiltinFunction): 

def eval_helper(self, this_f, builtin_f, initial_val, args): 

""" 

EXAMPLES:: 

 

sage: max_symbolic(3,5,x) # indirect doctest 

max(x, 5) 

sage: min_symbolic(3,5,x) 

min(x, 3) 

""" 

# __call__ ensures that if args is a singleton, the element is iterable 

arg_is_iter = False 

if len(args) == 1: 

arg_is_iter = True 

args = args[0] 

 

symb_args = [] 

res = initial_val 

num_non_symbolic_args = 0 

for x in args: 

if isinstance(x, Expression): 

symb_args.append(x) 

else: 

num_non_symbolic_args += 1 

res = builtin_f(res, x) 

 

# if no symbolic arguments, return the result 

if len(symb_args) == 0: 

if res is None: 

# this is a hack to get the function to return None to the user 

# the convention to leave a given symbolic function unevaluated 

# is to return None from the _eval_ function, so we need 

# a trick to indicate that the return value of the function is 

# really None 

# this is caught in the __call__ method, which knows to return 

# None in this case 

raise ValueError("return None") 

return res 

 

# if all arguments were symbolic return 

if num_non_symbolic_args <= 1 and not arg_is_iter: 

return None 

 

if res is not None: symb_args.append(res) 

return this_f(*symb_args) 

 

def __call__(self, *args, **kwds): 

""" 

EXAMPLES:: 

 

sage: max_symbolic(3,5,x) 

max(x, 5) 

sage: max_symbolic(3,5,x, hold=True) 

max(3, 5, x) 

sage: max_symbolic([3,5,x]) 

max(x, 5) 

 

:: 

 

sage: min_symbolic(3,5,x) 

min(x, 3) 

sage: min_symbolic(3,5,x, hold=True) 

min(3, 5, x) 

sage: min_symbolic([3,5,x]) 

min(x, 3) 

 

TESTS: 

 

We get an exception if no arguments are given:: 

 

sage: max_symbolic() 

Traceback (most recent call last): 

... 

ValueError: number of arguments must be > 0 

 

Check if we return None, when the builtin function would:: 

 

sage: max_symbolic([None]) is None 

True 

sage: max_symbolic([None, None]) is None 

True 

sage: min_symbolic([None]) is None 

True 

sage: min_symbolic([None, None]) is None 

True 

 

Check if a single argument which is not iterable works:: 

 

sage: max_symbolic(None) 

Traceback (most recent call last): 

... 

TypeError: 'NoneType' object is not iterable 

sage: max_symbolic(5) 

Traceback (most recent call last): 

... 

TypeError: 'sage.rings.integer.Integer' object is not iterable 

sage: max_symbolic(x) 

Traceback (most recent call last): 

... 

TypeError: 'sage.symbolic.expression.Expression' object is not iterable 

sage: min_symbolic(5) 

Traceback (most recent call last): 

... 

TypeError: 'sage.rings.integer.Integer' object is not iterable 

sage: min_symbolic(x) 

Traceback (most recent call last): 

... 

TypeError: 'sage.symbolic.expression.Expression' object is not iterable 

""" 

if len(args) == 0: 

raise ValueError("number of arguments must be > 0") 

if len(args) == 1: 

try: 

args=(SR._force_pyobject(iter(args[0])),) 

except TypeError as e: 

raise e 

 

try: 

return BuiltinFunction.__call__(self, *args, **kwds) 

except ValueError as e: 

if e.args[0] == "return None": 

return None 

 

class MaxSymbolic(MinMax_base): 

def __init__(self): 

r""" 

Symbolic `\max` function. 

 

The Python builtin `\max` function doesn't work as expected when symbolic 

expressions are given as arguments. This function delays evaluation 

until all symbolic arguments are substituted with values. 

 

EXAMPLES:: 

 

sage: max_symbolic(3, x) 

max(3, x) 

sage: max_symbolic(3, x).subs(x=5) 

5 

sage: max_symbolic(3, 5, x) 

max(x, 5) 

sage: max_symbolic([3,5,x]) 

max(x, 5) 

 

TESTS:: 

 

sage: loads(dumps(max_symbolic(x,5))) 

max(x, 5) 

sage: latex(max_symbolic(x,5)) 

\max\left(x, 5\right) 

sage: max_symbolic(x, 5)._sympy_() 

Max(5, x) 

""" 

BuiltinFunction.__init__(self, 'max', nargs=0, latex_name="\max", 

conversions=dict(sympy='Max')) 

 

def _eval_(self, *args): 

""" 

EXAMPLES:: 

 

sage: t = max_symbolic(x, 5); t 

max(x, 5) 

sage: t.subs(x=3) # indirect doctest 

5 

sage: max_symbolic(5,3) 

5 

sage: u = max_symbolic(*(list(range(10))+[x])); u 

max(x, 9) 

sage: u.subs(x=-1) 

9 

sage: u.subs(x=10) 

10 

sage: max_symbolic([0,x]) 

max(x, 0) 

 

TESTS:: 

 

sage: max_symbolic() 

Traceback (most recent call last): 

... 

ValueError: number of arguments must be > 0 

""" 

return self.eval_helper(max_symbolic, builtin_max, None, args) 

 

def _evalf_(self, *args, **kwds): 

""" 

EXAMPLES:: 

 

sage: t = max_symbolic(sin(x), cos(x)) 

sage: t.subs(x=1).n(200) 

0.84147098480789650665250232163029899962256306079837106567275 

sage: var('y') 

y 

sage: t = max_symbolic(sin(x), cos(x), y) 

sage: u = t.subs(x=1); u 

max(sin(1), cos(1), y) 

sage: u.n() 

Traceback (most recent call last): 

... 

TypeError: cannot evaluate symbolic expression numerically 

 

:: 

 

sage: f = max_symbolic(sin(x), cos(x)) 

sage: r = integral(f, x, 0, 1) 

sage: r.n() 

0.8739124411567263 

""" 

return max_symbolic(args) 

 

max_symbolic = MaxSymbolic() 

 

 

class MinSymbolic(MinMax_base): 

def __init__(self): 

r""" 

Symbolic `\min` function. 

 

The Python builtin `\min` function doesn't work as expected when symbolic 

expressions are given as arguments. This function delays evaluation 

until all symbolic arguments are substituted with values. 

 

EXAMPLES:: 

 

sage: min_symbolic(3, x) 

min(3, x) 

sage: min_symbolic(3, x).subs(x=5) 

3 

sage: min_symbolic(3, 5, x) 

min(x, 3) 

sage: min_symbolic([3,5,x]) 

min(x, 3) 

 

TESTS:: 

 

sage: loads(dumps(min_symbolic(x,5))) 

min(x, 5) 

sage: latex(min_symbolic(x,5)) 

\min\left(x, 5\right) 

sage: min_symbolic(x, 5)._sympy_() 

Min(5, x) 

""" 

BuiltinFunction.__init__(self, 'min', nargs=0, latex_name="\min", 

conversions=dict(sympy='Min')) 

 

def _eval_(self, *args): 

""" 

EXAMPLES:: 

 

sage: t = min_symbolic(x, 5); t 

min(x, 5) 

sage: t.subs(x=3) # indirect doctest 

3 

sage: min_symbolic(5,3) 

3 

sage: u = min_symbolic(*(list(range(10))+[x])); u 

min(x, 0) 

sage: u.subs(x=-1) 

-1 

sage: u.subs(x=10) 

0 

sage: min_symbolic([3,x]) 

min(x, 3) 

 

TESTS:: 

 

sage: min_symbolic() 

Traceback (most recent call last): 

... 

ValueError: number of arguments must be > 0 

""" 

return self.eval_helper(min_symbolic, builtin_min, float('inf'), args) 

 

def _evalf_(self, *args, **kwds): 

""" 

EXAMPLES:: 

 

sage: t = min_symbolic(sin(x), cos(x)) 

sage: t.subs(x=1).n(200) 

0.54030230586813971740093660744297660373231042061792222767010 

sage: var('y') 

y 

sage: t = min_symbolic(sin(x), cos(x), y) 

sage: u = t.subs(x=1); u 

min(sin(1), cos(1), y) 

sage: u.n() 

Traceback (most recent call last): 

... 

TypeError: cannot evaluate symbolic expression numerically 

""" 

return min_symbolic(args) 

 

min_symbolic = MinSymbolic()