
// Make newform 384.4.f.h in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_384_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_384_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_384_4_f_h();".
// 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_384_4_f_h();".
// 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 := [1, 0, 0, 0, 23, 0, 0, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 96, 0, 0, 0, 4, 0], [0, -38, 0, -182, 0, -2, 0, -8], [0, 38, 0, -182, 0, 2, 0, -8], [0, -19, -110, 91, 0, -1, -5, 4], [0, 58, 0, 278, 0, 2, 0, 12], [0, 58, 0, -278, 0, 2, 0, -12], [92, 0, 0, 0, 8, 0, 0, 0]];
    Rf_basisdens := [1, 5, 5, 5, 5, 5, 5, 5];
    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_384_f();"
function MakeCharacter_384_f()
    N := 384;
    order := 2;
    char_gens := [127, 133, 257];
    v := [1, 1, 1];
    // 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_384_f_Hecke();"
function MakeCharacter_384_f_Hecke(Kf)
    N := 384;
    order := 2;
    char_gens := [127, 133, 257];
    char_values := [[-1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 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 := 4;
    raw_aps := [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, -2, 0, 0, 1, 0], [0, 0, -3, 0, 0, 2, 0, 0], [0, -8, 0, -1, -2, 0, 0, 0], [0, 7, 0, 4, 8, 0, 0, 0], [0, 0, 16, 0, 0, -2, 0, 0], [0, 0, 0, -7, 0, 0, 16, 0], [80, 0, 0, 0, 0, 0, 0, 2], [0, 0, 0, 14, 0, 0, -23, 0], [0, 0, -7, 0, 0, -6, 0, 0], [0, -31, 0, 28, 56, 0, 0, 0], [0, 0, 16, 0, 0, -24, 0, 0], [0, 0, 0, -49, 0, 0, -16, 0], [0, 0, 0, 0, 0, 0, 0, -16], [0, 0, 0, 70, 0, 0, 45, 0], [0, 0, 0, 5, 10, 0, 0, 0], [0, 7, 0, 68, 136, 0, 0, 0], [0, 0, 0, 91, 0, 0, -32, 0], [80, 0, 0, 0, 0, 0, 0, 50], [-154, 0, 0, 0, 0, 0, 0, -24], [0, 0, 189, 0, 0, 2, 0, 0], [0, -168, 0, -37, -74, 0, 0, 0], [0, 0, -128, 0, 0, 26, 0, 0], [714, 0, 0, 0, 0, 0, 0, -16], [0, 0, 0, 30, 0, 0, -143, 0], [0, 0, -7, 0, 0, 186, 0, 0], [0, -384, 0, 5, 10, 0, 0, 0], [0, -329, 0, -60, -120, 0, 0, 0], [0, 0, 0, 0, 0, -140, 0, 0], [0, 0, 49, 0, 0, 106, 0, 0], [0, -224, 0, 137, 274, 0, 0, 0], [0, 0, 112, 0, 0, -124, 0, 0], [0, 0, 0, 405, 0, 0, 0, 0], [0, 0, 0, -546, 0, 0, -47, 0], [0, 0, -231, 0, 0, -198, 0, 0], [0, -301, 0, -172, -344, 0, 0, 0], [0, 0, 0, -203, 0, 0, -48, 0], [1456, 0, 0, 0, 0, 0, 0, 62], [0, 0, 0, -562, 0, 0, -119, 0], [0, -384, 0, -263, -526, 0, 0, 0], [0, 189, 0, -212, -424, 0, 0, 0], [-928, 0, 0, 0, 0, 0, 0, 12], [1918, 0, 0, 0, 0, 0, 0, 88], [0, 0, 0, 630, 0, 0, 245, 0], [0, 0, -503, 0, 0, -294, 0, 0], [0, 0, 0, -161, 0, 0, -352, 0], [0, 0, -755, 0, 0, 98, 0, 0], [0, -392, 0, 339, 678, 0, 0, 0], [0, 497, 0, -36, -72, 0, 0, 0], [0, 0, -672, 0, 0, 274, 0, 0], [3840, 0, 0, 0, 0, 0, 0, 80], [686, 0, 0, 0, 0, 0, 0, -104], [0, -392, 0, -129, -258, 0, 0, 0], [0, 0, -96, 0, 0, 92, 0, 0], [2832, 0, 0, 0, 0, 0, 0, -246], [0, 0, 0, 422, 0, 0, 269, 0], [0, 0, -427, 0, 0, -206, 0, 0], [0, 717, 0, 364, 728, 0, 0, 0], [0, 0, 1120, 0, 0, 22, 0, 0], [0, 0, 0, 609, 0, 0, -384, 0], [0, 0, 0, 278, 0, 0, -539, 0], [0, 0, 0, -1085, 0, 0, 384, 0], [-4368, 0, 0, 0, 0, 0, 0, -138], [-2786, 0, 0, 0, 0, 0, 0, -136], [0, 0, 0, 294, 0, 0, 13, 0], [0, 0, 0, -7, 0, 0, -1184, 0], [-1290, 0, 0, 0, 0, 0, 0, -280], [0, -776, 0, -705, -1410, 0, 0, 0], [0, -2401, 0, 228, 456, 0, 0, 0], [0, 0, 576, 0, 0, -952, 0, 0], [-2992, 0, 0, 0, 0, 0, 0, -302], [0, 0, -631, 0, 0, -294, 0, 0], [0, 2121, 0, -68, -136, 0, 0, 0], [0, 0, 0, -217, 0, 0, 336, 0], [-2912, 0, 0, 0, 0, 0, 0, 340], [0, 0, 0, 742, 0, 0, -1251, 0], [0, -385, 0, 420, 840, 0, 0, 0], [0, 0, 560, 0, 0, 290, 0, 0], [-5110, 0, 0, 0, 0, 0, 0, -80], [0, -168, 0, 859, 1718, 0, 0, 0], [0, 1033, 0, 252, 504, 0, 0, 0], [-3840, 0, 0, 0, 0, 0, 0, 80], [7322, 0, 0, 0, 0, 0, 0, 272], [0, 0, -519, 0, 0, 378, 0, 0], [0, 2136, 0, -57, -114, 0, 0, 0], [0, 0, 1904, 0, 0, 438, 0, 0], [506, 0, 0, 0, 0, 0, 0, 336], [0, 0, 0, 478, 0, 0, 33, 0], [0, 0, -455, 0, 0, 1338, 0, 0], [0, -840, 0, -1037, -2074, 0, 0, 0], [-10304, 0, 0, 0, 0, 0, 0, -104], [0, 0, -483, 0, 0, 1602, 0, 0], [0, -64, 0, 397, 794, 0, 0, 0], [0, 0, 0, -2681, 0, 0, -32, 0], [1008, 0, 0, 0, 0, 0, 0, 150], [0, 0, 0, -1122, 0, 0, -63, 0], [0, 0, -912, 0, 0, -1956, 0, 0], [0, 0, 0, 1611, 0, 0, 784, 0], [0, 1487, 0, -476, -952, 0, 0, 0], [0, 0, 0, 1505, 0, 0, 1584, 0], [0, 0, 0, -266, 0, 0, -1787, 0], [0, 1848, 0, -1485, -2970, 0, 0, 0], [0, 0, 1232, 0, 0, 220, 0, 0], [0, 0, 0, 2569, 0, 0, -1152, 0], [7462, 0, 0, 0, 0, 0, 0, -88], [0, 2912, 0, 437, 874, 0, 0, 0], [0, 0, 1888, 0, 0, -1016, 0, 0], [528, 0, 0, 0, 0, 0, 0, 378], [-3850, 0, 0, 0, 0, 0, 0, -280], [0, 0, 1177, 0, 0, -70, 0, 0], [0, 189, 0, -1172, -2344, 0, 0, 0], [0, 0, 1344, 0, 0, 2662, 0, 0], [0, 0, 0, -575, 0, 0, -3360, 0], [0, 0, -763, 0, 0, -174, 0, 0], [0, 0, -1008, 0, 0, -542, 0, 0], [0, 0, 0, 1281, 0, 0, -336, 0], [-3920, 0, 0, 0, 0, 0, 0, 574], [0, 0, 0, -3122, 0, 0, 201, 0], [0, 2848, 0, -119, -238, 0, 0, 0], [0, 3185, 0, 2076, 4152, 0, 0, 0], [490, 0, 0, 0, 0, 0, 0, 720], [0, 0, 0, 142, 0, 0, 2649, 0], [0, -3240, 0, -121, -242, 0, 0, 0], [0, 0, 0, 2029, 0, 0, -1232, 0], [0, 0, 0, -2170, 0, 0, 1885, 0], [0, 833, 0, 796, 1592, 0, 0, 0], [2464, 0, 0, 0, 0, 0, 0, -316], [0, 0, 161, 0, 0, 2762, 0, 0], [0, -2961, 0, -92, -184, 0, 0, 0], [0, 0, 0, 3311, 0, 0, 1152, 0], [37104, 0, 0, 0, 0, 0, 0, 166], [0, 0, 749, 0, 0, -1438, 0, 0], [0, -5859, 0, 1452, 2904, 0, 0, 0], [0, 0, 3568, 0, 0, 704, 0, 0], [15358, 0, 0, 0, 0, 0, 0, -808], [0, 0, 0, -1314, 0, 0, -2543, 0], [0, 0, 0, -755, 0, 0, 1456, 0], [0, 0, 0, 6886, 0, 0, -403, 0], [0, 0, -560, 0, 0, -1760, 0, 0], [0, 0, 0, 883, 0, 0, 336, 0], [0, 0, 0, 3598, 0, 0, 281, 0], [0, 0, -1715, 0, 0, 738, 0, 0], [0, -4768, 0, 2317, 4634, 0, 0, 0], [0, 1659, 0, 1716, 3432, 0, 0, 0], [-16688, 0, 0, 0, 0, 0, 0, -318], [0, -1911, 0, -1732, -3464, 0, 0, 0], [0, 0, 1104, 0, 0, 2332, 0, 0], [0, 0, 0, -5369, 0, 0, -48, 0], [4000, 0, 0, 0, 0, 0, 0, 788], [0, 7427, 0, 1364, 2728, 0, 0, 0], [0, 0, 7936, 0, 0, 540, 0, 0], [0, 0, 0, -3871, 0, 0, -1200, 0], [22960, 0, 0, 0, 0, 0, 0, -1074], [0, 0, 0, 6055, 0, 0, -1104, 0], [7360, 0, 0, 0, 0, 0, 0, -600], [0, 0, 8477, 0, 0, -1534, 0, 0], [0, 0, 6032, 0, 0, 610, 0, 0], [8162, 0, 0, 0, 0, 0, 0, 352], [0, 0, 0, 558, 0, 0, -551, 0], [0, 7904, 0, 321, 642, 0, 0, 0], [0, 0, -5488, 0, 0, -1724, 0, 0], [0, 0, 1701, 0, 0, -2606, 0, 0], [0, -3304, 0, -73, -146, 0, 0, 0], [0, 0, 4368, 0, 0, -3546, 0, 0], [-36624, 0, 0, 0, 0, 0, 0, -426], [0, 0, 2877, 0, 0, 5186, 0, 0], [0, -9863, 0, 1084, 2168, 0, 0, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_384_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_384_4_f_h();".
// 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_384_4_f_h(:prec:=8)
    chi := MakeCharacter_384_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_384_4_f_h();".
// 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_384_4_f_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_384_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![1600, 0, -304, 0, 1]>,<23,R![5056, -160, 1]>],Snew);
    return Vf;
end function;