1 import scipy
2
3 try:
4 import SloppyCell.Plotting as Plotting
5 except:
6 pass
7
8
9
10
11
12
13
14
15
17 n = len(mat1[0])
18 mat2 = mat1.copy()
19 for ii in range(n):
20 mat2[:,ii] = mat2[:,ii]/scipy.sqrt(scipy.dot(mat2[:,ii],mat2[:,ii]))
21 return mat2
22
24 n = len(mat1[0])
25 mat2 = mat1.copy()
26 for ii in range(n-1):
27 if scipy.dot(mat2[:,ii],mat2[:,ii+1]) < 0.:
28 mat2[:,ii+1] = -1.*mat2[:,ii+1]
29 return mat2
30
32 covarMat = scipy.dot(scipy.transpose(origMat),origMat)
33 n = len(covarMat)
34 jaa = scipy.outer(scipy.diagonal(covarMat),scipy.ones(n,'d'))
35 jbb = scipy.outer(scipy.ones(n,'d'),scipy.diagonal(covarMat))
36 covarMat = covarMat/scipy.sqrt(jaa)
37 covarMat = covarMat/scipy.sqrt(jbb)
38 return covarMat
39
41 n = len(jtj)
42 Dmat = scipy.zeros((n,n),'d')
43 for alpha in range(n):
44 for beta in range(alpha,n):
45 Dmat[alpha,beta] = (jtj[alpha,alpha]-jtj[beta,beta])**2 + 4.*(jtj[alpha,beta]**2)
46 Dmat = Dmat + scipy.transpose(Dmat)
47 for alpha in range(n):
48 Dmat[alpha,alpha] = 4.*(jtj[alpha,alpha]**2)
49 return Dmat
50
52 n = len(jtj)
53 jaa = scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d'))
54 jbb = scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj))
55 crosstermMat = 2.*lambdaMat*scipy.sqrt(1.-lambdaMat**2.)*jtj
56 diagtermMat = (lambdaMat**2.)*jaa + (1.-lambdaMat**2.)*jbb
57 return crosstermMat, diagtermMat
58
60
61 n = len(jtj)
62 jaa = scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d'))
63 jbb = scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj))
64 r2mat = (1./2.)*(-scipy.sqrt(Dmat)+4.*(jtj**2)/scipy.sqrt(Dmat) + (jaa - 4.*jtj*scipy.sqrt((jtj**2)/Dmat) + jbb))
65
66 return r2mat
67
69 n = len(jtj)
70 lambdaMat = -(1./scipy.sqrt(2.))*scipy.sqrt(1.+(-scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d')) + scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj)))/scipy.sqrt(Dmat))
71 return lambdaMat
72
74 n = len(jtj)
75 jaa = scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d'))
76 jbb = scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj))
77 r2mat = (1./2.)*(scipy.sqrt(Dmat)-4.*(jtj**2)/scipy.sqrt(Dmat) + (jaa + 4.*jtj*scipy.sqrt((jtj**2)/Dmat) + jbb))
78 return r2mat
79
81 n = len(jtj)
82 lambdaMat = (1./scipy.sqrt(2.))*scipy.sqrt(1.+(scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d')) - scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj)))/scipy.sqrt(Dmat))
83 return lambdaMat
84
86 n = len(jtj)
87 jaa = scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d'))
88 jbb = scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj))
89 r2mat = (1./2.)*(scipy.sqrt(Dmat)-4.*(jtj**2)/scipy.sqrt(Dmat) + (jaa - 4.*jtj*scipy.sqrt((jtj**2)/Dmat) + jbb))
90 return r2mat
91
93 n = len(jtj)
94 lambdaMat = -(1./scipy.sqrt(2.))*scipy.sqrt(1.+(scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d')) - scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj)))/scipy.sqrt(Dmat))
95 return lambdaMat
96
98 n = len(jtj)
99 jaa = scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d'))
100 jbb = scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj))
101 r2mat = (1./2.)*(-scipy.sqrt(Dmat)+4.*(jtj**2)/scipy.sqrt(Dmat) + (jaa + 4.*jtj*scipy.sqrt((jtj**2)/Dmat) + jbb))
102 return r2mat
103
105 n = len(jtj)
106 lambdaMat = (1./scipy.sqrt(2.))*scipy.sqrt(1.+(-scipy.outer(scipy.diagonal(jtj),scipy.ones(n,'d')) + scipy.outer(scipy.ones(n,'d'),scipy.diagonal(jtj)))/scipy.sqrt(Dmat))
107 return lambdaMat
108
110 lambdas = scipy.arange(-1.,1.,0.001)
111 residuals = [l*l*jtj[a,a]+(1.-l*l)*jtj[b,b]+2.*l*scipy.sqrt(1.-l*l)*jtj[a,b] for l in lambdas]
112 Plotting.plot(lambdas,residuals)
113 Plotting.plot([lMat[a,b]],[r2Mat[a,b]],'o')
114 Plotting.title(str(a)+', '+str(b))
115 return
116
118 minInd = 0
119 minVal = listofNums[minInd]
120 for ii in range(1,len(listofNums)):
121 if listofNums[ii] < minVal:
122 minInd = ii
123 minVal = listofNums[minInd]
124 return minInd
125
127 numMats = len(lambdaMats)
128 numParams = len(lambdaMats[0])
129 minR2 = scipy.zeros((numParams,numParams),'d')
130 minLambda = scipy.zeros((numParams,numParams),'d')
131 for ii in range(numParams):
132 for jj in range(numParams):
133 minIndex = getMinIndex([res2Mats[k][ii,jj] for k in range(numMats)])
134 minR2[ii,jj] = res2Mats[minIndex][ii,jj]
135 minLambda[ii,jj] = lambdaMats[minIndex][ii,jj]
136 return minLambda, minR2
137
139 lambdaMats = [calcLambdaMat_1(jtj,Dmat),calcLambdaMat_2(jtj,Dmat), calcLambdaMat_3(jtj,Dmat),calcLambdaMat_4(jtj,Dmat)]
140 r2Mats = [calcR2mat_1(jtj,Dmat),calcR2mat_2(jtj,Dmat),calcR2mat_3(jtj,Dmat),calcR2mat_4(jtj,Dmat)]
141 lambdaMat, R2Mat = getMinLambdaR2(lambdaMats, r2Mats)
142 return lambdaMat, R2Mat
143