[R] Hadamard transformation
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Sep 18 20:16:13 CEST 2023
Às 18:45 de 18/09/2023, mohan radhakrishnan escreveu:
> Hello,
>
> I am attempting to port the R code which is an answer to
> https://codegolf.stackexchange.com/questions/194229/implement-the-2d-hadamard-transform
>
>
> function(M){for(i in 1:log2(nrow(M)))T=T%x%matrix(1-2*!3:0,2)/2; print(T);
> T%*%M%*%T}
>
> The code, 3 inputs and the corresponding outputs are shown in
> https://tio.run/##PYyxCsIwFEX3fkUcAu@VV7WvcSl2dOwi8QNqNSXQJhAqrYjfHoOIwz3D4XBDNOJYiGgerp@td9Diy/gAVlgnynr0A4MLfkkeUTdarnLq5mBXKAvON1W9J8YdZ1rmsk3T72jgV/TAVBHTAROYrs/00@jz5YSY/aOSFKmvGP1yD9sk4Wa7ARSSRowf
>
> These are the inputs.
>
> f(matrix(c(2,3,2,5),2,2,byrow=TRUE))
> f(matrix(1,4,4))
> f(lower.tri(diag(4),T))
>
> My attempt to port this R code to another framework(Tensorflow) was only
> partially successful
> because I didn't fully understand the cryptic R code. The second input
> shown above works after
> hacking Tensorflow for a long time.
>
> My question is this. Can anyone code this in a clear way so that I can
> understand ? I understand
> Kronecker Product and matrix multiplication and can port that code but I am
> missing something as the same ported code does not work for all inputs.
>
> Thanks,
> Mohan
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,
Is this what you want?
(I have changed the notation a bit.)
H <- function(M){
H0 <- 1
Transf <- matrix(c(1, 1, 1, -1), 2L)
for(i in 1:log2(nrow(M))) {
H0 <- H0 %x% Transf/2
}
H0 %*% M %*% H0
}
x <- matrix(c(2, 3, 2, 5), 2, 2, byrow = TRUE)
y <- matrix(1, 4, 4)
z <- lower.tri(diag(4), TRUE)
z[] <- apply(z, 2, as.integer)
H(x)
H(y)
H(z)
Hope this helps,
Rui Barradas
More information about the R-help
mailing list