-
Notifications
You must be signed in to change notification settings - Fork 0
/
PredefFitFuns.py
executable file
·1307 lines (1073 loc) · 41.9 KB
/
PredefFitFuns.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
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
#!/usr/bin/env python
## ADD YOUR OWN FUNCTIONS HERE
## Just remember, function(variable, parameter) where:
## variable is list (for multiple dimentional fitting) of dependant variables to fit over
## parameter is list of parameters to extract out of fit.
## you can also define derivatives of the function w.r.t the paremters to feed into the fitting method.
## just remember to add 2 lines to DerOfFun (above)
## you can also add initial conditions for your function in GuessFromFun (above)
import numpy as np
from MiscFuns import logNA
nM_phys = 939.5653
def makexunit(xin):
try:
return np.array(len(xin)*[1.0])
except Exception as err:
return 1.0
# def ChiDistribution(dof,chi):
# return (np.exp(-chi/2.)*(chi**((dof/2.) - 1)))/(gamma(dof/2.)*2**(dof/2.))
def Ratio(*a):
return a[0]/a[1]
# return a[1]
def RatioDer(*a):
return [1/a[1],-a[0]/(a[1]**2)]
def eyeFun(*a):
return a[0]
def eyeFunDer(*a):
return [1.]
# def IntChiDist(dof,alpha,chiList):
# # chiList = np.append(chiList,chiList[-1]*10)
# # if alpha == 0.5:
# # for ichi,ifun in zip(chiList,ChiDistribution(dof,np.array(chiList))):
# # print ichi/dof, ifun
# for ichi,chival in enumerate(chiList):
# intval = integrate.simps(ChiDistribution(dof,np.array(chiList[ichi:])),chiList[ichi:])
# if intval < alpha:
# return chiList[ichi]
# return 1000.0
# def AlphaVsChiDOF(dof):
# alphalist = np.arange(0.01,1.01,0.01)
# chiList = np.arange(0.01,100.1,0.01)
# chinu = []
# for ialpha in alphalist:
# chinu.append(IntChiDist(dof,ialpha,chiList))
# return np.array(chinu)/dof,alphalist
def Chi_Prop_Fit(x,p):
try:
return p[0] + p[1]*(-p[2]*x[0]).Exp()
except Exception as err:
return p[0] + p[1]*np.exp(-p[2]*x[0])
def Chi_Prop_Fit_2exp(x,p):
try:
return p[0] + p[1]*(-p[2]*x[0]).Exp() + p[3]*(-p[4]*x[0]).Exp()
except Exception as err:
return p[0] + p[1]*np.exp(-p[2]*x[0]) + p[3]*np.exp(-p[4]*x[0])
def c3FitFun(x,p):
try:
tf = np.sqrt(x[0])
except Exception as err:
tf = x[0].Sqrt()
return 1+p[0] + p[1]/tf + p[2]*tf
def c3FFDer(x,p):
try:
tf = np.sqrt(x[0])
except Exception as err:
tf = x[0].Sqrt()
return [makexunit(tf),1/tf,tf]
def c3FitFun_log(x,p):
try:
tf = np.sqrt(x[0])
except Exception as err:
tf = x[0].Sqrt()
return 1+p[0] + p[1]/tf + p[2]*tf + p[3]*np.log(tf)
def c3FFDer_log(x,p):
try:
tf = np.sqrt(x[0])
except Exception as err:
tf = x[0].Sqrt()
return [makexunit(tf),1/tf,tf,np.log(tf)]
def c3FitFun_nosqrt(x,p):
return 1+p[0] + p[1]*(x[0]**-2) + p[2]*x[0]**2
def c3FFDer_nosqrt(x,p):
return [makexunit(x[0]),x[0]**-2,x[0]**2]
def c3FitFun_nosqrt_log(x,p):
return 1+p[0] + p[1]*(x[0]**-2) + p[2]*x[0]**2 + p[3]*np.log(x[0]**2)
def c3FFDer_nosqrt_log(x,p):
return [makexunit(x[0]),x[0]**-2,x[0]**2,np.log(x[0]**2)]
def c3FitFun_V2(x,p):
try:
tf = np.sqrt(x[0])
except Exception as err:
tf = x[0].Sqrt()
return 1+p[0] + p[1]/tf + p[2]*tf
def c3FFDer_V2(x,p):
try:
tf = np.sqrt(x[0])
except Exception as err:
tf = x[0].Sqrt()
return [makexunit(tf),1/tf,tf]
def c3PolyFun(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
p0 = p[0]
# return 1+p0*x[0]**2 + p1*x[0]**4+ p2*x[0]**6
return makexunit(x[0])+p0*(x[0])
def c3PFDer(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
# return [p0*x[0]**2,p1*x[0]**4,p2*x[0]**6]
return [x[0]]
def c3PolyFun2(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
p0,p1 = p[0],p[1]
# return 1+p0*x[0]**2 + p1*x[0]**4+ p2*x[0]**6
return makexunit(x[0])+p0*(x[0]) + p1*(x[0]**2)
def c3PFDer2(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
# return [p0*x[0]**2,p1*x[0]**4,p2*x[0]**6]
return [x[0],x[0]**2]
def c3PolyFun3(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
p0,p1,p2 = p[0],p[1],p[2]
# return 1+p0*x[0]**2 + p1*x[0]**4+ p2*x[0]**6
return makexunit(x[0])+p0*(x[0]) + p1*(x[0]**2)+ p2*(x[0]**3)
def c3PFDer3(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
# return [p0*x[0]**2,p1*x[0]**4,p2*x[0]**6]
return [x[0],x[0]**2,x[0]**3]
def c3PolyFun_skip1(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
p0 = p[0]
# return 1+p0*x[0]**2 + p1*x[0]**4+ p2*x[0]**6
return makexunit(x[0])+p0*(x[0]**2)
def c3PFDer_skip1(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
# return [p0*x[0]**2,p1*x[0]**4,p2*x[0]**6]
return [x[0]**2]
def c3PolyFun2_skip1(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
p0,p1 = p[0],p[1]
# return 1+p0*x[0]**2 + p1*x[0]**4+ p2*x[0]**6
return makexunit(x[0])+p0*(x[0]**2) + p1*(x[0]**4)
def c3PFDer2_skip1(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
# return [p0*x[0]**2,p1*x[0]**4,p2*x[0]**6]
return [x[0]**2,x[0]**4]
def c3PolyFun3_skip1(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
p0,p1,p2 = p[0],p[1],p[2]
# return 1+p0*x[0]**2 + p1*x[0]**4+ p2*x[0]**6
return makexunit(x[0])+p0*(x[0]**2) + p1*(x[0]**4)+ p2*(x[0]**6)
def c3PFDer3_skip1(x,p):
# try:
# p0,p1,p2 = np.exp(p[0]),np.exp(p[1]),np.exp(p[2])
# except Exception as err:
# p0,p1,p2 = p[0].Exp(),p[1].Exp(),p[2].Exp()
# return [p0*x[0]**2,p1*x[0]**4,p2*x[0]**6]
return [x[0]**2,x[0]**4,x[0]**6]
l8 = np.log(8)
def c3FitFun_NoA(x,p):
try:
logx = 2*np.log(x[0])-l8
except Exception as err:
logx = 2*x[0].Log()-l8
try:
p0,p1 = np.exp(p[0]),np.exp(p[1])
except Exception as err:
p0,p1 = p[0].Exp(),p[1].Exp()
return 1+ p0/(logx - p1)
def c3FFDer_NoA(x,p):
try:
logx = 2*np.log(x[0])-l8
except Exception as err:
logx = 2*x[0].Log()-l8
return [1/(p[1]+logx),-p[0]/(p[1]+logx)**2]
def c3ExpFitFun2(x,p):
try:
return (np.exp(-p[0]*x[0]) + np.exp(-p[1]*x[0]))/2.
except Exception as err:
return ((-p[0]*x[0]).Exp() + (-p[1]*x[0]).Exp())/2.
def c3EFFDer2(x,p):
try:
return [-x[0]*np.exp(-p[0]*x[0])/2,-x[0]*np.exp(-p[1]*x[0])/2]
except Exception as err:
return [-x[0]*(-p[0]*x[0]).Exp()/2,-x[0]*(-p[1]*x[0]).Exp()/2]
def c3ExpFitFun2_nosqrt(x,p):
try:
return (np.exp(-p[0]*(x[0]**2)/8) + np.exp(-p[1]*(x[0]**2)/8))/2.
except Exception as err:
return ((-p[0]*(x[0]**2)/8).Exp() + (-p[1]*(x[0]**2)/8).Exp())/2.
def c3EFFDer2_nosqrt(x,p):
try:
return [np.exp(-p[0]*(x[0]**2)/8)*(-x[0]**2)/16,np.exp(-p[1]*(x[0]**2)/8)*(-x[0]**2)/16]
except Exception as err:
return [(-p[0]*(x[0]**2)/8).Exp()*(-x[0]**2)/16,(-p[1]*(x[0]**2)/8).Exp()*(-x[0]**2)/16]
def c3ExpFitFun3_nosqrt(x,p):
try:
return (np.exp(-p[0]*(x[0]**2)/8) + np.exp(-p[1]*(x[0]**2)/8)+ np.exp(-p[2]*(x[0]**2)/8))/3.
except Exception as err:
return ((-p[0]*(x[0]**2)/8).Exp() + (-p[1]*(x[0]**2)/8).Exp()+ (-p[2]*(x[0]**2)/8).Exp())/3.
def c3EFFDer3_nosqrt(x,p):
try:
return [np.exp(-p[0]*(x[0]**2)/8)*(-x[0]**2)/24,
np.exp(-p[1]*(x[0]**2)/8)*(-x[0]**2)/24,
np.exp(-p[2]*(x[0]**2)/8)*(-x[0]**2)/24]
except Exception as err:
return [(-p[0]*(x[0]**2)/8).Exp()*(-x[0]**2)/24,
(-p[1]*(x[0]**2)/8).Exp()*(-x[0]**2)/24,
(-p[2]*(x[0]**2)/8).Exp()*(-x[0]**2)/24]
def c3ExpFitFun(x,p):
try:
return np.exp(-p[0]*x[0])
except Exception as err:
return (-p[0]*x[0]).Exp()
def c3EFFDer(x,p):
try:
return [-x[0]*np.exp(-p[0]*x[0])]
except Exception as err:
return [-x[0]*(-p[0]*x[0]).Exp()]
def c3ExpFitFun_nosqrt(x,p):
try:
return np.exp(-p[0]*(x[0]**2)/8)
except Exception as err:
return (-p[0]*(x[0]**2)/8).Exp()
def c3EFFDer_nosqrt(x,p):
try:
return [np.exp(-p[0]*(x[0]**2)/8)*(-x[0]**2)/8]
except Exception as err:
return [(-p[0](x[0]**2)/8).Exp()*(-x[0]**2)/8]
def Chi_Prop_Der(x,p):
try:
return [x[0]/x[0],(-p[2]*x[0]).Exp(),-x[0]*p[1]*(-p[2]*x[0]).Exp()]
except Exception as err:
return [x[0]/x[0],np.exp(-p[2]*x[0]),-x[0]*p[1]*np.exp(-p[2]*x[0])]
def Chi_Prop_Der_2exp(x,p):
try:
return [x[0]/x[0] ,(-p[2]*x[0]).Exp(),-x[0]*p[1]*(-p[2]*x[0]).Exp()
,(-p[4]*x[0]).Exp(),-x[0]*p[3]*(-p[4]*x[0]).Exp()]
except Exception as err:
return [x[0]/x[0] ,np.exp(-p[2]*x[0]),-x[0]*p[1]*np.exp(-p[2]*x[0])
,np.exp(-p[4]*x[0]),-x[0]*p[3]*np.exp(-p[4]*x[0])]
def Alpha_Prop_Fit(x,p):
try:
return p[0] + p[1]*((-p[2]*(x[2]-x[1])).Exp()-(-p[2]*x[1]).Exp())
except Exception as err:
return p[0] + p[1]*(np.exp(-p[2]*(x[2]-x[1])) - np.exp(-p[2]*x[1]))
def Alpha_Prop_Fit_plin(x,p):
Tf = (x[1]-x[0]/2.)
try:
return p[0] + p[1]*(-p[2]*Tf).Exp() + p[3]*x[1]
except Exception as err:
return p[0] + p[1]*np.exp(-p[2]*Tf) + p[3]*x[1]
def Alpha_Prop_Fit_2exp(x,p):
Tfmint = x[1]-x[0]
# TminTf = x[2]-x[1]
# try:
# return p[0] + p[1]*((-p[2]*TminTf).Exp()-(-p[2]*Tfmint).Exp()) + \
# p[3]*((-p[4]*TminTf).Exp()-(-p[4]*Tfmint).Exp())
# except Exception as err:
# return p[0] + p[1]*(np.exp(-p[2]*TminTf) - np.exp(-p[2]*Tfmint)) + \
# p[3]*(np.exp(-p[4]*TminTf) - np.exp(-p[4]*Tfmint))
try:
return p[0] + p[1]*(-p[2]*Tfmint).Exp() + \
p[3]*(-p[4]*Tfmint).Exp()
except Exception as err:
return p[0] + p[1]*np.exp(-p[2]*Tfmint) + \
p[3]*np.exp(-p[4]*Tfmint)
def Alpha_Prop_Der(x,p):
try:
return [x[0]/x[0],(-p[2]*(x[2]-x[1])).Exp()-(-p[2]*x[1]).Exp(),
(x[2]-x[1])*p[1]*(-p[2]*(x[2]-x[1])).Exp()+x[1]*p[1]*(-p[2]*x[1]).Exp()]
except Exception as err:
return [x[0]/x[0],(np.exp(-p[2]*(x[2]-x[1])) - np.exp(-p[2]*x[1])),
(x[2]-x[1])*p[1]*np.exp(-p[2]*(x[2]-x[1])) +x[1]*p[1]*np.exp(-p[2]*x[1])]
def Alpha_Prop_Der_plin(x,p):
Tf = (x[1]-x[0]/2.)
try:
return [x[0]/x[0],
(-p[2]*Tf).Exp(),
-Tf*p[1]*(-p[2]*Tf).Exp(),
x[1]]
except Exception as err:
return [x[0]/x[0],
np.exp(-p[2]*Tf),
-Tf*p[1]*np.exp(-p[2]*Tf),
x[1]]
def Alpha_Prop_Der_2exp(x,p):
Tfmint = x[1]-x[0]
# TminTf = x[2]-x[1]
# try:
# return [x[0]/x[0],
# (-p[2]*TminTf).Exp()-(-p[2]*Tfmint).Exp(),
# TminTf*p[1]*(-p[2]*TminTf).Exp()+Tfmint*p[1]*(-p[2]*Tfmint).Exp(),
# (-p[4]*TminTf).Exp()-(-p[4]*Tfmint).Exp(),
# TminTf*p[3]*(-p[4]*TminTf).Exp()+Tfmint*p[3]*(-p[4]*Tfmint).Exp()]
# except Exception as err:
# return [x[0]/x[0],
# (np.exp(-p[2]*TminTf) - np.exp(-p[2]*Tfmint)),
# TminTf*p[1]*np.exp(-p[2]*TminTf) +Tfmint*p[1]*np.exp(-p[2]*Tfmint),
# (np.exp(-p[3]*TminTf) - np.exp(-p[3]*Tfmint)),
# TminTf*p[3]*np.exp(-p[4]*TminTf) +Tfmint*p[3]*np.exp(-p[4]*Tfmint)]
try:
return [x[0]/x[0],
(-p[2]*Tfmint).Exp(),
-Tfmint*p[1]*(-p[2]*Tfmint).Exp(),
(-p[4]*Tfmint).Exp(),
-Tfmint*p[3]*(-p[4]*Tfmint).Exp()]
except Exception as err:
return [x[0]/x[0],
np.exp(-p[2]*Tfmint),
-Tfmint*p[1]*np.exp(-p[2]*Tfmint),
np.exp(-p[3]*Tfmint),
-Tfmint*p[3]*np.exp(-p[4]*Tfmint)]
## only difference here is that the current insersion time is included as x[0]
def RF_Prop_Fit(x,p):
try:
return p[0] + p[1]*((-p[2]*(x[2]-x[1])).Exp()-(-p[2]*x[1]).Exp())
except Exception as err:
return p[0] + p[1]*(np.exp(-p[2]*(x[2]-x[1])) - np.exp(-p[2]*x[1]))
def RF_Prop_Der(x,p):
try:
return [x[1]/x[1],(-p[2]*(x[2]-x[1])).Exp()-(-p[2]*x[1]).Exp(),
(x[2]-x[1])*p[1]*(-p[2]*(x[2]-x[1])).Exp()+x[1]*p[1]*(-p[2]*x[1]).Exp()]
except Exception as err:
return [x[1]/x[1],(np.exp(-p[2]*(x[2]-x[1])) - np.exp(-p[2]*x[1])),
(x[2]-x[1])*p[1]*np.exp(-p[2]*(x[2]-x[1])) +x[1]*p[1]*np.exp(-p[2]*x[1])]
def CreateFFO(thislen):
def FormFactorO(x,p):
return sum([ip*ix for ip,ix in zip(p[:thislen],x[:thislen])])
def FormFactorODer(x,p):
return np.array(x[:thislen]).tolist()
return FormFactorO, FormFactorODer
def ParmDivX(x,p):
return p[0] * (x[0]**(-1))
def ParmDivXDer(x,p):
return [x[0]**(-1)]
## Sigma/(1/m_u + 1/m_d + 1/m_s) = Sigma/(2/m_ud + 1/m_s)
def Chipt_Chit(x,p):
sum_part = 2*x[0]**-1 + x[1]**-1
return p[0]*sum_part**-1
## A e^(-mt) + B(1-e^(-mt))
def RandTFitFun(x,p):
try:
thisexp = np.exp(-p[2]*x[0])
except Exception as err:
thisexp = (-p[2]*x[0]).Exp()
return p[0]*thisexp + p[1]*(1-thisexp)
def ChitFitFun(x,p):
Denom = (x[0]*(logNA(x[0])+p[2]))**(-1)
return (p[0] *x[0] + p[1])*Denom
def ChitFitFunDer(x,p):
Denom = (x[0]*(logNA(x[0])+p[2]))**(-1)
return np.array([Denom*x[0],Denom,-x[0]*(Denom**2)])
## A*x*exp(-m*x)
def A_x_exp_minus_m_x(x,p):
try:
return p[0]*x[0]*np.exp(-p[1]*x[0])
except Exception as err:
return p[0]*x[0]*(-p[1]*x[0]).Exp()
## broken for some reason
def ParmDivXP(x,p):
return p[0] * (x[0]**(-1)) + p[1]
def ParmDivXPDer(x,p):
return np.array([x[0]**(-1),makexunit(x[0])])
def DPfitfun(x,p):
return p[0]/((1+(x[0]/p[1]))**2)
def DPfitfunDer(x,p):
# return [1/((1+(x[0]/p[1]))**2), (-2*p[0]*p[1]**2)/((x[0]+p[1])**3)]
return [1/((1+(x[0]/p[1]))**2),(2*p[0]*x[0]/(p[1]**2))/((1+(x[0]/p[1]))**3)]
def DPfitfunOnePar(x,p):
return 1/((1+(x[0]/p[0]))**2)
def DPfitfunOneParDer(x,p):
# return [1/((1+(x[0]/p[1]))**2), (-2*p[0]*p[1]**2)/((x[0]+p[1])**3)]
return [(-2/p[0])*1/((1+(x[0]/p[0]))**3)]
def DPfitfun2(x,p):
return p[0]/(1+(x[0]/p[1])**2)**2
def DPfitfun2Der(x,p):
return [1/(1+(x[0]/p[1])**2)**2,-4*p[0]*x[0]/(p[1]**2*(1+(x[0]/p[1])**2)**3)]
def FormFactorO1(x,p):return p[0]*x[0]
def FormFactorO1Der(x,p):return [x[0]]
def FormFactorO2(x,p):return p[0]*x[0]+p[1]*x[1]
def FormFactorO2Der(x,p): return [x[0],x[1]]
def FormFactorO3(x,p):return p[0]*x[0]+p[1]*x[1]+p[2]*x[2]
def FormFactorO3Der(x,p):return [x[0],x[1],x[2]]
def FormFactorO4(x,p):return p[0]*x[0]+p[1]*x[1]+p[2]*x[2]+p[3]*x[3]
def FormFactorO4Der(x,p):return [x[0],x[1],x[2],x[3]]
def FormFactorO5(x,p):return p[0]*x[0]+p[1]*x[1]+p[2]*x[2]+p[3]*x[3]+p[4]*x[4]
def FormFactorO5Der(x,p):return [x[0],x[1],x[2],x[3],x[4]]
def FormFactorO6(x,p):return p[0]*x[0]+p[1]*x[1]+p[2]*x[2]+p[3]*x[3]+p[4]*x[4]+p[5]*x[5]
def FormFactorO6Der(x,p):return [x[0],x[1],x[2],x[3],x[4],x[5]]
def FormFactorO7(x,p):return p[0]*x[0]+p[1]*x[1]+p[2]*x[2]+p[3]*x[3]+p[4]*x[4]+p[5]*x[5]+p[6]*x[6]
def FormFactorO7Der(x,p):return [x[0],x[1],x[2],x[3],x[4],x[5],x[6]]
def FormFactorO8(x,p):return p[0]*x[0]+p[1]*x[1]+p[2]*x[2]+p[3]*x[3]+p[4]*x[4]+p[5]*x[5]+p[6]*x[6]+p[7]*x[7]
def FormFactorO8Der(x,p):return [x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7]]
FormList = [FormFactorO8,
FormFactorO7,
FormFactorO6,
FormFactorO5,
FormFactorO4,
FormFactorO3,
FormFactorO2,
FormFactorO1]
def ConstantFitFun(x,p):
return p[0]*makexunit(x[0])
def ConstFFDer(x,p):
return makexunit(x[0])
def CreateOORFF(Const):
def OneOnRootNFitFun(x,p):
return Const + p[0]*(np.array(x[0])**(-1/2.)-1)
return OneOnRootNFitFun
def OORNFFDer(x,p):
return [(-p[0]/2.)*(np.array(x[0])**(-3/2.))]
# def SchiffFitFun(x,p):
# return p[0]*np.array(x[0])**2+p[1] + p[2]*np.array(x[1])
#
# def SchiffFFDer(x,p):
# return [np.array(x[0])**2,makexunit(x[0]),np.array(x[1])]
#
#
# def SchiffFitFun(x,p):
# return p[0]*np.array(x[0])**2 + p[1]*np.array(x[1]) + p[2]*np.array(x[1])**2
#
# def SchiffFFDer(x,p):
# return [np.array(x[0])**2,np.array(x[1]),np.array(x[1])**2]
# def SchiffFitFun(x,p):
# x = np.array(x)
# return p[0]*x[0]**2 + p[1]*x[1]**2 + p[2]*np.log(x[1]**2)*x[1]**2
#
# def SchiffFFDer(x,p):
# x = np.array(x)
# return [x[0]**2,x[1]**2,np.log(x[1]**2)*x[1]**2]
def SchiffFitFun(x,p):
return p[0]*np.array(x[0])**2 + p[1]
def SchiffFFDer(x,p):
return [np.array(x[0])**2,makexunit(x[1])]
def SchiffFitFun_noa(x,p):
return p[1]
def SchiffFFDer_noa(x,p):
return [makexunit(x[1])]
def c3ContFitFun(x,p):
return p[0] + p[1]*np.array(x[0])**2 + p[2]*np.array(x[1])
def c3ContFFDer(x,p):
return [makexunit(x[0]),np.array(x[0])**2,np.array(x[1])]
def hx_fun(x):
out = np.sqrt(1+1/x)
out = out * np.log((out + 1)/(out-1))
out = out - (2*(1+x/3))
return (-15/4) * out
def fullq2FitFun(x,p):
## x[0] is q^2, x[1] is (m_\pi)^2
## both must be in same units, I suggest GeV!
## equation 7 in notes
term_2 = hx_fun(x[0]/(4*x[1]))*8*x[1]/5
return p[1] + p[0] * (x[0] -term_2)
def fullq2FFDer(x,p):
term_2 = hx_fun(x[0]/(4*x[1]))*8*x[1]/5
return [x[0] -term_2,makexunit(x[0])]
def LinearFitFun(x,p):
return p[0]*np.array(x[0])+p[1]
def LinFFDer(x,p):
return [x[0],makexunit(x[0])]
def LinearFitFun_x0(x,p):
return p[0]*np.array(x[0])
def LinFFDer_x0(x,p):
return [x[0]]
def SqPlLogFitFun(x,p):
this_x = np.array(x[0])**2
return p[0]*this_x+p[1] + p[2]*this_x * np.log(this_x)
def SqPlLogFFDer(x,p):
this_x = np.array(x[0])**2
return [this_x,makexunit(x[0]),this_x*np.log(this_x)]
def SqPlLogFitFun_x0(x,p):
this_x = np.array(x[0])**2
return p[0]*this_x + p[1]*this_x * np.log(this_x)
def SqPlLogFFDer_x0(x,p):
this_x = np.array(x[0])**2
return [this_x,this_x*np.log(this_x)]
def SquaredFitFun(x,p):
return p[0]*np.array(x[0])**2+p[1]
def SquaredFFDer(x,p):
return [np.array(x[0])**2,makexunit(x[0])]
def SquaredFitFun_x0(x,p):
return p[0]*np.array(x[0])**2
def SquaredFFDer_x0(x,p):
return [np.array(x[0])**2]
def ContPlLogFitFun(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return p[0]*this_x+p[1] + p[2]*this_x * np.log(this_x) + p[3]*this_a
def ContPlLogFFDer(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return [this_x,makexunit(x[0]),this_x*np.log(this_x),this_a]
def ContPlLogFitFun_x0(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return p[0]*this_x + p[1]*this_x * np.log(this_x) + p[2]*this_a
def ContPlLogFFDer_x0(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return [this_x,this_x*np.log(this_x),this_a]
def ContPlLogFitFun_x0_dN(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
## physical neutron mass = 939.5653 MeV
return p[0]*this_x + p[1]*this_x * np.log(this_x/(nM_phys**2)) + p[2]*this_a
def ContPlLogFFDer_x0_dN(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return [this_x,this_x*np.log(this_x/(nM_phys**2)),this_a]
## third dim > 0 means neutron, < 0 means proton
def ContPlLogFitFun_NP_nompi3(x,p):
if isinstance(x[2],(list,tuple,np.ndarray)):
output = []
for ix0,ix1,ix2 in zip(x[0],x[1],x[2]):
if ix2 > 0:
##neutron case
output.append(ContPlLogFitFun_x0_dN([ix0,ix1],[p[0],-p[2],p[3]]))
elif ix2 < 1:
output.append(ContPlLogFitFun_x0_dN([ix0,ix1],[p[1],p[2],p[4]]))
return np.array(output)
else:
if x[2] > 0:
return ContPlLogFitFun_x0_dN(x,[p[0],-p[2],p[3]])
elif x[2] < 1:
return ContPlLogFitFun_x0_dN(x,[p[1],p[2],p[4]])
def ContPlLogFFDer_NP_nompi3(x,p):
if isinstance(x[2],(list,tuple,np.ndarray)):
output = []
for ix0,ix1,ix2 in zip(x[0],x[1],x[2]):
out_list = np.array([makexunit(ix0)-makexunit(ix0) for i in p])
if ix2 > 0:
##neutron case
out_list[0],out_list[2],out_list[3] = ContPlLogFFDer_x0_dN([ix0,ix1],[p[0],p[2],p[3]])
out_list[2] = -out_list[2]
elif ix2 < 1:
out_list[1],out_list[2],out_list[4] = ContPlLogFFDer_x0_dN([ix0,ix1],[p[1],p[2],p[4]])
output.append(out_list)
output = list(np.array(output).T)
else:
output = [0 for i in p]
if x[2] > 0:
output[0],output[2],output[3] = ContPlLogFFDer_x0_dN(x,[p[0],p[2],p[3]])
output[2] = -output[2]
elif x[2] < 1:
output[1],output[2],output[4] = ContPlLogFFDer_x0_dN(x,[p[1],p[2],p[4]])
return output
def ContPlLogFitFun_x0_N(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
## physical neutron mass = nM_phys MeV
log_term = (np.log(this_x/(nM_phys**2))+(np.pi*np.array(x[1])/(2*nM_phys)))
return p[0]*this_x - p[1]*this_x * log_term + p[2]*this_a
def ContPlLogFFDer_x0_N(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
log_term = (np.log(this_x/(nM_phys**2))+(np.pi*np.array(x[1])/(2*nM_phys)))
return [this_x,-this_x*log_term,this_a]
def ContPlLogFitFun_x0_P(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
## physical neutron mass = nM_phys MeV
log_term = (np.log(this_x/(nM_phys**2))+(2*np.pi*np.array(x[1])/(nM_phys)))
return p[0]*this_x + p[1]*this_x * log_term + p[2]*this_a
def ContPlLogFFDer_x0_P(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
log_term = (np.log(this_x/(nM_phys**2))+(2*np.pi*np.array(x[1])/(nM_phys)))
return [this_x,this_x*log_term,this_a]
## third dim > 0 means neutron, < 0 means proton
def ContPlLogFitFun_NP(x,p):
if isinstance(x[2],(list,tuple,np.ndarray)):
output = []
for ix0,ix1,ix2 in zip(x[0],x[1],x[2]):
if ix2 > 0:
output.append(ContPlLogFitFun_x0_N([ix0,ix1],[p[0],p[2],p[3]]))
elif ix2 < 1:
output.append(ContPlLogFitFun_x0_P([ix0,ix1],[p[1],p[2],p[4]]))
return np.array(output)
else:
if x[2] > 0:
return ContPlLogFitFun_x0_N(x,[p[0],p[2],p[3]])
elif x[2] < 1:
return ContPlLogFitFun_x0_P(x,[p[1],p[2],p[4]])
def ContPlLogFFDer_NP(x,p):
if isinstance(x[2],(list,tuple,np.ndarray)):
output = []
for ix0,ix1,ix2 in zip(x[0],x[1],x[2]):
out_list = np.array([makexunit(ix0)-makexunit(ix0) for i in p])
if ix2 > 0:
out_list[0],out_list[2],out_list[3] = ContPlLogFFDer_x0_N([ix0,ix1],[p[0],p[2],p[3]])
elif ix2 < 1:
out_list[1],out_list[2],out_list[4] = ContPlLogFFDer_x0_P([ix0,ix1],[p[1],p[2],p[4]])
output.append(out_list)
output = list(np.array(output).T)
else:
output = [0 for i in p]
if x[2] > 0:
output[0],output[2],output[3] = ContPlLogFFDer_x0_N(x,[p[0],p[2],p[3]])
elif x[2] < 1:
output[1],output[2],output[4] = ContPlLogFFDer_x0_P(x,[p[1],p[2],p[4]])
return output
## third dim > 0 means neutron, < 0 means proton
def ContPlLogFitFun_NP_mC2(x,p):
if isinstance(x[2],(list,tuple,np.ndarray)):
output = []
for ix0,ix1,ix2 in zip(x[0],x[1],x[2]):
if ix2 > 0:
output.append(ContPlLogFitFun_x0_N([ix0,ix1],[p[0],-p[2],p[3]]))
elif ix2 < 1:
output.append(ContPlLogFitFun_x0_P([ix0,ix1],[p[1],-p[2],p[4]]))
return np.array(output)
else:
if x[2] > 0:
return ContPlLogFitFun_x0_N(x,[p[0],-p[2],p[3]])
elif x[2] < 1:
return ContPlLogFitFun_x0_P(x,[p[1],-p[2],p[4]])
def ContPlLogFFDer_NP_mC2(x,p):
if isinstance(x[2],(list,tuple,np.ndarray)):
output = []
for ix0,ix1,ix2 in zip(x[0],x[1],x[2]):
out_list = np.array([makexunit(ix0)-makexunit(ix0) for i in p])
if ix2 > 0:
out_list[0],out_list[2],out_list[3] = ContPlLogFFDer_x0_N([ix0,ix1],[p[0],-p[2],p[3]])
elif ix2 < 1:
out_list[1],out_list[2],out_list[4] = ContPlLogFFDer_x0_P([ix0,ix1],[p[1],p[2],p[4]])
output.append(out_list)
output = list(np.array(output).T)
else:
output = [0 for i in p]
if x[2] > 0:
output[0],output[2],output[3] = ContPlLogFFDer_x0_N(x,[p[0],-p[2],p[3]])
elif x[2] < 1:
output[1],output[2],output[4] = ContPlLogFFDer_x0_P(x,[p[1],p[2],p[4]])
return output
def ContPlLogFitFun_x0_N_latmN(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
## mpi / mN
this_mrat = np.array(x[2])**2
log_term = np.log(this_mrat)-np.pi*np.array(x[2])/2
return p[0]*this_x - p[1]*this_x * log_term + p[2]*this_a
def ContPlLogFFDer_x0_N_latmN(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mrat = np.array(x[2])**2
log_term = np.log(this_mrat)-np.pi*np.array(x[2])/2
return [this_x,-this_x*log_term,this_a]
def ContPlLogFitFun_x0_P_latmN(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
## physical neutron mass = this_mN MeV
this_mrat = np.array(x[2])**2
log_term = np.log(this_mrat)-2*np.pi*np.array(x[2])
return p[0]*this_x + p[1]*this_x * log_term + p[2]*this_a
def ContPlLogFFDer_x0_P_latmN(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mrat = np.array(x[2])**2
log_term = np.log(this_mrat)-2*np.pi*np.array(x[2])
return [this_x,this_x*log_term,this_a]
## third dim > 0 means neutron, < 0 means proton
## forth dim is nucleon mass in same units as pion mass
def ContPlLogFitFun_NP_latmN(x,p):
if isinstance(x[2],(list,tuple,np.ndarray)):
output = []
for ix0,ix1,ix2,ix3 in zip(x[0],x[1],x[2],x[3]):
if ix2 > 0:
output.append(ContPlLogFitFun_x0_N_latmN([ix0,ix1,ix3],[p[0],p[2],p[3]]))
elif ix2 < 1:
output.append(ContPlLogFitFun_x0_P_latmN([ix0,ix1,ix3],[p[1],p[2],p[4]]))
return np.array(output)
else:
if x[2] > 0:
return ContPlLogFitFun_x0_N_latmN([x[0],x[1],x[3]],[p[0],p[2],p[3]])
elif x[2] < 1:
return ContPlLogFitFun_x0_P_latmN([x[0],x[1],x[3]],[p[1],p[2],p[4]])
def ContPlLogFFDer_NP_latmN(x,p):
if isinstance(x[2],(list,tuple,np.ndarray)):
output = []
for ix0,ix1,ix2,ix3 in zip(x[0],x[1],x[2],x[3]):
out_list = np.array([makexunit(ix0)-makexunit(ix0) for i in p])
if ix2 > 0:
out_list[0],out_list[2],out_list[3] = ContPlLogFFDer_x0_N_latmN([ix0,ix1,ix3],[p[0],p[2],p[3]])
elif ix2 < 1:
out_list[1],out_list[2],out_list[4] = ContPlLogFFDer_x0_P_latmN([ix0,ix1,ix3],[p[1],p[2],p[4]])
output.append(out_list)
output = list(np.array(output).T)
else:
output = [0 for i in p]
if x[2] > 0:
output[0],output[2],output[3] = ContPlLogFFDer_x0_N_latmN([x[0],x[1],x[3]],[p[0],p[2],p[3]])
elif x[2] < 1:
output[1],output[2],output[4] = ContPlLogFFDer_x0_P_latmN([x[0],x[1],x[3]],[p[1],p[2],p[4]])
return output
def ContFitFun(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return p[0]*this_x+p[1] + p[2]*this_a
def ContFFDer(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return [this_x,makexunit(x[0]),this_a]
def ContFitFun_x0(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return p[0]*this_x + p[1]*this_a
def ContFFDer_x0(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
return [this_x,this_a]
def ContPlLogFitFun_mix(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mix = this_a * this_x
return p[0]*this_x+p[1] + p[2]*this_x * np.log(this_x) + p[3]*this_a + p[4]*this_mix
def ContPlLogFFDer_mix(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mix = this_a * this_x
return [this_x,makexunit(x[0]),this_x*np.log(this_x),this_a,this_mix]
def ContPlLogFitFun_x0_mix(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mix = this_a * this_x
return p[0]*this_x + p[1]*this_x * np.log(this_x) + p[2]*this_a + p[3]*this_mix
def ContPlLogFFDer_x0_mix(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mix = this_a * this_x
return [this_x,this_x*np.log(this_x),this_a,this_mix]
def ContFitFun_mix(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mix = this_a * this_x
return p[0]*this_x+p[1] + p[2]*this_a + p[3]*this_mix
def ContFFDer_mix(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mix = this_a * this_x
return [this_x,makexunit(x[0]),this_a,this_mix]
def ContFitFun_x0_mix(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mix = this_a * this_x
return p[0]*this_x + p[1]*this_a + p[2]*this_mix
def ContFFDer_x0_mix(x,p):
this_x = np.array(x[1])**2
this_a = np.array(x[0])**2
this_mix = this_a * this_x
return [this_x,this_a,this_mix]
def LogFitFun(x,p):
return p[0]*logNA(x[0])+p[1]
def LogFFDer(x,p):
return [logNA(x[0]),makexunit(x[0])]
def LogFitFun_x0(x,p):
return p[0]*logNA(x[0])
def LogFFDer_x0(x,p):
return [logNA(x[0])]
def LinearFF_2D(x,p):
return p[0]*np.array(x[0])+p[1]*np.array(x[1])+p[2]
def LinFFDer_2D(x,p):
return [x[0],x[1],makexunit(x[0])]
def TestTwoVarFitFun(x,p):
return p[0]*x[0]+ p[1]*x[1]**2
def TestTwoVarFFDer(x,p):
return [x[0],x[1]**2]
##ONE STATE FIT FUNCTIONS###
def C2OSFAntiper(t,p,tsink):
A0,Ep = p[0],p[1]
Tmint = tsink-t[0]
try:
return A0*(np.exp(-Ep*t[0])+np.exp(-Ep*Tmint))
except Exception as err:
return A0*((-Ep*t[0]).Exp() + (-Ep*Tmint).Exp())
def C2OSFAntiperDer(t,p,tsink):
A0,Ep = p[0],p[1]
Tmint = tsink-t[0]
try:
return [(np.exp(-Ep*t[0])+np.exp(-Ep*Tmint)),-A0*(t[0]*np.exp(-Ep*t[0])+Tmint*np.exp(-Ep*Tmint))]