
// Make newform 5175.2.a.a in Magma, downloaded from the LMFDB on 03 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5175_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_5175_2_a_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_5175_2_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_5175_a();"
function MakeCharacter_5175_a()
    N := 5175;
    order := 1;
    char_gens := [4601, 3727, 2926];
    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_5175_a_Hecke(Kf)
    return MakeCharacter_5175_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], [0], [0], [1], [0], [2], [5], [8], [1], [5], [-5], [7], [7], [4], [2], [1], [-3], [-6], [13], [-13], [8], [-14], [3], [14], [14], [-15], [0], [-9], [18], [1], [-20], [0], [-6], [9], [0], [8], [-3], [24], [-16], [-6], [4], [-14], [8], [-12], [0], [2], [-9], [-14], [0], [4], [-6], [1], [6], [-18], [-22], [13], [15], [-15], [-26], [12], [11], [-29], [14], [-4], [21], [24], [-5], [26], [-8], [23], [0], [6], [-13], [-6], [6], [-3], [-22], [16], [-2], [-19], [2], [10], [12], [19], [-28], [24], [-15], [1], [18], [-4], [27], [28], [-32], [31], [-11], [9], [-26], [20], [-28], [14], [-4], [33], [25], [12], [18], [22], [12], [-30], [48], [-25], [-38], [6], [47], [-10], [-20], [24], [37], [18], [-2], [-22], [-16], [24], [3], [-46], [-28], [28], [4], [45], [27], [7], [7], [32], [14], [-35], [-25], [10], [42], [17], [15], [25], [31], [-30], [-34], [9], [37], [-14], [8], [-24], [43], [18], [8], [-54], [-20], [-42], [9], [12], [4], [-35], [54], [-2], [-14], [-54], [-18], [6], [39], [-21], [19], [20], [-6], [8], [-52], [41], [-44], [-61], [-48], [-50], [-16], [-11], [35], [-64], [-36], [-24], [-30], [-42], [9], [-8], [16], [-37], [49], [-37], [-64], [-26], [-38], [3], [-12], [27], [12], [46], [62], [-24], [-12], [-40], [26], [8], [-42], [-48], [14], [60], [-29], [28], [-20], [-62], [-28], [-12], [37], [-64], [-14], [51], [-39], [64], [-31], [44], [-3], [15], [-40], [-1], [6], [33], [0], [-17], [2], [2], [66], [57], [-31], [16], [6], [-10], [-75], [3], [-21], [27], [-28], [-26], [-56], [0], [46], [-46], [-40], [-41], [30], [-50], [17], [6], [-14], [21], [38], [-52], [24], [44], [79], [-75], [7], [13], [70], [0], [70], [-8], [-29], [62], [-63], [17], [51], [-44], [48], [-34], [-18], [-11], [42], [-26], [-36], [38], [-72], [-16], [-8], [-45], [63], [-38], [-32], [-30], [-38], [53], [22], [12], [58], [34], [-14], [20], [35], [10], [17], [-59], [44], [48], [-52], [-32], [22], [-56], [14], [-84], [-22], [20], [-40], [60], [-15], [66], [33], [85], [11], [-18], [-42], [13], [30], [56], [29], [64], [20], [14], [32], [-13], [55], [12], [17], [-66], [8], [20], [-20], [54], [-58], [29], [-32], [90], [-74], [-8], [23], [15], [17], [-60], [-52], [-61], [13], [-32], [-47], [31], [38], [46], [-50], [60], [-16], [72], [59], [27], [-22], [34], [-22], [-22], [32], [65], [-32], [51], [-49], [-44], [20], [-60], [14], [21], [-36], [-25], [36], [100], [10], [-30], [-22], [26], [40], [-18], [-61], [-48], [-47], [-37], [86], [-40], [51], [-46], [-38], [-5], [34], [96], [-74], [31], [-37], [22], [-69], [-5], [-19], [-75], [-83], [-62], [-12], [8], [4], [-72], [-60], [44], [13], [42], [40], [12], [37], [72], [34], [42], [94], [-84], [14], [54], [-36], [-91], [-60], [7]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5175_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5175_2_a_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_5175_2_a_a(:prec:=1)
    chi := MakeCharacter_5175_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_5175_2_a_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_5175_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5175_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![2, 1]>,<7,R![-1, 1]>,<11,R![0, 1]>],Snew);
    return Vf;
end function;