
// Make newform 1682.2.a.b in Magma, downloaded from the LMFDB on 31 December 2025.
// 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_b();".
// 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_b();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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 := 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]];
    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], [-2], [0], [-1], [-6], [-4], [-3], [-4], [9], [0], [5], [2], [-6], [-4], [-9], [-12], [6], [2], [8], [3], [11], [11], [0], [3], [17], [-6], [5], [0], [-10], [-9], [8], [6], [9], [14], [-18], [-19], [-4], [14], [0], [6], [0], [-16], [3], [14], [-12], [-7], [8], [23], [0], [20], [27], [-9], [17], [-30], [15], [-12], [-6], [-25], [14], [-9], [14], [30], [-28], [0], [2], [-6], [8], [-1], [-24], [-16], [-9], [15], [-1], [-10], [2], [33], [24], [26], [-3], [-10], [-12], [26], [3], [-19], [-31], [-6], [-30], [-22], [-18], [-1], [18], [-15], [20], [6], [-34], [12], [-24], [21], [-4], [-16], [8], [12], [-12], [-39], [-4], [11], [12], [-18], [0], [23], [8], [20], [-18], [26], [41], [-30], [-16], [-21], [-18], [-24], [-10], [-43], [-42], [6], [50], [-12], [-16], [24], [32], [-4], [-10], [0], [35], [-52], [-3], [50], [36], [26], [18], [18], [38], [42], [-4], [-42], [-34], [-12], [-28], [-3], [14], [45], [-4], [-30], [-34], [-45], [-28], [15], [-16], [45], [23], [42], [36], [6], [-13], [42], [-30], [-15], [56], [32], [-7], [-36], [24], [20], [3], [-1], [-19], [-9], [32], [42], [11], [-46], [-1], [60], [38], [27], [60], [60], [32], [2], [17], [33], [14], [24], [-22], [-24], [18], [6], [35], [38], [15], [-27], [30], [11], [-10], [-25], [-6], [-12], [-13], [12], [-21], [-58], [50], [18], [-1], [24], [51], [2], [-61], [45], [-21], [-6], [50], [-16], [-6], [-52], [-54], [26], [15], [57], [44], [18], [8], [-10], [-52], [39], [2], [9], [17], [6], [18], [21], [-36], [-4], [-13], [-22], [-42], [-45], [32], [-30], [-16], [-15], [2], [66], [45], [62], [42], [36], [-70], [-46], [-42], [74], [-55], [-36], [-58], [56], [-27], [44], [-48], [-33], [20], [24], [-22], [2], [59], [-67], [83], [11], [30], [-58], [47], [48], [69], [-31], [-9], [-10], [-10], [45], [2], [30], [35], [-45], [42], [66], [-54], [48], [-58], [-24], [44], [-30], [48], [44], [77], [-24], [53], [-72], [8], [-7], [-18], [50], [72], [-88], [27], [36], [9], [26], [3], [-1], [-60], [-51], [23], [-69], [80], [5], [-78], [-37], [-39], [-70], [-64], [86], [72], [36], [-28], [-60], [80], [6], [-10], [-24], [32], [39], [59], [-64], [44], [42], [-84], [-25], [54], [-30], [74], [-4], [-45], [-42], [86], [-31], [60], [77], [-4], [3], [81], [60], [81], [9], [38], [51], [57], [-90], [-58], [47], [-72], [-52], [2], [-12], [2], [24], [66], [8], [32], [-36], [-75], [86], [-15], [-55], [-18], [69], [11], [3], [-40], [93], [-31], [8], [-34], [-96], [-49], [-6], [-54], [-64], [57], [-31], [11], [-51], [-52], [60], [-34], [-63], [-52], [21], [90], [-55], [-46], [33], [32], [54], [-85], [24], [6], [-10], [-58], [18], [-9], [-88], [-63], [33], [-48], [44], [21], [6], [-19], [-72], [36], [75], [38], [57]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1682_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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