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

# Copyright 2011-2013 Kwant authors. 

# 

# This file is part of Kwant. It is subject to the license terms in the file 

# LICENSE.rst found in the top-level directory of this distribution and at 

# http://kwant-project.org/license. A list of Kwant authors can be found in 

# the file AUTHORS.rst at the top-level directory of this distribution and at 

# http://kwant-project.org/authors. 

 

__all__ = ['schur', 'convert_r2c_schur', 'order_schur', 'evecs_from_schur', 

'gen_schur', 'order_gen_schur', 'convert_r2c_gen_schur', 

'evecs_from_gen_schur'] 

 

from math import sqrt 

import numpy as np 

from . import lapack 

 

 

def schur(a, calc_q=True, calc_ev=True, overwrite_a=False): 

"""Compute the Schur form of a square matrix a. 

 

The Schur form is a decomposition of the form a = q * t * q^dagger, where q 

is a unitary matrix and t a upper triagonal matrix when computing the Schur 

form of a complex matrix, and a quasi-upper triagonal matrix with only 1x1 

and 2x2 blocks on the diagonal when computing the Schur form of a real 

matrix (In the latter case, the 1x1 blocks correspond to real eigenvalues, 

the 2x2 blocks to conjugate pairs of complex eigenvalues). 

 

The Schur form is closely related to the eigenvalue problem (the entries of 

the diagonal of the complex Schur form are the eigenvalues of the matrix), 

and the routine can optionally also return the eigenvalues. 

 

Parameters 

---------- 

a : array, shape (M, M) 

Matrix for which to compute the Schur form. 

calc_q : boolean 

Whether to compute the unitary/orthogonal matrix `q`. 

calc_ev : boolean 

Whether to return the eigenvalues as a separate array. 

overwrite_a : boolean 

Whether to overwrite data in `a` (may increase performance). 

 

Returns 

------- 

t : array, shape (M, M) 

Schur form of the original matrix (complex or real, depending on the 

input matrix). 

 

(if calc_q == True) 

q : array, shape (M, M) 

Unitary transformation matrix. 

 

(if calc_ev == True) 

ev: array, shape (M,) 

Array of eigenvalues of the matrix `a`. Can be complex even if a is 

real. In the latter case, the complex eigenvalues come in conjugated 

pairs with the eigenvalue with positive imaginary part coming 

first. 

 

Raises 

------ 

LinAlgError 

If the underlying QR iteration fails to converge. 

""" 

a = lapack.prepare_for_lapack(overwrite_a, a) 

return lapack.gees(a, calc_q, calc_ev) 

 

 

def convert_r2c_schur(t, q): 

"""Convert a real Schur form (with possibly 2x2 blocks on the diagonal) 

into a complex Schur form that is completely triangular. 

 

This function is equivalent to the scipy.linalg.rsf2csf pendant (though the 

implementation is different), but there is additionally the guarantee that 

in the case of a 2x2 block at rows and columns i and i+1, t[i, i] will 

contain the eigenvalue with the positive part, and t[i+1, i+1] the one with 

the negative part. This ensures that the list of eigenvalues (more 

precisely, their order) returned originally from schur() is still valid for 

the newly formed complex Schur form. 

 

Parameters 

---------- 

t : array, shape (M, M) 

Real Schur form of the original matrix 

q : array, shape (M, M) 

Schur transformation matrix 

 

Returns 

------- 

t : array, shape (M, M) 

Complex Schur form of the original matrix 

q : array, shape (M, M) 

Schur transformation matrix corresponding to the complex form 

""" 

 

# First find the positions of 2x2-blocks 

blockpos = np.diagonal(t, -1).nonzero()[0] 

 

# Check if there are actually any 2x2-blocks 

if not blockpos.size: 

return (t, q) 

else: 

t2 = t.astype(np.common_type(t, np.array([], np.complex64))) 

q2 = q.astype(np.common_type(q, np.array([], np.complex64))) 

 

for i in blockpos: 

# Bringing a 2x2 block to complex triangular form is relatively simple: 

# the 2x2 blocks are guaranteed to be of the form [[a, b], [c, a]], 

# where b*c < 0. The eigenvalues of this matrix are a +/- i sqrt(-b*c), 

# the corresponding eigenvectors are [ +/- sqrt(-b*c), c]. The Schur 

# form can be achieved by a unitary 2x2 matrix with one of the 

# eigenvectors in the first column, and the second column an orthogonal 

# vector. 

 

a = t[i, i] 

b = t[i, i+1] 

c = t[i+1, i] 

 

x = 1j * sqrt(-b * c) 

y = c 

norm = sqrt(-b * c + c * c) 

 

U = np.array([[x / norm, -y / norm], [y / norm, -x / norm]]) 

 

t2[i, i] = a + x 

t2[i+1, i] = 0 

t2[i, i+1] = -b - c 

t2[i+1, i+1] = a - x 

 

t2[:i, i:i+2] = np.dot(t2[:i, i:i+2], U) 

t2[i:i+2, i+2:] = np.dot(np.conj(U.T), t2[i:i+2, i+2:]) 

 

q2[:, i:i+2] = np.dot(q2[:, i:i+2], U) 

 

return t2, q2 

 

 

def order_schur(select, t, q, calc_ev=True, overwrite_tq=False): 

"""Reorder the Schur form, selecting a cluster of eigenvalues. 

 

This function reorders the generalized Schur form such that the cluster of 

eigenvalues determined by select appears in the leading diagonal block of 

the Schur form (this is useful, as the Schur vectors corresponding to the 

leading diagonal block form an orthogonal basis for the subspace of 

eigenvectors). 

 

If a real Schur form is reordered, it is converted to complex form 

(eliminating the 2x2 blocks on the diagonal) if in a complex conjugated 

pair of eigenvalues only one eigenvalue is chosen. In this case, the real 

Schur from cannot be reordered in real form without splitting a 2x2 block 

on the diagonal, hence switching to complex form is mandatory. 

 

Parameters 

---------- 

t : array, shape (M, M) 

Schur form 

q : array, shape (M, M) 

Unitary/orthogonal transformation matrices. 

calc_ev : boolean, optional 

Whether to return the reordered generalized eigenvalues of as two 

separate arrays. Default: True 

overwrite_tq : boolean, optional 

Whether to overwrite data in `t` and `q` (may increase performance) 

Default: False 

 

Returns 

------- 

t : array, shape (M, M) 

Reordered Schur form. If the original Schur form is real, and the 

desired reordering separates complex conjugated pairs of generalized 

eigenvalues, the resulting Schur form will be complex. 

q : array, shape (M, M) 

Unitary/orthogonal transformation matrix. Only computed if q is 

provided (not None) as input. If the Schur form is converted from real 

to complex, the transformation matrix is also converted from real 

orthogonal to complex unitary 

alpha : array, shape (M) 

beta : array, shape (M) 

Reordered eigenvalues. If the reordered Schur form is real, complex 

conjugated pairs of eigenvalues are ordered such that the eigenvalue 

with the positive imaginary part comes first. Only computed if 

``calc_ev == True`` 

""" 

 

t, q = lapack.prepare_for_lapack(overwrite_tq, t, q) 

 

# Figure out if select is a function or array. 

isfun = isarray = True 

try: 

select(0) 

except: 

isfun = False 

try: 

select[0] 

except: 

isarray = False 

 

198 ↛ 199line 198 didn't jump to line 199, because the condition on line 198 was never true if not (isarray or isfun): 

raise ValueError("select must be either a function or an array") 

elif isarray: 

select = np.array(select, dtype=lapack.logical_dtype, order='F') 

else: 

select = np.array(np.vectorize(select)(np.arange(t.shape[0])), 

dtype=lapack.logical_dtype, order='F') 

 

# Now check if the reordering can actually be done as desired, 

# if we have a real Schur form (i.e. if the 2x2 blocks would be 

# separated). If this is the case, convert to complex Schur form first. 

for i in np.diagonal(t, -1).nonzero()[0]: 

if bool(select[i]) != bool(select[i+1]): 

t, q = convert_r2c_schur(t, q) 

return order_schur(select, t, q, calc_ev, True) 

 

return lapack.trsen(select, t, q, calc_ev) 

 

 

def evecs_from_schur(t, q, select=None, left=False, right=True, 

overwrite_tq=False): 

"""Compute eigenvectors from Schur form. 

 

This function computes either all or selected eigenvectors for the matrix 

that is represented by the Schur form t and the unitary matrix q, (not the 

eigenvectors of t, but of q*t*q^dagger). 

 

Parameters 

---------- 

t : array, shape (M, M) 

Schur form 

q : array, shape (M, M) 

Unitary/orthogonal transformation matrix. 

select : boolean function or array, optional 

The value of ``select(i)`` or ``select[i]`` is used to decide whether 

the eigenvector corresponding to the i-th eigenvalue should be 

computed or not. If select is not provided (None), all eigenvectors 

are computed. Default: None 

left : boolean, optional 

Whether to compute left eigenvectors. Default: False 

right : boolean, optional 

Whether to compute right eigenvectors. Default: True 

overwrite_tq : boolean, optional 

Whether to overwrite data in `t` and `q` (may increase performance) 

Default: False 

 

Returns 

------- 

vl : array, shape(M, N) 

Left eigenvectors. N is the number of eigenvectors selected b 

`select`, or equal to M if select is not provided. The eigenvectors 

may be complex, even if `t` and `q` are real. Only computed if 

``left == True``. 

vr : array, shape(M, N) 

Right eigenvectors. N is the number of eigenvectors selected by 

`select`, or equal to M if select is not provided. The eigenvectors 

may be complex, even if `t` and `q` are real. Only computed if 

``right == True``. 

""" 

 

t, q = lapack.prepare_for_lapack(overwrite_tq, t, q) 

 

# check if select is a function or an array 

if select is not None: 

isfun = isarray = True 

try: 

select(0) 

except: 

isfun = False 

 

try: 

select[0] 

except: 

isarray = False 

 

273 ↛ 274line 273 didn't jump to line 274, because the condition on line 273 was never true if not (isarray or isfun): 

raise ValueError("select must be either a function, " 

"an array or None") 

elif isarray: 

selectarr = np.array(select, dtype=lapack.logical_dtype, 

order='F') 

else: 

selectarr = np.array(np.vectorize(select)(np.arange(t.shape[0])), 

dtype=lapack.logical_dtype, order='F') 

else: 

selectarr = None 

 

return lapack.trevc(t, q, selectarr, left, right) 

 

 

def gen_schur(a, b, calc_q=True, calc_z=True, calc_ev=True, 

overwrite_ab=False): 

"""Compute the generalized Schur form of a matrix pencil (a, b). 

 

The generalized Schur form is a decomposition of the form a = q * s * 

z^dagger and b = q * t * z^dagger, where q and z are unitary matrices 

(orthogonal for real input), t is an upper triagonal matrix with 

non-negative real diagonal, and s is a upper triangular matrix for complex 

matrices, and a quasi-upper triangular matrix with only 1x1 and 2x2 blocks 

on the diagonal for real matrices. (In the latter case, the 1x1 blocks 

correspond to real generalized eigenvalues, the 2x2 blocks to conjugate 

pairs of complex generalized eigenvalues). 

 

The generalized Schur form is closely related to the generalized eigenvalue 

problem (the entries of the diagonal of the complex Schur form are the 

eigenvalues of the matrix, for example), and the routine can optionally 

also return the generalized eigenvalues in the form (alpha, beta), such 

that alpha/beta is a generalized eigenvalue of the pencil (a, b) (see also 

gen_eig()). 

 

Parameters 

---------- 

a : array, shape (M, M) 

b : array, shape (M, M) 

Matrix pencil for which to compute the generalized Schur form 

calc_q : boolean, optional 

calc_z : boolean, optional 

Whether to compute the unitary/orthogonal matrices `q` and `z`. 

Default: True 

calc_ev : boolean, optional 

Whether to return the generalized eigenvalues as two separate 

arrays. Default: True 

overwrite_ab : boolean, optional 

Whether to overwrite data in `a` and `b` (may increase performance) 

Default: False 

 

Returns 

------- 

s : array, shape (M, M) 

t : array, shape (M, M) 

Generalized Schur form of the original matrix pencil (`a`,`b`) 

(complex or real, depending on the input matrices) 

q : array, shape (M, M) 

z : array, shape (M, M) 

Unitary/orthogonal transformation matrices. Only computed if 

``calc_q == True`` or ``calc_z == True``, respectively. 

alpha : array, shape (M) 

beta : array, shape (M) 

Generalized eigenvalues of the matrix pencil (`a`, `b`) given 

as numerator (`alpha`) and denominator (`beta`), such that the 

generalized eigenvalues are given as ``alpha/beta``. alpha can 

be complex even if a is real. In the latter case, complex 

eigenvalues come in conjugated pairs with the eigenvalue with 

positive imaginary part coming first. Only computed if 

``calc_ev == True``. 

 

Raises 

------ 

LinAlError 

If the underlying QZ iteration fails to converge. 

""" 

a, b = lapack.prepare_for_lapack(overwrite_ab, a, b) 

return lapack.gges(a, b, calc_q, calc_z, calc_ev) 

 

 

def order_gen_schur(select, s, t, q=None, z=None, calc_ev=True, 

overwrite_stqz=False): 

"""Reorder the generalized Schur form. 

 

This function reorders the generalized Schur form such that the cluster of 

eigenvalues determined by select appears in the leading diagonal blocks of 

the Schur form (this is useful, as the Schur vectors corresponding to the 

leading diagonal blocks form an orthogonal basis for the subspace of 

eigenvectors). 

 

If a real generalized Schur form is reordered, it is converted to complex 

form (eliminating the 2x2 blocks on the diagonal) if in a complex 

conjugated pair of eigenvalues only one eigenvalue is chosen. In this 

case, the real Schur from cannot be reordered in real form without 

splitting a 2x2 block on the diagonal, hence switching to complex form is 

mandatory. 

 

Parameters 

---------- 

s : array, shape (M, M) 

t : array, shape (M, M) 

Matrices describing the generalized Schur form. 

q : array, shape (M, M), optional 

z : array, shape (M, M), optional 

Unitary/orthogonal transformation matrices. Default: None. 

calc_ev : boolean, optional 

Whether to return the reordered generalized eigenvalues of as two 

separate arrays. Default: True. 

overwrite_stqz : boolean, optional 

Whether to overwrite data in `s`, `t`, `q`, and `z` (may 

increase performance) Default: False. 

 

Returns 

------- 

s : array, shape (M, M) 

t : array, shape (M, M) 

Reordered general Schur form. If the original Schur form is real, and 

the desired reordering separates complex conjugated pairs of 

generalized eigenvalues, the resulting Schur form will be complex. 

q : array, shape (M, M) 

z : array, shape (M, M) 

Unitary/orthogonal transformation matrices. Only computed if 

`q` and `z` are provided (not None) on entry, respectively. If 

the generalized Schur form is converted from real to complex, 

the transformation matrices are also converted from real 

orthogonal to complex unitary 

alpha : array, shape (M) 

beta : array, shape (M) 

Reordered generalized eigenvalues. If the reordered Schur form is real, 

complex conjugated pairs of eigenvalues are ordered such that the 

eigenvalue with the positive imaginary part comes first. Only computed 

if ``calc_ev == True``. 

 

Raises 

------ 

LinAlError 

If the problem is too ill-conditioned. 

""" 

s, t, q, z = lapack.prepare_for_lapack(overwrite_stqz, s, t, q, z) 

 

 

# Figure out if select is a function or array. 

isfun = isarray = True 

try: 

select(0) 

except: 

isfun = False 

try: 

select[0] 

except: 

isarray = False 

 

425 ↛ 426line 425 didn't jump to line 426, because the condition on line 425 was never true if not (isarray or isfun): 

raise ValueError("select must be either a function or an array") 

elif isarray: 

select = np.array(select, dtype=lapack.logical_dtype, order='F') 

else: 

select = np.array(np.vectorize(select)(np.arange(t.shape[0])), 

dtype=lapack.logical_dtype, order='F') 

 

# Now check if the reordering can actually be done as desired, if we have a 

# real Schur form (i.e. if the 2x2 blocks would be separated). If this is 

# the case, convert to complex Schur form first. 

for i in np.diagonal(s, -1).nonzero()[0]: 

if bool(select[i]) != bool(select[i+1]): 

# Convert to complex Schur form 

439 ↛ 441line 439 didn't jump to line 441, because the condition on line 439 was never false if q is not None and z is not None: 

s, t, q, z = convert_r2c_gen_schur(s, t, q, z) 

elif q is not None: 

s, t, q = convert_r2c_gen_schur(s, t, q=q, z=None) 

elif z is not None: 

s, t, z = convert_r2c_gen_schur(s, t, q=None, z=z) 

else: 

s, t = convert_r2c_gen_schur(s, t) 

 

return order_gen_schur(select, s, t, q, z, calc_ev, True) 

 

return lapack.tgsen(select, s, t, q, z, calc_ev) 

 

 

def convert_r2c_gen_schur(s, t, q=None, z=None): 

"""Convert a real generallzed Schur form (with possibly 2x2 blocks on the 

diagonal) into a complex Schur form that is completely triangular. If the 

input is already completely triagonal (real or complex), the input is 

returned unchanged. 

 

This function guarantees that in the case of a 2x2 block at rows and 

columns i and i+1, the converted, complex Schur form will contain the 

generalized eigenvalue with the positive imaginary part in s[i,i] and 

t[i,i], and the one with the negative imaginary part in s[i+1,i+1] and 

t[i+1,i+1]. This ensures that the list of eigenvalues (more precisely, 

their order) returned originally from gen_schur() is still valid for the 

newly formed complex Schur form. 

 

Parameters 

---------- 

s : array, shape (M, M) 

t : array, shape (M, M) 

Real generalized Schur form of the original matrix 

q : array, shape (M, M), optional 

z : array, shape (M, M), optional 

Schur transformation matrix. Default: None 

 

Returns 

------- 

s : array, shape (M, M) 

t : array, shape (M, M) 

Complex generalized Schur form of the original matrix, 

completely triagonal 

q : array, shape (M, M) 

z : array, shape (M, M) 

Schur transformation matrices corresponding to the complex 

form. `q` or `z` are only computed if they are provided (not 

None) on input. 

 

Raises 

------ 

LinAlgError 

If it fails to convert a 2x2 block into complex form (unlikely). 

""" 

 

s, t, q, z = lapack.prepare_for_lapack(True, s, t, q, z) 

# Note: overwrite=True does not mean much here, the arrays are all copied 

 

497 ↛ 500line 497 didn't jump to line 500, because the condition on line 497 was never true if (s.ndim != 2 or t.ndim != 2 or 

(q is not None and q.ndim != 2) or 

(z is not None and z.ndim != 2)): 

raise ValueError("Expect matrices as input") 

 

502 ↛ 508line 502 didn't jump to line 508, because the condition on line 502 was never true if ((s.shape[0] != s.shape[1] or t.shape[0] != t.shape[1] or 

s.shape[0] != t.shape[0]) or 

(q is not None and (q.shape[0] != q.shape[1] or 

s.shape[0] != q.shape[0])) or 

(z is not None and (z.shape[0] != z.shape[1] or 

s.shape[0] != z.shape[0]))): 

raise ValueError("Invalid Schur decomposition as input") 

 

# First, find the positions of 2x2-blocks. 

blockpos = np.diagonal(s, -1).nonzero()[0] 

 

# Check if there are actually any 2x2-blocks. 

if not blockpos.size: 

s2 = s 

t2 = t 

q2 = q 

z2 = z 

else: 

s2 = s.astype(np.common_type(s, np.array([], np.complex64))) 

t2 = t.astype(np.common_type(t, np.array([], np.complex64))) 

522 ↛ 524line 522 didn't jump to line 524, because the condition on line 522 was never false if q is not None: 

q2 = q.astype(np.common_type(q, np.array([], np.complex64))) 

524 ↛ 527line 524 didn't jump to line 527, because the condition on line 524 was never false if z is not None: 

z2 = z.astype(np.common_type(z, np.array([], np.complex64))) 

 

for i in blockpos: 

# In the following, we use gen_schur on individual 2x2 blocks (that are 

# promoted to complex form) to compute the complex generalized Schur 

# form. If necessary, order_gen_schur is used to ensure the desired 

# order of eigenvalues. 

 

sb, tb, qb, zb, alphab, betab = gen_schur(s2[i:i+2, i:i+2], 

t2[i:i+2, i:i+2]) 

 

# Ensure order of eigenvalues. (betab is positive) 

537 ↛ 541line 537 didn't jump to line 541, because the condition on line 537 was never false if alphab[0].imag < alphab[1].imag: 

sb, tb, qb, zb, alphab, betab = order_gen_schur([False, True], 

sb, tb, qb, zb) 

 

s2[i:i+2, i:i+2] = sb 

t2[i:i+2, i:i+2] = tb 

 

s2[:i, i:i+2] = np.dot(s2[:i, i:i+2], zb) 

s2[i:i+2, i+2:] = np.dot(qb.T.conj(), s2[i:i+2, i+2:]) 

t2[:i, i:i+2] = np.dot(t2[:i, i:i+2], zb) 

t2[i:i+2, i+2:] = np.dot(qb.T.conj(), t2[i:i+2, i+2:]) 

 

549 ↛ 551line 549 didn't jump to line 551, because the condition on line 549 was never false if q is not None: 

q2[:, i:i+2] = np.dot(q[:, i:i+2], qb) 

551 ↛ 527line 551 didn't jump to line 527, because the condition on line 551 was never false if z is not None: 

z2[:, i:i+2] = np.dot(z[:, i:i+2], zb) 

 

554 ↛ 556line 554 didn't jump to line 556, because the condition on line 554 was never false if q is not None and z is not None: 

return s2, t2, q2, z2 

elif q is not None: 

return s2, t2, q2 

elif z is not None: 

return s2, t2, z2 

else: 

return s2, t2 

 

 

def evecs_from_gen_schur(s, t, q=None, z=None, select=None, 

left=False, right=True, overwrite_qz=False): 

"""Compute eigenvectors from Schur form. 

 

This function computes either all or selected eigenvectors for the matrix 

that is represented by the generalized Schur form (s, t) and the unitary 

matrices q and z, (not the generalized eigenvectors of (s,t), but of 

(q*s*z^dagger, q*t*z^dagger)). 

 

Parameters 

---------- 

s : array, shape (M, M) 

t : array, shape (M, M) 

Generalized Schur form. 

q : array, shape (M, M), optional 

z : array, shape (M, M), optional 

Unitary/orthogonal transformation matrices. If the left eigenvectors 

are to be computed, `q` must be provided, if the right eigenvectors are 

to be computed, `z` must be provided. 

select : boolean function or array, optional 

The value of ``select(i)`` or ``select[i]`` is used to decide 

whether the eigenvector corresponding to the i-th eigenvalue 

should be computed or not. If select is not provided, all 

eigenvectors are computed. Default: None. 

left : boolean, optional 

Whether to compute left eigenvectors. Default: False. 

right : boolean, optional 

Whether to compute right eigenvectors. Default: True. 

overwrite_qz : boolean, optional 

Whether to overwrite data in `q` and `z` (may increase performance). 

Note that s and t remain always unchanged Default: False. 

 

Returns 

------- 

(if left == True) 

vl : array, shape(M, N) 

Left generalized eigenvectors. N is the number of eigenvectors 

selected by select, or equal to M if select is not 

provided. The eigenvectors may be complex, even if `s`, `t`, 

`q` and `z` are real. 

 

(if right == True) 

vr : array, shape(M, N) 

Right generalized eigenvectors. N is the number of 

eigenvectors selected by select, or equal to M if select is 

not provided. The eigenvectors may be complex, even if `s`, 

`t`, `q` and `z` are real. 

 

""" 

 

s, t, q, z = lapack.prepare_for_lapack(overwrite_qz, s, t, q, z) 

 

616 ↛ 617line 616 didn't jump to line 617, because the condition on line 616 was never true if left and q is None: 

raise ValueError("Matrix q must be provided for left eigenvectors") 

 

619 ↛ 620line 619 didn't jump to line 620, because the condition on line 619 was never true if right and z is None: 

raise ValueError("Matrix z must be provided for right eigenvectors") 

 

# Check if select is a function or an array. 

if select is not None: 

isfun = isarray = True 

try: 

select(0) 

except: 

isfun = False 

 

try: 

select[0] 

except: 

isarray = False 

 

635 ↛ 636line 635 didn't jump to line 636, because the condition on line 635 was never true if not (isarray or isfun): 

raise ValueError("select must be either a function, " 

"an array or None") 

elif isarray: 

selectarr = np.array(select, dtype=lapack.logical_dtype, 

order='F') 

else: 

selectarr = np.array(np.vectorize(select)(np.arange(t.shape[0])), 

dtype=lapack.logical_dtype, order='F') 

else: 

selectarr = None 

 

return lapack.tgevc(s, t, q, z, selectarr, left, right)