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

r""" 

Interface to Scilab 

 

Scilab is a scientific software package for numerical computations 

providing a powerful open computing environment for engineering and 

scientific applications. Scilab includes hundreds of mathematical 

functions with the possibility to add interactively programs from 

various languages (C, C++, Fortran...). It has sophisticated data 

structures (including lists, polynomials, rational functions, linear 

systems...), an interpreter and a high level programming language. 

 

The commands in this section only work if you have the "scilab" 

interpreter installed and available in your PATH. It's not necessary 

to install any special Sage packages. 

 

EXAMPLES:: 

 

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

'ans =\n \n 4.' 

sage: scilab('2+2') # optional - scilab 

4. 

sage: a = scilab(10) # optional - scilab 

sage: a**10 # optional - scilab 

1.000D+10 

 

Tutorial based the MATLAB interface tutorial: 

 

EXAMPLES:: 

 

sage: scilab('4+10') # optional - scilab 

14. 

sage: scilab('date') # optional - scilab; random output 

15-Feb-2010 

sage: scilab('5*10 + 6') # optional - scilab 

56. 

sage: scilab('(6+6)/3') # optional - scilab 

4. 

sage: scilab('9')^2 # optional - scilab 

81. 

sage: a = scilab(10); b = scilab(20); c = scilab(30) # optional - scilab 

sage: avg = (a+b+c)/3 # optional - scilab 

sage: avg # optional - scilab 

20. 

sage: parent(avg) # optional - scilab 

Scilab 

 

sage: my_scalar = scilab('3.1415') # optional - scilab 

sage: my_scalar # optional - scilab 

3.1415 

sage: my_vector1 = scilab('[1,5,7]') # optional - scilab 

sage: my_vector1 # optional - scilab 

1. 5. 7. 

sage: my_vector2 = scilab('[1;5;7]') # optional - scilab 

sage: my_vector2 # optional - scilab 

1. 

5. 

7. 

sage: my_vector1 * my_vector2 # optional - scilab 

75. 

 

sage: row_vector1 = scilab('[1 2 3]') # optional - scilab 

sage: row_vector2 = scilab('[3 2 1]') # optional - scilab 

sage: matrix_from_row_vec = scilab('[%s; %s]'%(row_vector1.name(), row_vector2.name())) # optional - scilab 

sage: matrix_from_row_vec # optional - scilab 

1. 2. 3. 

3. 2. 1. 

 

sage: column_vector1 = scilab('[1;3]') # optional - scilab 

sage: column_vector2 = scilab('[2;8]') # optional - scilab 

sage: matrix_from_col_vec = scilab('[%s %s]'%(column_vector1.name(), column_vector2.name())) # optional - scilab 

sage: matrix_from_col_vec # optional - scilab 

1. 2. 

3. 8. 

 

sage: my_matrix = scilab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]') # optional - scilab 

sage: my_matrix # optional - scilab 

8. 12. 19. 

7. 3. 2. 

12. 4. 23. 

8. 1. 1. 

 

sage: combined_matrix = scilab('[%s, %s]'%(my_matrix.name(), my_matrix.name())) # optional - scilab 

sage: combined_matrix # optional - scilab 

8. 12. 19. 8. 12. 19. 

7. 3. 2. 7. 3. 2. 

12. 4. 23. 12. 4. 23. 

8. 1. 1. 8. 1. 1. 

 

sage: tm = scilab('0.5:2:10') # optional - scilab 

sage: tm # optional - scilab 

0.5 2.5 4.5 6.5 8.5 

 

sage: my_vector1 = scilab('[1,5,7]') # optional - scilab 

sage: my_vector1(1) # optional - scilab 

1. 

sage: my_vector1(2) # optional - scilab 

5. 

sage: my_vector1(3) # optional - scilab 

7. 

 

Matrix indexing works as follows:: 

 

sage: my_matrix = scilab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]') # optional - scilab 

sage: my_matrix(3,2) # optional - scilab 

4. 

 

One can also use square brackets:: 

 

sage: my_matrix[3,2] # optional - scilab 

4. 

 

 

Setting using parenthesis cannot work (because of how the Python 

language works). Use square brackets or the set function:: 

 

sage: my_matrix = scilab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]') # optional - scilab 

sage: my_matrix.set(2,3, 1999) # optional - scilab 

sage: my_matrix # optional - scilab 

8. 12. 19. 

7. 3. 1999. 

12. 4. 23. 

8. 1. 1. 

sage: my_matrix[2,3] = -126 # optional - scilab 

sage: my_matrix # optional - scilab 

8. 12. 19. 

7. 3. - 126. 

12. 4. 23. 

8. 1. 1. 

 

TESTS:: 

 

sage: M = scilab(x) # optional - scilab 

Traceback (most recent call last): 

... 

TypeError: _interface_init_() takes exactly one argument (0 given) 

sage: M = scilab(matrix(3,range(9))); M # optional - scilab 

0. 1. 2. 

3. 4. 5. 

6. 7. 8. 

sage: M(10) # optional - scilab 

Traceback (most recent call last): 

... 

TypeError: Error executing code in Scilab 

... 

Invalid index. 

sage: M[10] # optional - scilab 

Traceback (most recent call last): 

... 

TypeError: Error executing code in Scilab 

... 

Invalid index. 

sage: M(4,2) # optional - scilab 

Traceback (most recent call last): 

... 

TypeError: Error executing code in Scilab 

... 

Invalid index. 

sage: M[2,4] # optional - scilab 

Traceback (most recent call last): 

... 

TypeError: Error executing code in Scilab 

... 

Invalid index. 

sage: M(9) = x # optional - scilab 

Traceback (most recent call last): 

... 

SyntaxError: can't assign to function call (..., line 1) 

 

AUTHORS: 

 

-- Ronan Paixao (2008-11-26), based on the MATLAB tutorial by 

William Stein (2006-10-11) 

""" 

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

# Copyright (C) 2006 William Stein <wstein@gmail.com> 

# Copyright (C) 2008 Ronan Paixao <ronanpaixao@yahoo.com.br> 

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import print_function 

from __future__ import absolute_import 

 

import os 

 

from .expect import Expect, ExpectElement 

from sage.docs.instancedoc import instancedoc 

 

 

class Scilab(Expect): 

""" 

Interface to the Scilab interpreter. 

 

EXAMPLES:: 

 

sage: a = scilab('[ 1, 1, 2; 3, 5, 8; 13, 21, 33 ]') # optional - scilab 

sage: b = scilab('[ 1; 3; 13]') # optional - scilab 

sage: c = a * b # optional - scilab 

sage: print(c) # optional - scilab 

30. 

122. 

505. 

""" 

def __init__(self, maxread=None, script_subdirectory=None, 

logfile=None, server=None,server_tmpdir=None, 

seed=None): 

""" 

Initializes the Scilab class. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.scilab import Scilab 

sage: sci_obj = Scilab() 

sage: del sci_obj 

""" 

Expect.__init__(self, 

name = 'scilab', 

prompt = '-->', 

command = "scilab -nw", 

server = server, 

server_tmpdir = server_tmpdir, 

script_subdirectory = script_subdirectory, 

restart_on_ctrlc = False, 

verbose_start = False, 

logfile = logfile, 

eval_using_file_cutoff=100) 

self._seed = seed 

 

def set_seed(self, seed=None): 

""" 

Set the seed for gp interpreter. 

 

The seed should be an integer. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.scilab import Scilab # optional - scilab 

sage: s = Scilab() # optional - scilab 

sage: s.set_seed(1) # optional - scilab 

1 

sage: [s.rand() for i in range(5)] # optional - scilab 

[ 

<BLANKLINE> 

0.6040239, 

<BLANKLINE> 

0.0079647, 

<BLANKLINE> 

0.6643966, 

<BLANKLINE> 

0.9832111, 

<BLANKLINE> 

0.5321420] 

""" 

if seed is None: 

seed = self.rand_seed() 

self.eval("rand('seed',%d)" % seed) 

self._seed = seed 

return seed 

 

def _quit_string(self): 

""" 

Returns the string used to quit the pexpect interface. 

 

EXAMPLES:: 

 

sage: scilab._quit_string() # optional - scilab 

'quit;' 

""" 

return 'quit;' 

 

def _install_hints(self): 

""" 

Hints for installing Scilab. 

 

EXAMPLES:: 

 

sage: print(scilab._install_hints()) # optional - scilab 

You must ... 

""" 

return """ 

You must obtain the Scilab program in order to use Scilab 

from Sage. You can read all about Scilab at 

http://www.scilab.org/ 

The executable must be accessible system-wide. 

""" 

 

def _start(self): 

""" 

Starts Scilab and sets some options. 

 

EXAMPLES:: 

 

sage: scilab._start() # optional - scilab 

""" 

Expect._start(self) 

self.eval("mode(0)") 

 

# set random seed 

self.set_seed(self._seed) 

 

def eval(self, command, *args, **kwds): 

""" 

Evaluates commands. 

 

EXAMPLES:: 

 

sage: scilab.eval("5") # optional - scilab 

'ans =\n \n 5.' 

sage: scilab.eval("d=44") # optional - scilab 

'd =\n \n 44.' 

""" 

s = Expect.eval(self, command, **kwds).replace("\x1b[?1l\x1b>","").strip() 

return s 

 

def whos(self, name=None, typ=None): 

""" 

Returns information about current objects. 

Arguments: 

nam: first characters of selected names 

typ: name of selected Scilab variable type 

 

EXAMPLES:: 

 

sage: scilab.whos("core") # optional - scilab 

'Name Type Size Bytes...' 

sage: scilab.whos(typ='function') # optional - scilab 

'Name Type Size Bytes...' 

""" 

parameters = "" 

if name: 

parameters += " -name %s" % (str(name)) 

if typ: 

parameters += " -type %s" % (str(typ)) 

return self.eval('whos' + parameters) 

 

def set(self, var, value): 

""" 

Set the variable var to the given value. 

 

EXAMPLES:: 

 

sage: scilab.set('a', 123) # optional - scilab 

sage: scilab.get('a') # optional - scilab 

'\n \n 123.' 

""" 

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

out = self.eval(cmd) 

if out.find("error") != -1: 

raise TypeError("Error executing code in Scilab\nCODE:\n\t%s\nScilab ERROR:\n\t%s"%(cmd, out)) 

 

def get(self, var): 

""" 

Get the value of the variable var. 

 

EXAMPLES:: 

 

sage: scilab.eval('b=124;') # optional - scilab 

'' 

sage: scilab.get('b') # optional - scilab 

'\n \n 124.' 

""" 

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

i = s.find('=') 

return s[i+1:] 

 

def console(self): 

""" 

Starts Scilab console. 

 

EXAMPLES:: 

 

sage: scilab.console() # optional - scilab; not tested 

 

""" 

scilab_console() 

 

def version(self): 

""" 

Returns the version of the Scilab software used. 

 

EXAMPLES:: 

 

sage: scilab.version() # optional - scilab 

'scilab-...' 

""" 

return scilab_version() 

 

def sage2scilab_matrix_string(self, A): 

""" 

Return a Scilab matrix from a Sage matrix. 

 

INPUT: 

A Sage matrix with entries in the rationals or reals. 

 

OUTPUT: 

A string that evaluates to an Scilab matrix. 

 

EXAMPLES:: 

 

sage: M33 = MatrixSpace(QQ,3,3) # optional - scilab 

sage: A = M33([1,2,3,4,5,6,7,8,0]) # optional - scilab 

sage: scilab.sage2scilab_matrix_string(A) # optional - scilab 

'[1, 2, 3; 4, 5, 6; 7, 8, 0]' 

 

""" 

return str(A.rows()).replace('), (', '; ').replace('(', '').replace(')','') 

 

def _object_class(self): 

""" 

Returns the class of the object. 

 

EXAMPLES:: 

 

sage: scilab._object_class() # optional - scilab 

<class 'sage.interfaces.scilab.ScilabElement'> 

sage: type(scilab(2)) # optional - scilab 

<class 'sage.interfaces.scilab.ScilabElement'> 

""" 

return ScilabElement 

 

 

@instancedoc 

class ScilabElement(ExpectElement): 

def __getitem__(self, n): 

""" 

Use parenthesis for Scilab matrices instead. 

 

EXAMPLES:: 

 

sage: M = scilab('[1,2,3;4,5,6;7,8,9]') # optional - scilab 

sage: M[1] # optional - scilab 

1. 

sage: M[7] # optional - scilab 

3. 

sage: M[3,2] # optional - scilab 

8. 

""" 

if isinstance(n, tuple): 

index = str(n)[1:-1] 

else: 

index = str(n) 

return self.parent()('%s(%s)' % (self._name, index)) 

 

def __setitem__(self, n, value): 

""" 

Sets an element of a matrix. 

 

EXAMPLES:: 

 

sage: M = scilab('[1,2,3;4,5,6;7,8,9]') # optional - scilab 

sage: M[6] = 0 # optional - scilab 

sage: M # optional - scilab 

1. 2. 3. 

4. 5. 6. 

7. 0. 9. 

sage: M[3,2] = 10 # optional - scilab 

sage: M # optional - scilab 

1. 2. 3. 

4. 5. 6. 

7. 10. 9. 

""" 

if isinstance(n, tuple): 

index = str(n)[1:-1] 

else: 

index = str(n) 

self.parent().eval('%s(%s) = %s' % (self._name, index, value)) 

 

def _matrix_(self, R): 

r""" 

Return \sage matrix from this scilab element. 

 

EXAMPLES:: 

 

sage: A = scilab('[1,2;3,4]') # optional - scilab 

sage: matrix(ZZ, A) # optional - scilab 

[1 2] 

[3 4] 

sage: A = scilab('[1,2;3,4.5]') # optional - scilab 

sage: matrix(RR, A) # optional - scilab 

[1.00000000000000 2.00000000000000] 

[3.00000000000000 4.50000000000000] 

""" 

from sage.matrix.all import MatrixSpace 

s = str(self).strip() 

v = s.split('\n ') 

nrows = len(v) 

if nrows == 0: 

return MatrixSpace(R, 0, 0)(0) 

ncols = len(v[0].split()) 

M = MatrixSpace(R, nrows, ncols) 

v = sum([[x.rstrip('.') for x in w.split()] for w in v], []) 

return M(v) 

 

def set(self, i, j, x): 

""" 

Set the variable var to the given value. 

 

EXAMPLES:: 

 

sage: scilab.set('c', 125) # optional - scilab 

sage: scilab.get('c') # optional - scilab 

'\n \n 125.' 

""" 

P = self._check_valid() 

z = P(x) 

P.eval('%s(%s,%s) = %s'%(self.name(), i, j, z.name())) 

 

# An instance 

scilab = Scilab() 

 

 

def scilab_console(): 

""" 

This requires that the optional Scilab program be installed and in 

your PATH, but no optional Sage packages need to be installed. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.scilab import scilab_console # optional - scilab 

sage: scilab_console() # optional - scilab; not tested 

___________________________________________ 

scilab-5.0.3 

 

Consortium Scilab (DIGITEO) 

Copyright (c) 1989-2008 (INRIA) 

Copyright (c) 1989-2007 (ENPC) 

___________________________________________ 

 

 

Startup execution: 

loading initial environment 

 

-->2+3 

ans = 

 

5. 

 

-->quit 

 

Typing quit exits the Scilab console and returns you to Sage. 

Scilab, like Sage, remembers its history from one session to 

another. 

""" 

os.system('scilab -nw') 

 

 

def scilab_version(): 

""" 

Return the version of Scilab installed. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.scilab import scilab_version # optional - scilab 

sage: scilab_version() # optional - scilab 

'scilab-...' 

""" 

return str(scilab('getversion()')).strip()