
// Make newform 6762.2.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6762_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_6762_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_6762_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_6762_a();"
function MakeCharacter_6762_a()
    N := 6762;
    order := 1;
    char_gens := [2255, 3727, 3823];
    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_6762_a_Hecke(Kf)
    return MakeCharacter_6762_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 := [[-1], [-1], [-3], [0], [-2], [3], [-3], [0], [-1], [-8], [10], [-2], [8], [-4], [9], [-9], [4], [-6], [3], [7], [3], [4], [-14], [10], [4], [-10], [17], [-8], [8], [-5], [-2], [15], [19], [10], [21], [-20], [-18], [0], [-21], [20], [15], [6], [-8], [5], [18], [-8], [-6], [8], [-14], [-20], [-4], [0], [-6], [-26], [14], [-12], [-20], [6], [9], [7], [-7], [-13], [16], [-13], [-28], [-6], [10], [16], [-15], [-25], [4], [-6], [-9], [10], [-29], [-22], [10], [9], [-39], [39], [28], [-4], [36], [-18], [26], [-17], [6], [-28], [-18], [-4], [-12], [0], [-20], [-29], [-40], [-2], [-18], [-41], [23], [-15], [-26], [18], [12], [5], [25], [-2], [25], [-18], [9], [18], [-38], [-42], [-9], [19], [-23], [37], [-20], [-48], [10], [-2], [40], [34], [-27], [-37], [-6], [33], [4], [41], [-40], [-24], [-24], [14], [-24], [-40], [-4], [-34], [17], [53], [11], [-6], [-46], [18], [-26], [-48], [25], [-12], [38], [-18], [40], [-51], [-41], [35], [-36], [8], [29], [-4], [45], [-36], [52], [18], [-3], [-46], [16], [-40], [-37], [-10], [14], [-38], [34], [36], [6], [43], [-20], [36], [-10], [-38], [-16], [62], [-11], [0], [-56], [-48], [-7], [18], [-24], [3], [-13], [44], [35], [-9], [26], [28], [-7], [52], [28], [41], [-52], [-46], [33], [-21], [-33], [56], [37], [-28], [-30], [-30], [29], [-3], [26], [-22], [-25], [-64], [16], [6], [24], [8], [-52], [-18], [-48], [-48], [-25], [45], [6], [-65], [28], [13], [-6], [43], [-53], [-53], [29], [-11], [52], [10], [19], [44], [40], [42], [-53], [8], [58], [-68], [26], [77], [26], [16], [-42], [72], [20], [24], [22], [-15], [-52], [-68], [6], [76], [-22], [-24], [4], [-62], [31], [6], [57], [-54], [22], [44], [3], [22], [-35], [22], [59], [20], [-52], [77], [-33], [-40], [-37], [73], [80], [30], [3], [-29], [42], [48], [-14], [-80], [-16], [27], [-18], [68], [54], [12], [52], [-18], [35], [33], [-3], [36], [51], [46], [-44], [-37], [5], [-48], [79], [2], [-49], [16], [-30], [70], [-84], [-45], [-25], [-76], [50], [-70], [-68], [32], [24], [42], [-53], [-4], [74], [-16], [-3], [-82], [20], [-78], [76], [34], [-83], [-56], [14], [15], [15], [3], [-20], [14], [93], [64], [33], [-63], [-26], [-52], [86], [69], [-81], [-18], [-88], [18], [-62], [79], [-8], [-1], [50], [6], [-90], [-18], [-52], [71], [-38], [-78], [48], [-30], [-72], [-55], [8], [-56], [2], [12], [-20], [-77], [54], [-43], [-37], [-68], [-12], [-2], [-9], [42], [30], [-9], [62], [-82], [15], [-59], [-30], [-86], [-67], [62], [26], [54], [-81], [-42], [-30], [44], [-45], [-53], [-91], [-46], [13], [-90], [-72], [33], [-84], [32], [-86], [-26], [11], [40], [-41], [48], [-50], [45], [-2], [-40], [81], [-14], [43], [-48], [62], [-26], [12], [39], [-35], [-12], [6], [2], [18], [-88]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6762_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6762_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_6762_2_a_b(:prec:=1)
    chi := MakeCharacter_6762_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_6762_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_6762_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6762_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 1]>,<11,R![2, 1]>,<13,R![-3, 1]>,<17,R![3, 1]>],Snew);
    return Vf;
end function;