
// Make newform 5775.2.a.o in Magma, downloaded from the LMFDB on 13 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5775_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_5775_2_a_o();".
// 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_5775_2_a_o();".
// 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_5775_a();"
function MakeCharacter_5775_a()
    N := 5775;
    order := 1;
    char_gens := [3851, 4852, 4126, 3676];
    v := [1, 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_5775_a_Hecke(Kf)
    return MakeCharacter_5775_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 := [[0], [1], [0], [-1], [1], [0], [-3], [-3], [1], [-7], [6], [8], [-2], [5], [6], [3], [-5], [-7], [-6], [-10], [-12], [-16], [3], [3], [1], [-14], [5], [-6], [-2], [19], [-19], [4], [2], [4], [-6], [14], [1], [-12], [-12], [14], [-18], [10], [-16], [-6], [0], [24], [-18], [-1], [3], [-14], [16], [-21], [10], [-12], [-20], [-16], [-1], [-1], [10], [-22], [8], [-19], [-26], [-32], [-21], [-2], [17], [1], [8], [-19], [10], [-21], [-7], [-11], [5], [-32], [12], [10], [-10], [-6], [33], [-23], [36], [-22], [1], [-32], [26], [37], [6], [18], [0], [0], [22], [-15], [-21], [13], [-11], [-27], [20], [-14], [-13], [-6], [4], [13], [36], [-18], [-36], [34], [30], [15], [38], [38], [-2], [46], [29], [-30], [-31], [4], [-39], [-15], [-18], [43], [25], [4], [2], [11], [-15], [15], [7], [6], [22], [-18], [53], [6], [-30], [9], [-26], [40], [30], [10], [12], [23], [24], [36], [38], [-17], [10], [-50], [46], [-39], [-45], [15], [-20], [-41], [44], [-4], [-14], [6], [38], [40], [45], [-30], [17], [47], [-49], [6], [31], [42], [32], [36], [39], [-30], [-15], [11], [50], [-42], [-20], [24], [-58], [-2], [-32], [32], [-54], [39], [-39], [47], [11], [-37], [-26], [-42], [-2], [-50], [20], [-55], [0], [-44], [-24], [-24], [-23], [-12], [-23], [-39], [55], [34], [-41], [12], [62], [-70], [13], [42], [-43], [-35], [-52], [12], [28], [-18], [35], [10], [8], [9], [-32], [8], [14], [8], [65], [-40], [-6], [-6], [24], [-29], [32], [-52], [20], [-11], [9], [63], [-8], [8], [-50], [-24], [-4], [1], [16], [23], [-3], [24], [64], [-47], [4], [48], [-30], [22], [76], [18], [39], [-49], [-7], [-42], [42], [-38], [25], [-72], [-49], [2], [-48], [-16], [-32], [-31], [9], [-28], [-10], [62], [49], [-74], [68], [-48], [13], [64], [10], [-52], [47], [-64], [12], [25], [-32], [64], [-38], [-52], [22], [-66], [-60], [36], [-36], [-32], [34], [-60], [36], [84], [-80], [80], [10], [-48], [40], [13], [53], [-82], [9], [52], [-2], [41], [48], [54], [87], [14], [30], [75], [-4], [-87], [-38], [9], [-72], [-22], [66], [-20], [16], [-60], [-60], [-29], [78], [-44], [-30], [-66], [-7], [36], [-30], [48], [15], [-6], [84], [-18], [94], [17], [-78], [-8], [-7], [31], [88], [-14], [56], [2], [-13], [-4], [12], [76], [-40], [-67], [-32], [48], [6], [71], [79], [70], [12], [87], [-93], [2], [-79], [-5], [-6], [-72], [-76], [-48], [9], [31], [-72], [-12], [29], [-16], [-44], [52], [51], [-63], [-8], [6], [35], [14], [-29], [-57], [60], [-60], [-55], [38], [-30], [-69], [-22], [-65], [100], [-54], [-67], [53], [-89], [-81], [-41], [68], [-28], [-35], [34], [2], [61], [69], [44], [-62], [-92], [-28], [-102], [-31], [-55], [53], [-56], [56], [38], [42], [-27], [-42], [-97], [-96], [-27], [-9], [-46], [-28]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5775_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5775_2_a_o();".
// 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_5775_2_a_o(:prec:=1)
    chi := MakeCharacter_5775_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_5775_2_a_o();".
// 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_5775_2_a_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5775_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>,<13,R![0, 1]>,<17,R![3, 1]>],Snew);
    return Vf;
end function;