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

r""" 

Interface to 4ti2 

 

http://www.4ti2.de 

 

You must have the 4ti2 Sage package installed on your computer 

for this interface to work. 

 

Use ``sage -i 4ti2`` to install the package. 

 

AUTHORS: 

 

- Mike Hansen (2009): Initial version. 

 

- Bjarke Hammersholt Roune (2009-06-26): Added Groebner, made code 

usable as part of the Sage library and added documentation and some 

doctests. 

 

- Marshall Hampton (2011): Minor fixes to documentation. 

""" 

 

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

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

# Copyright (C) 2009 Bjarke Hammersholt Roune <www.broune.com> 

# 

# 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 six import iteritems 

 

from sage.rings.integer_ring import ZZ 

import os 

 

 

class FourTi2(object): 

r""" 

This object defines an interface to the program 4ti2. Each command 

4ti2 has is exposed as one method. 

""" 

def __init__(self, directory=None): 

r""" 

Initialize this object. 

 

INPUT: 

 

- ``directory`` -- 4ti2 only deals with files, and this is the 

directory that Sage will write input files to and run 4ti2 

in. Use an appropriate temporary directory if the value is 

``None``. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import FourTi2 

sage: f = FourTi2("/tmp/") 

sage: f.directory() 

'/tmp/' 

""" 

self._directory = directory 

 

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

# Input / output # 

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

 

def directory(self): 

r""" 

Return the directory where the input files for 4ti2 are 

written by Sage and where 4ti2 is run. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import FourTi2 

sage: f = FourTi2("/tmp/") 

sage: f.directory() 

'/tmp/' 

""" 

from sage.misc.temporary_file import tmp_dir 

if self._directory is None: 

# we have to put this here rather than in the __init__ 

# method since apparently importing sage.misc.misc does not 

# work until Sage is done starting up. 

self._directory = tmp_dir() 

return self._directory 

 

def temp_project(self): 

r""" 

Return an input project file name that has not been used yet. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.temp_project() 

'project_...' 

""" 

n = 0 

while True: 

project = "project_%s" % n 

touch_file = os.path.join(self.directory(),project) + '.touch' 

if not os.path.exists(touch_file): 

break 

n += 1 

f = open(touch_file, 'w') 

f.write(' ') 

f.close() 

return project 

 

def write_matrix(self, mat, filename): 

r""" 

Write the matrix ``mat`` to the file ``filename`` in 4ti2 format. 

 

INPUT: 

 

- ``mat`` -- A matrix of integers or something that can be 

converted to that. 

- ``filename`` -- A file name not including a path. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.write_matrix([[1,2],[3,4]], "test_file") 

""" 

from sage.matrix.constructor import matrix 

from sage.structure.element import is_Matrix 

if not is_Matrix(mat): 

mat = matrix(ZZ, mat) 

if mat.base_ring() != ZZ: 

mat = mat.change_ring(ZZ) 

 

self.write_array(mat, mat.nrows(), mat.ncols(), filename) 

 

def write_single_row(self, row, filename): 

r""" 

Write the list ``row`` to the file ``filename`` in 4ti2 format 

as a matrix with one row. 

 

INPUT: 

 

- ``row`` -- A list of integers. 

- ``filename`` -- A file name not including a path. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.write_single_row([1,2,3,4], "test_file") 

""" 

self.write_array([row], 1, len(row), filename) 

 

def write_array(self, array, nrows, ncols, filename): 

r""" 

Write the matrix ``array`` of integers (can be represented as 

a list of lists) to the file ``filename`` in directory 

``directory()`` in 4ti2 format. The matrix must have ``nrows`` 

rows and ``ncols`` columns. 

 

INPUT: 

 

- ``array`` -- A matrix of integers. Can be represented as a list 

of lists. 

- ``nrows`` -- The number of rows in ``array``. 

- ``ncols`` -- The number of columns in ``array``. 

- ``file`` -- A file name not including a path. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.write_array([[1,2,3],[3,4,5]], 2, 3, "test_file") 

""" 

f = open(os.path.join(self.directory(), filename), 'w') 

f.write("%s %s\n"%(nrows, ncols)) 

for row in array: 

f.write(" ".join(map(str, row))) 

f.write("\n") 

f.close() 

 

def read_matrix(self, filename): 

r""" 

Read a matrix in 4ti2 format from the file ``filename`` in 

directory ``directory()``. 

 

INPUT: 

 

- ``filename`` -- The name of the file to read from. 

 

OUTPUT: 

 

The data from the file as a matrix over `\ZZ`. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.write_matrix([[1,2,3],[3,4,6]], "test_file") 

sage: four_ti_2.read_matrix("test_file") 

[1 2 3] 

[3 4 6] 

""" 

from sage.matrix.constructor import matrix 

try: 

f = open(os.path.join(self.directory(), filename)) 

lines = f.readlines() 

f.close() 

except IOError: 

return matrix(ZZ, 0, 0) 

 

nrows, ncols = map(ZZ, lines.pop(0).strip().split()) 

return matrix(ZZ, nrows, ncols, 

[[ZZ(_) for _ in line.strip().split()] for line in lines 

if line.strip() != ""]) 

 

def _process_input(self, kwds): 

r""" 

kwds is a dict, and the values are written to files with 

extension given by the keys, except for the keys ``self`` 

and ``project``. 

 

This interesting method is intended to be called as the first 

thing going on in a method implementing some action of 4ti2, 

where the value of ``locals()`` is passed as the dict, thus 

achieving to write out many project files to the right places 

just by giving the parameters of the method names that are the 

extension of the corresponding files. 

 

Nothing is written if the value is None. Otherwise the value 

is written as a matrix to the file given by the value of the 

key ``'project'`` with extension given by the key. 

 

INPUT: 

 

- kwds -- A dict controlling what data is written to what files. 

 

OUTPUT: 

 

The value of the key ``project``. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: pr = four_ti_2._process_input( \ 

....: {'project': "test_file", \ 

....: 'self': None, \ 

....: 'tst': [[1,2,3],[3,4,5]]}) 

sage: four_ti_2.read_matrix("test_file.tst") 

[1 2 3] 

[3 4 5] 

""" 

kwds.pop('self', None) 

 

# Get the project 

project = kwds.pop('project', None) 

if project is None: 

project = self.temp_project() 

 

for ext, value in iteritems(kwds): 

if value is None: 

continue 

 

if (isinstance(value, list) and 

not (len(value) and isinstance(value[0], list))): 

self.write_single_row(value, project + "." + ext) 

else: 

self.write_matrix(value, project + "." + ext) 

 

return project 

 

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

# Commands # 

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

 

def call(self, command, project, verbose=True): 

r""" 

Run the 4ti2 program ``command`` on the project named 

``project`` in the directory ``directory()``. 

 

INPUT: 

 

- command -- The 4ti2 program to run. 

- project -- The file name of the project to run on. 

- verbose -- Display the output of 4ti2 if ``True``. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.write_matrix([[6,10,15]], "test_file") 

sage: four_ti_2.call("groebner", "test_file", False) # optional - 4ti2 

sage: four_ti_2.read_matrix("test_file.gro") # optional - 4ti2 

[-5 0 2] 

[-5 3 0] 

""" 

import subprocess 

 

cmd = '%s %s' % (command, project) 

if verbose is False: 

cmd += " > /dev/null 2> /dev/null" 

subprocess.call(cmd, shell=True, cwd=self.directory()) 

 

def zsolve(self, mat=None, rel=None, rhs=None, sign=None, lat=None, project=None): 

r""" 

Run the 4ti2 program ``zsolve`` on the parameters. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: A = [[1,1,1],[1,2,3]] 

sage: rel = ['<', '<'] 

sage: rhs = [2, 3] 

sage: sign = [1,0,1] 

sage: four_ti_2.zsolve(A, rel, rhs, sign) # optional - 4ti2 

[ 

[ 1 -1 0] 

[ 0 -1 0] 

[0 0 1] [ 0 -3 2] 

[1 1 0] [ 1 -2 1] 

[0 1 0], [ 0 -2 1], [] 

] 

sage: four_ti_2.zsolve(lat=[[1,2,3],[1,1,1]]) # optional - 4ti2 

[ 

[1 2 3] 

[0 0 0], [], [1 1 1] 

] 

 

""" 

project = self._process_input(locals()) 

self.call('zsolve -q', project) 

return [self.read_matrix(project+'.'+ext) for ext in 

['zinhom', 'zhom', 'zfree']] 

 

def qsolve(self, mat=None, rel=None, sign=None, project=None): 

r""" 

Run the 4ti2 program ``qsolve`` on the parameters. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: A = [[1,1,1],[1,2,3]] 

sage: four_ti_2.qsolve(A) # optional - 4ti2 

[[], [ 1 -2 1]] 

""" 

project = self._process_input(locals()) 

self.call('qsolve -q -parbitrary', project) 

return [self.read_matrix(project+'.'+ext) for ext in 

['qhom', 'qfree']] 

 

def rays(self, mat=None, project=None): 

r""" 

Run the 4ti2 program ``rays`` on the parameters. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.rays(four_ti_2._magic3x3()) # optional - 4ti2 

[0 2 1 2 1 0 1 0 2] 

[1 0 2 2 1 0 0 2 1] 

[1 2 0 0 1 2 2 0 1] 

[2 0 1 0 1 2 1 2 0] 

""" 

project = self._process_input(locals()) 

self.call('rays -q -parbitrary', project) 

return self.read_matrix(project+'.ray') 

 

def hilbert(self, mat=None, lat=None, project=None): 

r""" 

Run the 4ti2 program ``hilbert`` on the parameters. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.hilbert(four_ti_2._magic3x3()) # optional - 4ti2 

[2 0 1 0 1 2 1 2 0] 

[1 0 2 2 1 0 0 2 1] 

[0 2 1 2 1 0 1 0 2] 

[1 2 0 0 1 2 2 0 1] 

[1 1 1 1 1 1 1 1 1] 

sage: four_ti_2.hilbert(lat=[[1,2,3],[1,1,1]]) # optional - 4ti2 

[2 1 0] 

[0 1 2] 

[1 1 1] 

""" 

project = self._process_input(locals()) 

self.call('hilbert -q', project) 

return self.read_matrix(project+'.hil') 

 

def graver(self, mat=None, lat=None, project=None): 

r""" 

Run the 4ti2 program ``graver`` on the parameters. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.graver([1,2,3]) # optional - 4ti2 

[ 2 -1 0] 

[ 3 0 -1] 

[ 1 1 -1] 

[ 1 -2 1] 

[ 0 3 -2] 

sage: four_ti_2.graver(lat=[[1,2,3],[1,1,1]]) # optional - 4ti2 

[ 1 0 -1] 

[ 0 1 2] 

[ 1 1 1] 

[ 2 1 0] 

""" 

project = self._process_input(locals()) 

self.call('graver -q', project) 

return self.read_matrix(project+'.gra') 

 

def ppi(self, n): 

r""" 

Run the 4ti2 program ``ppi`` on the parameters. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.ppi(3) # optional - 4ti2 

[-2 1 0] 

[ 0 -3 2] 

[-1 -1 1] 

[-3 0 1] 

[ 1 -2 1] 

 

""" 

self.call('ppi 2> /dev/null', n) 

return self.read_matrix('ppi%s.gra'%n) 

 

def circuits(self, mat=None, project=None): 

r""" 

Run the 4ti2 program ``circuits`` on the parameters. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.circuits([1,2,3]) # optional - 4ti2 

[ 0 3 -2] 

[ 2 -1 0] 

[ 3 0 -1] 

""" 

project = self._process_input(locals()) 

self.call('circuits -q -parbitrary', project) 

return self.read_matrix(project+'.cir') 

 

def minimize(self, mat=None, lat=None): 

r""" 

Run the 4ti2 program ``minimize`` on the parameters. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2.minimize() # optional - 4ti2 

Traceback (most recent call last): 

... 

NotImplementedError: 4ti2 command 'minimize' not implemented in Sage. 

""" 

raise NotImplementedError("4ti2 command 'minimize' not implemented " 

"in Sage.") 

 

def groebner(self, mat=None, lat=None, project=None): 

r""" 

Run the 4ti2 program ``groebner`` on the parameters. This 

computes a Toric Groebner basis of a matrix. See 

``http://www.4ti2.de/`` for details. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: A = [6,10,15] 

sage: four_ti_2.groebner(A) # optional - 4ti2 

[-5 0 2] 

[-5 3 0] 

sage: four_ti_2.groebner(lat=[[1,2,3],[1,1,1]]) # optional - 4ti2 

[-1 0 1] 

[ 2 1 0] 

""" 

project = self._process_input(locals()) 

self.call('groebner -q -parbitrary', project) 

return self.read_matrix(project+'.gro') 

 

def _magic3x3(self): 

r""" 

Return a matrix used for testing this class. 

 

EXAMPLES:: 

 

sage: from sage.interfaces.four_ti_2 import four_ti_2 

sage: four_ti_2._magic3x3() # optional - 4ti2 

[ 1 1 1 -1 -1 -1 0 0 0] 

[ 1 1 1 0 0 0 -1 -1 -1] 

[ 0 1 1 -1 0 0 -1 0 0] 

[ 1 0 1 0 -1 0 0 -1 0] 

[ 1 1 0 0 0 -1 0 0 -1] 

[ 0 1 1 0 -1 0 0 0 -1] 

[ 1 1 0 0 -1 0 -1 0 0] 

 

""" 

from sage.matrix.constructor import matrix 

return matrix \ 

(ZZ, 7, 9, 

[[1, 1, 1, -1, -1, -1, 0, 0, 0], 

[1, 1, 1, 0, 0, 0, -1, -1, -1], 

[0, 1, 1, -1, 0, 0, -1, 0, 0], 

[1, 0, 1, 0, -1, 0, 0, -1, 0], 

[1, 1, 0, 0, 0, -1, 0, 0, -1], 

[0, 1, 1, 0, -1, 0, 0, 0, -1], 

[1, 1, 0, 0, -1, 0, -1, 0, 0]]) 

 

# The instance that should be used outside this file. 

four_ti_2 = FourTi2()