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

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

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

r""" 

Piecewise-defined Functions 

 

This module implement piecewise functions in a single variable. See 

:mod:`sage.sets.real_set` for more information about how to construct 

subsets of the real line for the domains. 

 

EXAMPLES:: 

 

sage: f = piecewise([((0,1), x^3), ([-1,0], -x^2)]); f 

piecewise(x|-->x^3 on (0, 1), x|-->-x^2 on [-1, 0]; x) 

sage: 2*f 

2*piecewise(x|-->x^3 on (0, 1), x|-->-x^2 on [-1, 0]; x) 

sage: f(x=1/2) 

1/8 

sage: plot(f) # not tested 

 

TODO: 

 

- Implement max/min location and values, 

 

AUTHORS: 

 

- David Joyner (2006-04): initial version 

 

- David Joyner (2006-09): added __eq__, extend_by_zero_to, unextend, 

convolution, trapezoid, trapezoid_integral_approximation, 

riemann_sum, riemann_sum_integral_approximation, tangent_line fixed 

bugs in __mul__, __add__ 

 

- David Joyner (2007-03): adding Hann filter for FS, added general FS 

filter methods for computing and plotting, added options to plotting 

of FS (eg, specifying rgb values are now allowed). Fixed bug in 

documentation reported by Pablo De Napoli. 

 

- David Joyner (2007-09): bug fixes due to behaviour of 

SymbolicArithmetic 

 

- David Joyner (2008-04): fixed docstring bugs reported by J Morrow; added 

support for Laplace transform of functions with infinite support. 

 

- David Joyner (2008-07): fixed a left multiplication bug reported by 

C. Boncelet (by defining __rmul__ = __mul__). 

 

- Paul Butler (2009-01): added indefinite integration and default_variable 

 

- Volker Braun (2013): Complete rewrite 

 

- Ralf Stephan (2015): Rewrite of convolution() and other calculus 

functions; many doctest adaptations 

 

- Eric Gourgoulhon (2017): Improve documentation and user interface of 

Fourier series 

 

TESTS:: 

 

sage: fast_callable(f, vars=[x])(0.5) 

0.125000000000... 

""" 

 

#***************************************************************************** 

# Copyright (C) 2006 William Stein <wstein@gmail.com> 

# 2006 David Joyner <wdjoyner@gmail.com> 

# 2013 Volker Braun <vbraun.name@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

#***************************************************************************** 

 

from __future__ import absolute_import, division, print_function 

 

from sage.symbolic.function import BuiltinFunction 

from sage.sets.real_set import RealSet, InternalRealInterval 

from sage.symbolic.ring import SR 

from sage.rings.rational_field import QQ 

from sage.rings.infinity import minus_infinity, infinity 

 

from six import get_function_code 

 

 

class PiecewiseFunction(BuiltinFunction): 

def __init__(self): 

""" 

Piecewise function 

 

EXAMPLES:: 

 

sage: var('x, y') 

(x, y) 

sage: f = piecewise([((0,1), x^2*y), ([-1,0], -x*y^2)], var=x); f 

piecewise(x|-->x^2*y on (0, 1), x|-->-x*y^2 on [-1, 0]; x) 

sage: f(1/2) 

1/4*y 

sage: f(-1/2) 

1/2*y^2 

""" 

BuiltinFunction.__init__(self, "piecewise", 

latex_name="piecewise", 

conversions=dict(), nargs=2) 

 

def __call__(self, function_pieces, **kwds): 

r""" 

Piecewise functions 

 

INPUT: 

 

- ``function_pieces`` -- a list of pairs consisting of a 

domain and a symbolic function. 

 

- ``var=x`` -- a symbolic variable or ``None`` (default). The 

real variable in which the function is piecewise in. 

 

OUTPUT: 

 

A piecewise-defined function. A ``ValueError`` will be raised 

if the domains of the pieces are not pairwise disjoint. 

 

EXAMPLES:: 

 

sage: my_abs = piecewise([((-1, 0), -x), ([0, 1], x)], var=x); my_abs 

piecewise(x|-->-x on (-1, 0), x|-->x on [0, 1]; x) 

sage: [ my_abs(i/5) for i in range(-4, 5)] 

[4/5, 3/5, 2/5, 1/5, 0, 1/5, 2/5, 3/5, 4/5] 

 

TESTS:: 

 

sage: piecewise([([-1, 0], -x), ([0, 1], x)], var=x) 

Traceback (most recent call last): 

... 

ValueError: domains must be pairwise disjoint 

 

sage: step = piecewise([((-1, 0), -1), ([0, 0], 0), ((0, 1), 1)], var=x); step 

piecewise(x|-->-1 on (-1, 0), x|-->0 on {0}, x|-->1 on (0, 1); x) 

sage: step(-1/2), step(0), step(1/2) 

(-1, 0, 1) 

""" 

from types import FunctionType 

var = kwds.pop('var', None) 

parameters = [] 

domain_list = [] 

for piece in function_pieces: 

domain, function = piece 

if not isinstance(domain, RealSet): 

domain = RealSet(domain) 

if domain.is_empty(): 

continue 

if isinstance(function, FunctionType): 

if var is None: 

var = SR.var('x') 

if get_function_code(function).co_argcount == 0: 

function = function() 

else: 

function = function(var) 

function = SR(function) 

if var is None and len(function.variables()) > 0: 

var = function.variables()[0] 

parameters.append((domain, function)) 

domain_list.append(domain) 

if not RealSet.are_pairwise_disjoint(*domain_list): 

raise ValueError('domains must be pairwise disjoint') 

if var is None: 

var = self.default_variable() 

parameters = SR._force_pyobject(tuple(parameters), recursive=False) 

return BuiltinFunction.__call__(self, parameters, var, **kwds) 

 

def _print_(self, parameters, variable): 

""" 

Return a string representation 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: p = piecewise([((-2, 0), -x), ([0, 4], x)], var=x) 

sage: str(p) # indirect doctest 

'piecewise(x|-->-x on (-2, 0), x|-->x on [0, 4]; x)' 

""" 

s = 'piecewise(' 

args = [] 

for domain, func in parameters: 

args.append('{0}|-->{1} on {2}'.format(str(variable), str(func), str(domain))) 

s += ', '.join(args) + '; {0})'.format(str(variable)) 

return s 

 

def _subs_(self, subs_map, options, parameters, x): 

""" 

Callback from Pynac `subs()` 

 

EXAMPLES: 

 

If the substitution changes the piecewise variable, it must 

evaluate to a number so that we know which component we are 

on:: 

 

sage: p = piecewise([((-2, 0), -x), ([0, 4], x)], var=x) 

sage: p.subs(x=-1) 

1 

sage: (10+p).subs(x=-1) 

11 

sage: p.subs(x=pi) 

pi 

 

Auxiliary variables can be substituted arbitrarily:: 

 

sage: var('x,y') 

(x, y) 

sage: p = piecewise([((-2, 0), -x^y), ([0, 2], x-y)], var=x); p 

piecewise(x|-->-x^y on (-2, 0), x|-->x - y on [0, 2]; x) 

sage: p.subs(y=sin(y)) 

piecewise(x|-->-x^sin(y) on (-2, 0), x|-->x - sin(y) on [0, 2]; x) 

""" 

point = subs_map.apply_to(x, 0) 

if point == x: 

# substitution only in auxiliary variables 

new_params = [] 

for domain, func in parameters: 

new_params.append((domain, subs_map.apply_to(func, 0))) 

return piecewise(new_params, var=x) 

if ((point.is_numeric() or point.is_constant()) 

and (point.is_real())): 

if hasattr(point, 'pyobject'): 

# unwrap any numeric values 

point = point.pyobject() 

else: 

raise ValueError('substituting the piecewise variable must result in real number') 

 

for domain, func in parameters: 

if domain.contains(point): 

return subs_map.apply_to(func, 0) 

raise ValueError('point {} is not in the domain'.format(point)) 

 

@staticmethod 

def in_operands(ex): 

""" 

Return whether a symbolic expression contains a piecewise 

function as operand 

 

INPUT: 

 

- ``ex`` -- a symbolic expression. 

 

OUTPUT: 

 

Boolean 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]); f 

piecewise(x|-->sin(x) on {0}, x|-->cos(x) on (0, 2); x) 

sage: piecewise.in_operands(f) 

True 

sage: piecewise.in_operands(1+sin(f)) 

True 

sage: piecewise.in_operands(1+sin(0*f)) 

False 

""" 

def is_piecewise(ex): 

result = ex.operator() is piecewise 

for op in ex.operands(): 

result = result or is_piecewise(op) 

return result 

return is_piecewise(ex) 

 

 

@staticmethod 

def simplify(ex): 

""" 

Combine piecewise operands into single piecewise function 

 

OUTPUT: 

 

A piecewise function whose operands are not piecewiese if 

possible, that is, as long as the piecewise variable is the same. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]) 

sage: piecewise.simplify(f) 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

 

class EvaluationMethods(object): 

 

def expression_at(self, parameters, variable, point): 

""" 

Return the expression defining the piecewise function at 

``value`` 

 

INPUT: 

 

- ``point`` -- a real number. 

 

OUTPUT: 

 

The symbolic expression defining the function value at the 

given ``point``. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]); f 

piecewise(x|-->sin(x) on {0}, x|-->cos(x) on (0, 2); x) 

sage: f.expression_at(0) 

sin(x) 

sage: f.expression_at(1) 

cos(x) 

sage: f.expression_at(2) 

Traceback (most recent call last): 

... 

ValueError: point is not in the domain 

""" 

for domain, func in parameters: 

if domain.contains(point): 

return func 

raise ValueError('point is not in the domain') 

 

which_function = expression_at 

 

def domains(self, parameters, variable): 

""" 

Return the individual domains 

 

See also :meth:`~expressions`. 

 

OUTPUT: 

 

The collection of domains of the component functions as a 

tuple of :class:`~sage.sets.real_set.RealSet`. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]); f 

piecewise(x|-->sin(x) on {0}, x|-->cos(x) on (0, 2); x) 

sage: f.domains() 

({0}, (0, 2)) 

""" 

return tuple(dom for dom, fun in parameters) 

 

def domain(self, parameters, variable): 

""" 

Return the domain 

 

OUTPUT: 

 

The union of the domains of the individual pieces as a 

:class:`~sage.sets.real_set.RealSet`. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]); f 

piecewise(x|-->sin(x) on {0}, x|-->cos(x) on (0, 2); x) 

sage: f.domain() 

[0, 2) 

""" 

intervals = [] 

for domain, func in parameters: 

intervals += list(domain) 

return RealSet(*intervals) 

 

def __len__(self, parameters, variable): 

""" 

Return the number of "pieces" 

 

OUTPUT: 

 

Integer. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]); f 

piecewise(x|-->sin(x) on {0}, x|-->cos(x) on (0, 2); x) 

sage: len(f) 

2 

""" 

return len(parameters) 

 

def expressions(self, parameters, variable): 

""" 

Return the individual domains 

 

See also :meth:`~domains`. 

 

OUTPUT: 

 

The collection of expressions of the component functions. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]); f 

piecewise(x|-->sin(x) on {0}, x|-->cos(x) on (0, 2); x) 

sage: f.expressions() 

(sin(x), cos(x)) 

""" 

return tuple(fun for dom, fun in parameters) 

 

def items(self, parameters, variable): 

""" 

Iterate over the pieces of the piecewise function 

 

.. NOTE:: 

 

You should probably use :meth:`pieces` instead, which 

offers a nicer interface. 

 

OUTPUT: 

 

This method iterates over pieces of the piecewise 

function, each represented by a pair. The first element is 

the support, and the second the function over that 

support. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]) 

sage: for support, function in f.items(): 

....: print('support is {0}, function is {1}'.format(support, function)) 

support is {0}, function is sin(x) 

support is (0, 2), function is cos(x) 

""" 

for pair in parameters: 

yield pair 

 

def __call__(self, parameters, variable, value=None, **kwds): 

""" 

Call the piecewise function 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]); f 

piecewise(x|-->sin(x) on {0}, x|-->cos(x) on (0, 2); x) 

sage: f(0) 

0 

sage: f(1) 

cos(1) 

sage: f(2) 

Traceback (most recent call last): 

... 

ValueError: point 2 is not in the domain 

""" 

self = piecewise(parameters, var=variable) 

substitution = dict() 

for k, v in kwds.items(): 

substitution[SR.var(k)] = v 

if value is not None: 

substitution[variable] = value 

return self.subs(substitution) 

 

def _fast_float_(self, *args): 

""" 

Do not support the old ``fast_float`` 

 

OUTPUT: 

 

This method raises ``NotImplementedError`` so that 

plotting uses the newer `fast_callable` implementation. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]) 

sage: f._fast_float_() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

def _fast_callable_(self, parameters, variable, etb): 

""" 

Override the ``fast_callable`` 

 

OUTPUT: 

 

A :class:`~sage.ext.fast_callable.ExpressionCall` 

representing the piecewise function in the expression 

tree. 

 

EXAMPLES:: 

 

sage: p = piecewise([((-1, 0), -x), ([0, 1], x)], var=x) 

sage: from sage.ext.fast_callable import ExpressionTreeBuilder 

sage: etb = ExpressionTreeBuilder(vars=['x']) 

sage: p._fast_callable_(etb) 

{piecewise(x|-->-x on (-1, 0), x|-->x on [0, 1]; x)}(v_0) 

""" 

self = piecewise(parameters, var=variable) 

return etb.call(self, variable) 

 

def restriction(self, parameters, variable, restricted_domain): 

""" 

Restrict the domain 

 

INPUT: 

 

- ``restricted_domain`` -- a 

:class:`~sage.sets.real_set.RealSet` or something that 

defines one. 

 

OUTPUT: 

 

A new piecewise function obtained by restricting the domain. 

 

EXAMPLES:: 

 

sage: f = piecewise([((-oo, oo), x)]); f 

piecewise(x|-->x on (-oo, +oo); x) 

sage: f.restriction([[-1,1], [3,3]]) 

piecewise(x|-->x on [-1, 1] + {3}; x) 

""" 

restricted_domain = RealSet(*restricted_domain) 

new_param = [] 

for domain, func in parameters: 

domain = domain.intersection(restricted_domain) 

new_param.append((domain, func)) 

return piecewise(new_param, var=variable) 

 

def extension(self, parameters, variable, extension, extension_domain=None): 

""" 

Extend the function 

 

INPUT: 

 

- ``extension`` -- a symbolic expression 

 

- ``extension_domain`` -- a 

:class:`~sage.sets.real_set.RealSet` or ``None`` 

(default). The domain of the extension. By default, the 

entire complement of the current domain. 

 

EXAMPLES:: 

 

sage: f = piecewise([((-1,1), x)]); f 

piecewise(x|-->x on (-1, 1); x) 

sage: f(3) 

Traceback (most recent call last): 

... 

ValueError: point 3 is not in the domain 

 

sage: g = f.extension(0); g 

piecewise(x|-->x on (-1, 1), x|-->0 on (-oo, -1] + [1, +oo); x) 

sage: g(3) 

0 

 

sage: h = f.extension(1, RealSet.unbounded_above_closed(1)); h 

piecewise(x|-->x on (-1, 1), x|-->1 on [1, +oo); x) 

sage: h(3) 

1 

""" 

self = piecewise(parameters, var=variable) 

if extension_domain is None: 

extension_domain = self.domain().complement() 

ext = ((extension_domain, SR(extension)),) 

return piecewise(parameters + ext, var=variable) 

 

def unextend_zero(self, parameters, variable): 

""" 

Remove zero pieces. 

 

EXAMPLES:: 

 

sage: f = piecewise([((-1,1), x)]); f 

piecewise(x|-->x on (-1, 1); x) 

sage: g = f.extension(0); g 

piecewise(x|-->x on (-1, 1), x|-->0 on (-oo, -1] + [1, +oo); x) 

sage: g(3) 

0 

sage: h = g.unextend_zero() 

sage: bool(h == f) 

True 

""" 

result = [(domain, func) for domain,func in parameters 

if func != 0] 

return piecewise(result, var=variable) 

 

def pieces(self, parameters, variable): 

""" 

Return the "pieces". 

 

OUTPUT: 

 

A tuple of piecewise functions, each having only a single 

expression. 

 

EXAMPLES:: 

 

sage: p = piecewise([((-1, 0), -x), ([0, 1], x)], var=x) 

sage: p.pieces() 

(piecewise(x|-->-x on (-1, 0); x), 

piecewise(x|-->x on [0, 1]; x)) 

""" 

result = [] 

for domain, func in parameters: 

result.append(piecewise([(domain, func)], var=variable)) 

return tuple(result) 

 

def end_points(self, parameters, variable): 

""" 

Return a list of all interval endpoints for this function. 

 

EXAMPLES:: 

 

sage: f1(x) = 1 

sage: f2(x) = 1-x 

sage: f3(x) = x^2-5 

sage: f = piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3]]) 

sage: f.end_points() 

[0, 1, 2, 3] 

sage: f = piecewise([([0,0], sin(x)), ((0,2), cos(x))]); f 

piecewise(x|-->sin(x) on {0}, x|-->cos(x) on (0, 2); x) 

sage: f.end_points() 

[0, 2] 

""" 

s = set() 

for domain, func in parameters: 

for interval in domain: 

s.add(interval.lower()) 

s.add(interval.upper()) 

s.discard(minus_infinity) 

s.discard(infinity) 

return sorted(s) 

 

def piecewise_add(self, parameters, variable, other): 

""" 

Return a new piecewise function with domain the union 

of the original domains and functions summed. Undefined 

intervals in the union domain get function value `0`. 

 

EXAMPLES:: 

 

sage: f = piecewise([([0,1], 1), ((2,3), x)]) 

sage: g = piecewise([((1/2, 2), x)]) 

sage: f.piecewise_add(g).unextend_zero() 

piecewise(x|-->1 on (0, 1/2], x|-->x + 1 on (1/2, 1], x|-->x on (1, 2) + (2, 3); x) 

""" 

points = ([minus_infinity] + 

sorted(set(self.end_points() + other.end_points())) + 

[infinity]) 

domain = [] 

funcs = [] 

contains_lower = False 

contains_upper = False 

for i in range(len(points)-1): 

try: 

contains_lower = (self.domain().contains(points[i]) or 

other.domain().contains(points[i])) and not contains_upper 

contains_upper = (self.domain().contains(points[i+1]) or 

other.domain().contains(points[i+1])) 

if contains_lower: 

if contains_upper: 

rs = RealSet.closed(points[i],points[i+1]) 

else: 

rs = RealSet.closed_open(points[i],points[i+1]) 

else: 

if contains_upper: 

rs = RealSet.open_closed(points[i],points[i+1]) 

else: 

rs = RealSet.open(points[i],points[i+1]) 

point = (points[i+1] + points[i])/2 

except ValueError: 

if points[i] == minus_infinity and points[i+1] == infinity: 

rs = RealSet.open(minus_infinity, infinity) 

point = 0 

elif points[i] == minus_infinity: 

if contains_lower: 

rs = RealSet.unbounded_below_closed(points[i+1]) 

else: 

rs = RealSet.unbounded_below_open(points[i+1]) 

point = points[i+1]-1 

elif points[i+1] == infinity: 

if contains_upper: 

rs = RealSet.unbounded_above_closed(points[i]) 

else: 

rs = RealSet.unbounded_above_open(points[i]) 

point = points[i]+1 

else: 

raise 

try: 

ex1 = self.expression_at(point) 

except ValueError: 

ex1 = 0 

try: 

ex2 = other.expression_at(point) 

except ValueError: 

ex2 = 0 

ex = ex1 + ex2 

if i>0 and funcs[-1] == ex: 

# extend the previous domain 

rs += domain[-1] 

domain[-1] = rs 

else: 

domain += rs 

funcs.append(ex) 

return piecewise(zip(domain, funcs)) 

 

def integral(self, parameters, variable, x=None, a=None, b=None, definite=False): 

r""" 

By default, return the indefinite integral of the function. 

If definite=True is given, returns the definite integral. 

 

AUTHOR: 

 

- Paul Butler 

 

EXAMPLES:: 

 

sage: f1(x) = 1-x 

sage: f = piecewise([((0,1),1), ((1,2),f1)]) 

sage: f.integral(definite=True) 

1/2 

 

:: 

 

sage: f1(x) = -1 

sage: f2(x) = 2 

sage: f = piecewise([((0,pi/2),f1), ((pi/2,pi),f2)]) 

sage: f.integral(definite=True) 

1/2*pi 

 

sage: f1(x) = 2 

sage: f2(x) = 3 - x 

sage: f = piecewise([[(-2, 0), f1], [(0, 3), f2]]) 

sage: f.integral() 

piecewise(x|-->2*x + 4 on (-2, 0), x|-->-1/2*x^2 + 3*x + 4 on (0, 3); x) 

 

sage: f1(y) = -1 

sage: f2(y) = y + 3 

sage: f3(y) = -y - 1 

sage: f4(y) = y^2 - 1 

sage: f5(y) = 3 

sage: f = piecewise([[[-4,-3],f1],[(-3,-2),f2],[[-2,0],f3],[(0,2),f4],[[2,3],f5]]) 

sage: F = f.integral(y) 

sage: F 

piecewise(y|-->-y - 4 on [-4, -3], y|-->1/2*y^2 + 3*y + 7/2 on (-3, -2), y|-->-1/2*y^2 - y - 1/2 on [-2, 0], y|-->1/3*y^3 - y - 1/2 on (0, 2), y|-->3*y - 35/6 on [2, 3]; y) 

 

Ensure results are consistent with FTC:: 

 

sage: F(-3) - F(-4) 

-1 

sage: F(-1) - F(-3) 

1 

sage: F(2) - F(0) 

2/3 

sage: f.integral(y, 0, 2) 

2/3 

sage: F(3) - F(-4) 

19/6 

sage: f.integral(y, -4, 3) 

19/6 

sage: f.integral(definite=True) 

19/6 

 

:: 

 

sage: f1(y) = (y+3)^2 

sage: f2(y) = y+3 

sage: f3(y) = 3 

sage: f = piecewise([[(-infinity, -3), f1], [(-3, 0), f2], [(0, infinity), f3]]) 

sage: f.integral() 

piecewise(y|-->1/3*y^3 + 3*y^2 + 9*y + 9 on (-oo, -3), y|-->1/2*y^2 + 3*y + 9/2 on (-3, 0), y|-->3*y + 9/2 on (0, +oo); y) 

 

:: 

 

sage: f1(x) = e^(-abs(x)) 

sage: f = piecewise([[(-infinity, infinity), f1]]) 

sage: f.integral(definite=True) 

2 

sage: f.integral() 

piecewise(x|-->-1/2*((sgn(x) - 1)*e^(2*x) - 2*e^x*sgn(x) + sgn(x) + 1)*e^(-x) - 1 on (-oo, +oo); x) 

 

:: 

 

sage: f = piecewise([((0, 5), cos(x))]) 

sage: f.integral() 

piecewise(x|-->sin(x) on (0, 5); x) 

 

 

TESTS: 

 

Verify that piecewise integrals of zero work (:trac:`10841`):: 

 

sage: f0(x) = 0 

sage: f = piecewise([[[0,1],f0]]) 

sage: f.integral(x,0,1) 

0 

sage: f = piecewise([[[0,1], 0]]) 

sage: f.integral(x,0,1) 

0 

sage: f = piecewise([[[0,1], SR(0)]]) 

sage: f.integral(x,0,1) 

0 

 

""" 

if a is not None and b is not None: 

F = self.integral(x) 

return F(b) - F(a) 

 

if a is not None or b is not None: 

raise TypeError('only one endpoint given') 

 

area = 0 

new_pieces = [] 

 

if x is None: 

x = self.default_variable() 

 

# The integral is computed by iterating over the pieces in order. 

# The definite integral for each piece is calculated and accumulated in `area`. 

# The indefinite integral of each piece is also calculated, 

# and the `area` before each piece is added to the piece. 

# 

# If a definite integral is requested, `area` is returned. 

# Otherwise, a piecewise function is constructed from the indefinite integrals 

# and returned. 

# 

# An exception is made if integral is called on a piecewise function 

# that starts at -infinity. In this case, we do not try to calculate the 

# definite integral of the first piece, and the value of `area` remains 0 

# after the first piece. 

 

from sage.symbolic.assumptions import assume, forget 

for domain, fun in parameters: 

for interval in domain: 

start = interval.lower() 

end = interval.upper() 

if start == -infinity and not definite: 

fun_integrated = fun.integral(x, end, x) 

else: 

try: 

assume(start < x) 

except ValueError: # Assumption is redundant 

pass 

fun_integrated = fun.integral(x, start, x) + area 

forget(start < x) 

if definite or end != infinity: 

area += fun.integral(x, start, end) 

new_pieces.append([interval, SR(fun_integrated).function(x)]) 

 

if definite: 

return SR(area) 

else: 

return piecewise(new_pieces) 

 

def critical_points(self, parameters, variable): 

""" 

Return the critical points of this piecewise function. 

 

EXAMPLES:: 

 

sage: R.<x> = QQ[] 

sage: f1 = x^0 

sage: f2 = 10*x - x^2 

sage: f3 = 3*x^4 - 156*x^3 + 3036*x^2 - 26208*x 

sage: f = piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]]) 

sage: expected = [5, 12, 13, 14] 

sage: all(abs(e-a) < 0.001 for e,a in zip(expected, f.critical_points())) 

True 

 

TESTS: 

 

Use variables other than x (:trac:`13836`):: 

 

sage: R.<y> = QQ[] 

sage: f1 = y^0 

sage: f2 = 10*y - y^2 

sage: f3 = 3*y^4 - 156*y^3 + 3036*y^2 - 26208*y 

sage: f = piecewise([[(0,3),f1],[(3,10),f2],[(10,20),f3]]) 

sage: expected = [5, 12, 13, 14] 

sage: all(abs(e-a) < 0.001 for e,a in zip(expected, f.critical_points())) 

True 

""" 

from sage.calculus.calculus import maxima 

x = self.default_variable() 

crit_pts = [] 

for domain, f in parameters: 

for interval in domain: 

a = interval.lower() 

b = interval.upper() 

for root in maxima.allroots(SR(f).diff(x)==0): 

root = float(root.rhs()) 

if a < root < b: 

crit_pts.append(root) 

return crit_pts 

 

def convolution(self, parameters, variable, other): 

""" 

Return the convolution function, 

`f*g(t)=\int_{-\infty}^\infty f(u)g(t-u)du`, for compactly 

supported `f,g`. 

 

EXAMPLES:: 

 

sage: x = PolynomialRing(QQ,'x').gen() 

sage: f = piecewise([[[0,1],1]]) ## example 0 

sage: g = f.convolution(f); g 

piecewise(x|-->x on (0, 1], x|-->-x + 2 on (1, 2]; x) 

sage: h = f.convolution(g); h 

piecewise(x|-->1/2*x^2 on (0, 1], x|-->-x^2 + 3*x - 3/2 on (1, 2], x|-->1/2*x^2 - 3*x + 9/2 on (2, 3]; x) 

sage: f = piecewise([[(0,1),1],[(1,2),2],[(2,3),1]]) ## example 1 

sage: g = f.convolution(f) 

sage: h = f.convolution(g); h 

piecewise(x|-->1/2*x^2 on (0, 1], x|-->2*x^2 - 3*x + 3/2 on (1, 3], x|-->-2*x^2 + 21*x - 69/2 on (3, 4], x|-->-5*x^2 + 45*x - 165/2 on (4, 5], x|-->-2*x^2 + 15*x - 15/2 on (5, 6], x|-->2*x^2 - 33*x + 273/2 on (6, 8], x|-->1/2*x^2 - 9*x + 81/2 on (8, 9]; x) 

sage: f = piecewise([[(-1,1),1]]) ## example 2 

sage: g = piecewise([[(0,3),x]]) 

sage: f.convolution(g) 

piecewise(x|-->1/2*x^2 + x + 1/2 on (-1, 1], x|-->2*x on (1, 2], x|-->-1/2*x^2 + x + 4 on (2, 4]; x) 

sage: g = piecewise([[(0,3),1],[(3,4),2]]) 

sage: f.convolution(g) 

piecewise(x|-->x + 1 on (-1, 1], x|-->2 on (1, 2], x|-->x on (2, 3], x|-->-x + 6 on (3, 4], x|-->-2*x + 10 on (4, 5]; x) 

 

Check that the bugs raised in :trac:`12123` are fixed:: 

 

sage: f = piecewise([[(-2, 2), 2]]) 

sage: g = piecewise([[(0, 2), 3/4]]) 

sage: f.convolution(g) 

piecewise(x|-->3/2*x + 3 on (-2, 0], x|-->3 on (0, 2], x|-->-3/2*x + 6 on (2, 4]; x) 

sage: f = piecewise([[(-1, 1), 1]]) 

sage: g = piecewise([[(0, 1), x], [(1, 2), -x + 2]]) 

sage: f.convolution(g) 

piecewise(x|-->1/2*x^2 + x + 1/2 on (-1, 0], x|-->-1/2*x^2 + x + 1/2 on (0, 2], x|-->1/2*x^2 - 3*x + 9/2 on (2, 3]; x) 

""" 

from sage.symbolic.integration.integral import definite_integral 

f = self 

g = other 

if len(f.end_points())*len(g.end_points()) == 0: 

raise ValueError('one of the piecewise functions is nowhere defined') 

M = min(min(f.end_points()),min(g.end_points())) 

N = max(max(f.end_points()),max(g.end_points())) 

tt = SR.var('tt') 

uu = SR.var('uu') 

conv = 0 

fd,f0 = parameters[0] 

gd,g0 = next(other.items()) 

if len(f)==1 and len(g)==1: 

f = f.unextend_zero() 

g = g.unextend_zero() 

a1 = fd[0].lower() 

a2 = fd[0].upper() 

b1 = gd[0].lower() 

b2 = gd[0].upper() 

i1 = f0.subs({variable: uu}) 

i2 = g0.subs({variable: tt-uu}) 

fg1 = definite_integral(i1*i2, uu, a1, tt-b1).subs(tt = variable) 

fg2 = definite_integral(i1*i2, uu, tt-b2, tt-b1).subs(tt = variable) 

fg3 = definite_integral(i1*i2, uu, tt-b2, a2).subs(tt = variable) 

fg4 = definite_integral(i1*i2, uu, a1, a2).subs(tt = variable) 

if a1-b1<a2-b2: 

if a2+b1!=a1+b2: 

h = piecewise([[(a1+b1,a1+b2),fg1],[(a1+b2,a2+b1),fg2],[(a2+b1,a2+b2),fg3]]) 

else: 

h = piecewise([[(a1+b1,a1+b2),fg1],[(a1+b2,a2+b2),fg3]]) 

else: 

if a1+b2!=a2+b1: 

h = piecewise([[(a1+b1,a2+b1),fg1],[(a2+b1,a1+b2),fg4],[(a1+b2,a2+b2),fg3]]) 

else: 

h = piecewise([[(a1+b1,a2+b1),fg1],[(a2+b1,a2+b2),fg3]]) 

return (piecewise([[(minus_infinity,infinity),0]]).piecewise_add(h)).unextend_zero() 

 

if len(f)>1 or len(g)>1: 

z = piecewise([[(0,0),0]]) 

for fpiece in f.pieces(): 

for gpiece in g.pieces(): 

h = gpiece.convolution(fpiece) 

z = z.piecewise_add(h) 

return z.unextend_zero() 

 

def trapezoid(self, parameters, variable, N): 

""" 

Return the piecewise line function defined by the trapezoid rule 

for numerical integration based on a subdivision of each domain 

interval into N subintervals. 

 

EXAMPLES:: 

 

sage: f = piecewise([[[0,1], x^2], [RealSet.open_closed(1,2), 5-x^2]]) 

sage: f.trapezoid(2) 

piecewise(x|-->1/2*x on (0, 1/2), x|-->3/2*x - 1/2 on (1/2, 1), x|-->7/2*x - 5/2 on (1, 3/2), x|-->-7/2*x + 8 on (3/2, 2); x) 

sage: f = piecewise([[[-1,1], 1-x^2]]) 

sage: f.trapezoid(4).integral(definite=True) 

5/4 

sage: f = piecewise([[[-1,1], 1/2+x-x^3]]) ## example 3 

sage: f.trapezoid(6).integral(definite=True) 

1 

 

TESTS: 

 

Use variables or rings other than x (:trac:`13836`):: 

 

sage: R.<y> = QQ[] 

sage: f1 = y^2 

sage: f2 = 5-y^2 

sage: f = piecewise([[[0,1],f1], [RealSet.open_closed(1,2),f2]]) 

sage: f.trapezoid(2) 

piecewise(y|-->1/2*y on (0, 1/2), y|-->3/2*y - 1/2 on (1/2, 1), y|-->7/2*y - 5/2 on (1, 3/2), y|-->-7/2*y + 8 on (3/2, 2); y) 

""" 

def func(x0, x1): 

f0, f1 = self(x0), self(x1) 

return [[(x0,x1), f0 + (f1-f0) * (x1-x0)**(-1) 

* (self.default_variable()-x0)]] 

rsum = [] 

for domain, f in parameters: 

for interval in domain: 

a = interval.lower() 

b = interval.upper() 

h = (b-a)/N 

for i in range(N): 

x0 = a+i*h 

x1 = a+(i+1)*h 

rsum += func(x0, x1) 

return piecewise(rsum) 

 

def laplace(self, parameters, variable, x='x', s='t'): 

r""" 

Returns the Laplace transform of self with respect to the variable 

var. 

 

INPUT: 

 

- ``x`` - variable of self 

 

- ``s`` - variable of Laplace transform. 

 

We assume that a piecewise function is 0 outside of its domain and 

that the left-most endpoint of the domain is 0. 

 

EXAMPLES:: 

 

sage: x, s, w = var('x, s, w') 

sage: f = piecewise([[(0,1),1],[[1,2], 1-x]]) 

sage: f.laplace(x, s) 

-e^(-s)/s + (s + 1)*e^(-2*s)/s^2 + 1/s - e^(-s)/s^2 

sage: f.laplace(x, w) 

-e^(-w)/w + (w + 1)*e^(-2*w)/w^2 + 1/w - e^(-w)/w^2 

 

:: 

 

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

sage: f = piecewise([[[1,2], 1-y]]) 

sage: f.laplace(y, t) 

(t + 1)*e^(-2*t)/t^2 - e^(-t)/t^2 

 

:: 

 

sage: s = var('s') 

sage: t = var('t') 

sage: f1(t) = -t 

sage: f2(t) = 2 

sage: f = piecewise([[[0,1],f1],[(1,infinity),f2]]) 

sage: f.laplace(t,s) 

(s + 1)*e^(-s)/s^2 + 2*e^(-s)/s - 1/s^2 

""" 

from sage.all import assume, exp, forget 

x = SR.var(x) 

s = SR.var(s) 

assume(s>0) 

result = 0 

for domain, f in parameters: 

for interval in domain: 

a = interval.lower() 

b = interval.upper() 

result += (SR(f)*exp(-s*x)).integral(x,a,b) 

forget(s>0) 

return result 

 

def fourier_series_cosine_coefficient(self, parameters, 

variable, n, L=None): 

r""" 

Return the `n`-th cosine coefficient of the Fourier series of 

the periodic function `f` extending the piecewise-defined 

function ``self``. 

 

Given an integer `n\geq 0`, the `n`-th cosine coefficient of 

the Fourier series of `f` is defined by 

 

.. MATH:: 

 

a_n = \frac{1}{L}\int_{-L}^L 

f(x)\cos\left(\frac{n\pi x}{L}\right) dx, 

 

where `L` is the half-period of `f`. For `n\geq 1`, `a_n` is 

the coefficient of `\cos(n\pi x/L)` in the Fourier series of 

`f`, while `a_0` is twice the coefficient of the constant 

term `\cos(0 x)`, i.e. twice the mean value of `f` over one 

period (cf. :meth:`fourier_series_partial_sum`). 

 

INPUT: 

 

- ``n`` -- a non-negative integer 

 

- ``L`` -- (default: ``None``) the half-period of `f`; if none 

is provided, `L` is assumed to be the half-width of the domain 

of ``self`` 

 

OUTPUT: 

 

- the Fourier coefficient `a_n`, as defined above 

 

EXAMPLES: 

 

A triangle wave function of period 2:: 

 

sage: f = piecewise([((0,1), x), ((1,2), 2-x)]) 

sage: f.fourier_series_cosine_coefficient(0) 

1 

sage: f.fourier_series_cosine_coefficient(3) 

-4/9/pi^2 

 

If the domain of the piecewise-defined function encompasses 

more than one period, the half-period must be passed as the 

second argument; for instance:: 

 

sage: f2 = piecewise([((0,1), x), ((1,2), 2-x), 

....: ((2,3), x-2), ((3,4), 2-(x-2))]) 

sage: bool(f2.restriction((0,2)) == f) # f2 extends f on (0,4) 

True 

sage: f2.fourier_series_cosine_coefficient(3, 1) # half-period = 1 

-4/9/pi^2 

 

The default half-period is 2 and one has:: 

 

sage: f2.fourier_series_cosine_coefficient(3) # half-period = 2 

0 

 

The Fourier coefficient `-4/(9\pi^2)` obtained above is actually 

recovered for `n=6`:: 

 

sage: f2.fourier_series_cosine_coefficient(6) 

-4/9/pi^2 

 

Other examples:: 

 

sage: f(x) = x^2 

sage: f = piecewise([[(-1,1),f]]) 

sage: f.fourier_series_cosine_coefficient(2) 

pi^(-2) 

sage: f1(x) = -1 

sage: f2(x) = 2 

sage: f = piecewise([[(-pi,pi/2),f1],[(pi/2,pi),f2]]) 

sage: f.fourier_series_cosine_coefficient(5,pi) 

-3/5/pi 

 

""" 

from sage.all import cos, pi 

L0 = (self.domain().sup() - self.domain().inf()) / 2 

if not L: 

L = L0 

else: 

m = L0 / L 

if not (m.is_integer() and m > 0): 

raise ValueError("the width of the domain of " + 

"{} is not a multiple ".format(self) + 

"of the given period") 

x = SR.var('x') 

result = 0 

for domain, f in parameters: 

for interval in domain: 

a = interval.lower() 

b = interval.upper() 

result += (f*cos(pi*x*n/L)).integrate(x, a, b) 

return SR(result/L0).simplify_trig() 

 

def fourier_series_sine_coefficient(self, parameters, variable, 

n, L=None): 

r""" 

Return the `n`-th sine coefficient of the Fourier series of 

the periodic function `f` extending the piecewise-defined 

function ``self``. 

 

Given an integer `n\geq 0`, the `n`-th sine coefficient of 

the Fourier series of `f` is defined by 

 

.. MATH:: 

 

b_n = \frac{1}{L}\int_{-L}^L 

f(x)\sin\left(\frac{n\pi x}{L}\right) dx, 

 

where `L` is the half-period of `f`. The number `b_n` is 

the coefficient of `\sin(n\pi x/L)` in the Fourier 

series of `f` (cf. :meth:`fourier_series_partial_sum`). 

 

INPUT: 

 

- ``n`` -- a non-negative integer 

 

- ``L`` -- (default: ``None``) the half-period of `f`; if none 

is provided, `L` is assumed to be the half-width of the domain 

of ``self`` 

 

OUTPUT: 

 

- the Fourier coefficient `b_n`, as defined above 

 

EXAMPLES: 

 

A square wave function of period 2:: 

 

sage: f = piecewise([((-1,0), -1), ((0,1), 1)]) 

sage: f.fourier_series_sine_coefficient(1) 

4/pi 

sage: f.fourier_series_sine_coefficient(2) 

0 

sage: f.fourier_series_sine_coefficient(3) 

4/3/pi 

 

If the domain of the piecewise-defined function encompasses 

more than one period, the half-period must be passed as the 

second argument; for instance:: 

 

sage: f2 = piecewise([((-1,0), -1), ((0,1), 1), 

....: ((1,2), -1), ((2,3), 1)]) 

sage: bool(f2.restriction((-1,1)) == f) # f2 extends f on (-1,3) 

True 

sage: f2.fourier_series_sine_coefficient(1, 1) # half-period = 1 

4/pi 

sage: f2.fourier_series_sine_coefficient(3, 1) # half-period = 1 

4/3/pi 

 

The default half-period is 2 and one has:: 

 

sage: f2.fourier_series_sine_coefficient(1) # half-period = 2 

0 

sage: f2.fourier_series_sine_coefficient(3) # half-period = 2 

0 

 

The Fourier coefficients obtained from ``f`` are actually 

recovered for `n=2` and `n=6` respectively:: 

 

sage: f2.fourier_series_sine_coefficient(2) 

4/pi 

sage: f2.fourier_series_sine_coefficient(6) 

4/3/pi 

 

""" 

from sage.all import sin, pi 

L0 = (self.domain().sup() - self.domain().inf()) / 2 

if not L: 

L = L0 

else: 

m = L0 / L 

if not (m.is_integer() and m > 0): 

raise ValueError("the width of the domain of " + 

"{} is not a multiple ".format(self) + 

"of the given period") 

x = SR.var('x') 

result = 0 

for domain, f in parameters: 

for interval in domain: 

a = interval.lower() 

b = interval.upper() 

result += (f*sin(pi*x*n/L)).integrate(x, a, b) 

return SR(result/L0).simplify_trig() 

 

def fourier_series_partial_sum(self, parameters, variable, N, 

L=None): 

r""" 

Returns the partial sum up to a given order of the Fourier series 

of the periodic function `f` extending the piecewise-defined 

function ``self``. 

 

The Fourier partial sum of order `N` is defined as 

 

.. MATH:: 

 

S_{N}(x) = \frac{a_0}{2} + \sum_{n=1}^{N} \left[ 

a_n\cos\left(\frac{n\pi x}{L}\right) 

+ b_n\sin\left(\frac{n\pi x}{L}\right)\right], 

 

where `L` is the half-period of `f` and the `a_n`'s and `b_n`'s 

are respectively the cosine coefficients and sine coefficients 

of the Fourier series of `f` (cf. 

:meth:`fourier_series_cosine_coefficient` and 

:meth:`fourier_series_sine_coefficient`). 

 

INPUT: 

 

- ``N`` -- a positive integer; the order of the partial sum 

 

- ``L`` -- (default: ``None``) the half-period of `f`; if none 

is provided, `L` is assumed to be the half-width of the domain 

of ``self`` 

 

OUTPUT: 

 

- the partial sum `S_{N}(x)`, as a symbolic expression 

 

EXAMPLES: 

 

A square wave function of period 2:: 

 

sage: f = piecewise([((-1,0), -1), ((0,1), 1)]) 

sage: f.fourier_series_partial_sum(5) 

4/5*sin(5*pi*x)/pi + 4/3*sin(3*pi*x)/pi + 4*sin(pi*x)/pi 

 

If the domain of the piecewise-defined function encompasses 

more than one period, the half-period must be passed as the 

second argument; for instance:: 

 

sage: f2 = piecewise([((-1,0), -1), ((0,1), 1), 

....: ((1,2), -1), ((2,3), 1)]) 

sage: bool(f2.restriction((-1,1)) == f) # f2 extends f on (-1,3) 

True 

sage: f2.fourier_series_partial_sum(5, 1) # half-period = 1 

4/5*sin(5*pi*x)/pi + 4/3*sin(3*pi*x)/pi + 4*sin(pi*x)/pi 

sage: bool(f2.fourier_series_partial_sum(5, 1) == 

....: f.fourier_series_partial_sum(5)) 

True 

 

The default half-period is 2, so that skipping the second 

argument yields a different result:: 

 

sage: f2.fourier_series_partial_sum(5) # half-period = 2 

4*sin(pi*x)/pi 

 

An example of partial sum involving both cosine and sine terms:: 

 

sage: f = piecewise([((-1,0), 0), ((0,1/2), 2*x), 

....: ((1/2,1), 2*(1-x))]) 

sage: f.fourier_series_partial_sum(5) 

-2*cos(2*pi*x)/pi^2 + 4/25*sin(5*pi*x)/pi^2 

- 4/9*sin(3*pi*x)/pi^2 + 4*sin(pi*x)/pi^2 + 1/4 

 

""" 

from sage.all import pi, sin, cos, srange 

if not L: 

L = (self.domain().sup() - self.domain().inf()) / 2 

x = self.default_variable() 

a0 = self.fourier_series_cosine_coefficient(0, L) 

result = a0/2 + sum([(self.fourier_series_cosine_coefficient(n, L)*cos(n*pi*x/L) + 

self.fourier_series_sine_coefficient(n, L)*sin(n*pi*x/L)) 

for n in srange(1, N+1)]) 

return SR(result).expand() 

 

piecewise = PiecewiseFunction()