
// Make newform 1682.2.a.n in Magma, downloaded from the LMFDB on 01 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1682_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_1682_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_1682_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, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 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_1682_a();"
function MakeCharacter_1682_a()
    N := 1682;
    order := 1;
    char_gens := [843];
    v := [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_1682_a_Hecke(Kf)
    return MakeCharacter_1682_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 := [[1, 0, 0], [1, -1, 0], [2, 0, 1], [1, -2, 0], [0, 3, 0], [-1, 3, -2], [2, -2, -1], [5, -2, 3], [-3, 4, -5], [0, 0, 0], [-1, 3, -5], [-1, 5, -1], [3, 1, 4], [8, -3, 0], [1, 0, 1], [7, 3, 0], [-6, -2, -1], [-2, 1, -7], [-11, 0, 0], [-3, 3, -7], [6, -3, 7], [-4, -2, 0], [-4, 3, 5], [7, 2, -2], [-2, 4, -6], [-10, 5, -1], [0, -4, 7], [6, -6, 11], [6, 1, 2], [-8, 3, 5], [4, -11, -2], [2, 0, -7], [7, 0, 3], [-1, -7, -3], [0, -11, 1], [-5, -2, 2], [5, -4, 7], [14, -3, -2], [-7, 1, 4], [8, 2, 5], [-1, -6, 5], [-14, 7, -10], [-5, 0, -1], [11, -3, -3], [12, -11, 12], [-9, 11, -17], [-3, 4, -10], [8, -5, 4], [1, 0, 8], [6, -11, 7], [2, 6, 3], [1, 2, 1], [5, -9, 4], [-4, -13, 3], [-12, 7, -8], [-13, 0, -6], [1, -14, 14], [-1, -16, 13], [11, -9, -4], [14, -1, -4], [-25, 1, -4], [-22, 6, -1], [16, -7, 24], [-5, 8, -14], [-21, 3, 5], [-14, 12, -9], [4, 4, -3], [-12, 17, -14], [-18, 0, -10], [20, -4, -6], [-14, 13, -16], [-20, -2, 0], [3, 1, 4], [1, -9, -10], [-15, 1, 5], [-7, 0, 11], [-5, 4, -12], [-8, 6, -19], [-9, -2, -14], [-4, 2, -21], [6, 6, 17], [-8, 0, 18], [8, -15, 13], [25, -11, 9], [-10, 21, -6], [27, -19, 21], [12, -27, 12], [-15, 7, -8], [-4, 1, 4], [7, 11, 10], [-8, -12, 12], [-15, 14, -22], [-13, 4, -6], [5, -12, 11], [-9, 5, 2], [8, -17, 13], [32, -2, 3], [18, -10, -2], [17, -8, -5], [-6, 5, -9], [-6, -17, -1], [-10, 11, -21], [-9, 15, -18], [24, -12, 19], [36, -5, 15], [12, 11, -1], [8, -14, 10], [-3, 12, -16], [-1, 13, 7], [17, -21, 14], [29, 3, -5], [26, -6, -1], [-9, 0, -12], [15, 7, -2], [25, -11, 33], [8, -15, -2], [1, -18, 0], [1, -7, 7], [30, 5, 4], [-13, 13, 23], [18, -12, 23], [42, -10, 8], [-12, 23, -11], [-9, -2, 19], [1, 23, -7], [11, -6, 4], [10, 11, 6], [0, 29, -9], [24, 6, 6], [11, -9, -8], [0, 27, -12], [-31, 21, -9], [20, 9, 4], [-3, -7, -2], [31, -28, 40], [29, -9, 19], [23, -4, -9], [-37, 24, -29], [-9, -13, 3], [1, -27, 13], [-5, 16, -17], [11, -19, -1], [-3, 9, 8], [-2, 7, -14], [-36, 7, -24], [-21, 14, -23], [4, 11, -6], [22, 4, 12], [10, -27, 18], [-19, 9, -3], [-5, -19, -7], [-16, 18, -9], [20, -12, -22], [-18, 6, -16], [15, 10, -15], [-9, 1, 7], [22, -34, 15], [5, 13, -5], [24, -18, 40], [2, -18, -2], [-23, 22, -6], [-42, 7, -3], [-19, -8, 7], [28, 3, 1], [-28, 5, -8], [-11, 42, -14], [-32, 5, -29], [50, -10, 13], [16, -4, -16], [-12, 19, 0], [-16, 11, 0], [-11, 5, -13], [-30, 0, -5], [31, -20, -5], [-19, -11, -3], [36, -12, 29], [4, -1, 10], [10, -6, -16], [-3, 23, -8], [39, -7, -7], [-26, -3, 1], [27, -7, 14], [24, -7, -9], [-31, 13, -11], [-2, -34, 2], [-5, 22, -5], [-4, -6, -6], [-38, 14, -39], [9, -25, 27], [-26, 6, 1], [9, 15, 17], [-18, -20, 9], [15, -15, -10], [18, 27, -5], [16, -17, 30], [30, -25, 13], [5, 11, 8], [1, 39, -7], [23, -23, 19], [-19, 5, -29], [-5, -18, 7], [-26, 19, 1], [-1, -19, 6], [9, 6, -10], [-37, 19, -46], [-32, 21, -38], [-13, 17, -27], [7, -40, 28], [6, 15, 3], [42, -12, 18], [-4, -5, -13], [5, -21, -10], [6, 19, -3], [-4, -32, 2], [13, -7, 30], [-5, 8, -5], [11, -27, 33], [-14, 30, -3], [-7, -19, -7], [-37, -13, 8], [-20, 24, -24], [8, 9, -31], [8, -5, 31], [4, -13, -5], [8, 12, 15], [-15, 20, -1], [52, 1, 14], [3, 8, 31], [13, -19, 7], [-7, 13, -21], [38, -12, 47], [-17, 6, -38], [19, -4, -24], [-3, -9, 34], [2, 11, 18], [-2, -49, 18], [16, -7, 13], [13, 5, 34], [29, -7, -12], [-17, 23, 5], [24, -44, 44], [7, -21, 11], [2, -6, 23], [-28, 13, 1], [9, -3, 37], [-36, 35, -27], [40, -16, 30], [1, 8, 45], [-12, -13, 9], [1, -20, -2], [-12, 31, 6], [36, -32, 3], [-17, 15, 19], [-4, 2, -22], [-9, 37, -25], [-23, -11, -2], [24, -27, 30], [-25, 23, 9], [-34, 6, -5], [24, -43, 27], [-23, 8, -7], [-20, 39, -38], [5, -33, -11], [-30, 23, -13], [-3, 24, -14], [25, -2, 43], [-42, 7, -33], [-8, 0, 11], [-21, 23, -7], [-18, -18, 17], [-8, -2, -12], [7, -15, -23], [4, -8, -4], [18, -1, 26], [9, 16, 11], [-12, 41, 11], [22, -25, -30], [23, -13, 35], [6, 4, 11], [-30, 43, -34], [10, 4, -23], [6, -27, 27], [-36, 20, -38], [-13, -22, 4], [-19, 27, -42], [6, -9, -16], [-40, -3, -20], [25, -29, 24], [-8, 8, 31], [-18, 2, 19], [-54, 15, 16], [-62, 18, -40], [-30, 40, -29], [-37, 2, -12], [-42, 23, -50], [-46, -10, 10], [40, -26, 31], [-7, -40, 4], [-21, 19, 15], [57, -15, 20], [23, 43, -17], [-14, 41, 4], [-26, -31, 2], [17, -29, 19], [56, -3, 11], [-18, -38, 5], [14, 23, 23], [15, -14, 22], [-71, 9, 9], [-7, 41, -25], [-63, 15, -26], [-25, 2, -37], [-14, -18, -8], [-22, 45, -24], [-51, 19, 9], [10, -25, 21], [41, -1, 5], [-18, 18, -21], [9, -44, 20], [-45, -6, -9], [-14, -28, 16], [-20, 21, 25], [9, 29, -11], [-24, 38, 5], [-39, 23, -47], [6, 52, -9], [10, 15, -6], [69, -14, 30], [14, -22, -39], [-34, 11, -6], [19, -25, 11], [6, 34, -28], [-26, 49, -4], [10, 4, -44], [-47, 24, -57], [1, 2, -3], [-26, 12, -28], [14, 6, 11], [17, 30, -5], [-12, 54, -25], [-16, -3, -48], [1, -23, -13], [-9, -29, -4], [-35, 45, -16], [80, -14, 18], [23, -4, 38], [18, 13, -36], [-15, -37, 11], [0, 23, -40], [13, 36, -15], [8, 29, -36], [27, -11, -12], [-28, 58, -30], [-2, 44, -28], [-1, 15, 5], [10, -53, 32], [38, -27, 25], [24, 10, -18], [59, -31, 56], [34, -29, 20], [-44, 7, -12], [31, 29, -11], [26, -24, 22], [3, -17, 13], [25, -17, 9], [-1, 50, -14], [45, -45, 7], [49, -34, 43], [-14, -3, -9], [31, -12, 47], [-40, 47, -41], [20, -42, -20], [-4, -22, 0], [22, -43, -32], [51, -21, -5], [-11, -27, 34], [-7, 38, 3], [36, -28, 30], [-37, 19, -46], [6, -22, 54], [38, -1, -26], [-28, 71, -40], [-27, -11, 23], [-28, 37, -3], [-8, -8, -9], [8, -28, 17], [-10, 29, -22], [35, -4, 21], [-16, 32, -12], [-42, 17, -63], [7, 20, 41], [45, -31, 38], [26, 12, 19], [5, 29, -14], [5, -39, -5], [-48, 29, -7], [-5, -4, -30], [-62, 9, -45], [-14, 26, -10], [43, -26, -10], [-24, 53, -47], [-26, 47, -26], [19, -9, 35], [15, 19, 19], [34, 22, -3], [46, 1, 38], [-71, 33, -28], [8, -22, -26], [7, -31, -43], [63, -24, 41], [-10, 48, 4], [47, -24, 28], [66, -17, 6], [48, -21, 38], [11, 11, 0], [61, -42, 46], [-6, -21, -35], [3, 20, -20], [-3, 3, -1], [-34, -3, 32], [-3, 64, -27], [-22, 34, -21], [7, 2, -45], [-36, 42, -56], [-34, 30, 32], [52, -23, -16], [10, -70, 11], [-44, -31, 21], [-4, -30, 30], [11, -72, 32]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1682_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1682_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_1682_2_a_n(:prec:=3)
    chi := MakeCharacter_1682_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_1682_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_1682_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1682_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, -1, -2, 1]>,<5,R![-1, 6, -5, 1]>],Snew);
    return Vf;
end function;