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

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

r""" 

Interface to MuPAD 

 

AUTHOR: 

 

- Mike Hansen 

- William Stein 

 

You must have the optional commercial MuPAD interpreter installed and 

available as the command \code{mupkern} in your PATH in order to use 

this interface. You do not have to install any optional \sage 

packages. 

 

TESTS:: 

 

sage: mupad.package('"MuPAD-Combinat"') # optional - mupad 

sage: combinat = mupad.combinat # optional - mupad 

sage: examples = mupad.examples # optional - mupad 

sage: S = examples.SymmetricFunctions() # optional - mupad 

sage: S.s[2,1]^2 # optional - mupad 

s[3, 3] + s[4, 2] + s[2, 2, 1, 1] + s[2, 2, 2] + 2 s[3, 2, 1] + s[4, 1, 1] + 

s[3, 1, 1, 1] 

sage: S.omega( S.s[3] ) # optional - mupad 

s[1, 1, 1] 

sage: s = S.s # optional - mupad 

sage: p = S.p # optional - mupad 

sage: s(s[2,1] + p[2,1]) # optional - mupad 

s[2, 1] + s[3] - s[1, 1, 1] 

sage: s(_) # optional - mupad 

s[2, 1] + s[3] - s[1, 1, 1] 

 

sage: combinat.tableaux.list(3) # optional - mupad # note: the order of the result seems to depend on the version of MuPAD / MuPAD-Combinat 

-- +---+ -- 

| | 3 | | 

| +---+ +---+ +---+ | 

| | 3 | | 2 | | 2 | | 

| +---+---+---+ +---+---+ +---+---+ +---+ | 

| | 1 | 2 | 3 |, | 1 | 2 |, | 1 | 3 |, | 1 | | 

-- +---+---+---+ +---+---+ +---+---+ +---+ -- 

sage: three = mupad(3) # optional - mupad 

sage: three.combinat.tableaux.list() # optional - mupad 

-- +---+ -- 

| | 3 | | 

| +---+ +---+ +---+ | 

| | 3 | | 2 | | 2 | | 

| +---+---+---+ +---+---+ +---+---+ +---+ | 

| | 1 | 2 | 3 |, | 1 | 2 |, | 1 | 3 |, | 1 | | 

-- +---+---+---+ +---+---+ +---+---+ +---+ -- 

sage: t = _[1] # optional - mupad 

sage: t # optional - mupad 

+---+---+---+ 

| 1 | 2 | 3 | 

+---+---+---+ 

sage: combinat.tableaux.conjugate(t) # optional - mupad 

+---+ 

| 3 | 

+---+ 

| 2 | 

+---+ 

| 1 | 

+---+ 

 

sage: combinat.ribbonsTableaux.list([2,2],[1,1],2) # optional - mupad 

-- +---+---+ +---+---+ -- 

| | | 2 | | 2 | | 

| + + +, +---+---+ | 

| | 1 | | | 1 | | 

-- +---+---+ +---+---+ -- 

sage: combinat.tableaux.kAtom([2,1],3) # optional - mupad 

-- +---+ -- 

| | 2 | | 

| +---+---+ | 

| | 1 | 1 | | 

-- +---+---+ -- 

sage: M = S.Macdonald() # optional - mupad 

sage: a = M.P[1]^2 # optional - mupad 

sage: mupad.mapcoeffs(a, 'normal') # optional - mupad 

q - t + q t - 1 

P[2] + --------------- P[1, 1] 

q t - 1 

 

""" 

 

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

# Copyright (C) 2008 Mike Hansen <mhansen@gmail.com> 

# William Stein <wstein@gmail.com> 

# 

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

# 

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

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

from __future__ import print_function 

from __future__ import absolute_import 

 

import os 

 

from .expect import (Expect, ExpectElement, ExpectFunction, 

FunctionElement, AsciiArtString) 

from sage.interfaces.tab_completion import ExtraTabCompletion 

from sage.env import DOT_SAGE 

from sage.docs.instancedoc import instancedoc 

 

COMMANDS_CACHE = '%s/mupad_commandlist_cache.sobj'%DOT_SAGE 

PROMPT = ">>" 

seq = 0 

 

 

class Mupad(ExtraTabCompletion, Expect): 

""" 

Interface to the MuPAD interpreter. 

""" 

def __init__(self, maxread=None, script_subdirectory=None, server=None, server_tmpdir=None, logfile=None): 

""" 

Create an instance of the MuPAD interpreter. 

 

EXAMPLES:: 

 

sage: mupad == loads(dumps(mupad)) # optional - mupad 

True 

""" 

Expect.__init__(self, 

name = 'MuPAD', 

prompt = PROMPT, 

# the -U SAGE=TRUE allows for MuPAD programs to test whether they are run from Sage 

command = "mupkern -P e -U SAGE=TRUE", 

script_subdirectory = script_subdirectory, 

server = server, 

server_tmpdir = server_tmpdir, 

restart_on_ctrlc = False, 

verbose_start = False, 

logfile = None) 

 

 

 

 

def _function_class(self): 

""" 

EXAMPLES:: 

 

sage: mupad._function_class() 

<class 'sage.interfaces.mupad.MupadFunction'> 

 

sage: mdiff = mupad.diff; mdiff # optional - mupad 

diff 

sage: type(mdiff) # optional -- mupad 

<class 'sage.interfaces.mupad.MupadFunction'> 

""" 

return MupadFunction 

 

def __reduce__(self): 

""" 

EXAMPLES:: 

 

sage: mupad.__reduce__() 

(<function reduce_load_mupad at 0x...>, ()) 

 

""" 

return reduce_load_mupad, tuple([]) 

 

def _read_in_file_command(self, filename): 

""" 

EXAMPLES:: 

 

sage: mupad._read_in_file_command('test') 

'read("test")' 

 

sage: filename = tmp_filename() 

sage: f = open(filename, 'w') 

sage: _ = f.write('x := 2;\n') 

sage: f.close() 

sage: mupad.read(filename) # optional - MuPAD 

sage: mupad.get('x').strip() # optional - mupad 

'2' 

 

""" 

return 'read("%s")'%filename 

 

def _quit_string(self): 

""" 

EXAMPLES:: 

 

sage: mupad._quit_string() 

'quit' 

""" 

return 'quit' 

 

def _install_hints(self): 

""" 

Hints for installing MuPAD on your computer. 

 

EXAMPLES:: 

 

sage: print(mupad._install_hints()) 

<BLANKLINE> 

In order to use the MuPAD interface you need to have MuPAD installed 

... 

 

""" 

return """ 

In order to use the MuPAD interface you need to have MuPAD installed 

and have a script in your PATH called "mupkern" that runs the 

command-line version of MuPAD. 

 

(1) You might have to buy MuPAD. 

 

(2) * LINUX: The mupkern script comes standard with your Mupad install. 

 

* APPLE OS X: 

??? 

""" 

 

def expect(self): 

""" 

EXAMPLES:: 

 

sage: a = mupad(1) # optional - mupad 

sage: mupad.expect() # optional - mupad 

<pexpect.spawn instance at 0x...> 

""" 

return self._expect 

 

def console(self): 

""" 

Spawn a new MuPAD command-line session. 

 

EXAMPLES:: 

 

sage: mupad.console() #not tested 

 

*----* MuPAD Pro 4.0.2 -- The Open Computer Algebra System 

/| /| 

*----* | Copyright (c) 1997 - 2007 by SciFace Software 

| *--|-* All rights reserved. 

|/ |/ 

*----* Licensed to: ... 

 

""" 

mupad_console() 

 

def eval(self, code, strip=True, **kwds): 

""" 

EXAMPLES:: 

 

sage: mupad.eval('2+2') # optional - mupad 

4 

 

""" 

s = Expect.eval(self, code, **kwds) 

return AsciiArtString(s) 

 

def _eval_line(self, line, allow_use_file=True, wait_for_prompt=True, 

need_output=True, restart_if_needed=False): 

""" 

EXAMPLES:: 

 

sage: mupad._eval_line('2+2') # optional - mupad 

' 4' 

sage: mupad._eval_line('x::asdf') # optional - mupad 

Traceback (most recent call last): 

... 

RuntimeError: Unknown slot "x::asdf" [slot] 

 

""" 

if self._expect is None: 

self._start() 

if not need_output: 

E = self._expect 

E.sendline(line) 

return 

 

global seq 

seq += 1 

START = '__start__(%s+1)'%seq 

END = '__end__(%s+1)'%seq 

line = '%s; %s; %s;'%(START, line, END) 

START = '__start__(%s)'%(seq+1) 

END = '__end__(%s)'%(seq+1) 

 

E = self._expect 

E.sendline(line) 

E.expect(PROMPT) 

z = E.before 

i = z.find(START) 

if i == -1: 

raise RuntimeError("%s\nError evaluating code in MuPAD"%z) 

z = z[i+len(START)+2:] 

z = z.rstrip().rstrip(END).rstrip('"').rstrip().strip('\n').strip('\r').strip('\n').replace('\\\r\n','') 

i = z.find('Error: ') 

if i != -1: 

raise RuntimeError(z[i + 7:]) 

return z 

 

def cputime(self, t=None): 

""" 

EXAMPLES:: 

 

sage: t = mupad.cputime() #random, optional - MuPAD 

0.11600000000000001 

""" 

if t is None: 

return float(str(self('time()')))/1000 

else: 

return float(str(self('time() - %s'%float(t))))/1000 

 

def set(self, var, value): 

""" 

Set the variable var to the given value. 

 

EXAMPLES:: 

 

sage: mupad.set('a', 4) # optional - mupad 

sage: mupad.get('a').strip() # optional - mupad 

'4' 

""" 

cmd = '%s:=%s:'%(var,value) 

out = self.eval(cmd) 

i = out.find('Error: ') 

if i != -1: 

raise RuntimeError(out[i + 7:]) 

 

def get(self, var): 

""" 

Get the value of the variable var. 

 

EXAMPLES:: 

 

sage: mupad.set('a', 4) # optional - mupad 

sage: mupad.get('a').strip() # optional - mupad 

'4' 

 

""" 

s = self.eval('%s'%var) 

i = s.find('=') 

return s[i+1:] 

 

def _object_class(self): 

""" 

EXAMPLES:: 

 

sage: mupad._object_class() 

<class 'sage.interfaces.mupad.MupadElement'> 

""" 

return MupadElement 

 

def _equality_symbol(self): 

""" 

EXAMPLES:: 

 

sage: mupad._equality_symbol() 

'=' 

""" 

return '=' 

 

def _assign_symbol(self): 

""" 

EXAMPLES:: 

 

sage: mupad._assign_symbol() 

':=' 

""" 

return ":=" 

 

def _continuation_prompt(self): 

""" 

EXAMPLES:: 

 

sage: mupad._continuation_prompt() 

'&>' 

""" 

return "&>" 

 

def _commands(self): 

""" 

Return list of all commands defined in MuPAD. 

 

EXAMPLES:: 

 

sage: cmds = mupad._commands() # optional - mupad 

sage: len(cmds) > 100 # optional - mupad 

True 

sage: 'diff' in cmds # optional - mupad 

True 

""" 

try: 

v = sum([self.completions(chr(65+n)) for n in range(26)], []) + \ 

sum([self.completions(chr(97+n)) for n in range(26)], []) 

except RuntimeError: 

print("\n" * 3) 

print("*" * 70) 

print("WARNING: You do not have a working version of MuPAD installed!") 

print("*" * 70) 

v = [] 

v.sort() 

return v 

 

def _tab_completion(self, verbose=True, use_disk_cache=True): 

""" 

EXAMPLES:: 

 

sage: names = mupad._tab_completion() # optional - mupad 

sage: len(names) > 100 # optional - mupad 

True 

sage: 'combinat' in names # optional - mupad 

True 

""" 

try: 

return self.__tab_completion 

except AttributeError: 

import sage.misc.persist 

if use_disk_cache: 

try: 

self.__tab_completion = sage.misc.persist.load(COMMANDS_CACHE) 

return self.__tab_completion 

except IOError: 

pass 

if verbose: 

print("\nBuilding MuPAD command completion list (this takes") 

print("a few seconds only the first time you do it).") 

print("To force rebuild later, delete %s." % COMMANDS_CACHE) 

v = self._commands() 

self.__tab_completion = v 

if len(v) > 200: 

# MuPAD is actually installed. 

sage.misc.persist.save(v, COMMANDS_CACHE) 

return v 

 

def completions(self, string, strip=False): 

""" 

EXAMPLES:: 

 

sage: mupad.completions('linal') # optional - mupad 

['linalg'] 

""" 

res = self.eval('_pref(Complete)("%s")'%string).strip() 

res = res.replace('\n', '').split(',') 

res = [s.strip().strip('"') for s in res] 

res = [s for s in res if not s.endswith('::')] 

if strip: 

n = len(string) 

res = [s[n:] for s in res] 

 

return res if res != [''] else [] 

 

 

@instancedoc 

class MupadFunction(ExtraTabCompletion, ExpectFunction): 

def _instancedoc_(self): 

""" 

EXAMPLES:: 

 

sage: mupad.diff.__doc__ 

No help on diff available 

""" 

M = self._parent 

return M.help(self._name) 

 

def __getattr__(self, attrname): 

""" 

EXAMPLES:: 

 

sage: mupad.linalg.addRow 

linalg::addRow 

""" 

if attrname[:1] == "_": 

raise AttributeError 

return MupadFunction(self._parent, self._name+"::"+attrname) 

 

def _tab_completion(self): 

""" 

EXAMPLES:: 

 

sage: mupad.linalg._tab_completion() # optional - mupad 

['addCol', 

'addRow', 

... 

'wiedemann'] 

 

""" 

res = self._parent.completions(self._name+"::", strip=True) 

return res if res != [] else self._parent._tab_completion() 

 

 

@instancedoc 

class MupadFunctionElement(ExtraTabCompletion, FunctionElement): 

def _instancedoc_(self): 

""" 

EXAMPLES:: 

 

sage: x = mupad('x') # optional - mupad 

sage: x.diff.__doc__ # optional - mupad 

No help on diff available 

 

""" 

return self._obj.parent().help(self._name) 

 

def __getattr__(self, attrname): 

""" 

EXAMPLES:: 

 

sage: mupad.package('"MuPAD-Combinat"') # optional - mupad-Combinat 

sage: combinat = mupad.combinat # optional - mupad-Combinat 

sage: three = mupad(3) # optional - mupad-Combinat 

sage: type(three.combinat) # optional - mupad-Combinat 

<class 'sage.interfaces.mupad.MupadFunctionElement'> 

sage: tableaux = three.combinat.tableaux # optional - mupad-Combinat 

sage: type(tableaux) # optional - mupad-Combinat 

<class 'sage.interfaces.mupad.MupadFunctionElement'> 

""" 

P = self._obj.parent() 

if attrname[:1] == "_": 

if attrname not in self.__dict__: 

raise AttributeError 

else: 

return self.__dict__[attrname] 

name = self._name+"::"+attrname 

if P.eval('type(%s)'%name) == "DOM_DOMAIN": 

return MupadElement(P, name) 

else: 

return MupadFunctionElement(self._obj, name) 

 

def _tab_completion(self): 

""" 

EXAMPLES:: 

 

sage: three = mupad(3) # optional - mupad 

sage: 'list' in three.combinat.tableaux._tab_completion() # optional - mupad 

True 

""" 

P = self._obj.parent() 

res = P.completions(self._name+"::", strip=True) 

return res if res != [] else P._tab_completion() 

 

 

def __call__(self, *args): 

""" 

EXAMPLES:: 

 

sage: mupad.package('"MuPAD-Combinat"') # optional - mupad-Combinat 

sage: combinat = mupad.combinat # optional - mupad-Combinat 

sage: examples = mupad.examples # optional - mupad-Combinat 

sage: S = examples.SymmetricFunctions() # optional - mupad-Combinat 

sage: type(S.omega) # optional - mupad-Combinat 

<class 'sage.interfaces.mupad.MupadFunctionElement'> 

sage: S.omega(S.s[3]) # optional - mupad-Combinat 

s[1, 1, 1] 

""" 

P = self._obj.parent() 

if P.eval('type(%s)'%(self._obj.name())).strip() == "DOM_DOMAIN": 

return P.function_call(self._name, list(args)) 

else: 

return P.function_call(self._name, [self._obj] + list(args)) 

 

 

@instancedoc 

class MupadElement(ExtraTabCompletion, ExpectElement): 

 

def __getattr__(self, attrname): 

""" 

EXAMPLES:: 

 

sage: mupad.package('"MuPAD-Combinat"') # optional - mupad-Combinat 

sage: S = mupad.examples.SymmetricFunctions() # optional - mupad-Combinat 

sage: type(S) # optional - mupad-Combinat 

<class 'sage.interfaces.mupad.MupadElement'> 

sage: S.s # optional - mupad-Combinat 

(examples::SymmetricFunctions(Dom::ExpressionField()))::s 

 

sage: x = mupad('x') # optional - mupad-Combinat 

sage: x.diff(x) # optional - mupad-Combinat 

1 

 

""" 

if attrname[:1] == "_": 

if attrname not in self.__dict__: 

raise AttributeError 

else: 

return self.__dict__[attrname] 

P = self.parent() 

 

name = self._name + "::" + attrname 

try: 

if P.eval('type(%s::%s)'%(self.name(),attrname)).strip() == "DOM_DOMAIN": 

return P.new("%s::%s"%(self.name(),attrname)) 

else: 

return MupadFunctionElement(self, name) 

except RuntimeError as err: 

if 'Unknown slot' in str(err): 

return MupadFunctionElement(self, attrname) 

else: 

raise err 

 

def _tab_completion(self): 

""" 

EXAMPLES:: 

 

sage: mupad.package('"MuPAD-Combinat"') # optional - mupad-Combinat 

sage: S = mupad.examples.SymmetricFunctions() # optional - mupad-Combinat 

sage: 'HallLittlewood' in S._tab_completion() # optional - mupad-Combinat 

True 

""" 

res = self.parent().completions(self.name()+"::", strip=True) 

return res if res != [] else self.parent()._tab_completion() 

 

def _latex_(self): 

r""" 

EXAMPLES:: 

 

sage: mupad.package('"MuPAD-Combinat"') # optional - mupad-Combinat 

sage: S = mupad.examples.SymmetricFunctions() # optional - mupad-Combinat 

sage: latex(S) # optional - mupad-Combinat 

\mathrm{examples}{::}\mathrm{SymmetricFunctions}\left(\mathbb{E}\right) 

""" 

self._check_valid() 

P = self.parent() 

s = P._eval_line('generate::TeX(%s)'%self.name()) 

s = s.replace('\\\\','\\').strip().strip('"') 

return s 

 

def __len__(self): 

r""" 

The analogue in MuPAD of Python's len is the method nops 

 

EXAMPLES:: 

 

sage: len(mupad([1,2,3])) # indirect doctest # optional - mupad 

3 

sage: type(len(mupad([1,2,3]))) # optional - mupad 

<... 'int'> 

 

sage: len(mupad(4)) # optional - mupad 

1 

 

Implementing this is necessary for using MuPAD's lists as 

standard containers:: 

 

sage: list(map(ZZ, list(mupad([1,2,3])))) # optional - mupad 

[1, 2, 3] 

 

sage: [int(x) for x in mupad([1,2,3]) ] # optional - mupad 

[1, 2, 3] 

 

sage: [int(x) for x in mupad("{1,2,3,5}") ] # optional - mupad 

[1, 2, 3, 5] 

 

""" 

return mupad.nops(self) 

 

# An instance 

mupad = Mupad() 

 

def reduce_load_mupad(): 

""" 

EXAMPLES:: 

 

sage: from sage.interfaces.mupad import reduce_load_mupad 

sage: reduce_load_mupad() 

Mupad 

""" 

return mupad 

 

 

def mupad_console(): 

""" 

Spawn a new MuPAD command-line session. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.mupad import mupad_console 

sage: mupad_console() #not tested 

 

*----* MuPAD Pro 4.0.2 -- The Open Computer Algebra System 

/| /| 

*----* | Copyright (c) 1997 - 2007 by SciFace Software 

| *--|-* All rights reserved. 

|/ |/ 

*----* Licensed to: ... 

 

""" 

from sage.repl.rich_output.display_manager import get_display_manager 

if not get_display_manager().is_in_terminal(): 

raise RuntimeError('Can use the console only in the terminal. Try %%mupad magics instead.') 

os.system('mupkern') 

 

 

def __doctest_cleanup(): 

""" 

EXAMPLES:: 

 

sage: from sage.interfaces.mupad import __doctest_cleanup 

sage: m = mupad(2) # optional - mupad 

sage: mupad.is_running() # optional - mupad 

True 

sage: __doctest_cleanup() 

sage: mupad.is_running() # optional - mupad 

False 

""" 

import sage.interfaces.quit 

sage.interfaces.quit.expect_quitall()