
// Make newform 525.4.d.h in Magma, downloaded from the LMFDB on 19 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_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_525_4_d_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 := [256, 0, 33, 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, 17, 0, 1], [17, 0, 1, 0]];
    Rf_basisdens := [1, 1, 16, 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, 0, 0], [0, 0, 3, 0], [0, 0, 0, 0], [0, 0, 7, 0], [-12, 0, 0, 2], [0, -2, -12, 0], [0, -16, -66, 0], [-44, 0, 0, -14], [0, 20, 140, 0], [122, 0, 0, -48], [96, 0, 0, -42], [0, -36, 30, 0], [-38, 0, 0, -100], [0, 32, -156, 0], [0, -48, -304, 0], [0, -86, 120, 0], [380, 0, 0, 84], [-90, 0, 0, -24], [0, 64, 84, 0], [856, 0, 0, -42], [0, 202, -92, 0], [496, 0, 0, -104], [0, -216, 356, 0], [-298, 0, 0, 8], [0, 314, -104, 0], [-2, 0, 0, -28], [0, -64, 1032, 0], [0, 312, -332, 0], [74, 0, 0, 156], [0, -342, 92, 0], [0, 128, 1424, 0], [-1412, 0, 0, 392], [0, -454, 0, 0], [-84, 0, 0, -518], [-366, 0, 0, -436], [-56, 0, 0, -452], [0, -242, 1448, 0], [0, 344, 964, 0], [0, 16, -856, 0], [0, -140, -950, 0], [-652, 0, 0, -594], [-50, 0, 0, 508], [-576, 0, 0, -94], [0, -288, -3038, 0], [0, 682, 2732, 0], [1544, 0, 0, -470], [2316, 0, 0, -744], [0, 188, 4012, 0], [0, -1128, -1460, 0], [2530, 0, 0, -440], [0, 434, -1716, 0], [-1776, 0, 0, 250], [-5230, 0, 0, -116], [-1308, 0, 0, -844], [0, -1084, -1214, 0], [0, 32, 7464, 0], [10, 0, 0, -228], [-3856, 0, 0, 354], [0, 1244, 4238, 0], [-7286, 0, 0, -52], [0, -552, 1964, 0], [0, -84, 7210, 0], [0, 2124, 144, 0], [7304, 0, 0, 64], [0, -1826, -2680, 0], [0, 1054, 1208, 0], [-1932, 0, 0, 696], [0, 1376, 3886, 0], [0, -500, -2824, 0], [-70, 0, 0, 896], [0, 1232, -1934, 0], [4904, 0, 0, -474], [0, -348, -9884, 0], [0, 1320, 1430, 0], [-740, 0, 0, -716], [0, 208, -416, 0], [-7166, 0, 0, 724], [0, -1190, -604, 0], [7026, 0, 0, -752], [-5322, 0, 0, 1640], [3716, 0, 0, 704], [-11170, 0, 0, 336], [-9840, 0, 0, -698], [0, 998, 3832, 0], [-7752, 0, 0, 186], [0, -2652, -5448, 0], [3006, 0, 0, 1688], [0, -284, 6858, 0], [2806, 0, 0, 88], [0, 760, -2312, 0], [0, 1936, 12916, 0], [12896, 0, 0, 1736], [0, -1400, -12704, 0], [-6444, 0, 0, 178], [18036, 0, 0, -124], [0, -488, 9168, 0], [-1926, 0, 0, 1172], [2810, 0, 0, 36], [0, 868, -376, 0], [-4026, 0, 0, -2296], [0, -616, -11636, 0], [0, -634, 8272, 0], [0, 1848, -13484, 0], [-7338, 0, 0, 4576], [2692, 0, 0, -2372], [0, 5202, 3088, 0], [0, -5408, -1060, 0], [0, -472, 2266, 0], [536, 0, 0, 1094], [2634, 0, 0, 4780], [0, 1044, -6716, 0], [0, 12, -19382, 0], [0, -1834, 12220, 0], [332, 0, 0, 1746], [12008, 0, 0, -3616], [20482, 0, 0, -1908], [0, -700, 3920, 0], [0, 4528, -7256, 0], [0, -5378, -2988, 0], [21844, 0, 0, 1306], [11182, 0, 0, -4360], [0, 7300, 1382, 0], [0, 2708, -8170, 0], [0, 1764, -14904, 0], [-22740, 0, 0, 290], [25638, 0, 0, -176], [-12670, 0, 0, 32], [3216, 0, 0, -6908], [0, 5660, -5660, 0], [0, 2790, -3124, 0], [-732, 0, 0, 916], [0, -16, 8152, 0], [16528, 0, 0, -680], [0, -4008, 16746, 0], [4170, 0, 0, 5056], [-24450, 0, 0, 4924], [0, 772, 23138, 0], [0, 7108, 7272, 0], [0, -396, -626, 0], [14342, 0, 0, -5900], [-2156, 0, 0, 3710], [1486, 0, 0, 4944], [0, -784, -1240, 0], [0, 6888, 20788, 0], [346, 0, 0, -3644], [16744, 0, 0, 1876], [0, -8130, -1460, 0], [0, 2492, 4594, 0], [5308, 0, 0, -5378], [0, -4268, 5604, 0], [0, -6700, -14082, 0], [26354, 0, 0, -80], [0, 7592, 12612, 0], [0, -9032, -14112, 0], [0, 2280, -5212, 0], [44848, 0, 0, -1094], [-296, 0, 0, 208], [-22914, 0, 0, -6240], [0, -6010, -8564, 0], [-2986, 0, 0, -9232], [0, -4212, -39824, 0], [0, 4738, -24308, 0], [0, -2024, -43632, 0], [-19500, 0, 0, 924], [0, -5186, -28308, 0], [0, 3072, -13672, 0], [-5088, 0, 0, 4864], [0, 810, -26708, 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_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_525_4_d_h(: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_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_525_4_d_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_525_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![256, 0, 33, 0, 1]>,<11,R![56, 22, 1]>],Snew);
    return Vf;
end function;