
// Make newform 4025.2.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4025_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_4025_2_a_b();".
// 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_4025_2_a_b();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_4025_a();"
function MakeCharacter_4025_a()
    N := 4025;
    order := 1;
    char_gens := [2577, 1151, 3501];
    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;

function MakeCharacter_4025_a_Hecke(Kf)
    return MakeCharacter_4025_a();
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 := 2;
    raw_aps := [[-2], [1], [0], [1], [-5], [-3], [5], [0], [1], [3], [6], [4], [0], [2], [9], [6], [-6], [10], [-4], [-8], [-10], [-15], [-12], [-10], [-7], [10], [-1], [6], [-17], [-18], [-4], [-20], [6], [-8], [-2], [-9], [-2], [-8], [1], [3], [8], [-14], [27], [-6], [12], [-22], [-17], [-15], [3], [-8], [-18], [7], [2], [-20], [-2], [28], [2], [-18], [8], [-3], [25], [9], [-17], [-20], [15], [20], [24], [-2], [-12], [28], [-21], [32], [23], [-26], [-20], [-16], [31], [-5], [3], [-6], [-18], [-37], [-33], [-34], [-6], [6], [-9], [0], [32], [10], [21], [-18], [-10], [-41], [25], [-39], [36], [30], [16], [-9], [38], [14], [-20], [-2], [8], [-23], [12], [-27], [-43], [0], [-33], [-24], [12], [-10], [35], [26], [31], [-32], [36], [-21], [40], [-44], [39], [44], [26], [45], [-51], [26], [-16], [-29], [-43], [30], [53], [-8], [-36], [-16], [15], [-37], [-51], [9], [22], [-19], [-42], [36], [16], [36], [2], [-46], [40], [-30], [-8], [-34], [48], [16], [-4], [-16], [37], [-14], [-3], [20], [-12], [-50], [-8], [12], [48], [13], [40], [51], [-33], [58], [-20], [-40], [-49], [8], [-14], [-16], [29], [25], [-20], [-16], [-6], [30], [-6], [-6], [24], [-42], [66], [-29], [41], [42], [22], [-36], [-23], [-34], [-4], [27], [1], [-2], [49], [29], [-14], [-12], [-38], [48], [-38], [-29], [-30], [12], [50], [-52], [52], [20], [-28], [-5], [-50], [0], [66], [22], [-60], [6], [-46], [-50], [-15], [-48], [33], [-18], [9], [-27], [3], [-36], [38], [10], [-40], [-39], [-9], [-41], [-26], [-6], [11], [-32], [-56], [-36], [-19], [-34], [53], [0], [40], [2], [9], [-2], [56], [8], [-68], [20], [66], [-59], [-30], [-16], [-63], [-31], [62], [-6], [10], [23], [-70], [-20], [15], [-60], [-14], [-54], [-44], [-80], [5], [32], [34], [29], [-18], [62], [-1], [-14], [1], [-63], [4], [-26], [-39], [5], [-70], [0], [50], [70], [-57], [-79], [48], [28], [16], [46], [4], [69], [0], [81], [41], [-2], [59], [64], [-51], [16], [18], [46], [-24], [80], [-24], [29], [18], [34], [-38], [-48], [-70], [29], [-3], [29], [46], [8], [32], [-14], [54], [90], [-35], [0], [-42], [30], [34], [44], [10], [63], [-4], [61], [-57], [34], [10], [-33], [-58], [-60], [-18], [-65], [56], [-36], [-2], [-68], [2], [-71], [-28], [-82], [-47], [63], [39], [-54], [48], [48], [-58], [-34], [48], [88], [48], [39], [-8], [-8], [27], [-60], [35], [29], [70], [62], [-67], [2], [28], [-10], [-33], [7], [-98], [93], [-12], [-70], [-88], [-8], [0], [-91], [17], [93], [-42], [-7], [-85], [-21], [-9], [-36], [43], [-18], [-40], [88], [35], [66], [8], [92], [82], [97], [88], [40], [-44], [15], [8], [-48], [51], [-60], [-22], [-52], [-62], [18], [-87], [56], [22], [3], [87], [42], [-82], [-84], [-59], [75], [-46], [10], [64], [-36]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4025_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4025_2_a_b();".
// 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_4025_2_a_b(:prec:=1)
    chi := MakeCharacter_4025_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_4025_2_a_b();".
// 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_4025_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4025_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![2, 1]>,<3,R![-1, 1]>,<11,R![5, 1]>],Snew);
    return Vf;
end function;