In [109]:
Pkg.test("KroneckerMatrices")
In [1]:
using KroneckerMatrices
In [5]:
using Gadfly
In [6]:
using DataFrames
In [29]:
A = rand(3,3)
B = ones(4,4)
Cimpl = KroneckerProduct(A,B)
Cexpl = kronproduct(A,B)
Out[29]:
In [50]:
Λimpl,Vimpl = eig(Cimpl)
Λimpl,Vimpl = vec(convert(Matrix{eltype(Λimpl)},Λimpl)),convert(Matrix{eltype(Vimpl)},Vimpl)
Λexpl,Vexpl = eig(Cexpl)
Λimplflat = [abs(λ)>10*eps() ? 1. : 0. for λ in Λimpl]
Λexplflat = [abs(λ)>10*eps() ? 1. : 0. for λ in Λexpl]
Out[50]:
In [61]:
maximum(abs(Vimpl*diagm(Λimplflat)*inv(Vimpl)-Vexpl*diagm(Λexplflat)*inv(Vexpl)))
Out[61]:
In [57]:
round(3*abs(Vexpl*diagm(Λexplflat)*inv(Vexpl)))
Out[57]:
In [ ]:
#ΛU = diagm([λ >10*eps() ? 1. : 0. for λ in convert(Matrix{eltype(C)}eigvals(C)])
#ΛV = diagm([λ >10*eps() ? 1. : 0. for λ in eigvals(D)])
#err = maximum(abs(U*U'-V*V'))
In [21]:
Out[21]:
In [12]:
Λimpl[3,1]
Out[12]:
In [5]:
taco[:vectors]
Out[5]:
In [126]:
U*U'*V*V'-V*V'*U*U'
Out[126]:
In [121]:
methods(step)
Out[121]:
In [91]:
convert(Matrix{Float64},eigvecs(C))-D
Out[91]:
In [94]:
StridedMatrix
Out[94]:
In [76]:
hist
Out[76]:
In [31]:
V*U-U*V
Out[31]:
In [12]:
kronpower(A::Matrix,n::Integer) = n==1 ? A : kron([A for _ in 1:n]...)
Out[12]:
In [14]:
kronpower(A,2)
Out[14]:
In [12]:
taco = KroneckerProduct(Diagonal([1,2]),ones(Int64,2,2),ones(Int64,2,2))
Out[12]:
In [71]:
A = [[BigInt(4) BigInt(2)];[BigInt(1) BigInt(3)]]
Out[71]:
In [9]:
methods(eigvals)
Out[9]:
In [6]:
methods(eigvecs)
Out[6]:
In [107]:
chol([[1 1];[1 -1]])
In [92]:
det(Diagonal([1:20])),det(Diagonal([1:21]))
Out[92]:
In [200]:
type Totemyst{T}
As::Vector{AbstractMatrix{T}}
Totemyst(As::Vector{AbstractMatrix{T}}) = new(As)
end
In [230]:
type Boldygomet{T} <:AbstractMatrix{T}
vals::Vector{AbstractMatrix{T}}
end
In [236]:
import Base:size
In [ ]:
size(A::Boldygomet{Float64})
In [244]:
type KroneckerProducty{T}
terms::Vector{AbstractMatrix{T}}
end
In [245]:
function KroneckerProducty(terms...)
return KroneckerProduct(AbstractMatrix{Float64}[terms...])
end
Out[245]:
In [251]:
KroneckerProducty(Any[Diagonal([3,4]),rand(5,6),rand(5,5)]...)
Out[251]:
In [16]:
methods(factor)
Out[16]:
In [264]:
T = promote_type(Int64,Int64,Float64,Complex64)
Out[264]:
In [51]:
A = [[1 0];
[0 4]]
B = [[1 2];
[0 4]]
C = [[1 2];
[3 4]]
[eltype(det(M)) for M in (A,B,C)]
Out[51]:
In [50]:
det(C)
Out[50]:
In [58]:
zero(Int64)
Out[58]:
In [52]:
taco = lufact(A)
Out[52]:
In [53]:
methods(lufact)
Out[53]:
In [57]:
one(Int64)/one(Int64)
Out[57]:
In [193]:
bob = Totem(rand(3,3),rand(3,3))
In [222]:
Any[Diagonal([1,2,4]) rand(3,3)]
Out[222]:
In [225]:
abstract Lost{T} <: AbstractMatrix{T}
In [ ]:
type Lope{T} <:Lost{T}
end
In [189]:
function h(tacos::Matrix...)
return tacos[1]
end
Out[189]:
In [190]:
h(rand(4,5),rand(2,2))
Out[190]:
In [226]:
tom = Tote(AbstractMatrix{Float64}[rand(2,2), rand(2,2), rand(3,3)])
Out[226]:
In [169]:
bob = [A::Matrix{Float64} for A in Any[rand(2,2), rand(2,2), rand(3,3)]]
Out[169]:
In [170]:
Out[170]:
In [113]:
A = rand(2,3)
B = rand(3,4)
C = KroneckerProduct(A,B)
Out[113]:
In [109]:
sizes(C) = [size(term) for term in terms(C)]
Out[109]:
In [114]:
terms(C)
Out[114]:
In [149]:
tacos = [(2,2),(3,5),(4,4)]
Out[149]:
In [150]:
x,y = [[S...] for S in zip(tacos...)]
Out[150]:
In [151]:
y
Out[151]:
In [1]:
using Iterators
In [35]:
Ms = Any[rand(2,2) ones(3,3) rand(2,2)]
[prod([λs...])::Float64 for λs in product([eigvals(M) for M in Ms]...)]
Out[35]:
In [87]:
Ms,Ns = [[S...] for S in zip([(2,2),(3,3),(5,5)]...)]
Out[87]:
In [97]:
all(Ms .== Ns)
Out[97]:
In [89]:
K = prod(Ms)
Out[89]:
In [90]:
Ks = [div(K,M) for M in Ms]
Out[90]:
In [37]:
[prod([taco...]) for taco in product(tacos...)]
Out[37]:
In [151]:
A = ones(2,2)
B = rand(3,3)
C = kronproduct(A,B)
D = kronsum(A,B)
Out[151]:
In [45]:
EC,VC = eig(C)
ED,VD = eig(D)
Out[45]:
In [53]:
maximum(abs(C*D-D*C))
Out[53]:
In [39]:
all(issquare,terms(C))
Out[39]:
In [131]:
function spyy(A::AbstractMatrix)
M,N = size(A)
is, js, values = findnz(A)
df = DataFrames.DataFrame(i=is, j=js, value=values)
plot(df, x="j", y="i", color="value",
Coord.cartesian(yflip=true,fixed=true,ymin=1,ymax=M,xmin=1,xmax=N),
Guide.xlabel(""),Guide.ylabel(""),
Scale.color_continuous,
Theme(grid_line_width= 0pt),
Scale.x_continuous(maxvalue = N-5),
Scale.y_continuous(maxvalue = M-5),
Geom.rectbin,
Stat.identity)
end
Out[131]:
In [142]:
A = [1 1; 1 0]
Out[142]:
In [143]:
size(kronpower(A,5))
Out[143]:
In [147]:
tacobeast = spyy(kronpower(A,5))
Out[147]:
In [135]:
A = [1 1 1; 1 1 0; 1 0 0]
spyy(kronpower(A,3))
Out[135]:
In [82]:
boast = tacobeast.scales[2]
Out[82]:
In [92]:
boast.vars
Out[92]:
In [95]:
Scale.ContinuousScale()
In [32]:
A = [1 1 1 1 1; 1 1 1 1 0 ; 1 1 1 0 0; 1 1 0 1 1; 1 0 0 1 0]
Out[32]:
In [34]:
spyy(kronpower(A,2))
Out[34]:
In [172]:
P = [1,2,3,4]
N::Int = sqrt(length(P))
In [167]:
In [168]:
N::Int = 3
In [207]:
a = [1,2,3]
b = [1,1]
c = [4,3,2,1]
m = kron(kron(a,b),c)'
spark(m),m
Out[207]:
In [218]:
[mod(i-1,length(c)) for i in 1:24]'
Out[218]:
In [220]:
[mod(div(i-1,length(c)),length(b)) for i in 1:24]'
Out[220]:
In [222]:
[mod(div(i-1,length(c)*length(b)),length(a)) for i in 1:24]'
Out[222]:
In [230]:
function hindexes(i,widths)
indexes = zeros(Int64,length(widths))
q = 1
for (level,width) in enumerate(widths)
indexes[level] = mod(div(i-1,q),width)+1
q = q*width
end
return indexes
end
Out[230]:
In [293]:
Out[293]:
In [313]:
function hindexes(i::Int64,widths)
return [(mod(div(i-1,u),w)+1)::Int64 for (u,w) in zip(cumprod([1, widths]),widths)]
end
Out[313]:
In [275]:
widths = [4,2,3]
divisors = cumprod([1, widths])
Out[275]:
In [314]:
In [304]:
tacos = [(1,2),(3,4),(5,6)]
Out[304]:
In [305]:
x,y = zip(tacos...)
x,y
Out[305]:
In [312]:
typeof((1,"foo",2.5))
Out[312]:
In [292]:
widths = [4,2,3]
taco = [hindexes(i,widths) for i=1:24]
Out[292]:
In [251]:
x,y = zip([(1,2),(3,4),(5,6)]...)
Out[251]:
In [254]:
map(*,[(1,2),(3,4),(5,6)]...)
Out[254]:
In [ ]: