
// Make newform 2151.2.a.c in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2151_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_2151_2_a_c();".
// 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_2151_2_a_c();".
// 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_2151_a();"
function MakeCharacter_2151_a()
    N := 2151;
    order := 1;
    char_gens := [479, 1441];
    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_2151_a_Hecke(Kf)
    return MakeCharacter_2151_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, -1], [0, 0], [1, 0], [0, 0], [-3, 2], [-2, -2], [5, 0], [4, -2], [4, -4], [-5, 4], [-5, 6], [-6, 0], [-2, -6], [-10, 0], [-4, 6], [0, 4], [-2, 2], [1, 8], [-5, 2], [0, -8], [2, 8], [-2, -8], [3, -2], [-8, 8], [-2, 10], [6, -12], [4, -2], [4, -4], [-2, 0], [10, -8], [-15, -2], [14, 4], [-16, 4], [4, -8], [-6, 0], [-10, 10], [11, -20], [-19, 2], [-8, 12], [-4, 4], [8, -18], [2, -2], [14, -2], [-17, -4], [-21, 8], [2, 0], [-19, 2], [18, -18], [22, -6], [16, -22], [-12, 4], [1, 0], [6, -20], [7, 6], [7, -8], [-1, 10], [15, -4], [-1, 6], [0, -2], [-10, 0], [13, -2], [6, -8], [11, 14], [-1, -18], [-18, 2], [-6, -2], [18, -22], [-13, -4], [23, -14], [-17, 24], [-20, -6], [-12, -4], [1, 6], [-22, 16], [-12, 10], [8, -12], [29, 4], [20, 0], [-17, 24], [-3, 0], [1, 18], [26, -8], [3, -10], [-2, 2], [0, -12], [3, -6], [-6, 18], [18, -4], [-16, -4], [-2, 18], [20, 14], [-23, 18], [21, -10], [-2, 0], [12, -8], [11, -2], [-13, 28], [14, -20], [-3, 2], [2, -4], [-16, 26], [4, 2], [0, 24], [-33, 8], [-8, -16], [14, 16], [5, -2], [-24, 10], [-16, 16], [22, -32], [26, 4], [-2, 0], [-26, 6], [-32, 28], [-9, 18], [-21, -16], [27, -10], [21, 10], [-26, 10], [-8, 6], [-21, -4], [-28, 30], [0, -6], [-2, 26], [-23, -2], [0, 0], [28, -14], [-37, -6], [8, 16], [23, -8], [28, -24], [16, 6], [28, -4], [45, 4], [26, -32], [22, 18], [-8, -22], [0, 0], [25, -20], [16, -14], [-6, 0], [8, 0], [-38, 20], [23, -18], [-32, 6], [9, -22], [5, -12], [2, 12], [7, -26], [-10, 38], [19, 4], [4, -36], [17, -34], [-4, 20], [16, -26], [28, 16], [-27, 42], [26, -14], [5, 8], [14, 10], [18, -28], [22, -20], [-40, 8], [23, -46], [-4, 24], [-7, -30], [22, -18], [-36, 16], [12, 18], [8, -6], [8, 16], [-46, -4], [37, -10], [-46, 2], [5, 2], [19, 4], [-8, 24], [26, -12], [-6, -12], [27, -28], [-60, 4], [0, -28], [-36, 20], [-4, 34], [-7, 18], [-10, 28], [-7, 8], [30, -2], [-32, 52], [4, -24], [31, -44], [-36, 16], [-6, 26], [-14, -8], [-14, 42], [0, 26], [-10, 8], [-29, 48], [13, -16], [-6, 16], [-49, -4], [29, 2], [14, -22], [11, -28], [29, -2], [2, -34], [-12, 0], [12, -4], [-30, 24], [-44, -4], [1, -8], [-26, 6], [8, -24], [-30, 22], [39, 2], [-12, 0], [0, -16], [6, 20], [46, 6], [-10, 42], [-15, 36], [23, 30], [-34, 10], [24, 0], [25, -10], [-28, 42], [-32, 4], [-27, -14], [6, 0], [8, -36], [-8, 20], [-13, 18], [34, -62], [20, -40], [4, -16], [10, 36], [-7, -36], [2, -14], [46, 8], [-44, 6], [-16, 40], [6, -10], [-45, 18], [-20, -2], [-36, -8], [-36, 36], [21, 22], [-26, 26], [-4, 8], [36, -26], [23, 4], [-22, 46], [8, -48], [-34, 38], [60, -22], [28, 26], [14, -28], [-5, 6], [-68, 14], [-36, 18], [52, -2], [4, -24], [2, 22], [51, -4], [14, 8], [-20, 40], [-61, 20], [3, 12], [-23, -18], [6, 36], [-49, 20], [-22, -8], [1, -4], [22, -14], [10, 16], [61, 14], [-6, -4], [-49, 16], [-5, 12], [-42, 60], [-47, 30], [-18, 34], [4, -4], [44, -2], [-30, -30], [-3, -6], [15, -16], [15, -28], [-30, -14], [-37, -28], [-4, -42], [20, 16], [17, -44], [8, 2], [-8, 4], [70, -4], [-36, 0], [31, -24], [11, -10], [49, -42], [-2, -20], [14, -20], [36, -40], [28, -4], [-60, 32], [-16, -4], [-36, 70], [-40, 22], [35, 22], [38, 10], [70, -8], [-7, -24], [-9, -20], [-42, 18], [12, -48], [-56, 14], [3, 22], [40, -74], [-30, 24], [-38, 4], [16, 22], [-25, 32], [4, 18], [8, -4], [-26, -20], [-66, 0], [-40, 44], [-10, 14], [-44, -18], [2, 52], [8, -12], [-2, 26], [9, 46], [74, -2], [-4, -44], [-36, 8], [-2, -24], [67, -16], [44, -20], [-47, 46], [21, 32], [6, -12], [-58, 40], [11, -30], [-22, 4], [0, -32], [68, -26], [-40, 36], [9, 18], [-44, 24], [-23, 74], [42, -80], [-14, 18], [55, 14], [60, -14], [62, -24], [48, -16], [6, -32], [-77, -12], [-57, -2], [22, 0], [-23, 48], [-4, 4], [-64, 42], [-2, -44], [-46, 0], [14, 0], [32, -56], [-22, -4], [56, -46], [-28, 8], [16, -12], [22, 2], [-56, -8], [-30, -18], [-6, -46], [-27, 34], [50, -64], [-10, 22], [12, -40], [28, -40], [-51, 4], [-39, 22], [58, -66], [40, 12], [-99, 2], [40, -50], [-13, 32], [32, -72], [53, -54], [23, -56], [13, -48], [42, -66], [40, -2], [54, 12], [-28, -26], [48, 0], [-43, 72], [-49, -2], [-34, 20], [42, -84], [37, -52], [34, 6], [30, 12], [-77, 12], [43, -2], [-10, 58], [-58, 40], [-61, 14], [14, -68], [-78, 8], [-54, 42], [0, 24], [32, -30], [62, -18], [-22, 12], [-9, 18], [-30, 32], [-1, 44], [56, -22], [28, -48], [57, -8], [-10, 54], [-61, 2], [-37, 4], [-20, 44], [-34, -20], [-53, -20], [44, -58], [-34, 70]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2151_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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