18
1
Note that this challenge requires no handling or understanding of complex numbers.
Given a non-empty square matrix where every element is a two-element (Re,Im) integer list, determine (giving any truthy/falsy values or any two consistent values) whether this represents a Hermitian matrix.
Note that the input is a 3D array of integers; not a 2D array of complex numbers. If your language cannot take a 3D array directly, you may take a flat list (and the n×n or n×n×2 shape if that helps).
A matrix is Hermitian if it equals its own conjugate transpose. In other words, if you flip it across its top-left to bottom-right diagonal and negate the second element of all the two-element leaf-lists, it is identical to the input matrix. Note that the order of flipping and negating is irrelevant, so you may negate first, and flip afterwards.
Walk-though example
This example uses JSON with superfluous white-space to ease reading:
[[ [2, 0] , [2, 1] , [4, 0] ],
[ [2,-1] , [3, 0] , [0, 1] ],
[ [4, 0] , [0,-1] , [1, 0] ]]
Transpose (flip across NW—SE diagonal):
[[ [2, 0] , [2,-1] , [4, 0] ],
[ [2, 1] , [3, 0] , [0,-1] ],
[ [4, 0] , [0, 1] , [1, 0] ]]
Negate second elements of leaf-lists:
[[ [2, 0] , [2, 1] , [4, 0] ],
[ [2,-1] , [3, 0] , [0, 1] ],
[ [4, 0] , [0,-1] , [1, 0] ]]
As this is identical to the input, the matrix is Hermitian.
Test cases
Hermitian
[[[2,0],[2,1],[4,0]],[[2,-1],[3,0],[0,1]],[[4,0],[0,-1],[1,0]]]
[[[1,0],[2,0]],[[2,0],[1,0]]]
[[[1,0],[2,-3]],[[2,3],[1,0]]]
[[[42,0]]]
Non-Hermitian
[[[2,0],[2,1],[4,0]],[[2,-1],[3,0],[0,1]],[[4,0],[0,-1],[1,-1]]]
[[[0,1],[0,2]],[[0,2],[0,1]]]
[[[1,0],[2,3]],[[2,3],[1,0]]]
[[[3,2]]]
@LuisMendo I'm still thinking. Any ideas? – Adám – 2018-02-26T14:38:23.277
For the record, a new Meta-post. (I haven't voted to close, but I see someone has, so I'm curious what the community thinks about this).
– Stewie Griffin – 2018-02-26T15:06:08.997@LuisMendo What do other challenges do when 3+D arrays are required? – Adám – 2018-02-26T15:11:38.563
@LuisMendo Then I don't need to specify anything more, do I? – Adám – 2018-02-26T15:36:32.250
5@Adám I would make it as explicit as possible, but it's up to you. Flexibility in input and output formats is usually desired, but cannot be inferred by default, specially when you say the input is a 3D array of real numbers; not a 2D array of complex numbers. It's not clear how broad your concept of 3D array input format is – Luis Mendo – 2018-02-26T16:04:57.793
@WheatWizard every element is a two-element (Re,Im) integer list – Adám – 2018-02-26T16:47:18.447
3@Adám Can a pair of 2D matrices (one for real part, one for imaginary part) be taken as input? – dylnan – 2018-02-26T16:53:08.747
1@dylnan No. Input has to be a single structure representing some sort of 3-dimensionality where the leaf dimension contains the Re-Im pairs. – Adám – 2018-02-26T18:55:36.687
@LuisMendo What's unclear about the output? – Adám – 2018-02-26T18:56:13.547
@LuisMendo OK, I've updated output. I don't know how to fix input. Any ideas? How about JSON? – Adám – 2018-02-26T19:13:54.220
@Adam My idea would be to leave it flexible as usual, and to clearly state that in the challenge. For example, I don’t see why complex input should be forbidden (but of course keep that restriction if you have a reason). At least I would allow a flat array with shape specified separately, or two separate matrices, or of course a 3D array directly – Luis Mendo – 2018-02-26T19:45:22.683
@LuisMendo You don't need shape, though, as the last dimension is always length 2, and the first two dimensions have same length. Anyway, added. – Adám – 2018-02-26T19:47:45.780
@LuisMendo No. Added. – Adám – 2018-02-26T20:08:19.937