
// Make newform 3680.2.a.n in Magma, downloaded from the LMFDB on 11 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3680_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_3680_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_3680_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 := [-3, 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_3680_a();"
function MakeCharacter_3680_a()
    N := 3680;
    order := 1;
    char_gens := [1151, 1381, 737, 3041];
    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_3680_a_Hecke(Kf)
    return MakeCharacter_3680_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], [1, 0], [-1, 0], [1, -1], [-1, 1], [0, 1], [2, -2], [-1, 1], [-1, 0], [1, 4], [-2, -1], [-1, 1], [-1, -2], [-5, 1], [1, 4], [-4, -2], [0, -6], [-1, -5], [-3, 5], [0, -7], [-14, -1], [7, 5], [-4, 0], [-9, 5], [1, 5], [-4, 8], [2, -2], [-9, -1], [-11, -5], [-9, -1], [-3, -8], [4, -5], [2, -2], [-10, 3], [-2, -4], [8, 7], [1, 1], [-3, -2], [14, 4], [6, -8], [6, 5], [-9, -5], [8, 6], [-16, -5], [8, -5], [17, 3], [-2, -6], [-10, 4], [3, 1], [-1, -7], [-4, 11], [16, -1], [0, 14], [-8, 4], [2, -1], [5, -11], [-15, 4], [-2, -14], [-10, 11], [-1, -7], [4, 10], [0, 16], [8, -2], [-14, 11], [-12, 4], [14, -4], [-2, -1], [19, -9], [-6, -8], [-11, 0], [18, -1], [19, 5], [18, 2], [-14, 4], [6, 6], [1, 3], [-22, -6], [-4, 1], [18, -8], [-19, 0], [3, -1], [-11, -1], [-11, -3], [2, 4], [20, 3], [9, -14], [4, 8], [24, 4], [-15, 2], [4, 2], [18, -8], [-6, -18], [-13, 6], [-8, 5], [-10, -1], [-8, 6], [15, -10], [30, -6], [-23, -9], [11, 6], [7, -12], [-1, 7], [12, 0], [6, -8], [-2, -10], [4, 17], [-29, 2], [-12, -16], [24, -10], [-5, -4], [4, 6], [-10, -14], [27, 1], [-22, -14], [33, -3], [3, 13], [24, -2], [3, 2], [18, -3], [-28, 12], [-2, -16], [4, 19], [2, -4], [-1, 22], [-18, 6], [9, -1], [11, 9], [26, 6], [4, -18], [12, 12], [4, -21], [18, 18], [1, -15], [7, -3], [19, -12], [-6, 4], [7, -5], [4, 16], [33, 11], [-24, 4], [-8, 11], [-32, 8], [-15, -12], [-10, 6], [-22, -10], [27, -5], [-2, -12], [-6, -11], [2, 5], [-19, -14], [16, -6], [2, 6], [34, 0], [1, 22], [23, -1], [23, 9], [23, 3], [-9, 6], [37, -5], [-4, -24], [37, -2], [-8, 18], [7, 2], [-18, 0], [27, 17], [8, -18], [-44, 0], [-24, -10], [-25, -21], [14, 17], [-15, -17], [25, -10], [-6, -6], [-52, 0], [-18, 1], [15, -7], [44, -7], [7, 26], [-23, 9], [-14, 22], [-33, -14], [17, -23], [16, 15], [12, 18], [36, -2], [16, 4], [36, -8], [39, 7], [-5, -2], [-24, 8], [-2, -19], [44, 2], [38, 16], [-24, 20], [36, 2], [7, -9], [5, 5], [-26, -2], [6, 26], [-29, 12], [-30, -2], [-44, -2], [22, -21], [5, 9], [-40, 16], [40, 9], [7, -15], [-17, 16], [-7, 0], [-12, -15], [-48, 11], [25, -8], [17, -9], [4, 18], [-26, 2], [19, -19], [-13, 12], [-35, 16], [6, 8], [0, -31], [-17, -12], [-7, 15], [11, -14], [-63, -5], [-9, -10], [27, 18], [-39, 7], [-8, 37], [-18, -4], [0, -8], [-50, -4], [-15, 21], [28, -14], [-50, -8], [-2, -2], [9, -9], [-14, 26], [-15, 23], [-36, -7], [6, -1], [40, -4], [62, 3], [-43, -6], [-33, 14], [4, 14], [34, -7], [-19, 18], [-36, 14], [-44, -10], [-6, 24], [-12, 4], [-12, -6], [-40, -2], [-45, 17], [36, -20], [-20, -13], [-41, -9], [-54, 4], [-36, 17], [12, 33], [-32, -8], [37, 19], [-38, 2], [-19, -19], [-8, -12], [9, 39], [-21, -9], [-5, -7], [-7, 9], [6, -9], [-23, 6], [70, -4], [-23, -3], [27, 3], [-10, 1], [-25, 12], [-12, -40], [-38, -6], [-26, 4], [-26, -14], [3, -6], [24, -18], [11, -13], [12, -22], [12, -36], [-32, 4], [-6, -24], [-51, -1], [38, 10], [14, -18], [-24, -14], [-12, -36], [42, 4], [3, 37], [-28, 5], [12, -14], [-56, 8], [22, 15], [-6, -9], [25, 34], [35, 23], [-15, -9], [-10, 18], [-33, 6], [34, 22], [10, 7], [-36, -8], [8, -16], [32, -2], [-38, 12], [3, -30], [-13, -1], [14, -26], [19, -38], [-15, 29], [-1, 29], [-26, -17], [-68, 5], [-8, -36], [13, 32], [-25, -9], [-9, -17], [-19, 10], [-2, -24], [24, 10], [-26, -14], [2, -40], [5, -6], [75, -1], [52, -20], [-17, 0], [-32, -29], [-72, -13], [21, -8], [-23, -13], [9, 20], [16, 10], [-1, 51], [-43, 0], [-27, -9], [-52, 1], [51, -21], [78, -2], [4, 38], [-32, 18], [-4, 40], [29, 24], [4, 46], [0, -30], [-20, 38], [-32, -23], [-44, 15], [-42, 28], [28, 38], [0, 8], [12, -26], [28, -4], [-2, 0], [-14, 15], [-48, 18], [64, 10], [-59, -14], [45, -4], [-18, 0], [1, -32], [20, -43], [-30, -31], [-11, -35], [18, 28], [-72, -1], [64, -16], [15, -24], [6, -34], [50, -18], [-34, 9], [-36, 29], [-3, 47], [30, 20], [-6, 50], [-16, -17], [6, 36], [69, 15], [-16, -10], [-36, -25], [61, 1], [-63, 8], [-38, 31], [-32, 10], [16, 34], [-8, 0], [-14, -30], [0, -32], [-50, 3], [66, 8], [21, 17], [-62, 0], [51, 7], [55, 17], [2, 18], [-18, -46], [15, -44], [36, -5], [-37, 25], [46, -6], [65, 18], [-48, 15], [-35, 27], [11, -18], [-63, -5], [48, 16], [34, -13], [-42, 27], [-3, -39], [12, -10], [-13, -11], [43, -10], [24, 40], [-56, -22], [-29, 11], [-9, -3], [-55, -1], [-49, -5], [66, -16], [-8, -52], [48, -13], [54, 11], [5, -11], [-10, 12], [-52, 5], [36, 25]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3680_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3680_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_3680_2_a_n(:prec:=2)
    chi := MakeCharacter_3680_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_3680_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_3680_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3680_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1]>,<7,R![-2, -2, 1]>,<11,R![-2, 2, 1]>,<13,R![-3, 0, 1]>],Snew);
    return Vf;
end function;