
// Make newform 3630.2.a.bh in Magma, downloaded from the LMFDB on 25 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_bh();".
// 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_bh();".
// 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
        poly := [-3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0], [1, 0], [1, 0], [-2, 1], [0, 0], [-3, 0], [-2, -1], [1, -2], [-3, 1], [3, 0], [3, -3], [0, 1], [-3, 1], [-5, 1], [2, -4], [-7, -1], [1, 3], [-3, -3], [-8, 2], [-3, 4], [-1, 3], [-4, -2], [1, -6], [-6, 6], [-17, 1], [9, 2], [-1, 10], [-8, 2], [-10, 2], [0, -8], [-10, -6], [-3, 5], [1, 12], [-3, 10], [-10, -6], [-15, -1], [8, -3], [-1, -11], [18, -2], [1, 9], [-5, -7], [-1, -3], [15, -2], [-6, 2], [10, -2], [-6, 6], [3, 12], [3, -2], [0, -6], [-12, 0], [12, 8], [8, 1], [14, -5], [22, 2], [-11, -10], [13, -7], [-4, -9], [10, -8], [-12, 4], [-7, 11], [6, -14], [12, -4], [-5, 1], [0, -8], [-7, 7], [-3, 7], [-8, 13], [-25, -1], [-15, 6], [6, 8], [0, -4], [24, -2], [1, -8], [-13, -4], [-8, -17], [-10, 4], [-24, 8], [0, -15], [9, -5], [-14, 0], [-8, 2], [15, 11], [6, 7], [-4, 4], [2, 4], [0, 3], [0, 2], [1, -17], [-7, -14], [-8, 2], [-18, 7], [10, 1], [3, -2], [-3, -1], [8, 17], [19, 1], [-22, -8], [-7, 9], [21, 1], [7, 9], [-22, -4], [0, -6], [-3, 22], [31, 3], [28, -10], [0, 10], [-10, -19], [-2, -4], [-24, 6], [-26, 8], [-24, -9], [1, 20], [7, -12], [6, 15], [23, 3], [2, -4], [24, -8], [26, 10], [-21, 3], [-8, 6], [-13, 3], [4, -8], [15, -11], [-18, -7], [32, -11], [45, -2], [-13, -5], [28, 2], [-1, -28], [-4, 8], [-13, 2], [15, -11], [33, 3], [0, 2], [-30, -10], [12, 13], [29, -5], [-5, 15], [11, 21], [21, 1], [13, 0], [30, 10], [-9, 6], [-13, 24], [25, 7], [9, -12], [-23, -10], [36, -5], [-12, -6], [-12, -14], [1, 20], [21, -17], [35, 7], [-24, 12], [34, 12], [11, 24], [23, 5], [-3, -7], [-22, -16], [13, -4], [-24, 3], [18, 8], [-32, 2], [-6, 2], [10, 0], [17, -9], [30, 12], [-49, 6], [50, -3], [-34, 16], [30, 10], [-20, 6], [-14, 1], [13, -7], [9, -5], [-45, -1], [-9, -4], [46, 3], [22, -6], [11, -5], [-1, 22], [-23, -7], [-2, 8], [-22, -15], [-8, 20], [-12, -11], [-7, -22], [49, -5], [8, 10], [-18, -9], [13, -5], [9, 14], [2, -19], [-30, 7], [21, -24], [-37, 2], [22, -5], [32, 1], [-30, 11], [5, 27], [-14, 12], [7, 25], [32, 8], [14, -13], [0, -12], [20, -8], [1, -17], [-9, -2], [14, -32], [16, -11], [-22, 22], [0, -8], [20, 6], [-16, -8], [42, 7], [-11, 18], [-52, 0], [0, -4], [36, -2], [-27, -19], [-31, -15], [-34, -18], [-13, 25], [16, 22], [-21, -24], [-9, 15], [-15, -16], [17, -30], [-22, 25], [-22, -22], [14, 13], [-4, 8], [14, 24], [-12, 14], [27, 23], [-15, 31], [1, 36], [15, -7], [-39, -9], [21, 10], [28, -11], [47, -10], [0, -8], [-10, 4], [-24, -31], [18, -23], [-26, 24], [18, -28], [-9, 18], [-20, -18], [-42, 10], [12, 16], [47, 13], [-22, -30], [-27, -27], [3, 1], [-51, -13], [-1, -35], [54, 0], [26, 2], [16, 5], [25, 20], [-17, -5], [-37, -8], [-21, 16], [-10, 34], [-30, 7], [41, -7], [26, 24], [-22, -8], [-35, -1], [6, -8], [-25, -5], [-49, 1], [-22, -30], [42, -8], [16, -24], [-33, -17], [30, -21], [-48, -14], [-16, 30], [37, 7], [-5, -21], [38, -6], [14, -20], [36, 24], [15, -33], [-3, -1], [76, 0], [-50, -16], [-78, 0], [42, -3], [-28, 21], [-7, -11], [-37, 10], [-30, 12], [8, -38], [-7, 9], [-20, 4], [-31, 9], [22, -16], [-9, 7], [-34, -6], [0, 7], [0, 13], [4, 42], [36, 9], [-18, 18], [15, -2], [-19, 0], [-42, -26], [20, 25], [-38, -4], [19, -5], [-17, -27], [2, 15], [-28, 12], [0, 11], [6, -36], [16, -20], [-65, -2], [-18, -22], [-39, 26], [9, -2], [-26, 21], [-57, -16], [-8, 13], [26, -22], [54, -8], [-25, 11], [56, -20], [36, -26], [-37, -5], [-35, -4], [25, 22], [-38, 30], [-57, -1], [-6, -7], [-31, -28], [-24, -15], [38, 20], [15, -12], [-38, 12], [10, 16], [76, -6], [-21, -15], [-73, -1], [19, -5], [-42, 11], [62, 6], [4, 0], [-43, 9], [18, -9], [-50, -7], [-14, 22], [-22, 25], [-66, -4], [-42, 10], [30, -32], [39, 15], [-1, -10], [-24, -4], [-21, -21], [-19, -13], [19, -3], [23, -7], [-13, 35], [2, 7], [72, 7], [-25, 1], [-8, -3], [-62, 14], [-9, 44], [70, -14], [4, -48], [-36, 4], [2, -15], [62, -12], [-70, -2], [-21, 35], [54, 1], [-3, 6], [24, 31], [18, -16], [-40, -18], [-36, -10], [-22, 2], [72, 13], [2, -18], [-19, 31], [-33, 32], [42, -24], [-13, -25], [-7, -29], [28, -22], [-3, -23], [29, -13], [23, -13], [40, -5], [0, -16], [-38, -10], [-39, 24], [72, -7], [0, -56], [-10, -6], [59, -7], [-10, 5], [-7, 13], [32, 0], [40, 28], [57, -15], [4, 8], [36, 5], [32, 26], [34, 20], [22, -37], [-46, 14], [52, 26], [30, -16], [-7, 19], [86, -5], [29, -2], [-45, 5], [-52, -10], [-49, 11], [42, -2], [-8, 18], [12, 46], [28, 19], [-50, 15]];
    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_bh();".
// 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_bh(:prec:=2)
    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_bh();".
// 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_bh( : 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, 4, 1]>,<13,R![3, 1]>],Snew);
    return Vf;
end function;