
// Make newform 384.4.d.c in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_384_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_384_d_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_d_c();".
// 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_d_c();".
// 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 := [9, 0, 7, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 4, 0, 1], [0, 40, 0, 4], [28, 0, 8, 0]];
    Rf_basisdens := [1, 3, 3, 1];
    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_d();"
function MakeCharacter_384_d()
    N := 384;
    order := 2;
    char_gens := [127, 133, 257];
    v := [2, 1, 2];
    // 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_d_Hecke();"
function MakeCharacter_384_d_Hecke(Kf)
    N := 384;
    order := 2;
    char_gens := [127, 133, 257];
    char_values := [[1, 0, 0, 0], [-1, 0, 0, 0], [1, 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, -3, 0, 0], [0, -4, 1, 0], [-8, 0, 0, 1], [0, 4, 4, 0], [0, -36, -2, 0], [-18, 0, 0, -4], [0, 68, -4, 0], [-128, 0, 0, -2], [0, -76, 9, 0], [-40, 0, 0, -13], [0, 68, -4, 0], [218, 0, 0, -20], [0, 356, -4, 0], [-112, 0, 0, 14], [0, 172, 15, 0], [0, 324, 0, 0], [0, -324, 0, 0], [0, 228, 48, 0], [-1024, 0, 0, 2], [-330, 0, 0, 48], [248, 0, 0, 59], [0, 388, 28, 0], [-266, 0, 0, 8], [-610, 0, 0, -88], [0, -612, -77, 0], [-856, 0, 0, -73], [0, 244, -8, 0], [0, -1364, 42, 0], [114, 0, 0, 56], [2152, 0, 0, -35], [0, -1308, -48, 0], [1594, 0, 0, 68], [0, -284, 112, 0], [0, -212, 53, 0], [312, 0, 0, 69], [0, -1508, -140, 0], [0, -1676, 52, 0], [528, 0, 0, 60], [0, 2980, 23, 0], [0, -1292, 112, 0], [0, 308, -234, 0], [1568, 0, 0, 20], [-914, 0, 0, 272], [0, -980, 131, 0], [1064, 0, 0, 119], [0, -76, 32, 0], [-1720, 0, 0, 161], [0, -1628, 100, 0], [0, 516, 50, 0], [102, 0, 0, 8], [-864, 0, 0, -360], [-4162, 0, 0, -184], [0, -3660, 84, 0], [642, 0, 0, 160], [-1168, 0, 0, 416], [0, 2180, -227, 0], [-4248, 0, 0, 81], [0, 1476, 286, 0], [-3434, 0, 0, 224], [0, -1708, 272, 0], [0, -2452, -233, 0], [0, 3876, 240, 0], [2480, 0, 0, 284], [5018, 0, 0, -240], [0, 8116, 93, 0], [0, 10100, 56, 0], [5410, 0, 0, -80], [0, -4044, -12, 0], [0, -244, 224, 0], [-6270, 0, 0, -136], [1152, 0, 0, -486], [-3144, 0, 0, -525], [0, 6548, -328, 0], [0, -4540, -364, 0], [-6368, 0, 0, -356], [0, -3412, 237, 0], [0, 8636, -184, 0], [3694, 0, 0, 468], [2026, 0, 0, 424], [0, -3340, 836, 0], [0, 7236, 430, 0], [1200, 0, 0, 426], [2414, 0, 0, 144], [2472, 0, 0, 483], [0, -4188, -588, 0], [-8610, 0, 0, -308], [506, 0, 0, 56], [0, -4492, 355, 0], [-2968, 0, 0, 713], [0, -1836, 180, 0], [8240, 0, 0, -58], [3416, 0, 0, 5], [0, -2076, -312, 0], [0, -2188, -208, 0], [-5472, 0, 0, 882], [0, 484, 425, 0], [-16550, 0, 0, -292], [0, 12052, 100, 0], [0, 3372, -46, 0], [0, -4188, -804, 0], [0, -3804, 395, 0], [0, -588, -228, 0], [-502, 0, 0, 1068], [0, -21580, -232, 0], [12946, 0, 0, 560], [0, 7732, 424, 0], [2002, 0, 0, -424], [-7200, 0, 0, 162], [4710, 0, 0, -800], [22952, 0, 0, -331], [0, 7380, 576, 0], [17574, 0, 0, -80], [0, -4364, -80, 0], [5176, 0, 0, -467], [1022, 0, 0, -1124], [0, 1956, 1020, 0], [-6240, 0, 0, 222], [0, 1940, 495, 0], [0, 11700, 360, 0], [0, -10108, 1052, 0], [11550, 0, 0, 1184], [0, -4884, -775, 0], [0, 6772, 76, 0], [0, -10252, -460, 0], [0, -13116, -359, 0], [0, 2068, -1638, 0], [13872, 0, 0, -1590], [-17704, 0, 0, 737], [0, 3164, 966, 0], [0, 5428, 1576, 0], [-29072, 0, 0, -344], [-2152, 0, 0, -2233], [0, -27532, -134, 0], [810, 0, 0, -1948], [-3218, 0, 0, 384], [0, -12036, 1127, 0], [0, 8148, 228, 0], [0, 15732, -113, 0], [2842, 0, 0, -1500], [0, -17164, 188, 0], [0, 3052, 1715, 0], [-22952, 0, 0, -1127], [0, 4660, 88, 0], [0, -19412, 1434, 0], [-5856, 0, 0, -834], [0, 8180, 548, 0], [-10454, 0, 0, -468], [0, -1244, 1780, 0], [-35104, 0, 0, -796], [0, 10748, -232, 0], [15506, 0, 0, -240], [0, 12868, -1388, 0], [3024, 0, 0, -1728], [0, 2372, -220, 0], [-3552, 0, 0, -1608], [-9192, 0, 0, 897], [-4034, 0, 0, -1500], [-9210, 0, 0, -608], [0, -1356, -469, 0], [0, -21084, 912, 0], [-25622, 0, 0, -292], [23112, 0, 0, -2133], [0, -24268, 1292, 0], [51310, 0, 0, -508], [16528, 0, 0, -608], [10360, 0, 0, -17], [0, -19036, -1720, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_384_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_384_4_d_c();".
// 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_d_c(:prec:=4)
    chi := MakeCharacter_384_d();
    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_d_c();".
// 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_d_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_384_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![36864, 0, 448, 0, 1]>,<7,R![-144, 16, 1]>],Snew);
    return Vf;
end function;