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

""" 

Functional notation support for common calculus methods 

 

EXAMPLES: We illustrate each of the calculus functional functions. 

 

:: 

 

sage: simplify(x - x) 

0 

sage: a = var('a') 

sage: derivative(x^a + sin(x), x) 

a*x^(a - 1) + cos(x) 

sage: diff(x^a + sin(x), x) 

a*x^(a - 1) + cos(x) 

sage: derivative(x^a + sin(x), x) 

a*x^(a - 1) + cos(x) 

sage: integral(a*x*sin(x), x) 

-(x*cos(x) - sin(x))*a 

sage: integrate(a*x*sin(x), x) 

-(x*cos(x) - sin(x))*a 

sage: limit(a*sin(x)/x, x=0) 

a 

sage: taylor(a*sin(x)/x, x, 0, 4) 

1/120*a*x^4 - 1/6*a*x^2 + a 

sage: expand( (x-a)^3 ) 

-a^3 + 3*a^2*x - 3*a*x^2 + x^3 

sage: laplace( e^(x+a), x, a) 

e^a/(a - 1) 

sage: inverse_laplace( e^a/(a-1), x, a) 

ilt(e^a/(a - 1), x, a) 

""" 

from __future__ import absolute_import 

 

from .calculus import SR 

from sage.symbolic.expression import Expression 

 

def simplify(f): 

r""" 

Simplify the expression `f`. 

 

EXAMPLES: We simplify the expression `i + x - x`. 

 

:: 

 

sage: f = I + x - x; simplify(f) 

I 

 

In fact, printing `f` yields the same thing - i.e., the 

simplified form. 

""" 

try: 

return f.simplify() 

except AttributeError: 

return f 

 

def derivative(f, *args, **kwds): 

""" 

The derivative of `f`. 

 

Repeated differentiation is supported by the syntax given in the 

examples below. 

 

ALIAS: diff 

 

EXAMPLES: We differentiate a callable symbolic function:: 

 

sage: f(x,y) = x*y + sin(x^2) + e^(-x) 

sage: f 

(x, y) |--> x*y + e^(-x) + sin(x^2) 

sage: derivative(f, x) 

(x, y) |--> 2*x*cos(x^2) + y - e^(-x) 

sage: derivative(f, y) 

(x, y) |--> x 

 

We differentiate a polynomial:: 

 

sage: t = polygen(QQ, 't') 

sage: f = (1-t)^5; f 

-t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1 

sage: derivative(f) 

-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 

sage: derivative(f, t) 

-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5 

sage: derivative(f, t, t) 

-20*t^3 + 60*t^2 - 60*t + 20 

sage: derivative(f, t, 2) 

-20*t^3 + 60*t^2 - 60*t + 20 

sage: derivative(f, 2) 

-20*t^3 + 60*t^2 - 60*t + 20 

 

We differentiate a symbolic expression:: 

 

sage: var('a x') 

(a, x) 

sage: f = exp(sin(a - x^2))/x 

sage: derivative(f, x) 

-2*cos(-x^2 + a)*e^(sin(-x^2 + a)) - e^(sin(-x^2 + a))/x^2 

sage: derivative(f, a) 

cos(-x^2 + a)*e^(sin(-x^2 + a))/x 

 

Syntax for repeated differentiation:: 

 

sage: R.<u, v> = PolynomialRing(QQ) 

sage: f = u^4*v^5 

sage: derivative(f, u) 

4*u^3*v^5 

sage: f.derivative(u) # can always use method notation too 

4*u^3*v^5 

 

:: 

 

sage: derivative(f, u, u) 

12*u^2*v^5 

sage: derivative(f, u, u, u) 

24*u*v^5 

sage: derivative(f, u, 3) 

24*u*v^5 

 

:: 

 

sage: derivative(f, u, v) 

20*u^3*v^4 

sage: derivative(f, u, 2, v) 

60*u^2*v^4 

sage: derivative(f, u, v, 2) 

80*u^3*v^3 

sage: derivative(f, [u, v, v]) 

80*u^3*v^3 

""" 

try: 

return f.derivative(*args, **kwds) 

except AttributeError: 

pass 

if not isinstance(f, Expression): 

f = SR(f) 

return f.derivative(*args, **kwds) 

 

diff = derivative 

 

def integral(f, *args, **kwds): 

r""" 

The integral of `f`. 

 

EXAMPLES:: 

 

sage: integral(sin(x), x) 

-cos(x) 

sage: integral(sin(x)^2, x, pi, 123*pi/2) 

121/4*pi 

sage: integral( sin(x), x, 0, pi) 

2 

 

We integrate a symbolic function:: 

 

sage: f(x,y,z) = x*y/z + sin(z) 

sage: integral(f, z) 

(x, y, z) |--> x*y*log(z) - cos(z) 

 

:: 

 

sage: var('a,b') 

(a, b) 

sage: assume(b-a>0) 

sage: integral( sin(x), x, a, b) 

cos(a) - cos(b) 

sage: forget() 

 

:: 

 

sage: integral(x/(x^3-1), x) 

1/3*sqrt(3)*arctan(1/3*sqrt(3)*(2*x + 1)) - 1/6*log(x^2 + x + 1) + 1/3*log(x - 1) 

 

:: 

 

sage: integral( exp(-x^2), x ) 

1/2*sqrt(pi)*erf(x) 

 

We define the Gaussian, plot and integrate it numerically and 

symbolically:: 

 

sage: f(x) = 1/(sqrt(2*pi)) * e^(-x^2/2) 

sage: P = plot(f, -4, 4, hue=0.8, thickness=2) 

sage: P.show(ymin=0, ymax=0.4) 

sage: numerical_integral(f, -4, 4) # random output 

(0.99993665751633376, 1.1101527003413533e-14) 

sage: integrate(f, x) 

x |--> 1/2*erf(1/2*sqrt(2)*x) 

 

You can have Sage calculate multiple integrals. For example, 

consider the function `exp(y^2)` on the region between the 

lines `x=y`, `x=1`, and `y=0`. We find the 

value of the integral on this region using the command:: 

 

sage: area = integral(integral(exp(y^2),x,0,y),y,0,1); area 

1/2*e - 1/2 

sage: float(area) 

0.859140914229522... 

 

We compute the line integral of `\sin(x)` along the arc of 

the curve `x=y^4` from `(1,-1)` to 

`(1,1)`:: 

 

sage: t = var('t') 

sage: (x,y) = (t^4,t) 

sage: (dx,dy) = (diff(x,t), diff(y,t)) 

sage: integral(sin(x)*dx, t,-1, 1) 

0 

sage: restore('x,y') # restore the symbolic variables x and y 

 

Sage is unable to do anything with the following integral:: 

 

sage: integral( exp(-x^2)*log(x), x ) 

integrate(e^(-x^2)*log(x), x) 

 

Note, however, that:: 

 

sage: integral( exp(-x^2)*ln(x), x, 0, oo) 

-1/4*sqrt(pi)*(euler_gamma + 2*log(2)) 

 

This definite integral is easy:: 

 

sage: integral( ln(x)/x, x, 1, 2) 

1/2*log(2)^2 

 

Sage can't do this elliptic integral (yet):: 

 

sage: integral(1/sqrt(2*t^4 - 3*t^2 - 2), t, 2, 3) 

integrate(1/sqrt(2*t^4 - 3*t^2 - 2), t, 2, 3) 

 

A double integral:: 

 

sage: y = var('y') 

sage: integral(integral(x*y^2, x, 0, y), y, -2, 2) 

32/5 

 

This illustrates using assumptions:: 

 

sage: integral(abs(x), x, 0, 5) 

25/2 

sage: a = var("a") 

sage: integral(abs(x), x, 0, a) 

1/2*a*abs(a) 

sage: integral(abs(x)*x, x, 0, a) 

Traceback (most recent call last): 

... 

ValueError: Computation failed since Maxima requested additional 

constraints; using the 'assume' command before evaluation 

*may* help (example of legal syntax is 'assume(a>0)', 

see `assume?` for more details) 

Is a positive, negative or zero? 

sage: assume(a>0) 

sage: integral(abs(x)*x, x, 0, a) 

1/3*a^3 

sage: forget() # forget the assumptions. 

 

We integrate and differentiate a huge mess:: 

 

sage: f = (x^2-1+3*(1+x^2)^(1/3))/(1+x^2)^(2/3)*x/(x^2+2)^2 

sage: g = integral(f, x) 

sage: h = f - diff(g, x) 

 

:: 

 

sage: [float(h(i)) for i in range(5)] #random 

 

[0.0, 

-1.1102230246251565e-16, 

-5.5511151231257827e-17, 

-5.5511151231257827e-17, 

-6.9388939039072284e-17] 

sage: h.factor() 

0 

sage: bool(h == 0) 

True 

""" 

try: 

return f.integral(*args, **kwds) 

except AttributeError: 

pass 

 

if not isinstance(f, Expression): 

f = SR(f) 

return f.integral(*args, **kwds) 

 

integrate = integral 

 

def limit(f, dir=None, taylor=False, **argv): 

r""" 

Return the limit as the variable `v` approaches `a` 

from the given direction. 

 

:: 

 

limit(expr, x = a) 

limit(expr, x = a, dir='above') 

 

 

INPUT: 

 

- ``dir`` - (default: None); dir may have the value 

'plus' (or 'above') for a limit from above, 'minus' (or 'below') 

for a limit from below, or may be omitted (implying a two-sided 

limit is to be computed). 

 

- ``taylor`` - (default: False); if True, use Taylor 

series, which allows more limits to be computed (but may also 

crash in some obscure cases due to bugs in Maxima). 

 

- ``\*\*argv`` - 1 named parameter 

 

ALIAS: You can also use lim instead of limit. 

 

EXAMPLES:: 

 

sage: limit(sin(x)/x, x=0) 

1 

sage: limit(exp(x), x=oo) 

+Infinity 

sage: lim(exp(x), x=-oo) 

0 

sage: lim(1/x, x=0) 

Infinity 

sage: limit(sqrt(x^2+x+1)+x, taylor=True, x=-oo) 

-1/2 

sage: limit((tan(sin(x)) - sin(tan(x)))/x^7, taylor=True, x=0) 

1/30 

 

Sage does not know how to do this limit (which is 0), so it returns 

it unevaluated:: 

 

sage: lim(exp(x^2)*(1-erf(x)), x=infinity) 

-limit((erf(x) - 1)*e^(x^2), x, +Infinity) 

""" 

if not isinstance(f, Expression): 

f = SR(f) 

return f.limit(dir=dir, taylor=taylor, **argv) 

 

lim = limit 

 

def taylor(f, *args): 

""" 

Expands self in a truncated Taylor or Laurent series in the 

variable `v` around the point `a`, containing terms 

through `(x - a)^n`. Functions in more variables are also 

supported. 

 

INPUT: 

 

- ``*args`` - the following notation is supported 

 

- ``x, a, n`` - variable, point, degree 

 

- ``(x, a), (y, b), ..., n`` - variables with points, degree of polynomial 

 

EXAMPLES:: 

 

sage: var('x,k,n') 

(x, k, n) 

sage: taylor (sqrt (1 - k^2*sin(x)^2), x, 0, 6) 

-1/720*(45*k^6 - 60*k^4 + 16*k^2)*x^6 - 1/24*(3*k^4 - 4*k^2)*x^4 - 1/2*k^2*x^2 + 1 

 

:: 

 

sage: taylor ((x + 1)^n, x, 0, 4) 

1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1 

 

:: 

 

sage: taylor ((x + 1)^n, x, 0, 4) 

1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1 

 

Taylor polynomial in two variables:: 

 

sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,-1),4) 

(x - 1)*(y + 1)^3 - 3*(x - 1)*(y + 1)^2 + (y + 1)^3 + 3*(x - 1)*(y + 1) - 3*(y + 1)^2 - x + 3*y + 3 

""" 

if not isinstance(f, Expression): 

f = SR(f) 

return f.taylor(*args) 

 

def expand(x, *args, **kwds): 

""" 

EXAMPLES:: 

 

sage: a = (x-1)*(x^2 - 1); a 

(x^2 - 1)*(x - 1) 

sage: expand(a) 

x^3 - x^2 - x + 1 

 

You can also use expand on polynomial, integer, and other 

factorizations:: 

 

sage: x = polygen(ZZ) 

sage: F = factor(x^12 - 1); F 

(x - 1) * (x + 1) * (x^2 - x + 1) * (x^2 + 1) * (x^2 + x + 1) * (x^4 - x^2 + 1) 

sage: expand(F) 

x^12 - 1 

sage: F.expand() 

x^12 - 1 

sage: F = factor(2007); F 

3^2 * 223 

sage: expand(F) 

2007 

 

Note: If you want to compute the expanded form of a polynomial 

arithmetic operation quickly and the coefficients of the polynomial 

all lie in some ring, e.g., the integers, it is vastly faster to 

create a polynomial ring and do the arithmetic there. 

 

:: 

 

sage: x = polygen(ZZ) # polynomial over a given base ring. 

sage: f = sum(x^n for n in range(5)) 

sage: f*f # much faster, even if the degree is huge 

x^8 + 2*x^7 + 3*x^6 + 4*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1 

 

TESTS:: 

 

sage: t1 = (sqrt(3)-3)*(sqrt(3)+1)/6; 

sage: tt1 = -1/sqrt(3); 

sage: t2 = sqrt(3)/6; 

sage: float(t1) 

-0.577350269189625... 

sage: float(tt1) 

-0.577350269189625... 

sage: float(t2) 

0.28867513459481287 

sage: float(expand(t1 + t2)) 

-0.288675134594812... 

sage: float(expand(tt1 + t2)) 

-0.288675134594812... 

""" 

try: 

return x.expand(*args, **kwds) 

except AttributeError: 

return x