
// Make newform 3640.2.a.n in Magma, downloaded from the LMFDB on 08 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3640_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_3640_2_a_n();".
// 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_3640_2_a_n();".
// 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 := [-1, -1, 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_3640_a();"
function MakeCharacter_3640_a()
    N := 3640;
    order := 1;
    char_gens := [911, 1821, 1457, 521, 561];
    v := [1, 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_3640_a_Hecke(Kf)
    return MakeCharacter_3640_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, 1], [1, 0], [1, 0], [0, -4], [1, 0], [-3, -1], [-1, -1], [-6, 4], [-4, 1], [0, 5], [4, -5], [-7, -1], [-4, 2], [-2, 4], [2, -4], [8, -9], [-10, 4], [-1, -1], [-12, 6], [-4, 4], [11, -9], [-6, 0], [-4, 7], [-6, -6], [0, 0], [11, -1], [6, -14], [-6, 8], [-10, 2], [6, 4], [-6, 4], [-16, -3], [6, -6], [-6, 16], [12, 2], [-8, 5], [4, 5], [0, 2], [0, -13], [-7, 9], [-14, 2], [4, 9], [-11, 19], [-4, 5], [-8, -2], [-13, -5], [-10, -6], [26, 0], [1, -9], [4, 4], [14, 0], [-1, -3], [4, -6], [6, -16], [-6, 0], [4, -4], [12, -8], [-8, 4], [-12, 10], [-3, 21], [-12, 6], [-6, 0], [-10, 10], [4, -3], [14, -12], [-24, 0], [14, -12], [6, 16], [12, 11], [2, -8], [-22, 18], [-8, 8], [18, -18], [-22, 16], [-10, -10], [-31, -1], [12, -10], [16, -26], [-6, 20], [6, 10], [-10, -16], [6, -2], [-7, -17], [4, -20], [2, -8], [10, 18], [0, -3], [-32, 5], [3, 19], [9, 17], [-4, 21], [12, -11], [-32, 12], [16, 4], [24, -16], [5, 15], [-12, -20], [24, 0], [-12, 6], [18, -36], [1, 19], [19, 7], [24, -13], [32, -9], [40, 4], [10, -22], [20, -24], [-12, -8], [2, 6], [-16, -15], [22, -12], [-1, 5], [20, -9], [-10, 18], [-16, 7], [-10, 12], [8, 9], [4, 14], [-29, -5], [7, -35], [32, -16], [-6, 12], [-1, -13], [16, -23], [7, -27], [4, 12], [-14, 22], [36, -13], [-18, -8], [-2, -22], [3, -21], [12, -33], [-42, 0], [32, -9], [-42, -4], [14, -16], [40, 4], [-32, 25], [11, -39], [36, -8], [-8, 2], [-12, 32], [-8, 8], [32, -16], [28, -32], [4, 4], [28, -15], [18, 2], [1, -11], [-17, 25], [26, -42], [4, 16], [36, -7], [16, 0], [-31, 9], [-8, 25], [24, 7], [9, -29], [-31, 27], [-19, 17], [-36, 32], [28, 15], [-28, 24], [1, 11], [42, 8], [-17, 23], [-19, 7], [-34, 38], [11, 29], [24, -2], [-28, 35], [-12, 8], [-46, 20], [42, -20], [-12, 46], [-20, 6], [-36, 10], [20, -27], [24, -6], [16, -21], [18, -36], [28, -38], [-14, 2], [0, 8], [-44, 12], [20, -24], [-2, -14], [32, -28], [4, 35], [35, -15], [-20, 5], [-20, -25], [33, -1], [6, -38], [6, 28], [56, -14], [36, 12], [4, -40], [-56, 17], [48, -14], [-38, 28], [6, -18], [42, -16], [19, -33], [1, -41], [-25, 55], [-34, -4], [-30, 16], [24, -48], [26, 2], [-12, 30], [-18, 8], [-40, -6], [-48, 8], [-8, -7], [0, 10], [8, -16], [-12, 11], [-9, 5], [-45, 9], [-3, 17], [42, -36], [-27, 5], [-39, 17], [-20, 23], [-44, 13], [-44, -7], [-31, 41], [16, -22], [-48, 40], [-42, 18], [4, 16], [-8, 5], [17, -7], [32, -34], [-14, 4], [-20, 51], [-35, -15], [2, -20], [-32, -23], [4, -38], [52, 3], [40, -20], [-48, 24], [-18, 28], [38, -36], [-9, -29], [-4, 2], [-42, 12], [-27, 39], [-15, 11], [72, 4], [50, -34], [-14, -4], [20, -46], [-17, -27], [44, -8], [23, 5], [-36, 6], [14, 16], [-6, -34], [-60, 15], [16, -17], [58, -12], [-18, -2], [-24, 0], [13, 31], [-20, -17], [-38, -12], [-4, -32], [-7, 33], [-12, 48], [-1, -41], [-61, 29], [-12, -18], [-39, -7], [-30, 0], [64, 0], [-52, 10], [-57, 43], [12, -42], [-52, 35], [-30, -20], [-20, 28], [-11, 1], [12, -36], [22, -40], [27, -21], [16, -36], [-42, 32], [44, -64], [48, -11], [39, -29], [20, -54], [44, -52], [30, 16], [-33, 21], [-38, 40], [-16, 18], [3, 17], [-47, -9], [-36, 7], [-36, -16], [3, -49], [-11, -17], [-54, 18], [-52, 22], [-8, 32], [40, 22], [-41, -1], [-6, 50], [-21, 1], [34, -46], [24, -69], [12, -18], [-36, 16], [-44, 14], [4, -16], [-23, 7], [30, 24], [17, -37], [76, -10], [-72, 19], [-8, -9], [34, -10], [-18, -6], [42, 6], [-44, 38], [20, -28], [24, 2], [26, -24], [26, -30], [-40, 66], [40, 24], [24, 16], [-20, -32], [48, -18], [4, -11], [28, -15], [8, 23], [-24, -30], [8, 21], [48, 7], [14, 24], [-16, 25], [54, -8], [40, -68], [7, 31], [-2, -20], [12, -19], [18, -4], [-42, 4], [-24, -1], [-48, 15], [-28, 39], [-25, -25], [56, 23], [-22, 22], [-44, -15], [-32, 59], [18, -4], [-8, -18], [-2, 0], [4, 35], [22, 14], [-50, 64], [15, 23], [40, 17], [-43, 1], [-44, 58], [64, -31], [34, -24], [-46, 2], [12, 52], [-29, 63], [31, -5], [-94, 10], [28, -46], [52, -53], [-72, 3], [-14, 12], [7, -5], [55, 9], [8, -3], [-44, 42], [22, 8], [-43, -13], [24, 0], [-52, 3], [10, -62], [4, 24], [-10, -30], [20, 23], [-4, 50], [60, 2], [-20, 19], [26, -62], [-56, 51], [70, 0], [-65, 53], [0, -50], [28, 31], [-44, 62], [59, -51], [-20, 22], [27, -35], [-8, -44], [26, -66], [48, -16], [-10, 0], [-10, 4], [-38, 84], [3, -9], [30, 16], [32, 44], [20, -12], [-28, -24], [-22, 8], [44, -19], [-38, -32], [-86, -4], [7, 15], [-36, 28], [-4, 38], [26, -62], [44, -24], [-26, -10], [16, -4], [12, 26]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3640_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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