
// Make newform 2678.2.a.o in Magma, downloaded from the LMFDB on 05 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2678_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_2678_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_2678_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, -1, 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_2678_a();"
function MakeCharacter_2678_a()
    N := 2678;
    order := 1;
    char_gens := [1237, 417];
    v := [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_2678_a_Hecke(Kf)
    return MakeCharacter_2678_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], [-1, -1], [0, -2], [0, 0], [-2, 2], [1, 0], [-3, -1], [-3, 3], [0, 3], [4, 2], [-6, 1], [0, -2], [-4, 8], [5, -3], [0, -4], [-6, 0], [-6, 1], [0, 0], [0, 6], [10, -5], [5, -11], [-13, 3], [-9, 1], [10, -5], [10, -4], [0, 3], [-1, 0], [-8, 2], [-6, -2], [6, -6], [2, -8], [-14, 4], [0, -4], [-6, -6], [10, 7], [-18, -1], [0, -9], [6, -3], [0, 8], [11, -19], [-2, 4], [-12, 19], [4, -10], [-14, 9], [-8, 2], [2, 4], [0, -1], [4, -18], [-6, -12], [-6, 15], [26, -4], [-4, -4], [-6, -4], [23, -1], [0, -18], [-12, 10], [-6, 14], [-20, 0], [2, 16], [5, 17], [-12, 0], [-12, 6], [18, 2], [4, 7], [15, -3], [5, 1], [-4, -6], [6, -8], [6, 0], [14, -12], [-6, 19], [-26, 8], [-12, -9], [12, -16], [18, -12], [3, -15], [12, -9], [-4, 16], [20, 2], [24, 0], [-34, 8], [18, -13], [8, -16], [0, -24], [-6, -12], [-4, -7], [-9, 3], [-9, -5], [25, -7], [-11, -1], [8, -8], [9, -9], [-27, 15], [-30, -6], [6, 8], [0, 15], [-26, 13], [-8, -8], [18, 2], [30, -23], [6, -12], [30, -18], [20, -13], [4, 8], [-16, -6], [-3, -15], [21, -21], [-9, 19], [6, 10], [-2, 14], [-16, -3], [6, -15], [-33, 15], [6, 7], [-32, 18], [29, -25], [6, 3], [15, -9], [32, -11], [-24, -10], [-22, 18], [23, -3], [10, -2], [6, -6], [-30, 20], [-14, 28], [15, 3], [2, 8], [-24, 32], [-2, 0], [17, -5], [18, -29], [-20, 21], [-28, 0], [30, -25], [25, -31], [6, 5], [-22, 21], [-10, 4], [-6, 26], [2, 10], [-18, 1], [18, 0], [-18, -12], [-18, 0], [2, 20], [-21, 15], [21, -45], [-4, -24], [-35, -1], [-24, -10], [-42, 2], [-10, 30], [-35, 17], [-26, 24], [-18, -14], [-20, 20], [18, 6], [-54, 2], [2, -16], [-8, 4], [22, -20], [-40, -8], [-44, 1], [-12, 4], [-28, 20], [-36, 27], [1, 11], [42, -18], [21, -9], [-29, 1], [-28, 0], [20, -4], [-46, 9], [36, -12], [24, -3], [28, -6], [6, 22], [-16, 9], [3, 33], [-17, 21], [-28, 20], [18, -18], [-10, -7], [33, 3], [62, -2], [-16, 27], [26, -5], [-5, 3], [10, -34], [-28, 40], [9, -21], [-24, 28], [4, -2], [8, -16], [-16, 29], [8, 24], [-3, 11], [-19, 5], [12, 26], [22, 26], [-1, -29], [5, -15], [-24, -3], [20, 8], [-40, -14], [14, -21], [-1, 23], [18, 0], [-50, -4], [12, 21], [15, -15], [64, -6], [-30, 6], [-20, 52], [-5, 11], [-15, -27], [-58, 20], [-22, -22], [57, 9], [-8, -33], [-6, 42], [61, 5], [44, -24], [0, 40], [-14, 48], [-30, 0], [-47, 29], [-10, 42], [17, -37], [-15, -33], [-36, -12], [12, -12], [34, -38], [-18, 54], [2, -25], [-14, 33], [12, -18], [29, -35], [34, -14], [39, -27], [-8, 35], [0, -32], [-3, 25], [-34, 52], [-25, 11], [15, 33], [6, 39], [-9, -27], [1, -11], [-5, -9], [-26, 10], [42, 11], [-11, 27], [-30, 62], [-12, 18], [20, -14], [-52, 0], [-2, -16], [-18, -20], [-11, -39], [-34, -28], [16, -18], [-31, 15], [-18, 36], [-32, 21], [-30, -13], [45, -11], [52, -12], [49, -53], [-64, 22], [-40, 34], [-66, 24], [23, -21], [-39, 63], [-28, 42], [54, -37], [-10, 52], [50, -18], [52, 4], [43, 7], [-33, 45], [-24, 42], [42, 3], [-20, 36], [-36, 15], [-36, -13], [6, 24], [8, -24], [-2, -38], [29, -13], [45, -7], [-34, 4], [34, 22], [-4, -50], [-18, -36], [-16, 3], [68, -16], [-32, 7], [-12, -36], [0, -43], [-26, 46], [18, 6], [18, -24], [12, -17], [54, -30], [-64, 1], [24, -12], [-50, 28], [-29, 5], [6, -6], [10, 25], [50, -14], [60, -12], [0, 44], [-12, 51], [30, -66], [-24, 28], [-33, 43], [-38, -14], [52, -25], [-82, 4], [-42, 66], [10, -40], [40, 8], [40, -12], [80, -8], [44, -20], [-36, 30], [-42, 12], [18, -12], [57, -41], [-60, 13], [56, -26], [-34, 18], [23, 1], [24, 12], [-20, 14], [12, 0], [20, 0], [-10, 48], [6, 14], [24, -8], [66, -12], [-10, -4], [16, 43], [34, -28], [13, -9], [57, -9], [28, -32], [6, -19], [40, -70], [4, -26], [40, 3], [-17, 57], [-56, 10], [54, -48], [-12, -12], [-84, 12], [34, -35], [21, 9], [-8, 7], [6, 30], [40, 4], [-66, 6], [-27, -21], [-24, 36], [32, -36], [-1, -11], [62, -8], [-51, -15], [78, -27], [33, 3], [-72, 31], [-13, 9], [-35, -5], [48, 16], [54, 4], [24, -39], [50, 18], [9, -15], [42, 6], [-12, 8], [18, 14], [20, -20], [-3, -57], [38, 27], [20, 27], [6, -40], [-60, 24], [-39, 41], [34, -23], [-38, 40], [-76, 42], [12, 24], [20, 6], [-10, 58], [8, 20], [3, -29], [51, -57], [-58, -6], [-6, 40], [61, -69], [29, -47], [-38, 43], [22, -78], [66, -28], [-10, 25], [0, 52], [69, -33], [-13, 57], [16, 4], [60, -7], [64, 0], [9, -9], [18, -8], [-46, 94], [-30, 22], [10, -64], [-20, -32], [2, 10], [41, -27], [5, 11], [-24, -34], [-34, 24], [4, 10], [36, -18], [-41, 43], [4, 18], [12, -4]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2678_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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