
// Make newform 3648.2.a.be in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3648_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_3648_2_a_be();".
// 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_3648_2_a_be();".
// 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_3648_a();"
function MakeCharacter_3648_a()
    N := 3648;
    order := 1;
    char_gens := [2623, 2053, 1217, 1921];
    v := [1, 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_3648_a_Hecke(Kf)
    return MakeCharacter_3648_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 := [[0], [1], [1], [1], [-5], [-4], [-3], [1], [0], [4], [2], [-4], [-4], [-5], [7], [2], [-10], [-15], [-14], [-2], [-3], [14], [0], [10], [4], [-6], [6], [2], [-2], [-16], [-4], [13], [-3], [-5], [15], [-6], [-2], [20], [16], [6], [12], [-14], [-3], [6], [-26], [17], [-22], [-8], [0], [-29], [-21], [21], [16], [-3], [-28], [3], [6], [-24], [-1], [30], [7], [-24], [6], [15], [-2], [18], [-14], [-16], [17], [23], [-14], [-19], [12], [16], [-10], [-2], [21], [-25], [22], [22], [28], [-26], [12], [16], [-8], [33], [-20], [-1], [39], [-25], [25], [0], [10], [-20], [21], [-24], [-12], [6], [6], [29], [36], [-17], [0], [-16], [-40], [-9], [-3], [30], [-38], [-14], [-16], [15], [21], [12], [-7], [-20], [-21], [21], [23], [6], [-32], [-34], [32], [-48], [-43], [-6], [26], [1], [-9], [46], [-25], [6], [-52], [33], [21], [-29], [-42], [-12], [18], [19], [-40], [31], [-23], [-26], [52], [-14], [6], [-26], [-29], [16], [14], [9], [7], [6], [38], [54], [48], [-18], [-43], [38], [12], [30], [0], [28], [-22], [12], [20], [3], [-38], [11], [-24], [54], [20], [46], [-20], [43], [52], [-9], [40], [53], [47], [-20], [50], [2], [-63], [-61], [-6], [10], [-40], [36], [-20], [43], [-50], [-40], [-4], [24], [61], [-58], [14], [-29], [-60], [-4], [10], [-12], [-68], [-45], [7], [-4], [-47], [10], [-58], [30], [21], [-48], [4], [12], [-64], [4], [-22], [9], [-50], [68], [48], [-53], [-42], [25], [-66], [-6], [-38], [55], [-19], [10], [-38], [30], [1], [17], [-31], [69], [-36], [14], [-42], [33], [-24], [18], [-44], [-13], [55], [30], [8], [19], [-30], [-13], [12], [6], [-35], [12], [51], [44], [-72], [-14], [-56], [54], [13], [14], [-57], [74], [-70], [-13], [-38], [-51], [-26], [-16], [-7], [21], [-62], [-72], [7], [-50], [10], [83], [-66], [80], [-77], [-18], [-85], [-3], [-29], [16], [-71], [40], [42], [51], [24], [-46], [-6], [78], [64], [9], [-78], [37], [25], [-62], [17], [82], [17], [82], [-60], [-48], [-15], [-2], [-3], [-34], [20], [-76], [-48], [-38], [33], [-10], [79], [9], [-36], [-43], [-14], [-62], [-54], [82], [0], [48], [-52], [59], [-55], [-26], [56], [-37], [-25], [-83], [-78], [4], [-73], [36], [-14], [1], [-64], [58], [-14], [-12], [54], [40], [88], [-41], [28], [20], [-43], [90], [-18], [-12], [-48], [-48], [-19], [-76], [-1], [-42], [80], [-50], [-19], [-56], [90], [-2], [-2], [-84], [-52], [39], [36], [-11], [-65], [-84], [8], [-13], [-21], [-12], [-42], [-37], [79], [27], [-56], [-88], [12], [58], [-11], [8], [-34], [50], [12], [25], [-70], [102], [-22], [-48], [-16], [22], [86], [39], [-88], [-54], [-10], [55], [-5], [-52], [12], [65], [94], [-11], [66], [-39], [-38], [2], [-100], [-14], [58], [-50], [-14], [-92], [79], [-72], [44], [4], [-36], [-1], [43], [51]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3648_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3648_2_a_be();".
// 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_3648_2_a_be(:prec:=1)
    chi := MakeCharacter_3648_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_3648_2_a_be();".
// 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_3648_2_a_be( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3648_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-1, 1]>,<7,R![-1, 1]>,<11,R![5, 1]>,<23,R![0, 1]>,<31,R![-2, 1]>],Snew);
    return Vf;
end function;