
// Make newform 3648.2.a.bd in Magma, downloaded from the LMFDB on 19 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_bd();".
// 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_bd();".
// 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], [-3], [0], [-7], [-1], [8], [0], [-2], [-4], [-4], [1], [-3], [-6], [6], [5], [2], [-2], [-11], [-10], [-16], [-14], [-8], [18], [-14], [14], [10], [4], [16], [3], [1], [9], [-1], [-6], [-2], [4], [20], [-26], [-16], [-14], [7], [10], [-2], [-17], [10], [-8], [20], [23], [-17], [-1], [0], [11], [12], [9], [-10], [-24], [11], [-10], [-19], [0], [-18], [21], [22], [6], [-6], [-12], [23], [-29], [2], [15], [4], [-16], [-6], [18], [13], [-5], [18], [-26], [-12], [-10], [0], [-28], [20], [-33], [12], [15], [15], [-39], [7], [24], [-22], [36], [7], [0], [-40], [18], [34], [-7], [-40], [-9], [32], [36], [16], [-9], [-21], [6], [-6], [-34], [-12], [27], [-39], [4], [-17], [0], [-31], [15], [31], [-6], [24], [34], [28], [4], [-41], [42], [34], [-45], [-7], [14], [-3], [-10], [44], [29], [-15], [27], [-14], [20], [-2], [-1], [-28], [23], [-49], [54], [-28], [30], [46], [-30], [-55], [-48], [22], [-35], [-3], [38], [-38], [58], [56], [14], [-35], [14], [12], [18], [8], [36], [10], [36], [44], [7], [6], [-21], [24], [14], [-28], [38], [56], [-33], [-44], [31], [0], [9], [41], [0], [-46], [22], [-53], [11], [42], [26], [-20], [-60], [48], [-19], [2], [-32], [-4], [-20], [13], [-50], [-42], [-15], [12], [68], [-42], [-32], [60], [11], [-23], [12], [-35], [-34], [-50], [-26], [-29], [-56], [-64], [28], [16], [12], [-42], [-47], [10], [28], [20], [45], [-42], [-59], [-14], [-14], [26], [-15], [9], [-34], [-50], [-30], [35], [3], [17], [-75], [-4], [38], [-54], [-21], [40], [38], [40], [65], [49], [-2], [-40], [-39], [-62], [63], [-36], [-62], [-11], [-44], [23], [48], [8], [-38], [-64], [-42], [65], [-66], [27], [-42], [-38], [-33], [-46], [5], [-42], [56], [49], [-69], [-42], [-64], [-31], [-66], [30], [-3], [-2], [-56], [49], [82], [-15], [-81], [-13], [-36], [47], [-4], [-30], [-3], [84], [10], [-2], [-26], [-8], [-63], [-50], [-89], [-71], [10], [31], [-82], [27], [-78], [-12], [52], [-53], [-26], [-65], [-50], [-36], [-4], [48], [50], [-9], [-30], [-41], [-35], [68], [61], [34], [-34], [-70], [-18], [-4], [64], [52], [19], [53], [54], [8], [-83], [-11], [75], [-38], [-64], [55], [-52], [-34], [53], [68], [50], [-90], [8], [6], [92], [-60], [-49], [-40], [-28], [69], [30], [-30], [56], [64], [-32], [-31], [24], [-77], [-90], [0], [-18], [95], [52], [66], [26], [78], [-36], [88], [21], [36], [-21], [83], [0], [56], [35], [-49], [-100], [78], [31], [-79], [-57], [-44], [-68], [20], [26], [23], [-12], [-38], [-86], [84], [35], [82], [-42], [-46], [36], [4], [54], [-6], [51], [-100], [-30], [-6], [17], [-81], [60], [-24], [-89], [-50], [-51], [-2], [-5], [34], [66], [56], [74], [-54], [2], [54], [104], [-99], [28], [-108], [-88], [-48], [27], [-95], [-47]];
    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_bd();".
// 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_bd(: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_bd();".
// 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_bd( : 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![3, 1]>,<23,R![-8, 1]>,<31,R![2, 1]>],Snew);
    return Vf;
end function;