
// Make newform 3630.2.a.h in Magma, downloaded from the LMFDB on 19 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_h();".
// 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_h();".
// 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], [1], [0], [-5], [3], [-5], [0], [3], [2], [5], [-6], [-2], [-6], [0], [0], [-8], [2], [9], [-14], [-14], [3], [-12], [-10], [-15], [-1], [0], [16], [18], [4], [12], [15], [-23], [6], [-20], [-13], [-16], [-12], [-12], [12], [-16], [-21], [4], [24], [-16], [-5], [5], [0], [20], [-6], [21], [7], [-24], [-27], [-18], [15], [-2], [22], [0], [16], [12], [22], [0], [-34], [-12], [-13], [-14], [21], [-2], [-18], [-24], [23], [13], [5], [-6], [6], [29], [-30], [-14], [-30], [-10], [15], [-16], [-26], [-9], [30], [-8], [3], [32], [33], [21], [-25], [-18], [5], [24], [18], [-36], [16], [10], [46], [6], [9], [-6], [-32], [2], [-33], [-30], [-24], [10], [-5], [-5], [15], [29], [-4], [42], [-16], [36], [-30], [-18], [26], [-32], [-30], [33], [29], [3], [-46], [-36], [53], [22], [25], [36], [-22], [2], [12], [1], [24], [28], [-12], [18], [-29], [18], [41], [33], [44], [45], [31], [39], [44], [42], [25], [-6], [2], [48], [-22], [-15], [-20], [-24], [-2], [3], [27], [42], [40], [-60], [-54], [42], [50], [-23], [-5], [-18], [-48], [26], [39], [-14], [14], [-12], [37], [-39], [52], [-8], [-19], [0], [-22], [15], [12], [39], [7], [-28], [-14], [-57], [-34], [-51], [53], [15], [33], [-39], [-41], [41], [51], [-36], [-42], [-68], [-34], [25], [48], [-24], [50], [15], [30], [41], [52], [42], [-28], [12], [-21], [-1], [40], [60], [-42], [-30], [-68], [4], [30], [-16], [-57], [28], [-51], [39], [7], [36], [-43], [-20], [-50], [18], [26], [-72], [-25], [-6], [-36], [45], [-33], [37], [32], [-34], [-39], [21], [56], [18], [-29], [-66], [34], [60], [-30], [50], [24], [-6], [-40], [-62], [-30], [40], [19], [-75], [-80], [19], [-69], [8], [27], [36], [-26], [-18], [-52], [-58], [8], [52], [40], [-16], [-12], [-32], [-29], [-30], [42], [50], [-30], [-68], [-2], [36], [-52], [78], [20], [-54], [42], [-21], [-39], [12], [-17], [-54], [-10], [66], [12], [-32], [-74], [-60], [52], [9], [5], [38], [69], [-40], [33], [-35], [24], [-15], [-78], [14], [-12], [61], [60], [45], [14], [-60], [-47], [-28], [-81], [-43], [-9], [65], [11], [-52], [-78], [36], [-32], [-6], [-26], [-15], [-11], [12], [-34], [-15], [-85], [55], [-46], [-27], [6], [-46], [30], [30], [74], [62], [33], [-12], [52], [-64], [-39], [1], [16], [-3], [48], [24], [-66], [-24], [-11], [0], [-60], [-18], [80], [-58], [0], [7], [-47], [24], [-55], [-12], [-15], [-68], [-34], [-24], [-27], [22], [0], [-98], [45], [-21], [43], [30], [88], [66], [44], [59], [88], [66], [-91], [-6], [54], [8], [-12], [10], [52], [84], [-85], [-18], [76], [-9], [43], [-42], [90], [-92], [47], [90], [-16], [-48], [58], [36], [-69], [76], [34], [27], [72], [-52], [18], [24], [-39], [-11], [54], [-66], [-88], [36], [-60], [-6], [11], [-63]];
    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_h();".
// 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_h(: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_h();".
// 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_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3630_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-1, 1]>,<13,R![5, 1]>],Snew);
    return Vf;
end function;