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

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

""" 

Detecting external software 

 

This module makes up a list of external software that Sage interfaces. Availability 

of each software is tested only when necessary. This is mainly used for the doctests 

which require certain external software installed on the system. 

 

AUTHORS: 

 

- Kwankyu Lee (2016-03-09) -- initial version, based on code by Robert Bradshaw and Nathann Cohen 

""" 

 

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

# Copyright (C) 2016 KWANKYU LEE <ekwankyu@gmail.com> 

# 

# 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 multiprocessing import Array 

 

# Functions in this module whose name is of the form 'has_xxx' tests if the 

# software xxx is available to Sage. 

prefix = 'has_' 

 

def has_internet(): 

""" 

Test if Internet is available. 

 

Failure of connecting to the site "http://www.sagemath.org" within a second 

is regarded as internet being not available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_internet 

sage: has_internet() # random 

True 

""" 

from six.moves import urllib 

try: 

urllib.request.urlopen("http://www.sagemath.org",timeout=1) 

return True 

except urllib.error.URLError: 

return False 

 

def has_latex(): 

""" 

Test if Latex is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_latex 

sage: has_latex() # random 

True 

""" 

from sage.misc.latex import _run_latex_, _latex_file_ 

from sage.misc.temporary_file import tmp_filename 

try: 

f = tmp_filename(ext='.tex') 

O = open(f, 'w') 

O.write(_latex_file_('2+3')) 

O.close() 

_run_latex_(f) 

return True 

except Exception: 

return False 

 

def has_magma(): 

""" 

Test if Magma is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_magma 

sage: has_magma() # random 

True 

""" 

from sage.interfaces.magma import magma 

try: 

magma('2+3') 

return True 

except Exception: 

return False 

 

def has_matlab(): 

""" 

Test if Matlab is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_matlab 

sage: has_matlab() # random 

True 

""" 

from sage.interfaces.matlab import matlab 

try: 

matlab('2+3') 

return True 

except Exception: 

return False 

 

def has_mathematica(): 

""" 

Test if Mathematica is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_mathematica 

sage: has_mathematica() # random 

True 

""" 

from sage.interfaces.mathematica import mathematica 

try: 

mathematica('2+3') 

return True 

except Exception: 

return False 

 

def has_maple(): 

""" 

Test if Maple is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_maple 

sage: has_maple() # random 

True 

""" 

from sage.interfaces.maple import maple 

try: 

maple('2+3') 

return True 

except Exception: 

return False 

 

def has_macaulay2(): 

""" 

Test if Macaulay2 is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_macaulay2 

sage: has_macaulay2() # random 

True 

""" 

from sage.interfaces.macaulay2 import macaulay2 

try: 

macaulay2('2+3') 

return True 

except Exception: 

return False 

 

def has_octave(): 

""" 

Test if Octave is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_octave 

sage: has_octave() # random 

True 

""" 

from sage.interfaces.octave import octave 

try: 

octave('2+3') 

return True 

except Exception: 

return False 

 

def has_scilab(): 

""" 

Test if Scilab is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_scilab 

sage: has_scilab() # random 

True 

""" 

from sage.interfaces.scilab import scilab 

try: 

scilab('2+3') 

return True 

except Exception: 

return False 

 

def has_cplex(): 

""" 

Test if CPLEX is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_cplex 

sage: has_cplex() # random 

True 

""" 

from sage.numerical.mip import MixedIntegerLinearProgram 

try: 

MixedIntegerLinearProgram(solver='cplex') 

return True 

except Exception: 

return False 

 

def has_gurobi(): 

""" 

Test if Gurobi is available. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import has_gurobi 

sage: has_gurobi() # random 

True 

""" 

from sage.numerical.mip import MixedIntegerLinearProgram 

try: 

MixedIntegerLinearProgram(solver='gurobi') 

return True 

except Exception: 

return False 

 

def external_software(): 

""" 

Return the alphabetical list of external software supported by this module. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import external_software 

sage: sorted(external_software) == external_software 

True 

""" 

supported = list() 

for func in globals(): 

if func.startswith(prefix): 

supported.append(func[len(prefix):]) 

return sorted(supported) 

 

external_software = external_software() 

 

def _lookup(software): 

""" 

Test if the software is available on the system. 

 

EXAMPLES:: 

 

sage: sage.doctest.external._lookup('internet') # random 

True 

""" 

if software in external_software: 

func = globals().get(prefix + software) 

return func() 

else: 

return False 

 

class AvailableSoftware(object): 

""" 

This class keeps the set of available software whose availability is detected lazily 

from the list of external software. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import external_software, available_software 

sage: external_software 

['cplex', 

'gurobi', 

'internet', 

'latex', 

'macaulay2', 

'magma', 

'maple', 

'mathematica', 

'matlab', 

'octave', 

'scilab'] 

sage: 'internet' in available_software # random 

True 

sage: available_software.issuperset(set(['internet','latex'])) # random 

True 

""" 

def __init__(self): 

""" 

Initialization. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import AvailableSoftware 

sage: S = AvailableSoftware() 

sage: S.seen() # random 

[] 

""" 

# For multiprocessing of doctests, the data self._seen should be  

# shared among subprocesses. Thus we use Array class from the  

# multiprocessing module. 

self._seen = Array('i', len(external_software)) # initialized to zeroes 

 

def __contains__(self, item): 

""" 

Return ``True`` if ``item`` is available on the system. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import available_software 

sage: 'internet' in available_software # random 

True 

""" 

try: 

idx = external_software.index(item) 

except Exception: 

return False 

if not self._seen[idx]: 

if _lookup(item): 

self._seen[idx] = 1 # available 

else: 

self._seen[idx] = -1 # not available 

if self._seen[idx] == 1: 

return True 

elif self._seen[idx] == -1: 

return False 

else: 

raise AssertionError("Invalid value for self.seen") 

 

def issuperset(self, other): 

""" 

Return ``True`` if ``other`` is a subset of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import available_software 

sage: available_software.issuperset(set(['internet','latex','magma'])) # random 

True 

""" 

for item in other: 

if item not in self: 

return False 

return True 

 

def seen(self): 

""" 

Return the list of detected external software. 

 

EXAMPLES:: 

 

sage: from sage.doctest.external import available_software 

sage: available_software.seen() # random 

['internet', 'latex', 'magma'] 

""" 

return [external_software[i] for i in range(len(external_software)) if self._seen[i] > 0] 

 

available_software = AvailableSoftware()