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

700

"""Abstract linear algebra library. 

 

This module defines a class hierarchy that implements a kind of "lazy" 

matrix representation, called the ``LinearOperator``. It can be used to do 

linear algebra with extremely large sparse or structured matrices, without 

representing those explicitly in memory. Such matrices can be added, 

multiplied, transposed, etc. 

 

As a motivating example, suppose you want have a matrix where almost all of 

the elements have the value one. The standard sparse matrix representation 

skips the storage of zeros, but not ones. By contrast, a LinearOperator is 

able to represent such matrices efficiently. First, we need a compact way to 

represent an all-ones matrix:: 

 

>>> import numpy as np 

>>> class Ones(LinearOperator): 

... def __init__(self, shape): 

... super(Ones, self).__init__(dtype=None, shape=shape) 

... def _matvec(self, x): 

... return np.repeat(x.sum(), self.shape[0]) 

 

Instances of this class emulate ``np.ones(shape)``, but using a constant 

amount of storage, independent of ``shape``. The ``_matvec`` method specifies 

how this linear operator multiplies with (operates on) a vector. We can now 

add this operator to a sparse matrix that stores only offsets from one:: 

 

>>> from scipy.sparse import csr_matrix 

>>> offsets = csr_matrix([[1, 0, 2], [0, -1, 0], [0, 0, 3]]) 

>>> A = aslinearoperator(offsets) + Ones(offsets.shape) 

>>> A.dot([1, 2, 3]) 

array([13, 4, 15]) 

 

The result is the same as that given by its dense, explicitly-stored 

counterpart:: 

 

>>> (np.ones(A.shape, A.dtype) + offsets.toarray()).dot([1, 2, 3]) 

array([13, 4, 15]) 

 

Several algorithms in the ``scipy.sparse`` library are able to operate on 

``LinearOperator`` instances. 

""" 

 

from __future__ import division, print_function, absolute_import 

 

import numpy as np 

 

from scipy.sparse import isspmatrix 

from scipy.sparse.sputils import isshape, isintlike, asmatrix 

 

__all__ = ['LinearOperator', 'aslinearoperator'] 

 

 

class LinearOperator(object): 

"""Common interface for performing matrix vector products 

 

Many iterative methods (e.g. cg, gmres) do not need to know the 

individual entries of a matrix to solve a linear system A*x=b. 

Such solvers only require the computation of matrix vector 

products, A*v where v is a dense vector. This class serves as 

an abstract interface between iterative solvers and matrix-like 

objects. 

 

To construct a concrete LinearOperator, either pass appropriate 

callables to the constructor of this class, or subclass it. 

 

A subclass must implement either one of the methods ``_matvec`` 

and ``_matmat``, and the attributes/properties ``shape`` (pair of 

integers) and ``dtype`` (may be None). It may call the ``__init__`` 

on this class to have these attributes validated. Implementing 

``_matvec`` automatically implements ``_matmat`` (using a naive 

algorithm) and vice-versa. 

 

Optionally, a subclass may implement ``_rmatvec`` or ``_adjoint`` 

to implement the Hermitian adjoint (conjugate transpose). As with 

``_matvec`` and ``_matmat``, implementing either ``_rmatvec`` or 

``_adjoint`` implements the other automatically. Implementing 

``_adjoint`` is preferable; ``_rmatvec`` is mostly there for 

backwards compatibility. 

 

Parameters 

---------- 

shape : tuple 

Matrix dimensions (M,N). 

matvec : callable f(v) 

Returns returns A * v. 

rmatvec : callable f(v) 

Returns A^H * v, where A^H is the conjugate transpose of A. 

matmat : callable f(V) 

Returns A * V, where V is a dense matrix with dimensions (N,K). 

dtype : dtype 

Data type of the matrix. 

 

Attributes 

---------- 

args : tuple 

For linear operators describing products etc. of other linear 

operators, the operands of the binary operation. 

 

See Also 

-------- 

aslinearoperator : Construct LinearOperators 

 

Notes 

----- 

The user-defined matvec() function must properly handle the case 

where v has shape (N,) as well as the (N,1) case. The shape of 

the return type is handled internally by LinearOperator. 

 

LinearOperator instances can also be multiplied, added with each 

other and exponentiated, all lazily: the result of these operations 

is always a new, composite LinearOperator, that defers linear 

operations to the original operators and combines the results. 

 

Examples 

-------- 

>>> import numpy as np 

>>> from scipy.sparse.linalg import LinearOperator 

>>> def mv(v): 

... return np.array([2*v[0], 3*v[1]]) 

... 

>>> A = LinearOperator((2,2), matvec=mv) 

>>> A 

<2x2 _CustomLinearOperator with dtype=float64> 

>>> A.matvec(np.ones(2)) 

array([ 2., 3.]) 

>>> A * np.ones(2) 

array([ 2., 3.]) 

 

""" 

def __new__(cls, *args, **kwargs): 

if cls is LinearOperator: 

# Operate as _CustomLinearOperator factory. 

return super(LinearOperator, cls).__new__(_CustomLinearOperator) 

else: 

obj = super(LinearOperator, cls).__new__(cls) 

 

if (type(obj)._matvec == LinearOperator._matvec 

and type(obj)._matmat == LinearOperator._matmat): 

raise TypeError("LinearOperator subclass should implement" 

" at least one of _matvec and _matmat.") 

 

return obj 

 

def __init__(self, dtype, shape): 

"""Initialize this LinearOperator. 

 

To be called by subclasses. ``dtype`` may be None; ``shape`` should 

be convertible to a length-2 tuple. 

""" 

if dtype is not None: 

dtype = np.dtype(dtype) 

 

shape = tuple(shape) 

if not isshape(shape): 

raise ValueError("invalid shape %r (must be 2-d)" % (shape,)) 

 

self.dtype = dtype 

self.shape = shape 

 

def _init_dtype(self): 

"""Called from subclasses at the end of the __init__ routine. 

""" 

if self.dtype is None: 

v = np.zeros(self.shape[-1]) 

self.dtype = np.asarray(self.matvec(v)).dtype 

 

def _matmat(self, X): 

"""Default matrix-matrix multiplication handler. 

 

Falls back on the user-defined _matvec method, so defining that will 

define matrix multiplication (though in a very suboptimal way). 

""" 

 

return np.hstack([self.matvec(col.reshape(-1,1)) for col in X.T]) 

 

def _matvec(self, x): 

"""Default matrix-vector multiplication handler. 

 

If self is a linear operator of shape (M, N), then this method will 

be called on a shape (N,) or (N, 1) ndarray, and should return a 

shape (M,) or (M, 1) ndarray. 

 

This default implementation falls back on _matmat, so defining that 

will define matrix-vector multiplication as well. 

""" 

return self.matmat(x.reshape(-1, 1)) 

 

def matvec(self, x): 

"""Matrix-vector multiplication. 

 

Performs the operation y=A*x where A is an MxN linear 

operator and x is a column vector or 1-d array. 

 

Parameters 

---------- 

x : {matrix, ndarray} 

An array with shape (N,) or (N,1). 

 

Returns 

------- 

y : {matrix, ndarray} 

A matrix or ndarray with shape (M,) or (M,1) depending 

on the type and shape of the x argument. 

 

Notes 

----- 

This matvec wraps the user-specified matvec routine or overridden 

_matvec method to ensure that y has the correct shape and type. 

 

""" 

 

x = np.asanyarray(x) 

 

M,N = self.shape 

 

if x.shape != (N,) and x.shape != (N,1): 

raise ValueError('dimension mismatch') 

 

y = self._matvec(x) 

 

if isinstance(x, np.matrix): 

y = asmatrix(y) 

else: 

y = np.asarray(y) 

 

if x.ndim == 1: 

y = y.reshape(M) 

elif x.ndim == 2: 

y = y.reshape(M,1) 

else: 

raise ValueError('invalid shape returned by user-defined matvec()') 

 

return y 

 

def rmatvec(self, x): 

"""Adjoint matrix-vector multiplication. 

 

Performs the operation y = A^H * x where A is an MxN linear 

operator and x is a column vector or 1-d array. 

 

Parameters 

---------- 

x : {matrix, ndarray} 

An array with shape (M,) or (M,1). 

 

Returns 

------- 

y : {matrix, ndarray} 

A matrix or ndarray with shape (N,) or (N,1) depending 

on the type and shape of the x argument. 

 

Notes 

----- 

This rmatvec wraps the user-specified rmatvec routine or overridden 

_rmatvec method to ensure that y has the correct shape and type. 

 

""" 

 

x = np.asanyarray(x) 

 

M,N = self.shape 

 

if x.shape != (M,) and x.shape != (M,1): 

raise ValueError('dimension mismatch') 

 

y = self._rmatvec(x) 

 

if isinstance(x, np.matrix): 

y = asmatrix(y) 

else: 

y = np.asarray(y) 

 

if x.ndim == 1: 

y = y.reshape(N) 

elif x.ndim == 2: 

y = y.reshape(N,1) 

else: 

raise ValueError('invalid shape returned by user-defined rmatvec()') 

 

return y 

 

def _rmatvec(self, x): 

"""Default implementation of _rmatvec; defers to adjoint.""" 

if type(self)._adjoint == LinearOperator._adjoint: 

# _adjoint not overridden, prevent infinite recursion 

raise NotImplementedError 

else: 

return self.H.matvec(x) 

 

def matmat(self, X): 

"""Matrix-matrix multiplication. 

 

Performs the operation y=A*X where A is an MxN linear 

operator and X dense N*K matrix or ndarray. 

 

Parameters 

---------- 

X : {matrix, ndarray} 

An array with shape (N,K). 

 

Returns 

------- 

Y : {matrix, ndarray} 

A matrix or ndarray with shape (M,K) depending on 

the type of the X argument. 

 

Notes 

----- 

This matmat wraps any user-specified matmat routine or overridden 

_matmat method to ensure that y has the correct type. 

 

""" 

 

X = np.asanyarray(X) 

 

if X.ndim != 2: 

raise ValueError('expected 2-d ndarray or matrix, not %d-d' 

% X.ndim) 

 

M,N = self.shape 

 

if X.shape[0] != N: 

raise ValueError('dimension mismatch: %r, %r' 

% (self.shape, X.shape)) 

 

Y = self._matmat(X) 

 

if isinstance(Y, np.matrix): 

Y = asmatrix(Y) 

 

return Y 

 

def __call__(self, x): 

return self*x 

 

def __mul__(self, x): 

return self.dot(x) 

 

def dot(self, x): 

"""Matrix-matrix or matrix-vector multiplication. 

 

Parameters 

---------- 

x : array_like 

1-d or 2-d array, representing a vector or matrix. 

 

Returns 

------- 

Ax : array 

1-d or 2-d array (depending on the shape of x) that represents 

the result of applying this linear operator on x. 

 

""" 

if isinstance(x, LinearOperator): 

return _ProductLinearOperator(self, x) 

elif np.isscalar(x): 

return _ScaledLinearOperator(self, x) 

else: 

x = np.asarray(x) 

 

if x.ndim == 1 or x.ndim == 2 and x.shape[1] == 1: 

return self.matvec(x) 

elif x.ndim == 2: 

return self.matmat(x) 

else: 

raise ValueError('expected 1-d or 2-d array or matrix, got %r' 

% x) 

 

def __matmul__(self, other): 

if np.isscalar(other): 

raise ValueError("Scalar operands are not allowed, " 

"use '*' instead") 

return self.__mul__(other) 

 

def __rmatmul__(self, other): 

if np.isscalar(other): 

raise ValueError("Scalar operands are not allowed, " 

"use '*' instead") 

return self.__rmul__(other) 

 

def __rmul__(self, x): 

if np.isscalar(x): 

return _ScaledLinearOperator(self, x) 

else: 

return NotImplemented 

 

def __pow__(self, p): 

if np.isscalar(p): 

return _PowerLinearOperator(self, p) 

else: 

return NotImplemented 

 

def __add__(self, x): 

if isinstance(x, LinearOperator): 

return _SumLinearOperator(self, x) 

else: 

return NotImplemented 

 

def __neg__(self): 

return _ScaledLinearOperator(self, -1) 

 

def __sub__(self, x): 

return self.__add__(-x) 

 

def __repr__(self): 

M,N = self.shape 

if self.dtype is None: 

dt = 'unspecified dtype' 

else: 

dt = 'dtype=' + str(self.dtype) 

 

return '<%dx%d %s with %s>' % (M, N, self.__class__.__name__, dt) 

 

def adjoint(self): 

"""Hermitian adjoint. 

 

Returns the Hermitian adjoint of self, aka the Hermitian 

conjugate or Hermitian transpose. For a complex matrix, the 

Hermitian adjoint is equal to the conjugate transpose. 

 

Can be abbreviated self.H instead of self.adjoint(). 

 

Returns 

------- 

A_H : LinearOperator 

Hermitian adjoint of self. 

""" 

return self._adjoint() 

 

H = property(adjoint) 

 

def transpose(self): 

"""Transpose this linear operator. 

 

Returns a LinearOperator that represents the transpose of this one. 

Can be abbreviated self.T instead of self.transpose(). 

""" 

return self._transpose() 

 

T = property(transpose) 

 

def _adjoint(self): 

"""Default implementation of _adjoint; defers to rmatvec.""" 

shape = (self.shape[1], self.shape[0]) 

return _CustomLinearOperator(shape, matvec=self.rmatvec, 

rmatvec=self.matvec, 

dtype=self.dtype) 

 

 

class _CustomLinearOperator(LinearOperator): 

"""Linear operator defined in terms of user-specified operations.""" 

 

def __init__(self, shape, matvec, rmatvec=None, matmat=None, dtype=None): 

super(_CustomLinearOperator, self).__init__(dtype, shape) 

 

self.args = () 

 

self.__matvec_impl = matvec 

self.__rmatvec_impl = rmatvec 

self.__matmat_impl = matmat 

 

self._init_dtype() 

 

def _matmat(self, X): 

if self.__matmat_impl is not None: 

return self.__matmat_impl(X) 

else: 

return super(_CustomLinearOperator, self)._matmat(X) 

 

def _matvec(self, x): 

return self.__matvec_impl(x) 

 

def _rmatvec(self, x): 

func = self.__rmatvec_impl 

if func is None: 

raise NotImplementedError("rmatvec is not defined") 

return self.__rmatvec_impl(x) 

 

def _adjoint(self): 

return _CustomLinearOperator(shape=(self.shape[1], self.shape[0]), 

matvec=self.__rmatvec_impl, 

rmatvec=self.__matvec_impl, 

dtype=self.dtype) 

 

 

def _get_dtype(operators, dtypes=None): 

if dtypes is None: 

dtypes = [] 

for obj in operators: 

if obj is not None and hasattr(obj, 'dtype'): 

dtypes.append(obj.dtype) 

return np.find_common_type(dtypes, []) 

 

 

class _SumLinearOperator(LinearOperator): 

def __init__(self, A, B): 

if not isinstance(A, LinearOperator) or \ 

not isinstance(B, LinearOperator): 

raise ValueError('both operands have to be a LinearOperator') 

if A.shape != B.shape: 

raise ValueError('cannot add %r and %r: shape mismatch' 

% (A, B)) 

self.args = (A, B) 

super(_SumLinearOperator, self).__init__(_get_dtype([A, B]), A.shape) 

 

def _matvec(self, x): 

return self.args[0].matvec(x) + self.args[1].matvec(x) 

 

def _rmatvec(self, x): 

return self.args[0].rmatvec(x) + self.args[1].rmatvec(x) 

 

def _matmat(self, x): 

return self.args[0].matmat(x) + self.args[1].matmat(x) 

 

def _adjoint(self): 

A, B = self.args 

return A.H + B.H 

 

 

class _ProductLinearOperator(LinearOperator): 

def __init__(self, A, B): 

if not isinstance(A, LinearOperator) or \ 

not isinstance(B, LinearOperator): 

raise ValueError('both operands have to be a LinearOperator') 

if A.shape[1] != B.shape[0]: 

raise ValueError('cannot multiply %r and %r: shape mismatch' 

% (A, B)) 

super(_ProductLinearOperator, self).__init__(_get_dtype([A, B]), 

(A.shape[0], B.shape[1])) 

self.args = (A, B) 

 

def _matvec(self, x): 

return self.args[0].matvec(self.args[1].matvec(x)) 

 

def _rmatvec(self, x): 

return self.args[1].rmatvec(self.args[0].rmatvec(x)) 

 

def _matmat(self, x): 

return self.args[0].matmat(self.args[1].matmat(x)) 

 

def _adjoint(self): 

A, B = self.args 

return B.H * A.H 

 

 

class _ScaledLinearOperator(LinearOperator): 

def __init__(self, A, alpha): 

if not isinstance(A, LinearOperator): 

raise ValueError('LinearOperator expected as A') 

if not np.isscalar(alpha): 

raise ValueError('scalar expected as alpha') 

dtype = _get_dtype([A], [type(alpha)]) 

super(_ScaledLinearOperator, self).__init__(dtype, A.shape) 

self.args = (A, alpha) 

 

def _matvec(self, x): 

return self.args[1] * self.args[0].matvec(x) 

 

def _rmatvec(self, x): 

return np.conj(self.args[1]) * self.args[0].rmatvec(x) 

 

def _matmat(self, x): 

return self.args[1] * self.args[0].matmat(x) 

 

def _adjoint(self): 

A, alpha = self.args 

return A.H * alpha 

 

 

class _PowerLinearOperator(LinearOperator): 

def __init__(self, A, p): 

if not isinstance(A, LinearOperator): 

raise ValueError('LinearOperator expected as A') 

if A.shape[0] != A.shape[1]: 

raise ValueError('square LinearOperator expected, got %r' % A) 

if not isintlike(p) or p < 0: 

raise ValueError('non-negative integer expected as p') 

 

super(_PowerLinearOperator, self).__init__(_get_dtype([A]), A.shape) 

self.args = (A, p) 

 

def _power(self, fun, x): 

res = np.array(x, copy=True) 

for i in range(self.args[1]): 

res = fun(res) 

return res 

 

def _matvec(self, x): 

return self._power(self.args[0].matvec, x) 

 

def _rmatvec(self, x): 

return self._power(self.args[0].rmatvec, x) 

 

def _matmat(self, x): 

return self._power(self.args[0].matmat, x) 

 

def _adjoint(self): 

A, p = self.args 

return A.H ** p 

 

 

class MatrixLinearOperator(LinearOperator): 

def __init__(self, A): 

super(MatrixLinearOperator, self).__init__(A.dtype, A.shape) 

self.A = A 

self.__adj = None 

self.args = (A,) 

 

def _matmat(self, X): 

return self.A.dot(X) 

 

def _adjoint(self): 

if self.__adj is None: 

self.__adj = _AdjointMatrixOperator(self) 

return self.__adj 

 

 

class _AdjointMatrixOperator(MatrixLinearOperator): 

def __init__(self, adjoint): 

self.A = adjoint.A.T.conj() 

self.__adjoint = adjoint 

self.args = (adjoint,) 

self.shape = adjoint.shape[1], adjoint.shape[0] 

 

@property 

def dtype(self): 

return self.__adjoint.dtype 

 

def _adjoint(self): 

return self.__adjoint 

 

 

class IdentityOperator(LinearOperator): 

def __init__(self, shape, dtype=None): 

super(IdentityOperator, self).__init__(dtype, shape) 

 

def _matvec(self, x): 

return x 

 

def _rmatvec(self, x): 

return x 

 

def _matmat(self, x): 

return x 

 

def _adjoint(self): 

return self 

 

 

def aslinearoperator(A): 

"""Return A as a LinearOperator. 

 

'A' may be any of the following types: 

- ndarray 

- matrix 

- sparse matrix (e.g. csr_matrix, lil_matrix, etc.) 

- LinearOperator 

- An object with .shape and .matvec attributes 

 

See the LinearOperator documentation for additional information. 

 

Notes 

----- 

If 'A' has no .dtype attribute, the data type is determined by calling 

:func:`LinearOperator.matvec()` - set the .dtype attribute to prevent this 

call upon the linear operator creation. 

 

Examples 

-------- 

>>> from scipy.sparse.linalg import aslinearoperator 

>>> M = np.array([[1,2,3],[4,5,6]], dtype=np.int32) 

>>> aslinearoperator(M) 

<2x3 MatrixLinearOperator with dtype=int32> 

""" 

if isinstance(A, LinearOperator): 

return A 

 

elif isinstance(A, np.ndarray) or isinstance(A, np.matrix): 

if A.ndim > 2: 

raise ValueError('array must have ndim <= 2') 

A = np.atleast_2d(np.asarray(A)) 

return MatrixLinearOperator(A) 

 

elif isspmatrix(A): 

return MatrixLinearOperator(A) 

 

else: 

if hasattr(A, 'shape') and hasattr(A, 'matvec'): 

rmatvec = None 

dtype = None 

 

if hasattr(A, 'rmatvec'): 

rmatvec = A.rmatvec 

if hasattr(A, 'dtype'): 

dtype = A.dtype 

return LinearOperator(A.shape, A.matvec, 

rmatvec=rmatvec, dtype=dtype) 

 

else: 

raise TypeError('type not understood')