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

r""" 

Formatting utilities 

 

This module defines helper functions that are not class methods. 

 

AUTHORS: 

 

- Eric Gourgoulhon, Michal Bejger (2014-2015): initial version 

- Joris Vankerschaver (2010): for the function :func:`is_atomic()` 

 

""" 

 

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

# Copyright (C) 2015 Eric Gourgoulhon <eric.gourgoulhon@obspm.fr> 

# Copyright (C) 2015 Michal Bejger <bejger@camk.edu.pl> 

# 

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

# 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/ 

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

 

import six 

from sage.structure.sage_object import SageObject 

 

def is_atomic(expression): 

r""" 

Helper function to check whether some LaTeX expression is atomic. 

 

Adapted from method 

:meth:`~sage.tensor.differential_form_element.DifferentialFormFormatter._is_atomic` 

of class 

:class:`~sage.tensor.differential_form_element.DifferentialFormFormatter` 

written by Joris Vankerschaver (2010). 

 

INPUT: 

 

- ``expression`` -- string representing the expression (e.g. LaTeX string) 

 

OUTPUT: 

 

- ``True`` if additive operations are enclosed in parentheses and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import is_atomic 

sage: is_atomic("2*x") 

True 

sage: is_atomic("2+x") 

False 

sage: is_atomic("(2+x)") 

True 

 

""" 

if not isinstance(expression, six.string_types): 

raise TypeError("The argument must be a string") 

level = 0 

for n, c in enumerate(expression): 

if c == '(': 

level += 1 

elif c == ')': 

level -= 1 

if c == '+' or c == '-': 

if level == 0 and n > 0: 

return False 

return True 

 

 

def is_atomic_wedge_txt(expression): 

r""" 

Helper function to check whether some text-formatted expression is atomic 

in terms of wedge products. 

 

Adapted from method 

:meth:`~sage.tensor.differential_form_element.DifferentialFormFormatter._is_atomic` 

of class 

:class:`~sage.tensor.differential_form_element.DifferentialFormFormatter` 

written by Joris Vankerschaver (2010). 

 

INPUT: 

 

- ``expression`` -- string representing the text-formatted expression 

 

OUTPUT: 

 

- ``True`` if wedge products are enclosed in parentheses and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import is_atomic_wedge_txt 

sage: is_atomic_wedge_txt("a") 

True 

sage: is_atomic_wedge_txt(r"a/\b") 

False 

sage: is_atomic_wedge_txt(r"(a/\b)") 

True 

sage: is_atomic_wedge_txt(r"(a/\b)/\c") 

False 

sage: is_atomic_wedge_txt(r"(a/\b/\c)") 

True 

 

""" 

if not isinstance(expression, six.string_types): 

raise TypeError("The argument must be a string.") 

level = 0 

for n, c in enumerate(expression): 

if c == '(': 

level += 1 

elif c == ')': 

level -= 1 

if c == '/' and expression[n+1:n+2] == '\\': 

if level == 0 and n > 0: 

return False 

return True 

 

 

def is_atomic_wedge_latex(expression): 

r""" 

Helper function to check whether LaTeX-formatted expression is atomic in 

terms of wedge products. 

 

Adapted from method 

:meth:`~sage.tensor.differential_form_element.DifferentialFormFormatter._is_atomic` 

of class 

:class:`~sage.tensor.differential_form_element.DifferentialFormFormatter` 

written by Joris Vankerschaver (2010). 

 

INPUT: 

 

- ``expression`` -- string representing the LaTeX expression 

 

OUTPUT: 

 

- ``True`` if wedge products are enclosed in parentheses and 

``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import is_atomic_wedge_latex 

sage: is_atomic_wedge_latex(r"a") 

True 

sage: is_atomic_wedge_latex(r"a\wedge b") 

False 

sage: is_atomic_wedge_latex(r"(a\wedge b)") 

True 

sage: is_atomic_wedge_latex(r"(a\wedge b)\wedge c") 

False 

sage: is_atomic_wedge_latex(r"((a\wedge b)\wedge c)") 

True 

sage: is_atomic_wedge_latex(r"(a\wedge b\wedge c)") 

True 

sage: is_atomic_wedge_latex(r"\omega\wedge\theta") 

False 

sage: is_atomic_wedge_latex(r"(\omega\wedge\theta)") 

True 

sage: is_atomic_wedge_latex(r"\omega\wedge(\theta+a)") 

False 

 

""" 

if not isinstance(expression, six.string_types): 

raise TypeError("The argument must be a string.") 

level = 0 

for n, c in enumerate(expression): 

if c == '(': 

level += 1 

elif c == ')': 

level -= 1 

if c == '\\' and expression[n+1:n+6] == 'wedge': 

if level == 0 and n > 0: 

return False 

return True 

 

 

def format_mul_txt(name1, operator, name2): 

r""" 

Helper function for text-formatted names of results of multiplication or 

tensor product. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import format_mul_txt 

sage: format_mul_txt('a', '*', 'b') 

'a*b' 

sage: format_mul_txt('a+b', '*', 'c') 

'(a+b)*c' 

sage: format_mul_txt('a', '*', 'b+c') 

'a*(b+c)' 

sage: format_mul_txt('a+b', '*', 'c+d') 

'(a+b)*(c+d)' 

sage: format_mul_txt(None, '*', 'b') 

sage: format_mul_txt('a', '*', None) 

 

""" 

if name1 is None or name2 is None: 

return None 

if not is_atomic(name1) or not is_atomic_wedge_txt(name1): 

name1 = '(' + name1 + ')' 

if not is_atomic(name2) or not is_atomic_wedge_txt(name2): 

name2 = '(' + name2 + ')' 

return name1 + operator + name2 

 

 

def format_mul_latex(name1, operator, name2): 

r""" 

Helper function for LaTeX names of results of multiplication or tensor 

product. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import format_mul_latex 

sage: format_mul_latex('a', '*', 'b') 

'a*b' 

sage: format_mul_latex('a+b', '*', 'c') 

'\\left(a+b\\right)*c' 

sage: format_mul_latex('a', '*', 'b+c') 

'a*\\left(b+c\\right)' 

sage: format_mul_latex('a+b', '*', 'c+d') 

'\\left(a+b\\right)*\\left(c+d\\right)' 

sage: format_mul_latex(None, '*', 'b') 

sage: format_mul_latex('a', '*', None) 

 

""" 

if name1 is None or name2 is None: 

return None 

if not is_atomic(name1) or not is_atomic_wedge_latex(name1): 

name1 = r'\left(' + name1 + r'\right)' 

if not is_atomic(name2) or not is_atomic_wedge_latex(name2): 

name2 = r'\left(' + name2 + r'\right)' 

return name1 + operator + name2 

 

 

def format_unop_txt(operator, name): 

r""" 

Helper function for text-formatted names of results of unary operator. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import format_unop_txt 

sage: format_unop_txt('-', 'a') 

'-a' 

sage: format_unop_txt('-', 'a+b') 

'-(a+b)' 

sage: format_unop_txt('-', '(a+b)') 

'-(a+b)' 

sage: format_unop_txt('-', None) 

 

""" 

if name is None: 

return None 

if not is_atomic(name) or not is_atomic_wedge_txt(name): 

#!# is_atomic_otimes_txt should be added 

name = '(' + name + ')' 

return operator + name 

 

 

def format_unop_latex(operator, name): 

r""" 

Helper function for LaTeX names of results of unary operator. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import format_unop_latex 

sage: format_unop_latex('-', 'a') 

'-a' 

sage: format_unop_latex('-', 'a+b') 

'-\\left(a+b\\right)' 

sage: format_unop_latex('-', '(a+b)') 

'-(a+b)' 

sage: format_unop_latex('-', None) 

 

""" 

if name is None: 

return None 

if not is_atomic(name) or not is_atomic_wedge_latex(name): 

#!# is_atomic_otimes_latex should be added 

name = r'\left(' + name + r'\right)' 

return operator + name 

 

 

class FormattedExpansion(SageObject): 

r""" 

Helper class for displaying tensor expansions. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import FormattedExpansion 

sage: f = FormattedExpansion('v', r'\tilde v') 

sage: f 

v 

sage: latex(f) 

\tilde v 

sage: f = FormattedExpansion('x/2', r'\frac{x}{2}') 

sage: f 

x/2 

sage: latex(f) 

\frac{x}{2} 

 

""" 

def __init__(self, txt=None, latex=None): 

r""" 

TESTS:: 

 

sage: from sage.tensor.modules.format_utilities import FormattedExpansion 

sage: f = FormattedExpansion('v', r'\tilde v') 

sage: f 

v 

 

""" 

self._txt = txt 

self._latex = latex 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import FormattedExpansion 

sage: f = FormattedExpansion('v', r'\tilde v') 

sage: f._repr_() 

'v' 

 

""" 

return self._txt 

 

def _latex_(self): 

r""" 

Return a LaTeX representation of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.tensor.modules.format_utilities import FormattedExpansion 

sage: f = FormattedExpansion('v', r'\tilde v') 

sage: f._latex_() 

'\\tilde v' 

 

""" 

return self._latex