
// Make newform 525.4.d.i in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_525_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_525_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_525_4_d_i();".
// 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_525_4_d_i();".
// 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 := [16, 0, 9, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 5, 0, 1], [5, 0, 1, 0]];
    Rf_basisdens := [1, 1, 4, 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_525_d();"
function MakeCharacter_525_d()
    N := 525;
    order := 2;
    char_gens := [176, 127, 451];
    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_525_d_Hecke();"
function MakeCharacter_525_d_Hecke(Kf)
    N := 525;
    order := 2;
    char_gens := [176, 127, 451];
    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, 1, 4, 0], [0, 0, -3, 0], [0, 0, 0, 0], [0, 0, 7, 0], [-8, 0, 0, -10], [0, -22, -4, 0], [0, -28, -22, 0], [-100, 0, 0, 26], [0, 56, 120, 0], [-26, 0, 0, 84], [156, 0, 0, 18], [0, -24, 54, 0], [30, 0, 0, 140], [0, -68, 148, 0], [0, -108, -200, 0], [0, 214, 124, 0], [-164, 0, 0, -36], [522, 0, 0, -252], [0, -28, 380, 0], [392, 0, 0, -330], [0, -178, 300, 0], [-160, 0, 0, -88], [0, -264, 436, 0], [-382, 0, 0, 728], [0, -146, 176, 0], [210, 0, 0, 476], [0, -224, 784, 0], [0, 60, -760, 0], [554, 0, 0, 252], [0, 414, -728, 0], [0, 16, 312, 0], [636, 0, 0, 896], [0, 326, 1076, 0], [556, 0, 0, 290], [2782, 0, 0, -304], [1952, 0, 0, 308], [0, -1222, -1736, 0], [0, 1564, 332, 0], [0, 700, -2248, 0], [0, -836, -2082, 0], [1240, 0, 0, -1110], [-214, 0, 0, 296], [1144, 0, 0, 722], [0, -336, 1834, 0], [0, 526, 320, 0], [-2652, 0, 0, 110], [2940, 0, 0, -1416], [0, 268, 572, 0], [0, 792, -2716, 0], [806, 0, 0, -2932], [0, -34, -1696, 0], [4328, 0, 0, 778], [5706, 0, 0, -916], [-644, 0, 0, -268], [0, -472, 3214, 0], [0, -2836, -724, 0], [-970, 0, 0, 828], [1700, 0, 0, 54], [0, 1000, -2130, 0], [-2766, 0, 0, 3428], [0, 1848, 1100, 0], [0, 1332, -4570, 0], [0, -1692, 2784, 0], [-4896, 0, 0, 2728], [0, 2834, 1096, 0], [0, 802, -6604, 0], [-1740, 0, 0, -1032], [0, 3616, 1478, 0], [0, 784, -1732, 0], [-5858, 0, 0, -116], [0, -1780, -3650, 0], [-5480, 0, 0, -1794], [0, -372, -236, 0], [0, -420, -3082, 0], [-5260, 0, 0, 1292], [0, 196, 6456, 0], [-194, 0, 0, 1072], [0, 470, 3364, 0], [818, 0, 0, -728], [-11722, 0, 0, 2032], [-12484, 0, 0, 80], [-1042, 0, 0, 4368], [11552, 0, 0, -1754], [0, 4906, -1192, 0], [1980, 0, 0, -2634], [0, -2688, 6564, 0], [-4970, 0, 0, 1232], [0, -4876, -670, 0], [5770, 0, 0, -1712], [0, -2896, -8688, 0], [0, -2888, 9084, 0], [5048, 0, 0, 2336], [0, -2176, 8592, 0], [7472, 0, 0, 1750], [6092, 0, 0, -3380], [0, 2884, 16600, 0], [10198, 0, 0, -3676], [9310, 0, 0, 1836], [0, -2740, -6032, 0], [-14530, 0, 0, -1184], [0, 1228, 17972, 0], [0, -886, -4196, 0], [0, -1416, -22252, 0], [15734, 0, 0, -2792], [9476, 0, 0, 3164], [0, 4542, 1600, 0], [0, -3584, 2012, 0], [0, -10228, -5882, 0], [12944, 0, 0, -8602], [-1646, 0, 0, -6988], [0, 6564, 5476, 0], [0, -9528, 1306, 0], [0, -4054, -11048, 0], [17444, 0, 0, 2994], [-13376, 0, 0, 1000], [578, 0, 0, -6612], [0, 3988, 2592, 0], [0, 4660, 6552, 0], [0, 5746, 15016, 0], [-7592, 0, 0, 1750], [11250, 0, 0, 4468], [0, 10940, 4990, 0], [0, 1844, -390, 0], [0, 1608, 5460, 0], [-19492, 0, 0, -1718], [12986, 0, 0, -308], [-18662, 0, 0, 10792], [-640, 0, 0, 2788], [0, 4852, 16652, 0], [0, 4290, 30020, 0], [8428, 0, 0, -12388], [0, -4852, 14452, 0], [-21400, 0, 0, 8816], [0, -2580, -21198, 0], [-4618, 0, 0, -2456], [15118, 0, 0, -916], [0, -860, 3542, 0], [0, -3676, 25328, 0], [0, 12, -21718, 0], [32238, 0, 0, -6476], [-24348, 0, 0, -3482], [6050, 0, 0, -348], [0, -104, 26496, 0], [0, -10572, -3064, 0], [14006, 0, 0, -9232], [7648, 0, 0, 9148], [0, 6594, 15484, 0], [0, 3272, -19562, 0], [-1540, 0, 0, 254], [0, 8128, 28768, 0], [0, 4240, -27794, 0], [-7986, 0, 0, 6976], [0, -2468, -41420, 0], [0, -11996, 18088, 0], [0, 2196, 11636, 0], [26920, 0, 0, 730], [-7456, 0, 0, 12344], [32226, 0, 0, 1128], [0, -3158, 20444, 0], [1226, 0, 0, -21280], [0, 14064, 33548, 0], [0, -16922, -1656, 0], [0, -18736, -27352, 0], [1548, 0, 0, -3252], [0, 21250, 12472, 0], [0, 1620, -12632, 0], [40888, 0, 0, -6040], [0, -8538, -34820, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_525_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_525_4_d_i();".
// 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_525_4_d_i(:prec:=4)
    chi := MakeCharacter_525_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_525_4_d_i();".
// 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_525_4_d_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_525_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![64, 0, 33, 0, 1]>,<11,R![-256, 26, 1]>],Snew);
    return Vf;
end function;