JuliaMath / Hadamard.jl

Fast Walsh-Hadamard transforms for the Julia language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hadamard seems unnecessarily slow

alanedelman opened this issue · comments

the usual seems so much faster

function had(n) begin
        z=1
        for i=1:log2(n)
           z= kron(z,[1 1;1 -1])
        end
    end
    z
    end

n=1024
@time hadamard(n);
@time had(n);
elapsed time: 0.026615681 seconds (16779744 bytes allocated)
elapsed time: 0.006890752 seconds (11186592 bytes allocated)  

While on the subject of hadamard(n), it would be nice if a matrix would
come for n=1,2 and every multiple of 4 , say up to 1000, times a power of 2
(except of course 668 and such)

Fair enough. The hadamard function just takes the FWHT of the columns of the identity matrix, which is certainly not the most efficient way to do things. (Does it matter? I thought that performance-sensitive cases would mainly use the FWHT.)

Regarding non powers of two, I could certainly have a table of Hadamard matrices for various factors, and then build other sizes from Kronecker products of these. However, I have three concerns:

  • Is this actually useful? Who needs Hadamard matrices of non-power-of-two sizes?
  • Does it matter what ordering I choose? Ordering the factors in different ways seems like it will produce different matrices (albeit equivalent modulo reordering).
  • Does it matter which inequivalent Hadamard matrix I choose, in the common case where there are inequivalent choices for a given factor?

Note that it is even faster on my machine to do a "power-by-squaring" algorithm, which keeps the size of the matrices smaller until the last Kronecker product:

function hadsq(m)
    m == 0 && return reshape([1],1,1)
    m == 1 && return H2
    z = hadsq(m >> 1)
    z = kron(z, z)
    iseven(m) ? z : kron(H2, z)
end

Is this actually useful? Who needs Hadamard matrices of non-power-of-two sizes?

People use them from time to time for experimental design. Not saying it's the world's highest priority.

Does it matter what ordering I choose? Ordering the factors in different ways seems like it will produce different matrices (albeit equivalent modulo reordering).

Does it matter which inequivalent Hadamard matrix I choose, in the common case where there are inequivalent choices for a given factor?

I would say for starters, any one is fine, in part because I'm not sure it's practical to get an exhaustive list of non-Hadamard equivalent matrices for all n anyway, and generating Hadamard
equivalent matrices is easy enough for the user.

cute

On Mon, Jul 7, 2014 at 9:28 AM, Steven G. Johnson notifications@github.com
wrote:

Note that it is even faster on my machine to do a "power-by-squaring"
algorithm, which keeps the size of the matrices smaller until the last
Kronecker product:

function hadsq(m)
m == 0 && return reshape([1],1,1)
m == 1 && return H2
z = hadsq(m >> 1)
z = kron(z, z)
iseven(m) ? z : kron(H2, z)
end


Reply to this email directly or view it on GitHub
#2 (comment).

Should be mostly fixed. There are a few known orders of Hadamard matrices that aren't supported because I can't yet find the matrices online, but hopefully that will be fixed once a bug in the webhadamard site is fixed (their matrices are currently truncated).