
// Make newform 1502.2.a.c in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1502_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_1502_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_1502_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_1502_a();"
function MakeCharacter_1502_a()
    N := 1502;
    order := 1;
    char_gens := [3];
    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_1502_a_Hecke(Kf)
    return MakeCharacter_1502_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, -2], [-2, 1], [-1, -1], [3, -1], [-1, 0], [-5, 5], [-3, 6], [3, -4], [-4, 1], [-2, 7], [-2, -2], [8, -4], [1, -2], [3, 0], [10, -1], [-3, 4], [-9, -3], [-12, 5], [-3, 6], [-4, 6], [7, -2], [-1, -9], [-10, 6], [6, -6], [8, -16], [-16, 2], [-7, 11], [1, -3], [-14, -2], [5, -1], [7, -6], [-6, -9], [-1, -7], [5, 7], [17, -2], [0, -2], [-20, 5], [-18, 1], [11, -1], [-15, 1], [12, 1], [5, 6], [11, -20], [0, 11], [-3, -10], [-4, -12], [-16, 12], [11, -8], [0, 4], [11, -2], [23, -1], [-1, -8], [17, -2], [13, -5], [13, 4], [-13, 18], [-23, 3], [5, -6], [11, -18], [16, -12], [9, 10], [-19, 13], [9, -18], [10, 0], [-8, 17], [9, 2], [4, -13], [-23, 18], [3, 14], [-20, 3], [17, -4], [15, -7], [7, -20], [9, -13], [-27, 9], [5, -8], [-7, -4], [13, 12], [-19, 2], [13, 7], [-20, 3], [14, -24], [-19, -5], [1, -23], [-17, 14], [-34, -5], [17, 9], [-27, 8], [-21, 20], [14, -18], [-22, 2], [14, -16], [27, -22], [-14, 8], [15, -2], [38, -6], [-25, -4], [-35, 4], [-2, 22], [7, 11], [3, 0], [15, -2], [-35, 10], [-14, -2], [14, -11], [-4, 14], [-31, 5], [3, 13], [-13, 3], [-22, -12], [-7, 21], [-10, -4], [18, 7], [-5, 4], [6, 4], [-13, -13], [15, -12], [-1, 4], [13, 0], [1, 13], [-13, -20], [-32, 19], [0, 0], [25, -6], [25, -36], [23, 3], [5, -1], [-25, 14], [-19, -4], [-3, 6], [15, -21], [-1, 0], [23, -24], [-18, -19], [-48, 3], [4, -18], [-3, -13], [-19, -12], [24, 19], [-5, 20], [-5, -8], [11, -4], [4, -18], [20, -28], [-19, 10], [-36, 2], [-44, 19], [31, -11], [-20, 34], [-10, 27], [-5, -16], [-26, -2], [-10, 11], [33, -10], [-25, -13], [-5, 39], [0, 2], [13, 12], [-32, 16], [-4, 29], [3, 12], [22, -16], [-4, 6], [-17, 34], [-21, -1], [11, -36], [-28, 37], [-5, 3], [-32, -6], [24, -28], [-41, -6], [-4, -14], [19, -6], [11, 0], [34, -9], [-50, 4], [15, -12], [6, 32], [9, 32], [18, -20], [-35, 16], [25, -8], [6, 10], [-33, 22], [-26, 6], [-41, -7], [-11, -10], [8, -4], [1, -32], [-28, 46], [30, -7], [-14, 45], [15, -36], [25, -16], [22, -34], [-26, 56], [26, -18], [46, 7], [-32, 52], [29, -36], [-44, -4], [-24, 29], [20, 26], [29, 12], [7, -10], [24, 6], [-18, 40], [-1, -26], [38, -17], [-6, 0], [24, -42], [-51, -2], [6, -19], [15, -53], [3, 6], [3, -20], [-4, 29], [-52, 23], [-16, 21], [57, -20], [-10, 5], [20, 7], [15, 13], [45, -6], [30, 17], [-51, -2], [-8, 16], [-43, 27], [-18, -5], [-50, 3], [-19, 13], [-35, 16], [-18, -21], [5, -1], [5, 39], [57, 7], [26, -45], [13, 10], [-19, 16], [13, 1], [23, -6], [-49, 36], [-26, 15], [5, 15], [29, 6], [7, 19], [-19, -13], [-21, 10], [-1, 13], [49, 14], [-29, -2], [-8, -5], [-45, 33], [-35, 46], [-46, -6], [16, 27], [47, -50], [-41, 20], [-21, 7], [-14, -14], [-22, 49], [12, -21], [4, -34], [-10, 11], [11, 8], [-7, 10], [0, -1], [-39, 54], [6, 33], [9, 10], [3, 33], [-18, 2], [-30, 50], [66, -4], [3, -21], [1, -40], [28, -36], [-13, -10], [-44, 41], [46, -14], [23, -26], [47, 5], [25, -60], [-10, -8], [31, -21], [18, -42], [-37, 5], [42, -30], [31, -6], [36, -61], [12, 9], [-69, -1], [-29, 29], [51, -16], [35, -13], [-48, 18], [5, -2], [0, -7], [9, -10], [14, -10], [-8, 35], [51, -35], [26, -46], [-32, 8], [26, -31], [-2, -16], [-9, -20], [24, 9], [-15, 51], [25, 24], [55, -4], [5, 31], [41, 5], [-19, 10], [-23, -32], [-46, -3], [-44, -12], [-22, -10], [1, 12], [50, -23], [-7, 47], [17, -18], [-24, -9], [-35, -1], [-29, 40], [32, 11], [-33, 24], [72, -1], [-17, 51], [-20, -19], [45, 2], [40, -43], [12, 21], [-7, -18], [-40, -3], [28, -8], [-14, -25], [-14, -17], [8, -21], [-40, 18], [21, -16], [25, 25], [-6, -49], [35, -55], [18, 34], [-1, -7], [15, 46], [10, -27], [8, 29], [6, -5], [44, 6], [-33, 60], [-48, 10], [24, 28], [7, -46], [-15, 2], [55, -47], [-53, -6], [7, 38], [3, -30], [-74, 25], [-17, -39], [11, -18], [35, -55], [51, 14], [-10, -17], [-19, 10], [23, -1], [-36, -2], [35, -42], [37, 8], [41, -69], [40, 0], [32, -54], [36, -42], [-1, 11], [-55, -18], [-17, 4], [-10, 53], [15, 12], [45, -24], [0, 1], [-41, -9], [48, 11], [-29, -4], [-54, 8], [46, 18], [50, -31], [-60, 58], [-44, 68], [12, 33], [44, -55], [22, 34], [-18, 35], [39, 31], [25, -20], [4, -26], [55, 25], [-38, 45], [74, -18], [35, 0], [16, -36], [86, -6], [-11, 18], [45, 18], [-10, 8], [-23, 52], [-60, -1], [-46, 24], [-33, 59], [6, 32], [-39, 0], [0, 36], [-67, -2], [28, -68], [-44, -6], [7, -50], [63, -12], [-75, 30], [17, -28], [-86, 14], [-20, -51], [3, 24], [23, -14], [52, 14], [36, -29], [34, 40], [34, -26], [-43, 54]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1502_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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