
// Make newform 1502.2.a.d in Magma, downloaded from the LMFDB on 25 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_d();".
// 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_d();".
// 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], [0, -1], [1, 0], [-4, 2], [-3, -2], [-3, 4], [5, -1], [4, -1], [-4, -2], [-7, -2], [1, -6], [-7, 3], [1, 6], [-7, 2], [-3, 3], [-6, 9], [0, -2], [6, -13], [-7, 0], [5, -2], [8, -1], [-8, -1], [1, 4], [9, -8], [11, -6], [-4, -1], [-5, -4], [-13, 6], [5, -6], [-1, 5], [-3, -6], [-1, 5], [11, -12], [6, 0], [8, -9], [-4, -3], [15, -1], [-8, 3], [-5, 12], [1, 9], [14, 4], [-11, 6], [-16, 16], [18, -10], [3, -8], [-6, 18], [0, 10], [-1, 11], [-2, 0], [1, 0], [-13, -8], [-5, 10], [-3, 15], [5, 4], [0, 7], [13, -5], [-15, -7], [4, 1], [4, -21], [-10, 24], [-10, -9], [21, 4], [-16, 7], [16, -11], [-15, 7], [-7, 14], [-19, 7], [2, 11], [-21, 7], [-13, 14], [12, -2], [2, -11], [26, -18], [-6, 8], [-26, 7], [-21, 15], [-18, 6], [-10, 5], [1, -12], [-7, 6], [12, -21], [-14, 8], [-23, 22], [-6, -8], [21, -26], [-4, -12], [11, 16], [10, 7], [-20, -1], [-13, -10], [-12, 18], [10, -12], [-17, 26], [19, -32], [10, -18], [-9, -11], [8, -15], [-5, 28], [-16, -16], [-12, -21], [11, -11], [-1, 6], [29, 1], [-19, 5], [12, -7], [-16, 8], [-38, -5], [31, -19], [30, -19], [-39, 8], [23, -11], [-10, -8], [14, 16], [13, -16], [-6, 29], [-4, -20], [-17, -11], [31, -5], [32, -10], [0, 20], [-9, 11], [-29, 9], [26, -16], [-21, 19], [5, 15], [12, -36], [14, 4], [-28, 22], [31, 4], [-30, 21], [28, 6], [-7, -10], [1, 0], [24, -29], [18, -18], [33, -23], [7, -20], [-42, -5], [17, 21], [-1, -26], [2, 15], [-34, 6], [-38, 3], [-17, 16], [-19, 8], [-31, 5], [20, -33], [-7, -23], [28, -23], [-22, -1], [16, -33], [-2, 1], [-17, 32], [-1, 8], [-7, 0], [-24, 7], [23, -40], [-17, 14], [23, -28], [-18, 18], [-4, 6], [-23, -4], [21, -11], [28, 4], [2, -21], [-27, 4], [-16, 13], [-3, 35], [18, 27], [-39, -5], [-29, 6], [34, -23], [-23, 12], [2, 8], [13, 25], [-4, -12], [-20, -21], [46, -21], [1, -14], [2, -5], [19, -7], [3, -16], [-43, 3], [33, -6], [-9, -13], [35, -45], [42, 12], [-15, 24], [24, 22], [54, -8], [-23, -8], [-30, 17], [0, 16], [7, 13], [8, -30], [0, -23], [-48, -12], [-15, -27], [15, 24], [25, -33], [-29, 2], [-11, 28], [-33, -14], [-32, -20], [47, -14], [-46, 26], [7, 13], [9, -23], [15, -14], [18, 14], [42, -7], [13, 14], [34, 16], [21, 11], [-18, 24], [28, 13], [-29, -3], [30, -5], [22, 1], [-25, 52], [-13, -32], [-3, 42], [-14, 15], [-59, 3], [43, 0], [-62, 4], [40, -7], [57, -14], [14, -50], [-5, -15], [-1, -46], [-11, 22], [-5, -30], [-45, -2], [25, 10], [5, 18], [-25, -8], [-12, -1], [21, -21], [35, 4], [-40, 9], [23, 3], [-22, 1], [-36, 47], [34, 19], [-40, 3], [-11, 7], [52, 1], [-14, -32], [59, -16], [0, 33], [37, -20], [-42, 2], [41, 0], [26, 9], [21, 18], [-51, 43], [35, -36], [39, -18], [-26, -28], [46, 10], [57, 12], [-66, 5], [-37, -18], [63, -2], [-13, -11], [9, -12], [-57, 21], [34, -8], [-66, 18], [-35, 24], [-28, -18], [25, 19], [60, -17], [45, -8], [15, -37], [38, -46], [10, 2], [-26, 36], [-7, 4], [-18, 48], [47, 18], [42, 12], [6, -42], [-51, 30], [28, -53], [30, -24], [3, -28], [-35, 36], [21, 24], [-76, 4], [-37, 18], [33, 11], [-60, 1], [-52, 18], [21, 16], [32, 2], [27, -56], [10, 21], [-48, -14], [-8, 13], [29, -4], [57, -3], [23, 3], [-42, 50], [-2, 12], [-5, 17], [-5, -35], [-46, 62], [29, -28], [-44, 14], [-51, 13], [30, -9], [-38, 21], [30, -48], [-14, -26], [23, -61], [-3, -23], [-65, 12], [60, -33], [-17, 9], [57, 8], [58, -23], [4, 26], [-40, -3], [42, -30], [-5, -22], [-9, 44], [9, -12], [-5, -46], [4, 6], [-35, 26], [3, 34], [58, -9], [34, -4], [-53, 13], [-39, 30], [20, 30], [-61, 31], [27, 12], [-38, 50], [-49, 28], [47, -35], [63, -52], [-1, -38], [-51, -11], [31, -51], [-31, 32], [-30, -1], [-7, 54], [-15, 42], [14, 24], [-47, -14], [81, -15], [-44, 54], [-26, -14], [17, 44], [-6, 36], [-38, 4], [-83, 16], [42, 2], [18, 21], [-43, 58], [-53, -2], [-33, -5], [-27, -10], [-43, 63], [-76, 2], [58, 12], [-42, 15], [-45, 54], [-14, 8], [-9, -52], [19, 8], [14, 10], [37, -17], [-51, 39], [30, -38], [70, -2], [-15, 20], [-40, 30], [39, 10], [72, -33], [-63, -17], [-8, -42], [-13, 31], [7, -6], [-29, -9], [-37, -38], [-15, -30], [35, -24], [4, -45], [-9, -18], [-25, 21], [-41, 61], [-33, -3], [-50, -10], [16, -40], [-11, -12], [68, -7], [56, -5], [10, 14], [52, -23], [-19, 0], [64, 18], [43, -46], [-31, 48], [-39, -17], [23, -10], [-44, 71], [-29, -24], [-22, 3], [-9, -16], [35, 2], [18, 6], [17, 12], [38, -16], [-7, -49], [2, -27], [-8, 26], [9, 0], [-23, -8], [-62, 56], [-16, 19], [4, 13], [2, -5], [65, -32], [39, 16], [-78, -15], [-50, -3]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1502_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1, 1]>],Snew);
    return Vf;
end function;