
// Make newform 16.3.f.a in Magma, downloaded from the LMFDB on 11 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_16_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_16_f_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_16_3_f_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_16_3_f_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [8, -8, 6, -6, 3, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [-8, 6, 0, 3, 0, 1], [4, 2, 0, -3, 0, -1], [-8, 2, -4, 3, 0, 1], [20, -10, 12, -9, 4, -3], [14, -4, 8, -5, 2, -3]];
    Rf_basisdens := [1, 4, 4, 4, 4, 2];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_16_f();"
function MakeCharacter_16_f()
    N := 16;
    order := 4;
    char_gens := [15, 5];
    v := [2, 3];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_16_f_Hecke();"
function MakeCharacter_16_f_Hecke(Kf)
    N := 16;
    order := 4;
    char_gens := [15, 5];
    char_values := [[-1, 0, 0, 0, 0, 0], [0, 0, 0, -1, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 3;
    raw_aps := [[0, 0, 1, 0, 0, 0], [-1, 0, -1, 0, 0, 1], [0, -1, -1, 0, -1, -1], [-1, 1, 1, 1, 3, -1], [-2, -1, 2, -3, -3, 2], [-2, -3, -1, 2, 5, -1], [-2, 4, -2, -2, 0, 2], [5, -2, -3, -6, -2, -1], [11, -3, 1, 1, -1, -1], [0, 7, 1, -8, -9, 1], [0, 0, 4, -20, 0, 4], [14, 5, 17, -2, 5, -7], [0, 0, -12, -4, 0, -12], [-24, 1, -8, -17, 15, -8], [-8, -8, -4, 36, 8, 12], [10, 1, -3, -14, 1, 5], [36, -3, 4, 35, -5, 4], [2, 1, -5, 6, 9, -5], [-39, -14, -23, 30, -14, -5], [-37, -19, 17, 17, 15, -17], [-2, -2, 20, 0, 2, 24], [24, 24, 32, 48, -24, -16], [47, 0, -9, -56, 0, 9], [18, 18, 28, -16, -18, -8], [-2, 4, -26, -26, -48, 26], [10, -15, -43, -38, -15, 13], [-77, 37, -15, -15, 7, 15], [-54, -5, 6, -55, -7, 6], [56, 15, -15, 56, 15, -15], [26, -24, 20, 20, 16, -20], [16, 16, 60, -76, -16, 28], [-31, 26, 49, 54, 26, 3], [-28, -28, -44, 20, 28, 12], [-28, -19, -20, 11, 59, -20], [-76, 19, 19, 76, 19, 19], [67, -43, 1, 1, -41, -1], [-18, 29, -25, -22, 21, -25], [15, -16, -1, 0, -16, -31], [15, 17, -31, -31, -45, 31], [126, -35, 15, 146, 5, 15], [39, -8, -1, -32, -8, -15], [92, -5, -37, -124, -5, 27], [-16, -16, -60, 76, 16, -28], [14, -12, -2, -2, -16, 2], [-40, -9, 39, 88, -9, -57], [39, 9, 33, 33, 75, -33], [-93, 36, 11, 68, 36, 61], [0, 0, -12, 60, 0, -12], [-41, -40, -121, -40, -40, 41], [140, -37, 3, -100, -37, -77], [30, 30, 100, 48, -30, 40], [8, 8, 32, -176, -8, 16], [46, 84, 6, 6, 96, -6], [-16, 25, 24, -65, -73, 24], [-6, -24, -28, -28, -80, 28], [259, -11, -15, -15, -41, 15], [-232, -49, -31, -152, 111, -31], [-56, -56, -124, -36, 56, -12], [-218, 45, 17, 190, 45, 73], [-26, -26, -84, 56, 26, -32], [-106, 39, 18, -163, -75, 18], [184, 55, 119, -120, 55, -9], [-119, 18, 81, 182, 18, -45], [-249, -55, 17, 17, -21, -17], [8, 8, -36, 100, -8, -52], [76, 67, -51, 60, 35, -51], [238, 111, 18, 109, -147, 18], [162, -128, 64, 64, 0, -64], [-36, -75, -68, 107, 211, -68], [-16, -9, 49, -56, -89, 49], [106, 56, -68, -68, -80, 68], [355, 21, 49, 49, 119, -49], [56, 56, -4, -92, -56, -116], [218, -47, -83, -254, -47, -11], [-140, 77, -12, -205, -53, -12], [0, 0, 72, 408, 0, 72], [-154, 29, 41, 166, 29, 17], [-252, -53, -3, -196, 59, -3], [-114, 52, 22, 22, 96, -22], [120, 120, 236, 52, -120, -4], [-43, -34, 21, 98, -34, -89], [4, -13, -181, -172, -13, 155], [72, 72, 16, -224, -72, -128], [-82, 20, 54, 54, 128, -54], [127, -31, 81, 81, 131, -81], [198, -137, 82, 253, -27, 82], [-2, 68, -90, -90, -112, 90], [-16, -16, 100, -20, 16, 132], [-260, 35, 21, -316, -77, 21], [-104, -104, -88, 136, 104, 120], [-7, -30, -207, -170, -30, 147], [64, 64, 84, 220, -64, -44], [-209, -79, 1, 1, -77, -1], [-180, 85, -108, -157, 131, -108], [-125, 36, -37, 52, 36, 109], [-261, -19, 33, 33, 47, -33], [162, 97, 27, 38, -151, 27], [116, 116, 244, 84, -116, 12], [-70, -117, -26, 73, 169, -26], [18, -15, 27, 6, -39, 27], [153, 18, 217, 46, 18, -181], [258, -111, 35, 334, 41, 35], [477, 22, 101, -398, 22, -57], [-52, -52, -52, 364, 52, 52], [-18, -1, -86, 69, 173, -86], [-14, 80, -24, -24, 32, 24], [-326, 203, -90, -439, -23, -90], [242, 176, -56, -56, 64, 56], [-49, -79, -111, -111, -301, 111], [-166, -166, -132, 0, 166, 200], [48, 48, 80, -496, -48, -16], [108, 59, 131, -36, 59, -13], [-102, -102, -236, 520, 102, -32], [38, -89, 138, -11, -187, 138], [627, 5, -31, -31, -57, 31], [14, -76, 142, 142, 208, -142], [-287, 26, 17, 278, 26, 35], [171, 125, 177, 177, 479, -177], [-128, -105, 41, -64, 23, 41], [567, 40, 159, -448, 40, -79], [-52, -117, -37, 132, -117, -197], [-498, -12, -82, -82, -176, 82], [78, -59, 17, -2, -59, -135], [-616, -15, 104, -705, -193, 104], [-157, -188, -325, 20, -188, -51], [118, -283, 31, 370, 221, 31], [2, -87, -83, 2, -87, -91], [-8, -8, -288, -80, 8, -272], [647, 9, 17, 17, 43, -17], [84, 11, -91, 164, 171, -91], [-141, 4, -93, 44, 4, 101], [199, 105, -191, -191, -277, 191], [40, 40, 208, 672, -40, 128], [124, 91, 251, 36, 91, -69], [-128, -128, -132, -300, 128, 124], [-18, -44, -66, -66, -176, 66], [-238, -199, -515, -78, -199, 117], [-201, -88, -129, 160, -88, -47], [-38, 25, 227, -290, -479, 227], [-72, -72, -12, -500, 72, 132], [202, -69, -122, 393, 313, -122], [-174, 105, 405, 474, 105, -195], [39, 105, -175, -175, -245, 175], [430, 63, 74, 293, -211, 74], [-126, -127, -37, 38, 201, -37], [295, -375, 65, 65, -245, -65], [218, 177, 381, -14, 177, -27], [132, 132, 28, -276, -132, -236], [-140, -147, -28, 35, 203, -28], [-32, -32, -204, 828, 32, -140], [-252, -21, 109, -340, -197, 109], [370, 112, -184, -184, -256, 184], [225, 74, 233, -66, 74, -85], [-425, -39, -143, -143, -325, 143], [232, 33, -56, 255, 79, -56], [168, 168, 516, -100, -168, 180], [-5, 173, 33, 33, 239, -33], [-706, 132, 86, 86, 304, -86], [-18, -18, 4, -656, 18, 40], [-184, 415, -223, -376, 31, -223], [-55, 210, 209, 54, 210, 211], [180, 180, 428, 700, -180, 68], [-225, -223, 193, 193, 163, -193], [204, -267, 260, 211, -253, 260], [734, -156, 30, 30, -96, -30], [-1161, -71, 177, 177, 283, -177], [144, 144, 368, -336, -144, 80], [-494, -103, -211, 386, -103, 5]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_16_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_16_3_f_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_16_3_f_a(:prec:=6)
    chi := MakeCharacter_16_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_16_3_f_a();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_16_3_f_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_16_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;