
// Make newform 3240.2.a.o in Magma, downloaded from the LMFDB on 23 April 2024.
// 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_o();".
// 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_o();".
// 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 := [-6, 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_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)
    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+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, 0], [0, 0], [1, 0], [1, 1], [0, 0], [0, 0], [2, 0], [-2, -2], [5, 1], [-3, 2], [6, -2], [-6, 0], [5, 2], [2, 4], [7, 1], [2, -4], [6, -2], [-3, 0], [-1, -5], [0, -4], [4, 4], [-2, -2], [3, 1], [7, -4], [2, 0], [2, 0], [10, 0], [5, 3], [-1, -4], [0, 4], [-7, 3], [2, -2], [0, -8], [0, 8], [21, 0], [12, 0], [-14, -4], [2, -4], [-1, -3], [12, 0], [4, 4], [5, -6], [10, 2], [-12, 4], [-4, 8], [18, 2], [12, 0], [3, 1], [18, -4], [-9, 2], [10, 4], [-12, 4], [11, -6], [2, -2], [-18, -4], [-18, 0], [-3, -8], [26, 2], [-14, 0], [13, -2], [-11, 3], [2, -8], [-17, 5], [2, 2], [4, -8], [16, 4], [-2, -2], [-20, -4], [6, 8], [-17, 6], [0, 0], [-14, -6], [22, 0], [2, 8], [22, 2], [-2, 0], [-3, 8], [-22, 0], [-6, -8], [10, -8], [-20, 0], [6, 8], [-10, -2], [-2, -4], [-8, -4], [-9, 7], [-2, -8], [-2, 8], [-17, 2], [22, -4], [2, -4], [-6, -10], [6, -8], [4, 4], [-28, 0], [-3, -9], [9, -2], [-19, 4], [-27, -3], [17, -2], [-29, 5], [-34, -4], [3, 3], [-10, 0], [16, 4], [-26, 0], [-15, 3], [2, 12], [10, 6], [14, 0], [-15, 7], [-10, -8], [-4, 16], [-10, -6], [4, -8], [9, 2], [-3, -7], [-9, 1], [-18, -4], [-20, 4], [10, 0], [-16, 0], [-24, 4], [6, -12], [34, 2], [-1, -12], [-5, 6], [-6, 14], [-19, -5], [-10, -8], [-38, 2], [-15, 9], [36, -4], [12, 8], [-35, 0], [-15, 4], [-16, 4], [-30, -8], [8, 0], [30, 0], [18, -2], [7, -4], [-1, -3], [-7, -19], [11, 16], [10, -6], [28, -4], [-20, 8], [0, 16], [-55, -1], [-20, 8], [-11, 2], [-23, 9], [18, -8], [-29, 3], [14, -10], [10, 6], [-18, -8], [8, -20], [-13, 6], [29, -7], [24, -4], [17, 13], [-46, 6], [-16, 12], [9, 1], [14, 6], [6, -12], [-19, -12], [6, -4], [-28, -12], [29, 6], [28, 0], [-8, 0], [-28, -12], [-6, -16], [-36, 8], [30, -8], [3, -1], [35, -12], [-2, 0], [-2, -10], [-38, 8], [-24, -12], [-39, -1], [49, 2], [20, 8], [-7, 3], [-11, -2], [-6, 14], [4, 8], [15, -1], [-26, -2], [-21, -12], [1, 5], [-20, 0], [7, 2], [16, 4], [-30, 16], [5, 1], [-2, 8], [20, -4], [-32, 0], [-19, -4], [4, 0], [-12, -4], [-16, 16], [-13, -7], [27, -16], [-20, -8], [26, -16], [10, 8], [-41, -7], [-14, 4], [-20, -8], [31, 6], [25, -3], [26, 16], [11, -23], [36, 12], [5, -4], [32, -12], [51, 8], [-37, 7], [29, 9], [22, 0], [6, 28], [12, -4], [-11, -9], [-6, 26], [50, -4], [-16, 8], [8, -4], [41, -10], [-3, 1], [15, 3], [1, -10], [48, 4], [2, 18], [20, -8], [15, -11], [-4, -4], [-21, 13], [-1, 14], [50, 4], [-6, 18], [1, 13], [-42, -14], [-30, -10], [13, -3], [22, 0], [-39, -4], [26, 16], [-7, 22], [-4, -4], [0, -8], [-14, -24], [-17, -5], [-18, 0], [-10, -8], [43, 5], [1, 9], [-14, -16], [2, -24], [28, -8], [-14, 18], [-26, 8], [41, 8], [-47, 9], [-14, 0], [-10, -8], [15, -7], [10, -16], [46, 6], [60, 8], [27, -19], [45, -3], [-6, -16], [-21, -14], [0, -28], [-18, -16], [40, 16], [2, -16], [-11, 14], [7, 5], [60, -8], [-48, -12], [-20, -8], [52, -4], [51, -2], [2, 16], [30, 8], [42, -16], [-10, 6], [10, -12], [-11, -24], [-32, 4], [0, 12], [18, -18], [50, 0], [46, 4], [4, 28], [22, -18], [-18, 28], [-54, 14], [-52, 0], [49, 7], [-21, 10], [14, -2], [-16, 20], [10, -4], [-3, 6], [10, 16], [13, 11], [58, 8], [49, -4], [-10, 10], [12, 4], [-42, 20], [-66, 0], [-28, -16], [12, 4], [42, 8], [42, -8], [-22, 24], [15, 30], [-14, 18], [-7, -17], [75, -3], [22, -16], [-11, -8], [-26, -12], [4, -24], [46, -12], [-2, 6], [25, -7], [39, -4], [-60, 4], [-17, -10], [-6, 12], [-44, -8], [-36, -4], [53, -12], [14, 18], [74, 0], [-16, -12], [-34, 8], [19, 1], [2, -18], [-60, 8], [2, 2], [28, -20], [-33, 12], [-13, -25], [-73, 6], [2, -4], [-36, 0], [-56, 8], [8, 8], [-67, 3], [-4, 8], [9, -28], [18, 24], [4, 20], [-26, 20], [2, 8], [0, -12], [47, 9], [-10, 0], [-66, -6], [8, -16], [-19, -19], [-43, 4], [-40, 16], [-20, 28], [-24, -4], [-48, 0], [24, -16], [-45, 2], [14, 12], [-9, 26], [56, 8], [-3, -9], [12, -12], [38, -6], [-2, 8], [2, 6], [-24, -16], [25, 7], [-41, 7], [-51, -18], [-4, -4], [-22, -6], [22, -20], [38, -14], [-18, 12], [22, -30], [-26, -8], [50, -6], [-22, 32], [-17, 16], [-30, -16], [-6, -36], [-62, -4], [-63, 0], [-20, -12], [52, 12], [21, 24], [-23, -5], [-54, 14], [-36, 16], [-36, -24], [-1, -33], [-44, 12], [56, -12], [35, -14], [72, 12], [2, 20], [0, -12], [-39, 7], [-23, -4], [-8, 28], [18, 16], [-26, 30], [-2, 8], [36, -20], [23, 11], [78, 0], [-32, 4], [-18, 34]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3240_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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