or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-features.mddevices-distributed.mdindex.mdmathematical-functions.mdneural-networks.mdtensor-operations.mdtraining.md

mathematical-functions.mddocs/

0

# Mathematical Functions

1

2

Comprehensive mathematical operations including linear algebra, FFT, special functions, and statistical operations. PyTorch provides extensive mathematical functionality across multiple specialized modules.

3

4

## Capabilities

5

6

### Linear Algebra Operations (torch.linalg)

7

8

Advanced linear algebra operations for matrices and tensors.

9

10

```python { .api }

11

def matmul(input: Tensor, other: Tensor) -> Tensor:

12

"""Matrix multiplication supporting broadcasting."""

13

14

def solve(A: Tensor, B: Tensor) -> Tensor:

15

"""Solve linear system AX = B."""

16

17

def inv(A: Tensor) -> Tensor:

18

"""Matrix inverse."""

19

20

def pinv(A: Tensor, rcond=1e-15, hermitian=False) -> Tensor:

21

"""Moore-Penrose pseudo-inverse."""

22

23

def det(A: Tensor) -> Tensor:

24

"""Matrix determinant."""

25

26

def slogdet(A: Tensor) -> Tuple[Tensor, Tensor]:

27

"""Sign and log-determinant."""

28

29

def norm(A: Tensor, ord=None, dim=None, keepdim=False, *, dtype=None) -> Tensor:

30

"""Matrix or vector norm."""

31

32

def vector_norm(x: Tensor, ord=2, dim=None, keepdim=False, *, dtype=None) -> Tensor:

33

"""Vector norm."""

34

35

def matrix_norm(A: Tensor, ord='fro', dim=(-2, -1), keepdim=False, *, dtype=None) -> Tensor:

36

"""Matrix norm."""

37

38

def matrix_rank(A: Tensor, atol=None, rtol=None, hermitian=False) -> Tensor:

39

"""Matrix rank."""

40

41

def cond(A: Tensor, p=None) -> Tensor:

42

"""Matrix condition number."""

43

```

44

45

### Matrix Decompositions

46

47

Matrix factorization methods for numerical analysis.

48

49

```python { .api }

50

def svd(A: Tensor, full_matrices=True) -> Tuple[Tensor, Tensor, Tensor]:

51

"""Singular Value Decomposition."""

52

53

def svdvals(A: Tensor) -> Tensor:

54

"""Singular values only."""

55

56

def eig(A: Tensor) -> Tuple[Tensor, Tensor]:

57

"""Eigenvalue decomposition."""

58

59

def eigvals(A: Tensor) -> Tensor:

60

"""Eigenvalues only."""

61

62

def eigh(A: Tensor, UPLO='L') -> Tuple[Tensor, Tensor]:

63

"""Eigenvalue decomposition for Hermitian matrices."""

64

65

def eigvalsh(A: Tensor, UPLO='L') -> Tensor:

66

"""Eigenvalues for Hermitian matrices."""

67

68

def qr(A: Tensor, mode='reduced') -> Tuple[Tensor, Tensor]:

69

"""QR decomposition."""

70

71

def cholesky(A: Tensor, upper=False) -> Tensor:

72

"""Cholesky decomposition."""

73

74

def cholesky_ex(A: Tensor, upper=False, check_errors=False) -> Tuple[Tensor, Tensor]:

75

"""Cholesky decomposition with error checking."""

76

77

def lu_factor(A: Tensor, *, pivot=True) -> Tuple[Tensor, Tensor]:

78

"""LU factorization."""

79

80

def lu_factor_ex(A: Tensor, *, pivot=True, check_errors=False) -> Tuple[Tensor, Tensor, Tensor]:

81

"""LU factorization with error checking."""

82

```

83

84

### Fast Fourier Transform (torch.fft)

85

86

FFT operations for frequency domain analysis.

87

88

```python { .api }

89

def fft(input: Tensor, n=None, dim=-1, norm=None) -> Tensor:

90

"""One-dimensional discrete Fourier transform."""

91

92

def ifft(input: Tensor, n=None, dim=-1, norm=None) -> Tensor:

93

"""One-dimensional inverse discrete Fourier transform."""

94

95

def rfft(input: Tensor, n=None, dim=-1, norm=None) -> Tensor:

96

"""One-dimensional real-to-complex FFT."""

97

98

def irfft(input: Tensor, n=None, dim=-1, norm=None) -> Tensor:

99

"""One-dimensional complex-to-real inverse FFT."""

100

101

def fft2(input: Tensor, s=None, dim=(-2, -1), norm=None) -> Tensor:

102

"""Two-dimensional discrete Fourier transform."""

103

104

def ifft2(input: Tensor, s=None, dim=(-2, -1), norm=None) -> Tensor:

105

"""Two-dimensional inverse discrete Fourier transform."""

106

107

def rfft2(input: Tensor, s=None, dim=(-2, -1), norm=None) -> Tensor:

108

"""Two-dimensional real-to-complex FFT."""

109

110

def irfft2(input: Tensor, s=None, dim=(-2, -1), norm=None) -> Tensor:

111

"""Two-dimensional complex-to-real inverse FFT."""

112

113

def fftn(input: Tensor, s=None, dim=None, norm=None) -> Tensor:

114

"""N-dimensional discrete Fourier transform."""

115

116

def ifftn(input: Tensor, s=None, dim=None, norm=None) -> Tensor:

117

"""N-dimensional inverse discrete Fourier transform."""

118

119

def rfftn(input: Tensor, s=None, dim=None, norm=None) -> Tensor:

120

"""N-dimensional real-to-complex FFT."""

121

122

def irfftn(input: Tensor, s=None, dim=None, norm=None) -> Tensor:

123

"""N-dimensional complex-to-real inverse FFT."""

124

125

def fftfreq(n: int, d=1.0, *, dtype=None, device=None, requires_grad=False) -> Tensor:

126

"""Discrete Fourier Transform sample frequencies."""

127

128

def rfftfreq(n: int, d=1.0, *, dtype=None, device=None, requires_grad=False) -> Tensor:

129

"""Real-valued discrete Fourier Transform sample frequencies."""

130

131

def fftshift(input: Tensor, dim=None) -> Tensor:

132

"""Shift zero-frequency component to center."""

133

134

def ifftshift(input: Tensor, dim=None) -> Tensor:

135

"""Inverse of fftshift."""

136

```

137

138

### Special Functions (torch.special)

139

140

Special mathematical functions for advanced computations.

141

142

```python { .api }

143

def erf(input: Tensor) -> Tensor:

144

"""Error function."""

145

146

def erfc(input: Tensor) -> Tensor:

147

"""Complementary error function."""

148

149

def erfcx(input: Tensor) -> Tensor:

150

"""Scaled complementary error function."""

151

152

def erfinv(input: Tensor) -> Tensor:

153

"""Inverse error function."""

154

155

def digamma(input: Tensor) -> Tensor:

156

"""Digamma function (logarithmic derivative of gamma)."""

157

158

def gammaln(input: Tensor) -> Tensor:

159

"""Log gamma function."""

160

161

def polygamma(n: int, input: Tensor) -> Tensor:

162

"""Polygamma function."""

163

164

def multigammaln(input: Tensor, p: int) -> Tensor:

165

"""Multivariate log gamma function."""

166

167

def gammainc(input: Tensor, other: Tensor) -> Tensor:

168

"""Regularized lower incomplete gamma function."""

169

170

def gammaincc(input: Tensor, other: Tensor) -> Tensor:

171

"""Regularized upper incomplete gamma function."""

172

173

def bessel_j0(input: Tensor) -> Tensor:

174

"""Bessel function of the first kind of order 0."""

175

176

def bessel_j1(input: Tensor) -> Tensor:

177

"""Bessel function of the first kind of order 1."""

178

179

def bessel_y0(input: Tensor) -> Tensor:

180

"""Bessel function of the second kind of order 0."""

181

182

def bessel_y1(input: Tensor) -> Tensor:

183

"""Bessel function of the second kind of order 1."""

184

185

def modified_bessel_i0(input: Tensor) -> Tensor:

186

"""Modified Bessel function of the first kind of order 0."""

187

188

def modified_bessel_i1(input: Tensor) -> Tensor:

189

"""Modified Bessel function of the first kind of order 1."""

190

191

def modified_bessel_k0(input: Tensor) -> Tensor:

192

"""Modified Bessel function of the second kind of order 0."""

193

194

def modified_bessel_k1(input: Tensor) -> Tensor:

195

"""Modified Bessel function of the second kind of order 1."""

196

197

def i0(input: Tensor) -> Tensor:

198

"""Modified Bessel function of the first kind of order 0."""

199

200

def i0e(input: Tensor) -> Tensor:

201

"""Exponentially scaled modified Bessel function of order 0."""

202

203

def ndtr(input: Tensor) -> Tensor:

204

"""Standard normal cumulative distribution function."""

205

206

def ndtri(input: Tensor) -> Tensor:

207

"""Inverse of standard normal cumulative distribution function."""

208

209

def log_ndtr(input: Tensor) -> Tensor:

210

"""Log of standard normal cumulative distribution function."""

211

212

def expit(input: Tensor) -> Tensor:

213

"""Expit function (sigmoid)."""

214

215

def logit(input: Tensor, eps=None) -> Tensor:

216

"""Logit function (inverse sigmoid)."""

217

218

def xlogy(input: Tensor, other: Tensor) -> Tensor:

219

"""Elementwise x * log(y)."""

220

221

def xlog1py(input: Tensor, other: Tensor) -> Tensor:

222

"""Elementwise x * log1p(y)."""

223

224

def zeta(input: Tensor, other: Tensor) -> Tensor:

225

"""Hurwitz zeta function."""

226

227

def logsumexp(input: Tensor, dim, keepdim=False) -> Tensor:

228

"""Log of sum of exponentials."""

229

230

def softmax(input: Tensor, dim, dtype=None) -> Tensor:

231

"""Softmax function."""

232

233

def log_softmax(input: Tensor, dim, dtype=None) -> Tensor:

234

"""Log softmax function."""

235

```

236

237

### Statistical Functions

238

239

Statistical operations and probability distributions.

240

241

```python { .api }

242

def mean(input: Tensor, dim=None, keepdim=False, *, dtype=None) -> Tensor:

243

"""Mean along specified dimensions."""

244

245

def median(input: Tensor, dim=None, keepdim=False) -> Tensor:

246

"""Median along specified dimensions."""

247

248

def mode(input: Tensor, dim=None, keepdim=False) -> Tensor:

249

"""Mode along specified dimensions."""

250

251

def std(input: Tensor, dim=None, unbiased=True, keepdim=False) -> Tensor:

252

"""Standard deviation."""

253

254

def var(input: Tensor, dim=None, unbiased=True, keepdim=False) -> Tensor:

255

"""Variance."""

256

257

def std_mean(input: Tensor, dim=None, unbiased=True, keepdim=False) -> Tuple[Tensor, Tensor]:

258

"""Standard deviation and mean."""

259

260

def var_mean(input: Tensor, dim=None, unbiased=True, keepdim=False) -> Tuple[Tensor, Tensor]:

261

"""Variance and mean."""

262

263

def cov(input: Tensor, *, correction=1, fweights=None, aweights=None) -> Tensor:

264

"""Covariance matrix."""

265

266

def corrcoef(input: Tensor) -> Tensor:

267

"""Correlation coefficient matrix."""

268

269

def bincount(input: Tensor, weights=None, minlength=0) -> Tensor:

270

"""Count occurrences of each value."""

271

272

def histogram(input: Tensor, bins, *, range=None, weight=None, density=False) -> Tuple[Tensor, Tensor]:

273

"""Compute histogram of tensor values."""

274

275

def histogramdd(input: Tensor, bins, *, range=None, weight=None, density=False) -> Tuple[Tensor, List[Tensor]]:

276

"""Compute multidimensional histogram."""

277

```

278

279

### Sparse Operations (torch.sparse)

280

281

Operations for sparse tensors and matrices.

282

283

```python { .api }

284

def sparse.mm(mat1: Tensor, mat2: Tensor) -> Tensor:

285

"""Sparse matrix multiplication."""

286

287

def sparse.addmm(bias: Tensor, mat1: Tensor, mat2: Tensor, *, beta=1, alpha=1) -> Tensor:

288

"""Sparse addmm operation."""

289

290

def sparse.sum(input: Tensor, dim=None, dtype=None) -> Tensor:

291

"""Sum of sparse tensor elements."""

292

293

def sparse.softmax(input: Tensor, dim: int, *, dtype=None) -> Tensor:

294

"""Sparse softmax."""

295

296

def sparse.log_softmax(input: Tensor, dim: int, *, dtype=None) -> Tensor:

297

"""Sparse log softmax."""

298

299

def sparse.spsolve(A: Tensor, B: Tensor) -> Tensor:

300

"""Solve sparse linear system."""

301

302

def sparse.sampled_addmm(bias: Tensor, input: Tensor, mat1: Tensor, mat2: Tensor, *, beta=1, alpha=1) -> Tensor:

303

"""Sampled sparse matrix multiplication and addition."""

304

```

305

306

### Random Sampling

307

308

Random number generation and sampling functions.

309

310

```python { .api }

311

def manual_seed(seed: int):

312

"""Set random seed for reproducibility."""

313

314

def initial_seed() -> int:

315

"""Return initial random seed."""

316

317

def seed() -> int:

318

"""Generate random seed."""

319

320

def get_rng_state() -> Tensor:

321

"""Get random number generator state."""

322

323

def set_rng_state(new_state: Tensor):

324

"""Set random number generator state."""

325

326

def bernoulli(input: Tensor, *, generator=None) -> Tensor:

327

"""Sample from Bernoulli distribution."""

328

329

def poisson(input: Tensor, generator=None) -> Tensor:

330

"""Sample from Poisson distribution."""

331

332

def normal(mean: float, std: float, size, *, dtype=None, device=None, requires_grad=False) -> Tensor:

333

"""Sample from normal distribution."""

334

335

def uniform(low: float, high: float, size, *, dtype=None, device=None, requires_grad=False) -> Tensor:

336

"""Sample from uniform distribution."""

337

338

def exponential(lambd: float, size, *, dtype=None, device=None, requires_grad=False) -> Tensor:

339

"""Sample from exponential distribution."""

340

341

def geometric(p: float, size, *, dtype=None, device=None, requires_grad=False) -> Tensor:

342

"""Sample from geometric distribution."""

343

344

def multinomial(input: Tensor, num_samples: int, replacement=False, *, generator=None) -> Tensor:

345

"""Sample from multinomial distribution."""

346

```

347

348

## Usage Examples

349

350

### Linear Algebra Operations

351

352

```python

353

import torch

354

import torch.linalg as LA

355

356

# Matrix operations

357

A = torch.randn(3, 3)

358

B = torch.randn(3, 2)

359

360

# Basic operations

361

det_A = LA.det(A)

362

inv_A = LA.inv(A)

363

X = LA.solve(A, B) # Solve AX = B

364

365

# Matrix decompositions

366

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

367

eigenvals, eigenvecs = LA.eig(A)

368

Q, R = LA.qr(A)

369

370

# Norms and properties

371

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

372

spectral_norm = LA.norm(A, 2)

373

condition_number = LA.cond(A)

374

rank = LA.matrix_rank(A)

375

376

print(f"Determinant: {det_A}")

377

print(f"Condition number: {condition_number}")

378

print(f"Rank: {rank}")

379

```

380

381

### FFT Operations

382

383

```python

384

import torch

385

import torch.fft as fft

386

387

# Create signal

388

t = torch.linspace(0, 1, 1000)

389

signal = torch.sin(2 * torch.pi * 5 * t) + torch.sin(2 * torch.pi * 10 * t)

390

391

# FFT analysis

392

signal_fft = fft.fft(signal)

393

signal_rfft = fft.rfft(signal) # Real-valued input

394

frequencies = fft.fftfreq(len(signal))

395

396

# 2D FFT for images

397

image = torch.randn(256, 256)

398

image_fft = fft.fft2(image)

399

image_shifted = fft.fftshift(image_fft)

400

401

# Inverse transform

402

reconstructed = fft.ifft(signal_fft).real

403

404

print(f"Original signal shape: {signal.shape}")

405

print(f"FFT shape: {signal_fft.shape}")

406

print(f"Real FFT shape: {signal_rfft.shape}")

407

print(f"Reconstruction error: {torch.mean((signal - reconstructed) ** 2)}")

408

```

409

410

### Special Functions

411

412

```python

413

import torch

414

import torch.special as special

415

416

# Error functions

417

x = torch.linspace(-3, 3, 100)

418

erf_vals = special.erf(x)

419

erfc_vals = special.erfc(x)

420

421

# Gamma functions

422

gamma_vals = torch.exp(special.gammaln(x + 1)) # Gamma function via log

423

digamma_vals = special.digamma(x + 1)

424

425

# Bessel functions

426

bessel_j0 = special.bessel_j0(x)

427

bessel_y0 = special.bessel_y0(x + 0.1) # Avoid singularity at 0

428

429

# Probability functions

430

sigmoid_vals = special.expit(x) # Sigmoid

431

logit_vals = special.logit(torch.sigmoid(x)) # Should recover x

432

433

# Normal distribution

434

ndtr_vals = special.ndtr(x) # CDF

435

log_ndtr_vals = special.log_ndtr(x) # Log CDF

436

437

print(f"erf(1.0): {special.erf(torch.tensor(1.0))}")

438

print(f"Gamma(5): {torch.exp(special.gammaln(torch.tensor(5.0)))}")

439

print(f"Sigmoid(0): {special.expit(torch.tensor(0.0))}")

440

```

441

442

### Statistical Analysis

443

444

```python

445

import torch

446

447

# Generate sample data

448

data = torch.randn(1000, 10)

449

450

# Basic statistics

451

mean_vals = torch.mean(data, dim=0)

452

std_vals = torch.std(data, dim=0)

453

var_vals = torch.var(data, dim=0)

454

455

# Along different dimensions

456

overall_mean = torch.mean(data)

457

row_means = torch.mean(data, dim=1)

458

459

# Quantiles and percentiles

460

median_vals = torch.median(data, dim=0).values

461

q25 = torch.quantile(data, 0.25, dim=0)

462

q75 = torch.quantile(data, 0.75, dim=0)

463

464

# Correlation

465

correlation_matrix = torch.corrcoef(data.T)

466

covariance_matrix = torch.cov(data.T)

467

468

# Histogram

469

values = torch.randn(10000)

470

hist, bin_edges = torch.histogram(values, bins=50)

471

472

print(f"Data shape: {data.shape}")

473

print(f"Mean: {mean_vals[:5]}") # First 5 features

474

print(f"Std: {std_vals[:5]}")

475

print(f"Correlation matrix shape: {correlation_matrix.shape}")

476

```

477

478

### Sparse Matrix Operations

479

480

```python

481

import torch

482

483

# Create sparse matrices

484

indices = torch.LongTensor([[0, 1, 1], [2, 0, 2]])

485

values = torch.FloatTensor([3, 4, 5])

486

shape = (2, 3)

487

sparse_a = torch.sparse_coo_tensor(indices, values, shape)

488

489

# Dense matrix

490

dense_b = torch.randn(3, 4)

491

492

# Sparse matrix multiplication

493

result = torch.sparse.mm(sparse_a, dense_b)

494

495

# Sparse addition with bias

496

bias = torch.randn(2, 4)

497

result_with_bias = torch.sparse.addmm(bias, sparse_a, dense_b)

498

499

# Convert to dense for inspection

500

dense_a = sparse_a.to_dense()

501

502

print(f"Sparse matrix:\n{dense_a}")

503

print(f"Result shape: {result.shape}")

504

print(f"Sparse matrix multiplication completed")

505

```

506

507

### Advanced Mathematical Operations

508

509

```python

510

import torch

511

import torch.linalg as LA

512

513

# Batch operations

514

batch_size = 32

515

matrix_size = 64

516

batch_matrices = torch.randn(batch_size, matrix_size, matrix_size)

517

518

# Batch linear algebra

519

batch_det = LA.det(batch_matrices)

520

batch_eigenvals = LA.eigvals(batch_matrices)

521

522

# Solve batch of linear systems

523

batch_rhs = torch.randn(batch_size, matrix_size, 10)

524

batch_solutions = LA.solve(batch_matrices, batch_rhs)

525

526

# Batch SVD

527

U, S, V = LA.svd(batch_matrices)

528

529

# Complex number operations

530

complex_tensor = torch.complex(torch.randn(100), torch.randn(100))

531

complex_fft = torch.fft.fft(complex_tensor)

532

phase = torch.angle(complex_tensor)

533

magnitude = torch.abs(complex_tensor)

534

535

print(f"Batch determinants shape: {batch_det.shape}")

536

print(f"Batch eigenvalues shape: {batch_eigenvals.shape}")

537

print(f"Complex tensor magnitude range: {magnitude.min():.3f} to {magnitude.max():.3f}")

538

```