
// Make newform 3240.2.a.a in Magma, downloaded from the LMFDB on 13 March 2026.
// To make the character of type GrpDrchElt, type "MakeCharacter_3240_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_3240_2_a_a();".
// 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_3240_2_a_a();".
// 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_3240_a();"
function MakeCharacter_3240_a()
    N := 3240;
    order := 1;
    char_gens := [2431, 1621, 3161, 1297];
    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_3240_a_Hecke( : Kf := Kf)
    return MakeCharacter_3240_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]];
    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], [0], [-1], [-2], [3], [0], [-2], [1], [-2], [3], [-3], [0], [-5], [-4], [8], [-2], [-3], [6], [-10], [15], [-14], [-8], [0], [-1], [-16], [13], [-2], [-2], [-13], [-18], [2], [1], [-12], [-9], [-18], [-3], [4], [2], [-14], [6], [-7], [-25], [11], [12], [-8], [24], [-27], [0], [18], [6], [-4], [0], [17], [4], [-6], [18], [9], [-16], [-8], [14], [-14], [4], [28], [25], [-8], [-28], [25], [16], [-30], [1], [-12], [11], [-38], [-22], [16], [20], [-18], [2], [18], [-2], [20], [9], [13], [10], [-11], [-24], [-31], [16], [-7], [22], [-26], [-21], [12], [17], [5], [24], [-18], [10], [24], [-37], [-20], [-32], [24], [13], [19], [34], [-24], [-2], [41], [41], [24], [8], [28], [44], [-11], [15], [-36], [-6], [12], [-40], [25], [26], [-30], [-12], [52], [19], [-38], [-15], [-40], [2], [31], [18], [0], [36], [17], [9], [-32], [-18], [52], [-9], [9], [-25], [50], [-8], [23], [-1], [-50], [8], [-15], [-38], [28], [47], [-2], [-36], [-26], [-11], [-5], [-15], [26], [46], [-26], [36], [-46], [-29], [28], [18], [-43], [-6], [29], [-18], [-53], [-49], [35], [10], [32], [15], [60], [9], [0], [11], [-44], [56], [-14], [18], [12], [59], [38], [14], [-65], [24], [-26], [-60], [16], [51], [-16], [-34], [-23], [16], [-42], [-56], [-37], [-37], [-14], [-31], [-40], [-12], [17], [-56], [54], [-20], [38], [-1], [-20], [68], [-13], [13], [58], [13], [58], [18], [-43], [47], [-45], [-4], [-62], [-17], [6], [12], [-44], [-24], [-34], [35], [8], [10], [-12], [30], [-29], [30], [-44], [-35], [-6], [-28], [36], [-10], [-32], [-3], [-68], [-36], [9], [20], [4], [-15], [-20], [-25], [64], [12], [7], [-14], [18], [38], [-8], [-16], [-47], [-76], [32], [7], [50], [37], [-68], [-34], [-13], [-48], [16], [-17], [6], [12], [-18], [45], [-30], [0], [-48], [-32], [34], [-62], [-14], [63], [84], [20], [-71], [42], [19], [-36], [54], [-29], [22], [5], [-59], [6], [-51], [38], [-26], [38], [31], [66], [-3], [-34], [86], [9], [61], [-4], [74], [-57], [-43], [-56], [-22], [-23], [-77], [-45], [-72], [-75], [32], [-18], [39], [66], [88], [57], [-23], [-10], [-12], [-34], [25], [68], [79], [-58], [67], [8], [-45], [-54], [61], [12], [4], [-18], [49], [-40], [-62], [-20], [5], [-32], [-59], [0], [47], [-68], [-18], [74], [-55], [34], [-27], [-91], [34], [52], [44], [-27], [54], [29], [28], [32], [60], [44], [-7], [27], [-4], [46], [-59], [5], [-86], [75], [27], [24], [-51], [62], [-30], [34], [-36], [-30], [23], [26], [-76], [66], [-26], [38], [30], [-68], [52], [52], [-95], [-72], [37], [-58], [20], [-5], [-41], [12], [-18], [74], [-27], [40], [70], [-30], [52], [69], [30], [-78], [46], [-65], [56], [-14], [27], [86], [66], [54], [-34], [52], [-24], [23], [-44], [36], [-92], [102], [103], [48]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3240_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3240_2_a_a();".
// 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_3240_2_a_a(:prec:=1)
    chi := MakeCharacter_3240_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_3240_2_a_a();".
// 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_3240_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![2, 1]>,<11,R![-3, 1]>,<17,R![2, 1]>],Snew);
    return Vf;
end function;