
// Make newform 3640.2.a.o in Magma, downloaded from the LMFDB on 25 April 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_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_3640_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 := [-1, -4, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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, 0, -1], [-1, 0, 0], [-1, 0, 0], [0, 1, 1], [1, 0, 0], [-2, -2, -1], [0, 0, -1], [2, -3, 1], [-1, 3, 2], [5, 0, 1], [-5, -2, 1], [2, -3, 0], [-2, 0, 2], [2, 5, -3], [-2, 2, 2], [-1, -3, 4], [-6, -1, -1], [4, 1, 0], [2, 2, 4], [0, 7, -1], [4, 1, 2], [-6, -4, 0], [-5, 3, -2], [-4, 1, -5], [0, 7, -1], [0, -4, 5], [-8, 2, -4], [-2, -8, 4], [0, -7, 7], [2, 5, 5], [-14, -2, 2], [1, 1, -6], [4, 4, -6], [-6, -11, 5], [6, -8, 2], [-13, 4, -5], [5, 7, -4], [-10, 2, -8], [-11, 5, -6], [8, -2, 1], [0, -9, -3], [-9, -3, 0], [-2, 6, -3], [-11, -4, 1], [-10, 4, -2], [16, -4, -3], [4, -3, -1], [-6, 0, 0], [-14, -2, -3], [4, 11, -5], [-6, -4, -4], [2, 6, 3], [-6, 3, -7], [-10, -6, 2], [-6, -2, 6], [0, 13, 1], [12, -1, -5], [-24, 0, -4], [6, -4, 2], [4, -3, 12], [6, 6, 4], [-2, 2, -6], [16, 0, -2], [-1, -1, -6], [-10, 7, -9], [-12, 1, -11], [22, -1, 3], [-10, 2, -10], [7, -5, 2], [-2, -3, -3], [-12, 6, -16], [-16, -4, -4], [-4, -10, 4], [10, -10, 6], [28, 1, 3], [-2, 6, 11], [-14, -4, -6], [6, -10, -8], [-6, -20, 8], [-16, 5, -9], [-6, 7, -1], [0, 8, 10], [10, 10, -5], [12, 10, -6], [-2, 8, -8], [-12, 8, 2], [-7, 5, 6], [-3, 15, -8], [4, 6, -3], [4, -6, 1], [-7, 3, 0], [-3, -17, 12], [12, 0, -4], [12, -5, -5], [-8, -16, 0], [-10, 4, 3], [8, -6, 18], [0, 3, 7], [-6, 8, -2], [-2, -2, -2], [-14, -15, 4], [-16, -3, 2], [-7, -18, 7], [21, -5, -8], [4, 2, 6], [-12, 6, 4], [0, -2, 2], [-12, -17, 3], [24, -4, 6], [-9, 17, -4], [14, 1, 1], [14, 12, -3], [3, 9, 0], [24, -6, 16], [-3, -4, -15], [-14, -8, -4], [-25, -9, 2], [-22, -4, 6], [-8, 14, -9], [2, 12, -7], [16, -9, 23], [-10, -5, 3], [8, -11, 16], [1, -17, 0], [-2, -4, 3], [-4, 9, -3], [-20, 12, -2], [-7, 17, -22], [6, 2, 2], [24, 8, -2], [12, -12, 3], [-15, 0, 1], [22, 2, 2], [7, 2, -7], [14, -13, 3], [-38, 0, -4], [8, 22, -14], [-17, 2, -3], [-10, 10, 5], [12, 4, -4], [2, -24, 2], [0, 11, -9], [0, 12, 12], [-8, 10, 10], [12, -21, -1], [4, 0, 12], [19, -11, 4], [-4, 15, -19], [0, 20, -3], [-22, 7, 0], [24, 14, -12], [28, -6, -2], [-5, 3, -10], [32, -4, 8], [16, 6, -7], [11, -4, 3], [-1, 18, -15], [14, -12, 1], [-10, -2, -15], [-32, 2, 7], [8, -10, -6], [-1, -7, 0], [-8, -1, 3], [-6, -14, 1], [-10, -2, 2], [0, -7, 2], [22, -1, 12], [-16, 4, 2], [-10, -10, 17], [-10, 6, 16], [15, -5, 10], [4, 21, -15], [10, 0, -4], [-6, -16, 0], [-10, 4, -14], [14, -11, -1], [-10, -16, -2], [-17, 19, 10], [26, -7, -9], [-1, 9, -20], [-26, -3, -15], [2, 6, 4], [24, -7, 7], [-32, 2, -14], [-24, 2, -30], [16, 24, -16], [-8, -12, 2], [8, 10, -2], [7, -10, 13], [-6, 16, 7], [-19, 2, -13], [-23, -21, 0], [2, 6, -11], [-20, -6, 4], [-30, -6, 6], [-2, -16, -2], [-20, 10, -14], [-12, -14, 14], [15, 1, 4], [-2, -9, -11], [2, 28, 0], [-4, -6, 12], [10, 10, -2], [20, 12, 3], [-46, 2, -5], [0, -14, -7], [-38, 0, -4], [-10, 2, -2], [-16, 1, 1], [44, -3, 11], [34, -9, 25], [-26, 11, -17], [-18, 12, 2], [-32, 11, -1], [1, 0, 9], [-2, 25, 7], [-8, -26, 2], [11, 9, 12], [-26, 15, -4], [-2, -2, 9], [4, -17, -12], [-2, -6, 6], [-4, -12, -3], [-28, 11, 14], [-15, -5, -12], [-9, 3, 14], [-25, 20, -5], [-36, -22, 9], [38, 5, -9], [-36, 0, 4], [8, -4, 10], [8, 28, -8], [15, -6, -15], [20, -1, 18], [-14, 25, -13], [-22, -7, -23], [19, 2, 5], [-28, -4, 15], [-14, -30, 10], [-3, 16, -3], [6, -11, -17], [1, 9, -6], [-36, -10, 10], [-4, 3, 23], [10, 4, -4], [-42, -19, 9], [-12, 30, 1], [22, -1, 5], [-18, -13, -1], [18, -13, -14], [-2, 10, 13], [-4, 30, -6], [48, 10, -8], [-2, 27, -21], [6, -14, 12], [46, 8, -9], [32, -10, 18], [-6, -25, 6], [-50, 7, -3], [-2, -22, -14], [-16, -22, -4], [-13, -19, 8], [-23, 2, -1], [10, 17, 5], [-8, -4, -6], [-32, -23, 9], [-18, 17, 4], [23, -26, -7], [14, -12, -4], [64, 5, 1], [12, -1, 24], [44, 15, -1], [48, 10, -1], [-10, 14, 9], [-2, 10, -12], [-4, 14, 11], [2, -23, 21], [-24, 6, 10], [22, -2, 24], [-40, 22, 11], [-42, -12, -6], [-23, 9, 2], [2, 26, -18], [32, -12, 16], [12, -21, 2], [-4, -39, 13], [18, 0, -16], [32, 5, 12], [-40, -6, -14], [-42, 8, -16], [-36, 23, -13], [65, 11, -4], [12, 12, 23], [-26, 18, -12], [-68, 6, -14], [-14, -8, 20], [34, -7, 24], [-18, -23, 5], [-6, 12, 10], [-6, -26, 29], [22, -11, 28], [9, -5, 32], [-20, -16, -16], [-52, -4, 1], [34, 8, 3], [20, -16, 6], [6, 0, 22], [-8, -36, 8], [-14, 6, 12], [-20, -2, 7], [-16, -6, -4], [6, 43, -6], [-4, -6, 20], [15, 41, -28], [-18, 28, 10], [-12, -24, 8], [-14, -14, -16], [44, -14, 2], [22, 31, -8], [-26, 1, -23], [2, 12, -29], [-14, -9, 1], [-41, 7, 10], [-13, 21, 24], [-4, -28, 30], [44, 5, -5], [0, -4, 14], [10, -2, 0], [-4, 0, 16], [-18, 7, 9], [-34, -6, -18], [8, 20, -18], [34, 2, 12], [20, 23, -9], [28, -24, 12], [-32, 18, -14], [66, -13, 1], [-31, -7, -6], [35, 7, -6], [-5, 35, -10], [-2, -21, 13], [21, -7, 10], [-35, 6, -9], [2, 17, -3], [27, -15, -4], [2, -24, 24], [28, 0, -20], [0, 21, -24], [-14, -11, -11], [-7, 11, -16], [26, -24, 20], [-46, 11, -25], [9, 19, 0], [-27, 15, -32], [61, 7, -8], [8, 33, -20], [-25, -1, -26], [-12, 23, -19], [1, -13, 4], [-25, -25, -10], [-6, 10, 10], [-38, 1, -33], [38, -17, 23], [-55, 14, -29], [-8, -21, -7], [34, 18, -10], [12, 12, 23], [-61, -4, 7], [-12, -16, -5], [38, 17, 11], [-43, 3, -4], [-30, 16, -24], [16, 22, 8], [12, -24, 0], [-4, -12, -5], [20, -11, 26], [-36, -5, 5], [-30, -6, -36], [-31, 13, -38], [43, -5, 34], [-6, 4, 32], [0, -34, -11], [10, -28, 9], [69, -7, 22], [70, -2, -8], [26, 22, 6], [-14, -11, -10], [16, -12, -4], [-87, 5, -2], [28, 10, 12], [24, -30, 42], [28, -21, 41], [-59, 23, -4], [50, 14, 8], [-18, -7, 15], [23, -24, -9], [-56, 8, -26], [-7, 19, -20], [-38, -24, 12], [42, 6, 11], [6, -8, -26], [-49, 15, -18], [-22, -5, -15], [-18, -12, -23], [10, -14, 4], [2, 21, -14], [-20, -18, 6], [-16, 0, 6], [16, -4, 28], [38, 24, -8], [-18, 2, 10], [-26, 10, 2], [-20, -22, 23], [50, -3, 21], [4, 26, -22], [52, -8, 0], [52, -10, 6], [30, 1, 25], [47, -13, 34], [18, -2, -2], [-38, 28, 16], [52, 8, 11], [4, 16, 8], [30, 20, 6], [-20, 32, -6], [4, -38, 26], [-44, -18, 4], [-4, -42, 18], [30, 34, -16]];
    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_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_3640_2_a_o(:prec:=3)
    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_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_3640_2_a_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3640_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-7, -4, 2, 1]>,<11,R![-16, -12, 1, 1]>],Snew);
    return Vf;
end function;