
// Make newform 525.4.d.a 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_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_525_4_d_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 := [1, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [-1, 0], [1, 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, 5], [0, 3], [0, 0], [0, 7], [12, 0], [0, -30], [0, -134], [92, 0], [0, -112], [58, 0], [-224, 0], [0, -146], [18, 0], [0, -340], [0, 208], [0, 754], [-380, 0], [718, 0], [0, 412], [-960, 0], [0, -1066], [-896, 0], [0, -436], [1038, 0], [0, -702], [46, 0], [0, -1880], [0, 732], [378, 0], [0, -1458], [0, 608], [-956, 0], [0, -374], [-396, 0], [1874, 0], [-1096, 0], [0, 1918], [0, -2316], [0, -1736], [0, 2442], [4092, 0], [1270, 0], [4904, 0], [0, -2178], [0, -2850], [1144, 0], [412, 0], [0, 1632], [0, 4084], [3386, 0], [0, -5322], [-3736, 0], [210, 0], [-4212, 0], [0, 5130], [0, -848], [1274, 0], [864, 0], [0, -8530], [-5382, 0], [0, -6236], [0, 818], [0, -2268], [6648, 0], [0, -9818], [0, 934], [2292, 0], [0, -6062], [0, 1484], [-254, 0], [0, 10950], [-11376, 0], [0, -1136], [0, 8242], [-3620, 0], [0, 8464], [-3678, 0], [0, 12590], [2850, 0], [-1226, 0], [-612, 0], [5182, 0], [-4984, 0], [0, 1694], [-13864, 0], [0, 4644], [4926, 0], [0, -14694], [2006, 0], [0, -4896], [0, 2660], [5600, 0], [0, -6424], [-18900, 0], [15364, 0], [0, -2216], [3754, 0], [-4702, 0], [0, 22660], [-8634, 0], [0, -19284], [0, -19658], [0, 25612], [-7002, 0], [-4524, 0], [0, -6014], [0, -11748], [0, 9462], [-2320, 0], [4650, 0], [0, -14656], [0, -29166], [0, 28554], [3876, 0], [2904, 0], [9330, 0], [0, 18332], [0, -2088], [0, -22], [-16260, 0], [-23818, 0], [0, -31106], [0, -1090], [0, 12372], [3252, 0], [-5434, 0], [5330, 0], [7520, 0], [0, 19336], [0, 22498], [18292, 0], [0, -17904], [5408, 0], [0, 8318], [6690, 0], [-9266, 0], [0, -9678], [0, -6860], [0, 10950], [-26010, 0], [-14628, 0], [8718, 0], [0, 7432], [0, 17388], [-7902, 0], [31848, 0], [0, -30150], [0, -4350], [30676, 0], [0, 23688], [0, 31910], [50250, 0], [0, -5980], [0, -24568], [0, 13252], [-6744, 0], [45336, 0], [-30074, 0], [0, 21754], [14550, 0], [0, 46660], [0, -20810], [0, 2776], [27292, 0], [0, -62], [0, -37912], [10656, 0], [0, -29434]];
    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_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_525_4_d_a(:prec:=2)
    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_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_525_4_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_525_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![25, 0, 1]>,<11,R![-12, 1]>],Snew);
    return Vf;
end function;