'Hadamard matrix in Ruby is not unitary although it should be?

Even when trying to define Hadamard matrix in Ruby by using rational numbers (which helps us sometimes to keep good precision) the result is that Hadamard matrix is not unitary.

m = (1 / Math.sqrt(2)).to_r * Matrix[[1,  1],
                                     [1, -1]]

Then using available class Matrix method:

m.unitary?

results in false and result of m*m multiplication is as below:

Matrix[[2535301200456458353478625262249/2535301200456458802993406410752, 0/1], 
       [0/1, 2535301200456458353478625262249/2535301200456458802993406410752]]

But in reality Hadamard is unitary matrix.

How to fix that and other cases in best way globally?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source