
// Make newform 6762.2.a.o 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_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_6762_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_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], [0], [0], [-3], [2], [-3], [2], [-1], [3], [2], [2], [6], [2], [-3], [-6], [0], [2], [2], [3], [11], [11], [0], [-6], [8], [-3], [5], [12], [-1], [18], [2], [0], [-21], [-13], [12], [-4], [23], [23], [0], [-15], [6], [11], [12], [14], [15], [17], [-19], [-10], [-3], [-1], [0], [15], [2], [3], [6], [24], [15], [-22], [8], [-3], [8], [0], [11], [-3], [8], [-18], [8], [14], [6], [-22], [-18], [-12], [8], [-13], [20], [-6], [24], [-34], [-15], [29], [-21], [17], [30], [2], [8], [-24], [12], [20], [-30], [32], [-21], [30], [2], [0], [35], [30], [-21], [-42], [-22], [20], [17], [30], [-9], [-6], [-4], [-31], [-12], [-42], [15], [26], [14], [5], [33], [32], [17], [-3], [-28], [-15], [3], [3], [5], [-19], [-18], [48], [17], [-42], [-31], [-48], [23], [41], [-25], [-12], [32], [38], [48], [14], [-36], [-40], [30], [30], [-7], [33], [-22], [-21], [38], [-36], [-46], [-18], [11], [3], [-4], [-15], [20], [27], [44], [-24], [29], [42], [38], [54], [-6], [33], [14], [45], [18], [-18], [-40], [-52], [-52], [-6], [12], [-40], [-36], [44], [50], [3], [-7], [27], [8], [-1], [-10], [-45], [-16], [18], [-6], [-6], [20], [-46], [41], [-24], [29], [30], [-4], [30], [33], [-45], [-22], [-31], [66], [12], [30], [38], [-16], [-52], [21], [-30], [-64], [-30], [-42], [-16], [-22], [51], [17], [0], [-21], [-16], [-58], [-60], [-18], [15], [-64], [-31], [-66], [-7], [48], [-10], [54], [-63], [-55], [-48], [14], [-16], [-43], [-54], [56], [-66], [-58], [-12], [30], [0], [9], [-43], [32], [-28], [-12], [21], [26], [12], [-22], [6], [-34], [39], [-48], [8], [-42], [-24], [-61], [26], [-6], [47], [17], [36], [38], [-55], [60], [32], [-66], [39], [-4], [-51], [-76], [-28], [-4], [-64], [-58], [-34], [60], [-46], [-28], [-15], [60], [80], [-18], [-58], [23], [-51], [38], [-18], [50], [-42], [-30], [57], [-54], [12], [-70], [36], [-49], [-39], [-78], [-79], [-4], [-72], [-7], [0], [8], [59], [42], [-22], [24], [20], [-27], [-12], [27], [-37], [36], [-64], [24], [27], [8], [-60], [-46], [50], [-42], [-28], [-15], [-40], [68], [35], [6], [-60], [-10], [3], [80], [66], [20], [36], [-1], [81], [-58], [-49], [68], [-6], [-75], [44], [42], [72], [2], [53], [24], [48], [5], [17], [75], [-55], [65], [24], [0], [33], [18], [21], [-67], [24], [-3], [51], [-28], [-10], [18], [-76], [26], [-30], [68], [57], [-6], [77], [-40], [-18], [12], [-76], [-45], [26], [0], [-21], [62], [18], [44], [48], [44], [8], [38], [-24], [-70], [-27], [18], [-13], [60], [-34], [23], [30], [62], [27], [50], [6], [-67], [-78], [-63], [-64], [35], [-60], [50], [36], [71], [-81], [87], [-34], [8], [-6], [-99], [26], [-9], [-6], [-84], [-1], [75], [-90], [-70], [57], [12], [12], [65], [39]];
    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_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_6762_2_a_o(: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_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_6762_2_a_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6762_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<11,R![3, 1]>,<13,R![-2, 1]>,<17,R![3, 1]>],Snew);
    return Vf;
end function;