or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

core-arithmetic.mdelementary-functions.mdelliptic-modular-functions.mdindex.mdlinear-algebra.mdmathematical-constants.mdnumerical-calculus.mdpattern-recognition.mdsignal-processing.mdspecial-functions.mdvisualization.md

linear-algebra.mddocs/

0

# Linear Algebra

1

2

Matrix operations, linear system solving, eigenvalue computations, matrix decompositions, and matrix functions supporting arbitrary precision arithmetic.

3

4

## Capabilities

5

6

### Matrix Creation and Basic Operations

7

8

Creating matrices and basic operations.

9

10

```python { .api }

11

class matrix:

12

"""

13

Matrix class supporting arbitrary precision arithmetic.

14

15

Args:

16

*args: Matrix dimensions, data, or another matrix

17

"""

18

def __init__(self, *args): ...

19

def __getitem__(self, key): ...

20

def __setitem__(self, key, value): ...

21

def __str__(self) -> str: ...

22

def __repr__(self) -> str: ...

23

24

def eye(n):

25

"""

26

Create n×n identity matrix.

27

28

Args:

29

n: Matrix size

30

31

Returns:

32

n×n identity matrix

33

"""

34

35

def diag(diagonal):

36

"""

37

Create diagonal matrix from list of diagonal elements.

38

39

Args:

40

diagonal: List of diagonal elements

41

42

Returns:

43

Diagonal matrix

44

"""

45

46

def zeros(m, n=None):

47

"""

48

Create matrix of zeros.

49

50

Args:

51

m: Number of rows

52

n: Number of columns (default: same as m)

53

54

Returns:

55

m×n zero matrix

56

"""

57

58

def ones(m, n=None):

59

"""

60

Create matrix of ones.

61

62

Args:

63

m: Number of rows

64

n: Number of columns (default: same as m)

65

66

Returns:

67

m×n matrix of ones

68

"""

69

70

def hilbert(n):

71

"""

72

Create n×n Hilbert matrix.

73

74

Args:

75

n: Matrix size

76

77

Returns:

78

Hilbert matrix H[i,j] = 1/(i+j+1)

79

"""

80

81

def randmatrix(m, n=None):

82

"""

83

Create random matrix.

84

85

Args:

86

m: Number of rows

87

n: Number of columns (default: same as m)

88

89

Returns:

90

m×n random matrix

91

"""

92

93

def swap_row(A, i, j):

94

"""

95

Swap rows i and j in matrix A.

96

97

Args:

98

A: Matrix to modify

99

i, j: Row indices to swap

100

"""

101

102

def extend(A, b):

103

"""

104

Extend matrix A with column vector b.

105

106

Args:

107

A: Matrix

108

b: Column vector

109

110

Returns:

111

Extended matrix [A|b]

112

"""

113

```

114

115

### Vector and Matrix Norms

116

117

Various norms for vectors and matrices.

118

119

```python { .api }

120

def norm(x, p=2):

121

"""

122

Compute vector or matrix norm.

123

124

Args:

125

x: Vector or matrix

126

p: Norm type (1, 2, inf, 'fro' for Frobenius)

127

128

Returns:

129

Norm of x

130

"""

131

132

def mnorm(A, p=1):

133

"""

134

Compute matrix norm.

135

136

Args:

137

A: Matrix

138

p: Norm type (1, 2, inf, 'fro')

139

140

Returns:

141

Matrix norm

142

"""

143

144

def unitvector(n, i):

145

"""

146

Create unit vector with 1 in position i.

147

148

Args:

149

n: Vector length

150

i: Position of 1 (0-indexed)

151

152

Returns:

153

Unit vector

154

"""

155

```

156

157

### Matrix Properties

158

159

Functions to compute matrix properties.

160

161

```python { .api }

162

def det(A):

163

"""

164

Compute matrix determinant.

165

166

Args:

167

A: Square matrix

168

169

Returns:

170

Determinant of A

171

"""

172

173

def cond(A, p=None):

174

"""

175

Compute condition number of matrix.

176

177

Args:

178

A: Matrix

179

p: Norm type (default: 2-norm)

180

181

Returns:

182

Condition number ||A|| * ||A^(-1)||

183

"""

184

185

def residual(A, x, b):

186

"""

187

Compute residual ||Ax - b|| of linear system.

188

189

Args:

190

A: Coefficient matrix

191

x: Solution vector

192

b: Right-hand side

193

194

Returns:

195

Residual norm

196

"""

197

```

198

199

### Matrix Decompositions

200

201

Matrix factorizations for solving linear systems and analysis.

202

203

```python { .api }

204

def lu(A):

205

"""

206

LU decomposition with partial pivoting.

207

208

Args:

209

A: Matrix to decompose

210

211

Returns:

212

tuple: (P, L, U) where PA = LU

213

"""

214

215

def qr(A):

216

"""

217

QR decomposition.

218

219

Args:

220

A: Matrix to decompose

221

222

Returns:

223

tuple: (Q, R) where A = QR

224

"""

225

226

def cholesky(A):

227

"""

228

Cholesky decomposition of positive definite matrix.

229

230

Args:

231

A: Positive definite matrix

232

233

Returns:

234

Lower triangular matrix L such that A = LL^T

235

"""

236

237

def hessenberg(A):

238

"""

239

Reduce matrix to Hessenberg form.

240

241

Args:

242

A: Square matrix

243

244

Returns:

245

tuple: (P, H) where P^T A P = H (upper Hessenberg)

246

"""

247

248

def schur(A):

249

"""

250

Schur decomposition.

251

252

Args:

253

A: Square matrix

254

255

Returns:

256

tuple: (Q, T) where A = QTQ^T (T upper triangular)

257

"""

258

```

259

260

### Linear System Solvers

261

262

Solve linear systems Ax = b using various methods.

263

264

```python { .api }

265

def lu_solve(A, b):

266

"""

267

Solve linear system using LU decomposition.

268

269

Args:

270

A: Coefficient matrix

271

b: Right-hand side vector

272

273

Returns:

274

Solution vector x such that Ax = b

275

"""

276

277

def qr_solve(A, b):

278

"""

279

Solve linear system using QR decomposition.

280

281

Args:

282

A: Coefficient matrix

283

b: Right-hand side vector

284

285

Returns:

286

Solution vector x such that Ax = b

287

"""

288

289

def cholesky_solve(A, b):

290

"""

291

Solve linear system using Cholesky decomposition.

292

293

Args:

294

A: Positive definite coefficient matrix

295

b: Right-hand side vector

296

297

Returns:

298

Solution vector x such that Ax = b

299

"""

300

301

def inverse(A):

302

"""

303

Compute matrix inverse.

304

305

Args:

306

A: Invertible square matrix

307

308

Returns:

309

Inverse matrix A^(-1)

310

"""

311

```

312

313

### Eigenvalue Problems

314

315

Compute eigenvalues and eigenvectors.

316

317

```python { .api }

318

def eig(A):

319

"""

320

Compute eigenvalues and eigenvectors.

321

322

Args:

323

A: Square matrix

324

325

Returns:

326

tuple: (eigenvalues, eigenvectors)

327

"""

328

329

def eig_sort(A):

330

"""

331

Compute eigenvalues and eigenvectors, sorted by eigenvalue magnitude.

332

333

Args:

334

A: Square matrix

335

336

Returns:

337

tuple: (sorted_eigenvalues, sorted_eigenvectors)

338

"""

339

340

def eigsy(A):

341

"""

342

Compute eigenvalues of symmetric matrix.

343

344

Args:

345

A: Symmetric matrix

346

347

Returns:

348

List of eigenvalues

349

"""

350

351

def eighe(A):

352

"""

353

Compute eigenvalues of Hermitian matrix.

354

355

Args:

356

A: Hermitian matrix

357

358

Returns:

359

List of eigenvalues

360

"""

361

362

def eigh(A):

363

"""

364

Compute eigenvalues and eigenvectors of Hermitian matrix.

365

366

Args:

367

A: Hermitian matrix

368

369

Returns:

370

tuple: (eigenvalues, eigenvectors)

371

"""

372

```

373

374

### Singular Value Decomposition

375

376

SVD and related decompositions.

377

378

```python { .api }

379

def svd(A):

380

"""

381

Singular Value Decomposition.

382

383

Args:

384

A: Matrix to decompose

385

386

Returns:

387

tuple: (U, S, V) where A = U*diag(S)*V^T

388

"""

389

390

def svd_r(A):

391

"""

392

SVD for real matrices.

393

394

Args:

395

A: Real matrix

396

397

Returns:

398

tuple: (U, S, V) singular value decomposition

399

"""

400

401

def svd_c(A):

402

"""

403

SVD for complex matrices.

404

405

Args:

406

A: Complex matrix

407

408

Returns:

409

tuple: (U, S, V) singular value decomposition

410

"""

411

```

412

413

### Matrix Functions

414

415

Functions of matrices (matrix exponential, logarithm, etc.).

416

417

```python { .api }

418

def expm(A):

419

"""

420

Matrix exponential exp(A).

421

422

Args:

423

A: Square matrix

424

425

Returns:

426

Matrix exponential e^A

427

"""

428

429

def sqrtm(A):

430

"""

431

Matrix square root.

432

433

Args:

434

A: Square matrix

435

436

Returns:

437

Matrix square root (principal branch)

438

"""

439

440

def powm(A, n):

441

"""

442

Matrix power A^n.

443

444

Args:

445

A: Square matrix

446

n: Exponent

447

448

Returns:

449

Matrix power A^n

450

"""

451

452

def logm(A):

453

"""

454

Matrix logarithm log(A).

455

456

Args:

457

A: Square matrix

458

459

Returns:

460

Matrix logarithm (principal branch)

461

"""

462

463

def sinm(A):

464

"""

465

Matrix sine sin(A).

466

467

Args:

468

A: Square matrix

469

470

Returns:

471

Matrix sine

472

"""

473

474

def cosm(A):

475

"""

476

Matrix cosine cos(A).

477

478

Args:

479

A: Square matrix

480

481

Returns:

482

Matrix cosine

483

"""

484

```

485

486

### Quadrature Rules

487

488

Gauss quadrature rules for numerical integration.

489

490

```python { .api }

491

def gauss_quadrature(n, a=-1, b=1):

492

"""

493

Generate Gauss-Legendre quadrature rule.

494

495

Args:

496

n: Number of quadrature points

497

a: Lower integration limit (default: -1)

498

b: Upper integration limit (default: 1)

499

500

Returns:

501

tuple: (nodes, weights) for quadrature rule

502

"""

503

```

504

505

### Usage Examples

506

507

```python

508

import mpmath

509

from mpmath import mp

510

511

# Set precision

512

mp.dps = 25

513

514

# Create matrices

515

A = mp.matrix([[1, 2], [3, 4]])

516

print(f"Matrix A:\n{A}")

517

518

# Identity matrix

519

I = mp.eye(3)

520

print(f"3×3 Identity:\n{I}")

521

522

# Random matrix

523

R = mp.randmatrix(2, 3)

524

print(f"Random 2×3 matrix:\n{R}")

525

526

# Matrix operations

527

B = mp.matrix([[5, 6], [7, 8]])

528

C = A + B

529

print(f"A + B:\n{C}")

530

531

D = A * B

532

print(f"A * B:\n{D}")

533

534

# Determinant

535

det_A = mp.det(A)

536

print(f"det(A) = {det_A}")

537

538

# Matrix inverse

539

A_inv = mp.inverse(A)

540

print(f"A^(-1):\n{A_inv}")

541

542

# Verify inverse

543

product = A * A_inv

544

print(f"A * A^(-1):\n{product}")

545

546

# Solve linear system Ax = b

547

b = mp.matrix([1, 2])

548

x = mp.lu_solve(A, b)

549

print(f"Solution to Ax = b: {x}")

550

551

# Verify solution

552

residual = mp.norm(A * x - b)

553

print(f"Residual ||Ax - b|| = {residual}")

554

555

# Eigenvalues and eigenvectors

556

eigenvals, eigenvecs = mp.eig(A)

557

print(f"Eigenvalues: {eigenvals}")

558

print(f"Eigenvectors:\n{eigenvecs}")

559

560

# SVD

561

U, S, V = mp.svd(A)

562

print(f"SVD - U:\n{U}")

563

print(f"SVD - S: {S}")

564

print(f"SVD - V:\n{V}")

565

566

# Matrix functions

567

exp_A = mp.expm(A)

568

print(f"Matrix exponential exp(A):\n{exp_A}")

569

570

# Norms

571

frobenius_norm = mp.norm(A, 'fro')

572

print(f"Frobenius norm of A: {frobenius_norm}")

573

574

# Condition number

575

cond_A = mp.cond(A)

576

print(f"Condition number of A: {cond_A}")

577

578

# QR decomposition

579

Q, R = mp.qr(A)

580

print(f"QR - Q:\n{Q}")

581

print(f"QR - R:\n{R}")

582

583

# Verify QR decomposition

584

QR_product = Q * R

585

print(f"Q * R:\n{QR_product}")

586

print(f"Difference from A:\n{A - QR_product}")

587

```