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

r""" 

Generalized Functions 

 

Sage implements several generalized functions (also known as 

distributions) such as Dirac delta, Heaviside step functions. These 

generalized functions can be manipulated within Sage like any other 

symbolic functions. 

 

 

AUTHORS: 

 

- Golam Mortuza Hossain (2009-06-26): initial version 

 

 

EXAMPLES: 

 

Dirac delta function:: 

 

sage: dirac_delta(x) 

dirac_delta(x) 

 

Heaviside step function:: 

 

sage: heaviside(x) 

heaviside(x) 

 

Unit step function:: 

 

sage: unit_step(x) 

unit_step(x) 

 

Signum (sgn) function:: 

 

sage: sgn(x) 

sgn(x) 

 

Kronecker delta function:: 

 

sage: m,n=var('m,n') 

sage: kronecker_delta(m,n) 

kronecker_delta(m, n) 

 

""" 

 

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

# 

# Copyright (C) 2009 Golam Mortuza Hossain <gmhossain@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL v2+) 

# http://www.gnu.org/licenses/ 

# 

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

 

from sage.symbolic.function import (BuiltinFunction, GinacFunction) 

from sage.rings.all import ComplexIntervalField, ZZ 

 

class FunctionDiracDelta(BuiltinFunction): 

r""" 

The Dirac delta (generalized) function, `\delta(x)` (``dirac_delta(x)``). 

 

INPUT: 

 

- ``x`` - a real number or a symbolic expression 

 

DEFINITION: 

 

Dirac delta function `\delta(x)`, is defined in Sage as: 

 

`\delta(x) = 0` for real `x \ne 0` and 

`\int_{-\infty}^{\infty} \delta(x) dx = 1` 

 

Its alternate definition with respect to an arbitrary test 

function `f(x)` is 

 

`\int_{-\infty}^{\infty} f(x) \delta(x-a) dx = f(a)` 

 

EXAMPLES:: 

 

sage: dirac_delta(1) 

0 

sage: dirac_delta(0) 

dirac_delta(0) 

sage: dirac_delta(x) 

dirac_delta(x) 

sage: integrate(dirac_delta(x), x, -1, 1, algorithm='sympy') 

1 

 

REFERENCES: 

 

- :wikipedia:`Dirac_delta_function` 

 

""" 

def __init__(self): 

r""" 

The Dirac delta (generalized) function, ``dirac_delta(x)``. 

 

INPUT: 

 

- ``x`` - a real number or a symbolic expression 

 

EXAMPLES:: 

 

sage: dirac_delta(1) 

0 

sage: dirac_delta(0) 

dirac_delta(0) 

sage: dirac_delta(x) 

dirac_delta(x) 

sage: latex(dirac_delta(x)) 

\delta\left(x\right) 

 

sage: loads(dumps(dirac_delta(x))) 

dirac_delta(x) 

sage: dirac_delta(x)._sympy_() 

DiracDelta(x) 

""" 

BuiltinFunction.__init__(self, "dirac_delta", latex_name=r"\delta", 

conversions=dict(maxima='delta', 

mathematica='DiracDelta', 

sympy='DiracDelta', 

giac='Dirac')) 

 

def _eval_(self, x): 

""" 

INPUT: 

 

- ``x`` - a real number or a symbolic expression 

 

EXAMPLES:: 

 

sage: dirac_delta(1) 

0 

sage: dirac_delta(0) 

dirac_delta(0) 

sage: dirac_delta(x) 

dirac_delta(x) 

sage: dirac_delta(exp(-10000000000000000000)) 

0 

 

Evaluation test:: 

 

sage: dirac_delta(x).subs(x=1) 

0 

""" 

try: 

return self._evalf_(x) 

except (TypeError,ValueError): # x is symbolic 

pass 

return None 

 

def _evalf_(self, x, **kwds): 

""" 

TESTS:: 

 

sage: h(x) = dirac_delta(x) 

sage: h(pi).numerical_approx() 

0.000000000000000 

""" 

approx_x = ComplexIntervalField()(x) 

if bool(approx_x.imag() == 0): # x is real 

if bool(approx_x.real() == 0): # x is zero 

return None 

else: 

return 0 

raise ValueError("Numeric evaluation of symbolic expression") 

 

dirac_delta = FunctionDiracDelta() 

 

class FunctionHeaviside(GinacFunction): 

r""" 

The Heaviside step function, `H(x)` (``heaviside(x)``). 

 

INPUT: 

 

- ``x`` - a real number or a symbolic expression 

 

DEFINITION: 

 

The Heaviside step function, `H(x)` is defined in Sage as: 

 

`H(x) = 0` for `x < 0` and `H(x) = 1` for `x > 0` 

 

.. SEEALSO:: :func:`unit_step()<sage.functions.generalized.FunctionUnitStep>` 

 

EXAMPLES:: 

 

sage: heaviside(-1) 

0 

sage: heaviside(1) 

1 

sage: heaviside(0) 

heaviside(0) 

sage: heaviside(x) 

heaviside(x) 

 

sage: heaviside(-1/2) 

0 

sage: heaviside(exp(-1000000000000000000000)) 

1 

 

TESTS:: 

 

sage: heaviside(x)._sympy_() 

Heaviside(x) 

sage: heaviside(x).subs(x=1) 

1 

sage: heaviside(x).subs(x=-1) 

0 

 

:: 

 

sage: ex = heaviside(x)+1 

sage: t = loads(dumps(ex)); t 

heaviside(x) + 1 

sage: bool(t == ex) 

True 

sage: t.subs(x=1) 

2 

 

REFERENCES: 

 

- :wikipedia:`Heaviside_function` 

 

""" 

def __init__(self): 

r""" 

The Heaviside step function, ``heaviside(x)``. 

 

INPUT: 

 

- ``x`` - a real number or a symbolic expression 

 

EXAMPLES:: 

 

sage: heaviside(-1) 

0 

sage: heaviside(1) 

1 

sage: heaviside(0) 

heaviside(0) 

sage: heaviside(x) 

heaviside(x) 

sage: latex(heaviside(x)) 

H\left(x\right) 

sage: heaviside(x)._sympy_() 

Heaviside(x) 

sage: heaviside(x)._giac_() 

Heaviside(x) 

sage: h(x) = heaviside(x) 

sage: h(pi).numerical_approx() 

1.00000000000000 

""" 

GinacFunction.__init__(self, "heaviside", latex_name="H", 

conversions=dict(maxima='hstep', 

mathematica='HeavisideTheta', 

sympy='Heaviside', 

giac='Heaviside')) 

 

def _derivative_(self, x, diff_param=None): 

""" 

Derivative of Heaviside step function 

 

EXAMPLES:: 

 

sage: heaviside(x).diff(x) 

dirac_delta(x) 

""" 

return dirac_delta(x) 

 

heaviside = FunctionHeaviside() 

 

class FunctionUnitStep(GinacFunction): 

r""" 

The unit step function, `\mathrm{u}(x)` (``unit_step(x)``). 

 

INPUT: 

 

- ``x`` - a real number or a symbolic expression 

 

DEFINITION: 

 

The unit step function, `\mathrm{u}(x)` is defined in Sage as: 

 

`\mathrm{u}(x) = 0` for `x < 0` and `\mathrm{u}(x) = 1` for `x \geq 0` 

 

.. SEEALSO:: :func:`heaviside()<sage.functions.generalized.FunctionHeaviside>` 

 

EXAMPLES:: 

 

sage: unit_step(-1) 

0 

sage: unit_step(1) 

1 

sage: unit_step(0) 

1 

sage: unit_step(x) 

unit_step(x) 

sage: unit_step(-exp(-10000000000000000000)) 

0 

 

TESTS:: 

 

sage: unit_step(x).subs(x=1) 

1 

sage: unit_step(x).subs(x=0) 

1 

sage: h(x) = unit_step(x) 

sage: h(pi).numerical_approx() 

1.00000000000000 

""" 

def __init__(self): 

r""" 

The unit step function, ``unit_step(x)``. 

 

INPUT: 

 

- ``x`` - a real number or a symbolic expression 

 

EXAMPLES: 

 

sage: unit_step(-1) 

0 

sage: unit_step(1) 

1 

sage: unit_step(0) 

1 

sage: unit_step(x) 

unit_step(x) 

sage: latex(unit_step(x)) 

\mathrm{u}\left(x\right) 

 

TESTS:: 

 

sage: t = loads(dumps(unit_step(x)+1)); t 

unit_step(x) + 1 

sage: t.subs(x=0) 

2 

""" 

GinacFunction.__init__(self, "unit_step", latex_name=r"\mathrm{u}", 

conversions=dict(mathematica='UnitStep')) 

 

def _derivative_(self, x, diff_param=None): 

""" 

Derivative of unit step function 

 

EXAMPLES:: 

 

sage: unit_step(x).diff(x) 

dirac_delta(x) 

""" 

return dirac_delta(x) 

 

unit_step = FunctionUnitStep() 

 

class FunctionSignum(BuiltinFunction): 

r""" 

The signum or sgn function `\mathrm{sgn}(x)` (``sgn(x)``). 

 

INPUT: 

 

- ``x`` - a real number or a symbolic expression 

 

DEFINITION: 

 

The sgn function, `\mathrm{sgn}(x)` is defined as: 

 

`\mathrm{sgn}(x) = 1` for `x > 0`, 

`\mathrm{sgn}(x) = 0` for `x = 0` and 

`\mathrm{sgn}(x) = -1` for `x < 0` 

 

EXAMPLES:: 

 

sage: sgn(-1) 

-1 

sage: sgn(1) 

1 

sage: sgn(0) 

0 

sage: sgn(x) 

sgn(x) 

 

We can also use ``sign``:: 

 

sage: sign(1) 

1 

sage: sign(0) 

0 

sage: a = AA(-5).nth_root(7) 

sage: sign(a) 

-1 

 

TESTS: 

 

Check if conversion to sympy works :trac:`11921`:: 

 

sage: sgn(x)._sympy_() 

sign(x) 

 

REFERENCES: 

 

- :wikipedia:`Sign_function` 

 

""" 

def __init__(self): 

r""" 

The sgn function, ``sgn(x)``. 

 

EXAMPLES: 

 

sage: sgn(-1) 

-1 

sage: sgn(1) 

1 

sage: sgn(0) 

0 

sage: sgn(x) 

sgn(x) 

sage: sgn(x)._sympy_() 

sign(x) 

""" 

BuiltinFunction.__init__(self, "sgn", latex_name=r"\mathrm{sgn}", 

conversions=dict(maxima='signum',mathematica='Sign',sympy='sign'), 

alt_name="sign") 

 

def _eval_(self, x): 

""" 

 

EXAMPLES:: 

 

sage: sgn(-1) 

-1 

sage: sgn(1) 

1 

sage: sgn(0) 

0 

sage: sgn(x) 

sgn(x) 

sage: sgn(-exp(-10000000000000000000)) 

-1 

 

Evaluation test:: 

 

sage: sgn(x).subs(x=1) 

1 

sage: sgn(x).subs(x=0) 

0 

sage: sgn(x).subs(x=-1) 

-1 

 

More tests:: 

 

sage: sign(RR(2)) 

1 

sage: sign(RDF(2)) 

1 

sage: sign(AA(-2)) 

-1 

sage: sign(AA(0)) 

0 

""" 

try: 

return self._evalf_(x) 

except (TypeError,ValueError): # x is symbolic 

pass 

return None 

 

def _evalf_(self, x, **kwds): 

""" 

TESTS: 

 

Check that :trac:`16587` is fixed:: 

 

sage: M = sgn(3/2, hold=True); M 

sgn(3/2) 

sage: M.n() 

1 

sage: h(x) = sgn(x) 

sage: h(pi).numerical_approx() 

1.00000000000000 

""" 

if hasattr(x,'sign'): # First check if x has a sign method 

return x.sign() 

if hasattr(x,'sgn'): # or a sgn method 

return x.sgn() 

approx_x = ComplexIntervalField()(x) 

if bool(approx_x.imag() == 0): # x is real 

if bool(approx_x.real() == 0): # x is zero 

return ZZ(0) 

# Now we have a non-zero real 

if bool((approx_x**(0.5)).imag() == 0): # Check: x > 0 

return ZZ(1) 

else: 

return ZZ(-1) 

raise ValueError("Numeric evaluation of symbolic expression") 

 

def _derivative_(self, x, diff_param=None): 

""" 

Derivative of sgn function 

 

EXAMPLES:: 

 

sage: sgn(x).diff(x) 

2*dirac_delta(x) 

""" 

assert diff_param == 0 

return 2*dirac_delta(x) 

 

sgn = FunctionSignum() 

sign = sgn 

 

class FunctionKroneckerDelta(BuiltinFunction): 

r""" 

The Kronecker delta function `\delta_{m,n}` (``kronecker_delta(m, n)``). 

 

INPUT: 

 

- ``m`` - a number or a symbolic expression 

- ``n`` - a number or a symbolic expression 

 

DEFINITION: 

 

Kronecker delta function `\delta_{m,n}` is defined as: 

 

`\delta_{m,n} = 0` for `m \ne n` and 

`\delta_{m,n} = 1` for `m = n` 

 

EXAMPLES:: 

 

sage: kronecker_delta(1,2) 

0 

sage: kronecker_delta(1,1) 

1 

sage: m,n=var('m,n') 

sage: kronecker_delta(m,n) 

kronecker_delta(m, n) 

 

REFERENCES: 

 

- :wikipedia:`Kronecker_delta` 

 

""" 

def __init__(self): 

r""" 

The Kronecker delta function. 

 

EXAMPLES:: 

 

sage: kronecker_delta(1,2) 

0 

sage: kronecker_delta(1,1) 

1 

sage: y = var('y') 

sage: kronecker_delta(x, y)._sympy_() 

KroneckerDelta(x, y) 

""" 

BuiltinFunction.__init__(self, "kronecker_delta", nargs=2, 

conversions=dict(maxima='kron_delta', 

mathematica='KroneckerDelta', 

sympy='KroneckerDelta')) 

 

def _eval_(self, m, n): 

""" 

The Kronecker delta function. 

 

EXAMPLES:: 

 

sage: kronecker_delta(1,2) 

0 

sage: kronecker_delta(1,1) 

1 

 

Kronecker delta is a symmetric function. We keep arguments sorted to 

ensure that k_d(m, n) - k_d(n, m) cancels automatically:: 

 

sage: x,y=var('x,y') 

sage: kronecker_delta(x, y) 

kronecker_delta(x, y) 

sage: kronecker_delta(y, x) 

kronecker_delta(x, y) 

sage: kronecker_delta(x,2*x) 

kronecker_delta(2*x, x) 

 

Evaluation test:: 

 

sage: kronecker_delta(1,x).subs(x=1) 

1 

""" 

try: 

return self._evalf_(m,n) 

except (TypeError,ValueError): # x is symbolic 

pass 

return None 

 

def _evalf_(self, m, n, **kwds): 

""" 

TESTS:: 

 

sage: h(x) = kronecker_delta(3,x) 

sage: h(pi).numerical_approx() 

0.000000000000000 

""" 

if bool(repr(m) > repr(n)): 

return kronecker_delta(n, m) 

 

x = m - n 

approx_x = ComplexIntervalField()(x) 

if bool(approx_x.imag() == 0): # x is real 

if bool(approx_x.real() == 0): # x is zero 

return 1 

else: 

return 0 

else: 

return 0 # x is complex 

raise ValueError("Numeric evaluation of symbolic expression") 

 

def _derivative_(self, *args, **kwds): 

""" 

Derivative of Kronecker delta 

 

EXAMPLES:: 

 

sage: kronecker_delta(x,1).diff(x) 

0 

""" 

# Kronecker delta is non-zero (but finite) only in the set of 

# zero-measure unlike Dirac delta. Consequently, it is null 

# for the purpose of integration/differentiation. For *discrete sum* 

# Kronecker delta is however non-trivial. 

return 0 

 

def _print_latex_(self, m, n, **kwds): 

""" 

Return latex expression 

 

EXAMPLES:: 

 

sage: from sage.misc.latex import latex 

sage: m,n=var('m,n') 

sage: latex(kronecker_delta(m,n)) 

\delta_{m,n} 

 

""" 

from sage.misc.latex import latex 

return "\\delta_{%s,%s}"%(latex(m), latex(n)) 

 

kronecker_delta = FunctionKroneckerDelta()