
// Make newform 1680.2.bg.o in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1680_bg();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1680_bg_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_1680_2_bg_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_1680_2_bg_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, 0, -1, 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_1680_bg();"
function MakeCharacter_1680_bg()
    N := 1680;
    order := 3;
    char_gens := [1471, 421, 1121, 337, 241];
    v := [3, 3, 3, 3, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1680_bg_Hecke();"
function MakeCharacter_1680_bg_Hecke(Kf)
    N := 1680;
    order := 3;
    char_gens := [1471, 421, 1121, 337, 241];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [-1, 0, 1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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, 0], [0, 0, -1, 0], [-1, 0, 1, 0], [0, -1, 0, -2], [0, 1, -1, 1], [4, -2, 0, 1], [0, 1, -5, 1], [1, 2, -1, -4], [-3, -1, 3, 2], [1, -6, 0, 3], [0, -2, 3, -2], [-2, 3, 2, -6], [1, -2, 0, 1], [2, -6, 0, 3], [2, 0, -2, 0], [0, -6, -2, -6], [0, -3, 5, -3], [-4, 0, 4, 0], [0, -5, -6, -5], [-1, -6, 0, 3], [0, -5, -4, -5], [3, -6, -3, 12], [-3, -14, 0, 7], [-3, -7, 3, 14], [8, 8, 0, -4], [0, -1, 9, -1], [-4, -3, 4, 6], [-3, 3, 3, -6], [0, 0, -11, 0], [2, 8, 0, -4], [-10, 6, 0, -3], [12, 2, -12, -4], [0, -3, 3, -3], [1, -8, 0, 4], [8, -8, -8, 16], [0, -4, -2, -4], [0, -6, 4, -6], [8, 8, -8, -16], [9, 10, 0, -5], [-18, 2, 18, -4], [0, 0, -10, 0], [-7, -20, 0, 10], [-2, -4, 2, 8], [0, -3, 4, -3], [-9, 10, 0, -5], [0, 0, 22, 0], [-14, 8, 0, -4], [10, 12, 0, -6], [0, -5, 7, -5], [3, 0, -3, 0], [0, -10, 0, 20], [-14, 8, 0, -4], [0, -2, -10, -2], [9, 18, 0, -9], [-3, 5, 3, -10], [0, 6, -2, 6], [0, -2, 16, -2], [10, -4, -10, 8], [0, 5, 6, 5], [0, 16, 0, -8], [0, -7, -12, -7], [12, 8, 0, -4], [20, 14, 0, -7], [0, 7, -3, 7], [4, 5, -4, -10], [-1, 17, 1, -34], [15, -4, -15, 8], [-8, 30, 0, -15], [0, 4, -28, 4], [22, 0, 0, 0], [0, 7, -9, 7], [-3, 1, 3, -2], [0, 3, 6, 3], [-4, -13, 4, 26], [11, -20, 0, 10], [-6, -10, 6, 20], [0, 9, -21, 9], [26, 3, -26, -6], [6, 6, -6, -12], [0, 8, -17, 8], [32, 4, 0, -2], [7, -8, 0, 4], [0, 10, 0, 10], [10, 6, 0, -3], [4, -2, -4, 4], [6, 2, -6, -4], [-22, -16, 0, 8], [8, 5, -8, -10], [-9, 30, 0, -15], [-24, -2, 0, 1], [-14, 8, 14, -16], [0, 12, -12, 12], [0, 9, -16, 9], [14, 28, 0, -14], [-17, 2, 17, -4], [-4, -12, 0, 6], [-8, 2, 8, -4], [0, 2, 2, 2], [-26, -1, 26, 2], [15, -12, -15, 24], [2, 28, 0, -14], [0, -12, 6, -12], [0, 0, -18, 0], [-3, -17, 3, 34], [0, -6, 29, -6], [0, -5, -20, -5], [-39, -2, 0, 1], [5, -19, -5, 38], [0, -14, -14, -14], [-7, 8, 0, -4], [-2, 3, 2, -6], [0, 4, 20, 4], [-12, -28, 0, 14], [0, 4, -37, 4], [-28, -24, 0, 12], [0, 3, -9, 3], [18, -26, 0, 13], [0, -19, 5, -19], [29, 9, -29, -18], [4, -8, 0, 4], [0, -2, -7, -2], [-36, 10, 0, -5], [21, 7, -21, -14], [0, -11, -9, -11], [-5, 8, 5, -16], [7, -18, 0, 9], [8, -4, -8, 8], [-24, 2, 24, -4], [22, -10, 0, 5], [10, -5, -10, 10], [0, 12, 7, 12], [-17, 38, 0, -19], [25, -4, -25, 8], [-14, -16, 0, 8], [-45, -1, 45, 2], [35, 20, 0, -10], [0, -13, -21, -13], [0, 2, 10, 2], [-23, 22, 0, -11], [0, -16, 2, -16], [0, 4, 0, -2], [3, 13, -3, -26], [0, 18, 8, 18], [28, -28, 0, 14], [0, -6, 15, -6], [11, -34, 0, 17], [-6, -30, 0, 15], [0, -7, -17, -7], [4, 2, -4, -4], [4, 6, -4, -12], [18, -8, -18, 16], [-4, 16, 0, -8], [-38, -18, 0, 9], [-27, 1, 27, -2], [0, 9, 18, 9], [13, 2, 0, -1], [-17, -8, 17, 16], [23, 17, -23, -34], [30, 2, 0, -1], [0, 11, 11, 11], [3, -5, -3, 10], [2, -44, 0, 22], [-2, 14, 0, -7], [-24, 0, 24, 0], [0, 9, 1, 9], [0, -3, 15, -3], [0, -22, 17, -22], [0, 15, 30, 15], [-1, 44, 0, -22], [-19, 11, 19, -22], [0, -2, -30, -2], [-2, -36, 0, 18], [15, 23, -15, -46], [0, 17, 24, 17], [0, 0, 25, 0], [24, 0, 0, 0], [3, 4, 0, -2], [0, -2, -12, -2], [4, 64, 0, -32], [-7, -12, 7, 24], [2, -5, -2, 10], [-13, 30, 0, -15], [-8, -18, 0, 9], [-10, -6, 10, 12], [0, 4, -18, 4], [0, -2, 22, -2], [0, 2, 0, 2], [0, -3, -2, -3], [-35, -4, 35, 8], [0, -25, 5, -25], [14, 23, -14, -46], [19, -10, 0, 5], [48, 0, -48, 0], [16, 20, -16, -40], [0, -9, -27, -9], [0, 1, -49, 1], [0, 0, 17, 0], [48, 8, -48, -16], [-12, -28, 0, 14], [19, 17, -19, -34], [0, 22, -12, 22], [15, -36, 0, 18], [0, -26, 0, 52], [0, 8, -28, 8], [33, -10, 0, 5], [0, -22, 24, -22], [44, 12, -44, -24], [-46, -4, 46, 8], [30, 4, 0, -2], [0, 18, -16, 18], [-8, -1, 8, 2], [-41, 14, 0, -7], [30, 36, 0, -18], [-4, -12, 4, 24], [0, -7, 9, -7], [7, -4, -7, 8], [0, -16, -8, -16], [0, 32, 0, 32], [10, 2, -10, -4], [30, 16, 0, -8], [35, -14, -35, 28], [19, 52, 0, -26], [-33, 21, 33, -42], [-32, 8, 32, -16], [20, -12, 0, 6], [-4, -72, 0, 36], [-48, -12, 48, 24], [0, -10, -56, -10], [-26, 9, 26, -18], [15, 5, -15, -10], [0, 17, 18, 17], [0, 32, 4, 32], [-5, -72, 0, 36], [0, 27, -11, 27], [-38, 18, 0, -9], [0, -3, -15, -3], [21, 6, -21, -12], [53, 9, -53, -18], [0, 48, 0, -24], [-27, -18, 0, 9], [0, -9, -9, -9], [9, -2, -9, 4], [0, 25, -26, 25], [-6, -10, 6, 20], [-6, 36, 0, -18], [14, -6, -14, 12], [-20, -6, 0, 3], [0, 29, 11, 29], [0, -6, 32, -6], [-56, 16, 0, -8], [-20, 30, 0, -15], [28, 8, -28, -16], [0, -4, 42, -4], [-9, 64, 0, -32], [0, -18, 2, -18], [-3, -7, 3, 14], [0, 6, 35, 6], [0, -13, -22, -13], [-15, -30, 0, 15], [10, 20, -10, -40], [0, -23, -2, -23], [-10, 20, 0, -10], [0, 16, 31, 16], [-42, 6, 0, -3], [0, -2, -24, -2], [61, 8, -61, -16], [47, 34, 0, -17], [58, -8, 0, 4], [16, -4, 0, 2], [0, 15, 7, 15], [-17, 36, 17, -72], [0, 38, -8, 38], [0, -1, -8, -1], [-39, 18, 39, -36], [36, 20, 0, -10], [26, -2, -26, 4], [-51, 9, 51, -18], [0, -4, -27, -4], [-15, -10, 15, 20], [0, -4, 0, 8], [0, 14, 34, 14], [0, 4, 25, 4], [2, -64, 0, 32], [-3, 48, 0, -24], [54, -14, -54, 28], [33, -13, -33, 26], [0, 1, 26, 1], [3, -58, 0, 29], [0, -24, 23, -24], [33, -10, 0, 5], [0, -26, -22, -26], [0, 14, 10, 14], [33, -3, -33, 6], [21, 34, 0, -17], [-28, 22, 0, -11], [0, 40, 6, 40], [59, 4, -59, -8], [-33, -54, 0, 27], [-33, 13, 33, -26], [-26, -18, 0, 9], [6, -12, -6, 24], [16, -6, -16, 12], [0, 6, 16, 6]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1680_bg_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1680_2_bg_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_1680_2_bg_o(:prec:=4)
    chi := MakeCharacter_1680_bg();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1680_2_bg_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_1680_2_bg_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1680_bg();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<11,R![4, -4, 6, 2, 1]>,<13,R![13, -8, 1]>,<17,R![484, 220, 78, 10, 1]>],Snew);
    return Vf;
end function;