#OK to post homework
#Nick Belov, Feb 16nd 2025, Assignment 6
read "C7.txt";
read "C6.txt";

AllHM2 := proc(K)
    local a, b, c, d, out;
    out := [];

    for a from 1 to K do
        for b from 1 to K do
            for c from 1 to K do
                for d from 1 to K do
                    out := [ op(out), [[a, b + I*c], [b - I*c, d]] ];
                od;
            od;
        od;
    od;

    return out;
end proc:

RealSV2 := proc(K)
    local a, b, out, d;
    out := [];

    for a from 1 to K do
        for b from 1 to K do
            d := sqrt(a^2 + b^2);
            out := [ op(out), [ a/d, b/d ] ];
        od;
    od;

    return out;
end proc:

ComCheck := proc(A, B)
    local C;
    C := Com(A, B);
    return C = [[0,0],[0,0]];
end proc:

UncertaintyContest := proc(K)
    local M, V;
    local i, j, c, ratio;
    local minVal, maxVal, minTrip, maxTrip;
    M := AllHM2(K);
    V := RealSV2(K);
    minVal :=  infinity;
    maxVal := -infinity;
    minTrip := NULL;
    maxTrip := NULL;
    for i from 1 to nops(M) do
        for j from 1 to nops(M) do
            if not ComCheck(M[i], M[j]) then
                for c from 1 to nops(V) do
                    ratio := CheckUP(M[i], M[j], V[c]);
                    if ratio < minVal then
                        minVal := ratio;
                        minTrip := [M[i], M[j], V[c]];
                    fi;
                    if ratio > maxVal then
                        maxVal := ratio;
                        maxTrip := [M[i], M[j], V[c]];
                    fi;
                od;
            fi;
        od;
    od;
    return [minTrip, maxTrip];
end proc:

#UncertaintyContest(2) = [[[[1, 2 + I], [2 - I, 2]], [[2, 2 + I], [2 - I, 1]], [sqrt(2)/2, sqrt(2)/2]], [[[1, 1 + I], [1 - I, 1]], [[1, 1 + 2*I], [1 - 2*I, 2]], [(2*sqrt(5))/5, sqrt(5)/5]]]
#3 is taking too long!