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

r""" 

Real Interpolation using GSL 

""" 

  

from cysignals.memory cimport sig_malloc, sig_free 

from cysignals.signals cimport sig_on, sig_off 

  

  

cdef class Spline: 

""" 

Create a spline interpolation object. 

  

Given a list `v` of pairs, ``s = spline(v)`` is an object ``s`` such that 

`s(x)` is the value of the spline interpolation through the points 

in `v` at the point `x`. 

  

The values in `v` do not have to be sorted. Moreover, one can append 

values to `v`, delete values from `v`, or change values in `v`, and the 

spline is recomputed. 

  

EXAMPLES:: 

  

sage: S = spline([(0, 1), (1, 2), (4, 5), (5, 3)]); S 

[(0, 1), (1, 2), (4, 5), (5, 3)] 

sage: S(1.5) 

2.76136363636... 

  

Changing the points of the spline causes the spline to be recomputed:: 

  

sage: S[0] = (0, 2); S 

[(0, 2), (1, 2), (4, 5), (5, 3)] 

sage: S(1.5) 

2.507575757575... 

  

We may delete interpolation points of the spline:: 

  

sage: del S[2]; S 

[(0, 2), (1, 2), (5, 3)] 

sage: S(1.5) 

2.04296875 

  

We may append to the list of interpolation points:: 

  

sage: S.append((4, 5)); S 

[(0, 2), (1, 2), (5, 3), (4, 5)] 

sage: S(1.5) 

2.507575757575... 

  

If we set the `n`-th interpolation point, where `n` is larger than 

``len(S)``, then points `(0, 0)` will be inserted between the 

interpolation points and the point to be added:: 

  

sage: S[6] = (6, 3); S 

[(0, 2), (1, 2), (5, 3), (4, 5), (0, 0), (0, 0), (6, 3)] 

  

This example is in the GSL documentation:: 

  

sage: v = [(i + sin(i)/2, i+cos(i^2)) for i in range(10)] 

sage: s = spline(v) 

sage: show(point(v) + plot(s,0,9, hue=.8)) 

  

We compute the area underneath the spline:: 

  

sage: s.definite_integral(0, 9) 

41.196516041067... 

  

The definite integral is additive:: 

  

sage: s.definite_integral(0, 4) + s.definite_integral(4, 9) 

41.196516041067... 

  

Switching the order of the bounds changes the sign of the integral:: 

  

sage: s.definite_integral(9, 0) 

-41.196516041067... 

  

We compute the first and second-order derivatives at a few points:: 

  

sage: s.derivative(5) 

-0.16230085261803... 

sage: s.derivative(6) 

0.20997986285714... 

sage: s.derivative(5, order=2) 

-3.08747074561380... 

sage: s.derivative(6, order=2) 

2.61876848274853... 

  

Only the first two derivatives are supported:: 

  

sage: s.derivative(4, order=3) 

Traceback (most recent call last): 

... 

ValueError: Order of derivative must be 1 or 2. 

  

""" 

def __init__(self, v=[]): 

""" 

EXAMPLES:: 

  

sage: S = spline([(1,1), (2,3), (4,5)]); S 

[(1, 1), (2, 3), (4, 5)] 

sage: type(S) 

<type 'sage.calculus.interpolation.Spline'> 

""" 

self.v = list(v) 

self.started = 0 

  

def __dealloc__(self): 

self.stop_interp() 

  

def __setitem__(self, int i, xy): 

""" 

EXAMPLES:: 

  

sage: S = spline([(1,1), (2,3), (4,5)]); S 

[(1, 1), (2, 3), (4, 5)] 

sage: S(1.5) 

2.0625 

  

Replace `0`-th point, which changes the spline:: 

  

sage: S[0]=(0,1); S 

[(0, 1), (2, 3), (4, 5)] 

sage: S(1.5) 

2.5 

  

If you set the `n`-th point and `n` is larger than ``len(S)``, 

then `(0,0)` points are inserted and the `n`-th entry is set 

(which may be a weird thing to do, but that is what happens):: 

  

sage: S[4] = (6,10) 

sage: S 

[(0, 1), (2, 3), (4, 5), (0, 0), (6, 10)] 

""" 

cdef int j 

if i < len(self.v): 

self.v[i] = xy 

else: 

for j from len(self.v) <= j <= i: 

self.v.append((0,0)) 

self.v[i] = xy 

self.stop_interp() 

  

def __getitem__(self, int i): 

""" 

EXAMPLES:: 

  

sage: S = spline([(1,1), (2,3), (4,5)]); S[0] 

(1, 1) 

sage: S[-1] 

(4, 5) 

sage: S[1] 

(2, 3) 

""" 

return self.v[i] 

  

def __delitem__(self, int i): 

""" 

EXAMPLES:: 

  

sage: S = spline([(1,1), (2,3), (4,5)]); S 

[(1, 1), (2, 3), (4, 5)] 

sage: del S[1] 

sage: S 

[(1, 1), (4, 5)] 

  

The spline is recomputed when points are deleted (:trac:`13519`):: 

  

sage: S = spline([(1,1), (2,3), (4,5), (5, 5)]); S 

[(1, 1), (2, 3), (4, 5), (5, 5)] 

sage: S(3) 

4.375 

sage: del S[0]; S 

[(2, 3), (4, 5), (5, 5)] 

sage: S(3) 

4.25 

  

""" 

del self.v[i] 

self.stop_interp() 

  

def append(self, xy): 

""" 

EXAMPLES:: 

  

sage: S = spline([(1,1), (2,3), (4,5)]); S.append((5,7)); S 

[(1, 1), (2, 3), (4, 5), (5, 7)] 

  

The spline is recomputed when points are appended (:trac:`13519`):: 

  

sage: S = spline([(1,1), (2,3), (4,5)]); S 

[(1, 1), (2, 3), (4, 5)] 

sage: S(3) 

4.25 

sage: S.append((5, 5)); S 

[(1, 1), (2, 3), (4, 5), (5, 5)] 

sage: S(3) 

4.375 

  

""" 

self.v.append(xy) 

self.stop_interp() 

  

def list(self): 

""" 

Underlying list of points that this spline goes through. 

  

EXAMPLES:: 

  

sage: S = spline([(1,1), (2,3), (4,5)]); S.list() 

[(1, 1), (2, 3), (4, 5)] 

  

This is a copy of the list, not a reference (:trac:`13530`):: 

  

sage: S = spline([(1,1), (2,3), (4,5)]) 

sage: L = S.list(); L 

[(1, 1), (2, 3), (4, 5)] 

sage: L[2] = (3, 2) 

sage: L 

[(1, 1), (2, 3), (3, 2)] 

sage: S.list() 

[(1, 1), (2, 3), (4, 5)] 

  

""" 

return self.v[:] 

  

def __len__(self): 

""" 

Number of points that the spline goes through. 

  

EXAMPLES:: 

  

sage: len(spline([(1,1), (2,3), (4,5)])) 

3 

""" 

return len(self.v) 

  

def __repr__(self): 

""" 

EXAMPLES:: 

  

sage: spline([(1,1), (2,3), (4,5)]).__repr__() 

'[(1, 1), (2, 3), (4, 5)]' 

""" 

return str(self.v) 

  

cdef start_interp(self): 

if self.started: 

sig_free(self.x) 

sig_free(self.y) 

return 

v = list(self.v) 

v.sort() 

n = len(v) 

if n < 3: 

raise RuntimeError("must have at least 3 points in order to interpolate") 

self.x = <double*> sig_malloc(n*sizeof(double)) 

if self.x == <double*>0: 

raise MemoryError 

self.y = <double*> sig_malloc(n*sizeof(double)) 

if self.y == <double*>0: 

sig_free(self.x) 

raise MemoryError 

  

cdef int i 

for i from 0 <= i < n: 

self.x[i] = v[i][0] 

self.y[i] = v[i][1] 

  

self.acc = gsl_interp_accel_alloc () 

self.spline = gsl_spline_alloc (gsl_interp_cspline, n) 

gsl_spline_init (self.spline, self.x, self.y, n) 

self.started = 1 

  

cdef stop_interp(self): 

if not self.started: 

return 

sig_free(self.x) 

sig_free(self.y) 

gsl_spline_free (self.spline) 

gsl_interp_accel_free (self.acc) 

self.started = 0 

  

def __call__(self, double x): 

""" 

Value of the spline function at `x`. 

  

EXAMPLES:: 

  

sage: S = spline([(1,1), (2,3), (4,5)]) 

sage: S(1) 

1.0 

sage: S(2) 

3.0 

sage: S(4) 

5.0 

sage: S(3.5) 

4.65625 

""" 

if not self.started: 

self.start_interp() 

sig_on() 

y = gsl_spline_eval(self.spline, x, self.acc) 

sig_off() 

return y 

  

def derivative(self, double x, int order=1): 

""" 

Value of the first or second derivative of the spline at `x`. 

  

INPUT: 

  

- ``x`` -- value at which to evaluate the derivative. 

  

- ``order`` (default: 1) -- order of the derivative. Must be 1 or 2. 

  

EXAMPLES: 

  

We draw a cubic spline through three points and compute the 

derivatives:: 

  

sage: s = spline([(0, 0), (2, 3), (4, 0)]) 

sage: s.derivative(0) 

2.25 

sage: s.derivative(2) 

0.0 

sage: s.derivative(4) 

-2.25 

sage: s.derivative(1, order=2) 

-1.125 

sage: s.derivative(3, order=2) 

-1.125 

  

""" 

if (order!=1) and (order!=2): 

raise ValueError("Order of derivative must be 1 or 2.") 

  

if not self.started: 

self.start_interp() 

sig_on() 

  

if order == 1: 

d = gsl_spline_eval_deriv(self.spline, x, self.acc) 

else: 

d = gsl_spline_eval_deriv2(self.spline, x, self.acc) 

  

sig_off() 

return d 

  

def definite_integral(self, double a, double b): 

""" 

Value of the definite integral between `a` and `b`. 

  

INPUT: 

  

- ``a`` -- Lower bound for the integral. 

  

- ``b`` -- Upper bound for the integral. 

  

EXAMPLES: 

  

We draw a cubic spline through three points and compute the 

area underneath the curve:: 

  

sage: s = spline([(0, 0), (1, 3), (2, 0)]) 

sage: s.definite_integral(0, 2) 

3.75 

sage: s.definite_integral(0, 1) 

1.875 

sage: s.definite_integral(0, 1) + s.definite_integral(1, 2) 

3.75 

sage: s.definite_integral(2, 0) 

-3.75 

  

""" 

# GSL chokes when the upper bound is smaller than the lower bound 

bounds_swapped = False 

if b < a: 

a, b = b, a 

bounds_swapped = True 

  

if not self.started: 

self.start_interp() 

sig_on() 

I = gsl_spline_eval_integ(self.spline, a, b, self.acc) 

sig_off() 

  

if bounds_swapped: I = -I 

return I 

  

spline = Spline