
// Make newform 3630.2.a.m in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3630_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_3630_2_a_m();".
// 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_3630_2_a_m();".
// 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_3630_a();"
function MakeCharacter_3630_a()
    N := 3630;
    order := 1;
    char_gens := [1211, 727, 3511];
    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_3630_a_Hecke(Kf)
    return MakeCharacter_3630_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], [1], [5], [0], [5], [-3], [-1], [-6], [9], [-4], [5], [0], [8], [-6], [6], [-6], [14], [-10], [3], [-16], [-10], [3], [-12], [-4], [15], [5], [12], [-4], [-6], [8], [-18], [3], [5], [-18], [-10], [-13], [-22], [0], [6], [18], [14], [-3], [-16], [24], [20], [-13], [-1], [12], [-4], [-18], [9], [-19], [-12], [21], [0], [27], [2], [26], [-30], [-4], [-12], [20], [-24], [8], [-18], [-19], [32], [-27], [2], [30], [0], [17], [11], [11], [-6], [6], [5], [-12], [-22], [18], [8], [-33], [-16], [2], [21], [-6], [26], [21], [-4], [-33], [21], [29], [-24], [-37], [42], [6], [-30], [26], [-16], [26], [42], [-15], [-24], [20], [26], [-39], [-6], [36], [26], [-13], [-43], [3], [-1], [2], [-30], [-16], [12], [-12], [-18], [-4], [-28], [-12], [-9], [-13], [-3], [8], [0], [-37], [-22], [-19], [-6], [-40], [26], [36], [-1], [18], [-22], [6], [-12], [-13], [42], [-13], [-3], [14], [-33], [-43], [-27], [-28], [-18], [47], [-36], [-28], [12], [2], [51], [50], [42], [-46], [9], [-15], [-6], [8], [12], [-6], [12], [2], [-13], [-7], [42], [-36], [-58], [-9], [8], [-40], [-30], [-43], [9], [56], [-10], [-1], [-18], [-58], [33], [0], [3], [-43], [-46], [14], [-57], [-28], [-27], [-1], [15], [-3], [-39], [5], [17], [45], [-54], [-6], [38], [-10], [35], [-60], [-48], [-64], [63], [-42], [-1], [56], [18], [8], [36], [-45], [35], [32], [0], [42], [12], [-34], [-4], [-36], [44], [75], [-10], [-15], [33], [-37], [-36], [-31], [-28], [-58], [30], [44], [54], [-1], [12], [-30], [-45], [69], [65], [-16], [38], [51], [21], [-16], [30], [23], [18], [2], [36], [12], [38], [-6], [24], [38], [8], [-42], [44], [35], [33], [14], [-19], [-9], [-4], [27], [6], [-34], [-18], [14], [-22], [14], [-22], [-28], [-64], [12], [26], [-31], [30], [66], [-52], [-12], [8], [62], [-12], [38], [-36], [-28], [54], [-66], [-27], [-69], [42], [53], [30], [14], [60], [0], [26], [-10], [78], [56], [3], [-85], [-58], [63], [56], [-81], [-85], [-36], [21], [-6], [-76], [-18], [23], [72], [-15], [-46], [-36], [53], [8], [33], [-49], [-39], [65], [29], [8], [18], [-42], [92], [90], [-16], [21], [65], [48], [-28], [63], [35], [-73], [-10], [-15], [-54], [-58], [6], [48], [-40], [-52], [33], [-72], [-4], [14], [33], [-19], [44], [27], [-24], [-60], [-90], [18], [23], [12], [-42], [0], [-94], [44], [30], [-61], [-97], [-6], [-1], [24], [27], [-40], [62], [-96], [-27], [14], [0], [68], [-51], [75], [-61], [30], [-100], [-54], [8], [-13], [-16], [84], [-43], [-54], [96], [62], [0], [-40], [-70], [-30], [-19], [18], [-4], [-45], [-13], [-18], [-6], [-70], [59], [12], [-4], [36], [-28], [-72], [-39], [-88], [50], [51], [-12], [-40], [66], [54], [-63], [-49], [-72], [54], [-10], [84], [60], [-18], [-67], [-87]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3630_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3630_2_a_m();".
// 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_3630_2_a_m(:prec:=1)
    chi := MakeCharacter_3630_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_3630_2_a_m();".
// 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_3630_2_a_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3630_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-5, 1]>,<13,R![-5, 1]>],Snew);
    return Vf;
end function;