-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkr_defect.py
More file actions
389 lines (317 loc) · 10.6 KB
/
Copy pathkr_defect.py
File metadata and controls
389 lines (317 loc) · 10.6 KB
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
"""
kr_defect.py
============
The K-R Defect Framework — Python Library
Based on:
"A K-R Defect Framework for Classical Inequalities: Curvature Recovery,
Uniqueness, Stability, and Applications to Nonlinear Boundary Value Problems"
RamaKrishna Pasupuleti, Boundary Value Problems, SpringerOpen, 2026.
Author of library: RamaKrishna Pasupuleti
ORCID: 0009-0008-8418-1430
"""
import numpy as np
from typing import Callable, Tuple, Union, List, Optional
# ============================================================
# CORE DEFECT FUNCTIONS
# ============================================================
def kr_defect(f: Callable, x: float, y: float, K: float) -> float:
"""
Compute the raw K-R defect D_f(x, y; K, R).
D_f = K*f(x) + R*f(y) - f(K*x + R*y)
where R = 1 - K.
Parameters
----------
f : callable — the function to measure
x : float — left point
y : float — right point (y != x)
K : float — dominant weight, 0 < K < 1
Returns
-------
float : D_f value (>= 0 for convex f)
Example
-------
>>> import math
>>> kr_defect(math.exp, 0, 1, 0.6)
0.19481...
"""
R = 1.0 - K
z = K * x + R * y # interior point
return K * f(x) + R * f(y) - f(z)
def kr_normalised(f: Callable, x: float, y: float, K: float) -> float:
"""
Compute the normalised K-R defect Phi_f(x, y; K, R).
Phi_f = D_f / (K * R * (x - y)^2)
By the Curvature Recovery Theorem (Theorem 2.6):
Phi_f = (1/2) * f''(xi) for some xi between x and y.
By the Local Limit Theorem (Theorem 2.7):
Phi_f -> (1/2) * f''(x) as y -> x.
Parameters
----------
f : callable — the function
x : float — left point
y : float — right point (y != x)
K : float — dominant weight, 0 < K < 1
Returns
-------
float : Phi_f value, approximates (1/2)*f''(xi)
Example
-------
>>> import math
>>> kr_normalised(math.exp, 0.5, 0.51, 0.6)
0.8237... # close to (1/2)*e^0.5 = 0.8244
"""
R = 1.0 - K
D = kr_defect(f, x, y, K)
return D / (K * R * (x - y) ** 2)
def kr_second_derivative(
f: Callable,
x: float,
h: float = 1e-3,
K: float = 0.5,
average: bool = True,
n_avg: int = 9
) -> float:
"""
Estimate f''(x) using the K-R Local Limit Theorem.
f''(x) ≈ 2 * Phi_f(x, x+h; K, R)
Parameters
----------
f : callable — the function
x : float — point at which to estimate f''
h : float — step size (default 1e-3)
K : float — weight parameter (ignored if average=True)
average : bool — if True, average over multiple K values for noise reduction
n_avg : int — number of K values to average over (default 9)
Returns
-------
float : estimate of f''(x)
Notes
-----
When average=True, K values are uniformly spaced in (0.1, 0.9).
This exploits the K-R parameter flexibility to reduce measurement noise
— an advantage that classical finite differences do not have.
Example
-------
>>> import math
>>> kr_second_derivative(math.exp, 0.5, h=0.01)
1.6487... # close to e^0.5 = 1.6487
"""
if average:
K_values = np.linspace(0.1, 0.9, n_avg)
estimates = [2.0 * kr_normalised(f, x, x + h, k) for k in K_values]
return float(np.mean(estimates))
else:
return 2.0 * kr_normalised(f, x, x + h, K)
def kr_first_derivative(
f: Callable,
x: float,
h: float = 1e-3,
K: float = 0.5
) -> float:
"""
Estimate f'(x) using the K-R first-order operator.
Using the same three evaluation points {f(x), f(z), f(y)} where z=Kx+Ry:
Phi_f^(1) = [f(y) - f(z)] / (K * (y - x)) -> f'(x) as y -> x
Parameters
----------
f : callable — the function
x : float — point at which to estimate f'
h : float — step size
K : float — weight parameter, 0 < K < 1
Returns
-------
float : estimate of f'(x)
Example
-------
>>> import math
>>> kr_first_derivative(math.exp, 1.0, h=0.01)
2.718... # close to e^1 = 2.718
"""
R = 1.0 - K
y = x + h
z = K * x + R * y
return (f(y) - f(z)) / (K * h)
def kr_both_derivatives(
f: Callable,
x: float,
h: float = 1e-3,
K: float = 0.5
) -> Tuple[float, float]:
"""
Estimate both f'(x) and f''(x) from the same three function evaluations.
Three evaluations: f(x), f(z), f(y) where z = Kx + R(x+h).
These give both derivatives simultaneously — the complete derivative-free
differentiation system.
Parameters
----------
f : callable — the function
x : float — point
h : float — step size
K : float — weight parameter
Returns
-------
(f_prime, f_double_prime) : tuple of floats
Example
-------
>>> import math
>>> kr_both_derivatives(math.exp, 1.0, h=0.01)
(2.718..., 2.718...) # both close to e^1
"""
R = 1.0 - K
y = x + h
z = K * x + R * y
# Three evaluations — used for both operators
fx = f(x)
fz = f(z)
fy = f(y)
# First derivative: [f(y) - f(z)] / (K*h)
f_prime = (fy - fz) / (K * h)
# Second derivative: 2 * D_f / (KR*h^2)
D = K * fx + R * fy - fz
f_double_prime = 2.0 * D / (K * R * h ** 2)
return f_prime, f_double_prime
# ============================================================
# RECONSTRUCTION
# ============================================================
def kr_reconstruct(
f_data: np.ndarray,
x_grid: np.ndarray,
K: float = 0.5,
f0: float = 0.0,
fp0: float = 0.0
) -> Tuple[np.ndarray, np.ndarray]:
"""
Reconstruct f from K-R defect measurements using the Reconstruction Formula
(Theorem 2.10).
Steps:
1. Estimate f''(x_i) at each grid point from defect measurements.
2. Integrate twice to recover f.
3. The integration constants f(a) and f'(a) are supplied by the user.
Parameters
----------
f_data : array — measured function values at x_grid
x_grid : array — grid of x values (uniformly spaced)
K : float — K-R weight parameter
f0 : float — boundary value f(x_grid[0])
fp0 : float — boundary value f'(x_grid[0])
Returns
-------
(f''_estimated, f_reconstructed) : tuple of arrays
"""
n = len(x_grid)
h = x_grid[1] - x_grid[0]
# Step 1: Estimate f'' at each interior point
f_double_prime = np.zeros(n)
for i in range(n - 1):
R = 1.0 - K
x = x_grid[i]
y = x_grid[i + 1]
z_idx = K * x + R * y
# Interpolate f at interior point z
alpha = (z_idx - x) / h
fz = (1 - alpha) * f_data[i] + alpha * f_data[i + 1]
D = K * f_data[i] + R * f_data[i + 1] - fz
f_double_prime[i] = 2.0 * D / (K * R * h ** 2)
f_double_prime[-1] = f_double_prime[-2] # extrapolate boundary
# Step 2: Integrate f'' twice (trapezoidal rule)
f_prime_reconstructed = np.zeros(n)
f_prime_reconstructed[0] = fp0
for i in range(1, n):
f_prime_reconstructed[i] = f_prime_reconstructed[i-1] + \
0.5 * h * (f_double_prime[i-1] + f_double_prime[i])
f_reconstructed = np.zeros(n)
f_reconstructed[0] = f0
for i in range(1, n):
f_reconstructed[i] = f_reconstructed[i-1] + \
0.5 * h * (f_prime_reconstructed[i-1] + f_prime_reconstructed[i])
return f_double_prime, f_reconstructed
def kr_inverse_bvp(
u_data: np.ndarray,
x_grid: np.ndarray,
K: float = 0.5,
average: bool = True
) -> np.ndarray:
"""
Recover the unknown nonlinearity f in -u'' = f(u) from measurements of u.
Uses the K-R defect to estimate u'' from sensor data, then:
f(u(x_i)) = -u''(x_i) ≈ -2 * Phi_u(x_i, x_{i+1}; K, R)
This is the core inverse BVP application (Section 5.2 of the paper).
The Uniqueness Principle (Theorem 2.8) guarantees the recovered f is unique
up to affine terms fixed by boundary conditions.
Parameters
----------
u_data : array — measured values of u at x_grid
x_grid : array — uniformly spaced grid
K : float — K-R weight parameter
average: bool — if True, average over multiple K values
Returns
-------
array : f(u(x_i)) at each grid point
"""
n = len(x_grid)
h = x_grid[1] - x_grid[0]
f_values = np.zeros(n)
K_vals = np.linspace(0.1, 0.9, 9) if average else [K]
for i in range(n - 1):
estimates = []
for k in K_vals:
R = 1.0 - k
x = x_grid[i]
y = x_grid[i + 1]
alpha = k * 0.0 + R * 1.0 # fractional position of z in [x,y]
fz = (1 - alpha) * u_data[i] + alpha * u_data[i + 1]
D = k * u_data[i] + R * u_data[i + 1] - fz
phi = 2.0 * D / (k * R * h ** 2)
estimates.append(-phi) # f(u) = -u''
f_values[i] = np.mean(estimates)
f_values[-1] = f_values[-2]
return f_values
# ============================================================
# CONVEXITY DIAGNOSTIC
# ============================================================
def kr_convexity(
f: Callable,
x_grid: np.ndarray,
h: float = 1e-3,
K: float = 0.5
) -> np.ndarray:
"""
Classify local convexity/concavity of f at each grid point.
By the Convexity Classification Theorem (Theorem 2.13):
sign(Phi_f) = sign(f''(xi))
Returns
-------
array of +1 (convex), 0 (affine), -1 (concave) at each point
"""
signs = np.zeros(len(x_grid))
for i, x in enumerate(x_grid):
phi = kr_normalised(f, x, x + h, K)
if phi > 1e-10:
signs[i] = 1
elif phi < -1e-10:
signs[i] = -1
else:
signs[i] = 0
return signs
# ============================================================
# STABILITY CHECK
# ============================================================
def kr_stability_bound(epsilon: float, a: float, b: float) -> float:
"""
Compute the K-R Stability Theorem bound (Theorem 2.15).
If |Phi_f| <= epsilon everywhere and f(a) = f(b) = 0, then:
||f||_Linf <= epsilon * (b-a)^2 / 4
Parameters
----------
epsilon : float — maximum observed |Phi_f|
a, b : float — domain endpoints
Returns
-------
float : upper bound on ||f||_Linf
"""
return epsilon * (b - a) ** 2 / 4.0
print("K-R Defect Framework Library loaded successfully.")
print("Functions: kr_defect, kr_normalised, kr_second_derivative,")
print(" kr_first_derivative, kr_both_derivatives,")
print(" kr_reconstruct, kr_inverse_bvp, kr_convexity,")
print(" kr_stability_bound")