-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNKT_lib.py
530 lines (395 loc) · 19 KB
/
NKT_lib.py
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
from tmm import inc_tmm
from numpy import sqrt
def NKT_TMM_spectrum_wrapper(nk_fit, thickness, lamda, snell_angle_front, layer_index_of_fit, nk_f_list,
thickness_list, coherency_list, tm_polarization_fraction, spectrum):
#this is just a fancy wrapper for inc_tmm
# does the order matter?
nk_list = [ nk_f(lamda) for nk_f in nk_f_list]
nk_list[layer_index_of_fit] = nk_fit # overwrite input nk_f with the fit one. basically, it would make the code much uglier if I made an excption
local_thickness_list = [layer_thickness for layer_thickness in thickness_list]
local_thickness_list[layer_index_of_fit] = thickness
te_result = inc_tmm('s', nk_list, local_thickness_list, coherency_list, snell_angle_front, lamda)
tm_result = inc_tmm('p', nk_list, local_thickness_list, coherency_list, snell_angle_front, lamda)
T = tm_polarization_fraction * tm_result['T'] + (1.0-tm_polarization_fraction) * te_result['T']
R = tm_polarization_fraction * tm_result['R'] + (1.0-tm_polarization_fraction) * te_result['R']
A = 1 - T - R
if callable(spectrum)==False:
result_dict = { 'T': T,
'R': R,
'A': A }
result = result_dict[spectrum]
else:
result = spectrum(T,R) # allows you to create things like extiction where the spectrum is 1-T
return result
def NKT_spectrum_TMM_lamda(params): # This has to be at the top level because map is strange and wont pickle onless it is at the top level
'''Returns a list of the spectrum values for each single point at a Wavelength'''
lamda = params[0]
nk = params[1]
thickness = params[2]
parameter_list_generator = params[3]
list_of_parameters = parameter_list_generator(lamda)
spectrum_list = []
for parameters in list_of_parameters :
spectrum_list.append( TMM_spectrum_wrapper(nk_fit = nk, thickness = thickness, **parameters) )
return spectrum_list
def NKT_spectrum_lamda_error(params): # This has to be at the top level because map is strange and wont pickle onless it is at the top level
lamda = params[0] # these params are per Wavelength, we could get more ganular parallelism with this!
nk = params[1]
thickness = params[2]
spectrum_list_generator = params[3]
parameter_list_generator = params[4]
spectrum_list = spectrum_list_generator(lamda)
sqrt_point_multiplicity = sqrt(len(spectrum_list))
list_of_parameters = parameter_list_generator(lamda)
sub_error_list=[]
for spectrum, parameters in zip (spectrum_list, list_of_parameters ):
spectrum_calculated = NKT_TMM_spectrum_wrapper(nk_fit = nk, thickness = thickness, **parameters) # these parameters are per measurement, set of parameters for a single point tmm model
error = (spectrum_calculated - spectrum)/sqrt_point_multiplicity # in the formulation this gets squared later, and we look at the per Wavelength rms error and normalizing it gives portability to weights
#we'll put wieghting somewhere else so that the formulation is clear
sub_error_list.append( error)
## i should try this later
#def error( value_and_model_parameters_tuple ):
# tmm_parameters = value_and_model_parameters_tuple[1]
# value = value_and_model_parameters_tuple[0]
# error = (TMM_spectrum(nk_fit = nk, **tmm_parameters ) - value)/sqrt_point_multiplicity
# return error
#sub_error_list = map(error, zip(spectrum_list, list_of_parameters ) )
return sub_error_list
def NKT_TMM_spectra(lamda_list, nk_f, parameter_list_generator):
''''''
#returns data in block like spectra[lamda][ spectrum] there are computational reasons why this order is this way
muh_inputs = []
for lamda in lamda_list:
muh_inputs.append( (lamda, nk_f(lamda), parameter_list_generator ) )
from multiprocessing import Pool, cpu_count
my_pool = Pool(cpu_count())
spectra = my_pool.map(NKT_spectrum_TMM_lamda, muh_inputs)
my_pool.terminate()
#
return spectra
def NKT_pointwise_rms_error_sum_wrapper(params): # for each Wavelength, wraps the previous function TRA_lamda_error to quikcly compute error spectrum
from numpy import sqrt
point_error_list = NKT_spectrum_lamda_error(params)
sum_err_square = 0.0
for err in point_error_list:
sum_err_square += err**2
return sqrt(sum_err_square)
def NKT_rms_error_spectrum(lamda_list, nk_f, thickness, spectrum_list_generator, parameter_list_generator):
muh_inputs = []
for lamda in lamda_list:
muh_inputs.append( (lamda, nk_f(lamda), thickness, spectrum_list_generator, parameter_list_generator ) )
from multiprocessing import Pool, cpu_count
my_pool = Pool(cpu_count())
error_spectrum = my_pool.map(NKT_pointwise_rms_error_sum_wrapper, muh_inputs)
my_pool.terminate()
return error_spectrum
def NKT_sqr_rms_gradient_at_lamda(params, h_nk = 1e-6):
from numpy import array
def shift_nk(params, shift): # i use this code to make it clearer
return [params[0], params[1]+shift, params[2], params[3], params[4]]
e_p_0 = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, h_nk ))**2
e_m_0 = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, -h_nk ))**2
e_0_p = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, h_nk*1.0j ))**2
e_0_m = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, -h_nk*1.0j ))**2
e_n = (e_p_0 - e_m_0)/(2.0*h_nk)
e_k = (e_0_p - e_0_m)/(2.0*h_nk)
gradient = array([e_n, e_k])
return gradient
def NKT_sqr_rms_hessian_at_lamda(params, h_nk = 1e-4):
from numpy import array
def shift_nk(params, shift): # i use this code to make it clearer
return [params[0], params[1]+shift, params[2], params[3], params[4]]
e_0_0 = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, 0.0 ))**2
e_p_0 = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, h_nk*( 1.0+0.0j) ))**2
e_m_0 = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, -h_nk*( 1.0+0.0j) ))**2
e_0_p = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, h_nk*( 0.0+1.0j) ))**2
e_0_m = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, -h_nk*( 0.0+1.0j) ))**2
e_p_p = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, h_nk*( 1.0+1.0j) ))**2
e_m_m = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, h_nk*(-1.0-1.0j) ))**2
e_p_m = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, h_nk*( 1.0-1.0j) ))**2
e_m_p = NKT_pointwise_rms_error_sum_wrapper(shift_nk(params, h_nk*(-1.0+1.0j) ))**2
e_nn = (e_p_0 - 2.0*e_0_0 + e_m_0)/(h_nk**2.0)
e_kk = (e_0_p - 2.0*e_0_0 + e_0_m)/(h_nk**2.0)
e_nk = (e_p_p - e_p_0 - e_0_p + 2.0*e_0_0 - e_m_0 - e_0_m + e_m_m)/(2.0*h_nk*h_nk) # two difference methods..., the first is supposed to be cheaper
e_nk = (e_p_p - e_p_m - e_m_p + e_m_m)/(4.0*h_nk*h_nk)
hessian = array([[e_nn, e_nk],[e_nk, e_kk]])
return hessian
def NKT_pointwise_reducible_rms_error_sum_wrapper(params): # for each Wavelength, wraps the previous function TRA_lamda_error to quikcly compute error spectrum
## calculates numeric derivatives
#does some hessians and matrix match
#from wikipedia
e_0_0 = NKT_pointwise_rms_error_sum_wrapper(params = params)**2
grad = NKT_sqr_rms_gradient_at_lamda(params = params)# h_nk = h_nk)
hessian = NKT_sqr_rms_hessian_at_lamda(params = params)#, h_nk = h_nk)
from numpy.linalg import det, inv
from numpy import dot, array, sqrt
if det(hessian) > 0.0: # minima predicted
hessian_inv = inv(hessian)
#reducible_error = 1/2.0 * dot(grad, dot( hessian_inv, grad))
#if reducible_error > e_0_0: # can't go negative this way
# reducible_error = e_0_0
#irreducible_error = e_0_0 - reducible_error
S_change = 1.0/2.0 * dot(grad, dot( hessian_inv, grad))
Smin = e_0_0 - S_change
irreducible_error = Smin
reducible_error = e_0_0 - irreducible_error
if irreducible_error < 0.0: # removing this might make better results even if unrealisitc
reducible_error = e_0_0 # bascially its predicting negative error is posible, nah just meas it can go to zero
irreducible_error = 0.0
#if reducible_error < 0.0:
# print( params[0], det(hessian), S_change, [reducible_error, irreducible_error])
else: # what else should i do here, I assume it means the band is caught on a peak?
irreducible_error = e_0_0
reducible_error = 0.0
#print( params[0], det(hessian), [reducible_error, irreducible_error])
return [reducible_error, irreducible_error]
def NKT_reducible_rms_error_spectrum(lamda_list, nk_f, thickness, spectrum_list_generator, parameter_list_generator):
muh_inputs = []
for lamda in lamda_list:
muh_inputs.append( (lamda, nk_f(lamda), thickness, spectrum_list_generator, parameter_list_generator ) )
from numpy import array, sqrt
from multiprocessing import Pool, cpu_count
my_pool = Pool(cpu_count())
error_spectra = array(my_pool.map(NKT_pointwise_reducible_rms_error_sum_wrapper, muh_inputs)).T
my_pool.terminate()
reducible_error_spectrum = sqrt(error_spectra[0])
irreducible_error_spectrum = sqrt(error_spectra[1])
return reducible_error_spectrum, irreducible_error_spectrum
def NKT_single_lamda_rms_error_map(lamda, nlist, klist, thickness, spectrum_list_generator, parameter_list_generator):
muh_inputs = []
for n in nlist:
for k in klist:
muh_inputs.append( (lamda, n+1.0j*k, thickness, spectrum_list_generator, parameter_list_generator ) )
from multiprocessing import Pool, cpu_count
my_pool = Pool(cpu_count())
from numpy import reshape, array
error_list = my_pool.map(NKT_pointwise_rms_error_sum_wrapper, muh_inputs)
error_map = reshape( array(error_list), (len(nlist), len(klist) ))
my_pool.terminate()
return error_map #[nindex,kindex]
###################
def NKT_fit_spectra_nk_sqr(lamda_list, spectrum_list_generator, parameter_list_generator, nk_f_guess, thickness_guess, delta_weight = 0.1, tolerance = 1e-5, no_negative = True, interpolation_type = 'cubic', method = 'least_squares'):
'''n_front and n_back must be real valued for this to work without caveats.
thickness and lambda can be any units, so long as they are the same, lamda_list must be sorted'''
from numpy import pi, exp, abs, sqrt, array, zeros, savetxt, inf, diff, ones
from scipy.optimize import root, least_squares, minimize
from TRANK import extrap_c
#point_multiplicity = len(spectrum_list_generator(lamda_list[0]))
#print(point_multiplicity)
#point_multiplicity_list = [len(spectrum_list_generator(lamda)) for lamda in lamda_list ]
#point_multiplicity = point_multiplicity_list[0]
abs_delta_weight = sqrt(delta_weight**2 * (len(lamda_list)/(len(lamda_list)-1.0)))
from multiprocessing import Pool, cpu_count
my_pool = Pool(cpu_count())
#my_pool = Pool(1)
def F_error(nk_t_list): # t goes at the end
c_nk_list = []
muh_inputs = []
thickness = nk_t_list[len(lamda_list)]
for i in range(len(lamda_list)):
nk = nk_t_list[2*i] + 1.0j*nk_t_list[2*i+1]
c_nk_list.append(nk)
muh_inputs.append( (lamda_list[i], nk, thickness, spectrum_list_generator, parameter_list_generator ) )
error_list_lists = my_pool.map(NKT_spectrum_lamda_error, muh_inputs)
#combine the sub error lists into
error_list = []
for sub_error_list in error_list_lists:
error_list = error_list + sub_error_list
delta_array = diff(c_nk_list)*abs_delta_weight
error_list = error_list + list(delta_array.real) + list(delta_array.imag)
return error_list
####### now for a guess list ##############
nk_t_guess_list = []
for i in range(len(lamda_list)):
nk = nk_f_guess(lamda_list[i])
if no_negative:
nk_t_guess_list.append(abs(nk.real))
nk_t_guess_list.append(abs(nk.imag))
else:
nk_t_guess_list.append(nk.real)
nk_t_guess_list.append(nk.imag)
nk_t_guess_list.append(thickness_guess)
######### test
if False:
print(F_error(nk_t_guess_list))
print( 0.5*sum(array(F_error(nk_t_guess_list))**2))
############ nk guessing over, time for creating and minimizing error function
if method == 'least_squares':
inputs = dict(fun = F_error,
x0 = nk_t_guess_list,
ftol = tolerance,
xtol = tolerance,
gtol = tolerance,
verbose = 2)
if no_negative:
inputs.update(dict( bounds = [ [0]*(2*len(lamda_list)) + [0] , [inf] *(2*len(lamda_list)) + [inf] ]))
else:
inputs.update(dict( bounds = [ [-inf]*(2*len(lamda_list)) + [0] , [inf] *(2*len(lamda_list)) + [inf] ]))
solution = least_squares(**inputs ).x
elif method == 'L-BFGS-B':
inputs = dict(fun = lambda x: 0.5*sum(array(F_error(x))**2),
x0 = nk_t_guess_list,
method = 'L-BFGS-B',
tol = tolerance,
options = {'disp' : True, 'iprint': 2} )
if no_negative:
inputs.update(dict( bounds = 2*len(lamda_list)*[( 0,inf)] + [(0,inf)] ))
else:
inputs.update(dict( bounds = 2*len(lamda_list)*[(-inf,inf)] + [(0,inf)] ))
solution = minimize(**inputs ).x
elif method == 'SLSQP':
inputs = dict(fun = lambda x: 0.5*sum(array(F_error(x))**2),
x0 = nk_t_guess_list,
method = 'SLSQP',
tol = tolerance,
options = {'disp' : True, 'iprint': 2} )
if no_negative:
inputs.update(dict( bounds = 2*len(lamda_list)*[( 0,inf)] + [(0,inf)] ))
else:
inputs.update(dict( bounds = 2*len(lamda_list)*[(-inf,inf)] + [(0,inf)] ))
solution = minimize(**inputs ).x
elif method == 'TNC':
inputs = dict(fun = lambda x: 0.5*sum(array(F_error(x))**2),
x0 = nk_t_guess_list,
method = 'TNC',
tol = tolerance,
options = {'disp' : True, 'iprint': 2} )
if no_negative:
inputs.update(dict( bounds = 2*len(lamda_list)*[( 0,inf)] + [(0,inf)] ))
else:
inputs.update(dict( bounds = 2*len(lamda_list)*[(-inf,inf)] + [(0,inf)] ))
solution = minimize(**inputs ).x
else:
raise ValueError("Invalid minimization method!")
my_pool.terminate()
my_pool.close()
nk_list=[]
for i in range(len(lamda_list)):
nk_list.append(solution[2*i] + 1.0j*solution[2*i+1] )
thickness = solution[len(lamda_list)]
fit_nk_f = extrap_c(lamda_list, nk_list, kind = interpolation_type)
return fit_nk_f, thickness
def KK_lamda(lamda_list, lamda_fine, k, cshift = 1e-4) :
#KK transform in Wavelength
# cshift is basically scaled so its universal i think
# testing shows that cubic interpoltion and trapezoind rule excede the accurcy of just using the fine grid due to error cancelation
from scipy.integrate import simps, trapz
from scipy.interpolate import griddata
from numpy import array, zeros, pi
#print (lamda_list, k)
#rint (len(lamda_list), len(k))
k_fine = griddata(array(lamda_list), array(k), (array(lamda_fine)), method='cubic', fill_value = 0.0)
#print (k_fine)
n_out = zeros(len(lamda_list)) ### we use the fine lamda grid for the KK integral but we only need to evalute it at the coarse lamba grid points!
k_over_lamda = k_fine/lamda_fine
for i in range(len(lamda_list)): #parralelize this in the future!
lamda_out = lamda_list[i]
#n_out[i] = 1.0 + 2.0/pi * simps( k_over_lamda * (1.0/ ( (lamda_out/lamda_fine)**2 - 1.0 + cshift*1.0j)), lamda_fine).real
n_out[i] = 1.0 + 2.0/pi * trapz( k_over_lamda * (1.0/ ( (lamda_out/lamda_fine)**2 - 1.0 + cshift*1.0j)), lamda_fine).real
return n_out
def NKT_fit_spectra_nk_sqr_KK_compliant(lamda_list, lamda_fine, spectrum_list_generator, parameter_list_generator, nk_f_guess, thickness_guess,
delta_weight = 0.1, tolerance = 1e-5, no_negative = True, interpolation_type = 'cubic', method = 'least_squares'):
'''n_front and n_back must be real valued for this to work without caveats.
thickness and lambda can be any units, so long as they are the same, lamda_list must be sorted'''
from numpy import pi,exp,abs,sqrt, array, matmul, loadtxt, zeros, savetxt, inf, diff, ones, mean
from scipy.optimize import root, least_squares, minimize
from TRANK import extrap_c
#point_multiplicity = len(TR_pair_list_generator(lamda_list[0]))
#print(point_multiplicity)
abs_delta_weight = sqrt(delta_weight**2 * (len(lamda_list)/(len(lamda_list)-1.0)))
from multiprocessing import Pool, cpu_count
my_pool = Pool(cpu_count())
#my_pool = Pool(1)
def F_error(k_p_t_list):
# the last value is the principle value
#FYI -> k = array(k_and_p_list[0:-1])
k = k_p_t_list[0:len(lamda_list)]
p = k_p_t_list[len(lamda_list)]
thickness = k_p_t_list[len(lamda_list)+1]
n = p + KK_lamda(lamda_list = lamda_list, lamda_fine = lamda_fine, k = k )
muh_inputs = []
for i in range(len(lamda_list)):
nk = n[i]+1.0j*k[i]# double check this works properly later
muh_inputs.append( (lamda_list[i], nk, thickness, spectrum_list_generator, parameter_list_generator ) )
#print (zip(lamda_list, c_nk_list))
error_list_lists = my_pool.map(spectrum_lamda_error, muh_inputs)
#error_list_lists =my_pool.map(lamda_error, zip(lamda_list, c_nk_list))
#print (error_list_lists)
error_list = []
for sub_error_list in error_list_lists:
error_list = error_list + sub_error_list
error_list = error_list + list( abs_delta_weight*diff(n) ) + list( abs_delta_weight * diff(k))
return error_list
####### now for a guess list ##############
guess_k_p_t_list= []
p = 0.0
for i in range(len(lamda_list)):
nk = nk_f_guess(lamda_list[i])
if no_negative and nk.imag < 0.0:
k = 0
else:
k = nk.imag
guess_k_p_t_list.append( k)
p+= nk.real
# now we put p at the end
p = p/len(lamda_list) - 1.0 # this is a guess for the principle value
print ('principle value guess:',p)
guess_k_p_t_list.append(p)
guess_k_p_t_list.append(thickness)
######### test
if False: print(F_error(guess_k_p_t_list)) #use this to see if the TR_error works
############ nk guessing over, time for creating and minimizing error function
if method == 'least_squares':
inputs = dict(fun = F_error,
x0 = guess_k_p_t_list,
ftol = tolerance,
xtol = tolerance,
gtol = tolerance,
verbose = 2)
if no_negative:
inputs.update(dict( bounds = [ [0]*len(lamda_list) + [0, 0] , [inf]*len(lamda_list) + [inf, inf] ]))
else:
inputs.update(dict( bounds = [ [-inf]*len(lamda_list) + [-inf,0] , [inf]*len(lamda_list) + [inf, inf] ]))
#if no_negative:
# inputs.update(dict( bounds = [len(lamda_list)*[0.0]+[-inf],len(lamda_list)*[inf]+[inf]] ))
solution = least_squares(**inputs ).x
elif method == 'SLSQP':
inputs = dict(fun = lambda x: 0.5*sum(array(F_error(x))**2),
x0 = guess_k_and_p_list,
method = 'SLSQP',
tol = tolerance,
options = {'disp' : True, 'iprint': 2} )
if no_negative:
inputs.update(dict( bounds = 2*len(lamda_list)*[( 0,inf)] + [(0,inf)] ))
else:
inputs.update(dict( bounds = 2*len(lamda_list)*[(-inf,inf)] + [(0,inf)] ))
solution = minimize(**inputs ).x
elif method == 'L-BFGS-B':
inputs = dict(fun = lambda x: 0.5*sum(array(F_error(x))**2),
x0 = guess_k_and_p_list,
method = 'L-BFGS-B',
tol = tolerance,
options = {'disp' : True, 'iprint': 2} )
if no_negative:
inputs.update(dict( bounds = len(lamda_list)*[(0,inf)] + [(-inf,inf)] ))
solution = minimize(**inputs ).x
elif method == 'TNC':
inputs = dict(fun = lambda x: 0.5*sum(array(F_error(x))**2),
x0 = guess_k_and_p_list,
method = 'TNC',
tol = tolerance,
options = {'disp' : True, 'iprint': 2} )
if no_negative:
inputs.update(dict( bounds = len(lamda_list)*[(0,inf)] + [(-inf,inf)] ))
solution = minimize(**inputs ).x
else:
raise ValueError("Invalid minimization method!")
my_pool.terminate()
my_pool.close()
k_and_p_list = solution
k = k_and_p_list[0:-1]
p = k_and_p_list[-1]
n = p + KK_lamda(lamda_list = lamda_list, lamda_fine = lamda_fine, k = k )
print ('Final principle value:',p)
fit_nk_f = extrap_c(lamda_list, n + 1.0j*k, kind = interpolation_type)
return fit_nk_f