Skip to content

Commit 411d028

Browse files
committed
Added rand
Changed spectradatabase to use eigvalrand
1 parent 0f927db commit 411d028

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/InvariantEnsembles.jl

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ end
5555
InvariantEnsemble(basis,d::Vector)=InvariantEnsemble(basis,Interval(d))
5656

5757

58+
## n, where the invariant ensemble is n x n
59+
Base.size(ie::InvariantEnsemble)=size(ie.basis,2),size(ie.basis,2)
60+
Base.size(ie::InvariantEnsemble,n)=size(ie.basis,2)
61+
62+
63+
5864
#Takes in list of OPs, constructs phis
5965
# can be unstable for large n
6066
function InvariantEnsemble(p::Array{IFun},V::Function,d,n::Integer)
@@ -77,6 +83,11 @@ function InvariantEnsemble(p::Array{IFun},V::Function,d,n::Integer)
7783
end
7884

7985

86+
87+
88+
89+
90+
##Construct invariant ensemble from weight, first moment and recurrence relationship
8091
function adaptiveie(w,μ0,α,β,d)
8192
for logn = 4:20
8293
m=2^logn + 1
@@ -177,6 +188,20 @@ function RandomMatrices.eigvalrand(p::InvariantEnsemble,m::Integer)
177188
hcat([samplespectra(p.basis,p.domain,plan,pts) for i=1:m]...)'
178189
end
179190

191+
RandomMatrices.eigvalrand(p::InvariantEnsemble)=[eigvalrand(p,1)[1,:]...]
192+
193+
function Base.rand(p::InvariantEnsemble)
194+
Q=rand(Haar(2),size(p,1))
195+
Q*diagm(eigvalrand(p))*Q'
196+
end
197+
198+
function Base.rand(p::InvariantEnsemble,m::Integer)
199+
ei = eigvalrand(p,m)
200+
[( Q=rand(Haar(2),size(p,1));
201+
Q*diagm([ei[k,:]...])*Q') for k=1:size(ei,1)]
202+
end
203+
204+
180205
samplespectra(q::Array{Float64,2},d)=samplespectra(q,d,plan_chebyshevtransform(q[:,1]),chebyshevpoints(size(q,1)))
181206

182207
function samplespectra(q::Array{Float64,2},d,plan::Function,pts)
@@ -271,25 +296,25 @@ function spectradatabase(str,n::Integer,m::Integer)
271296

272297
ie = InvariantEnsemble(str,n)
273298

274-
A=hcat([samplespectra(ie) for i=1:m+1]...)';
275-
writedlm(file,A,',');
299+
A=eigvalrand(ie,m+1)
300+
writedlm(file,A,',')
276301
else
277-
A = readdlm(file,',');
302+
A = readdlm(file,',')
278303

279304
if ( m > size(A)[1])
280305
warn(string(m) * " is greater than current samples " * string(size(A)[1]) * ". Generating more samples");
281306

282307
ie = InvariantEnsemble(str,n)
283308

284-
An=hcat([samplespectra(ie) for i=1:(m-size(A)[1]+1)]...)';
309+
An=eigvalrand(ie,m-size(A)[1]+1)
285310

286-
A = vcat(A,An);
311+
A = vcat(A,An)
287312

288-
writedlm(file,A,',');
313+
writedlm(file,A,',')
289314
end
290315
end
291316

292-
return A[1:m,:];
317+
return A[1:m,:]
293318
end
294319

295320

0 commit comments

Comments
 (0)