
// Make newform 1045.2.a.d in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1045_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_1045_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_1045_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, 3, 3, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [-2, 0, 1, 0, 0], [0, -3, 0, 1, 0], [2, 0, -4, 0, 1]];
    Rf_basisdens := [1, 1, 1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_1045_a();"
function MakeCharacter_1045_a()
    N := 1045;
    order := 1;
    char_gens := [837, 761, 496];
    v := [1, 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_1045_a_Hecke(Kf)
    return MakeCharacter_1045_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, 1, 0, 0, -1], [-2, 0, -1, 1, -1], [1, 0, 0, 0, 0], [-1, -2, 2, -1, 1], [-1, 0, 0, 0, 0], [0, 1, -1, 1, 2], [-2, 1, -1, 3, -2], [1, 0, 0, 0, 0], [-1, 0, 2, -1, 0], [3, -3, -2, -2, 1], [1, -1, 6, -1, 2], [1, -4, 2, -4, 4], [4, 1, 2, -2, 2], [-1, 0, -1, -6, 3], [-6, 1, -5, 3, -1], [-4, 7, -3, 1, -4], [-2, -1, 3, -3, 0], [-2, 3, -4, 3, -3], [-2, -5, 2, -5, 6], [-1, 0, 0, -2, -1], [-3, 1, 0, -1, 1], [-1, 2, -4, 4, 2], [-10, 5, -3, 5, -4], [-5, 1, 1, 7, -2], [0, -5, 2, -2, 5], [0, 0, 3, -5, 2], [-6, -4, -3, 5, 0], [-2, 4, 4, 1, 3], [7, 3, 3, 4, -7], [-8, 3, -1, 0, -7], [1, -5, 4, -2, -2], [-10, 1, -2, 1, -4], [-6, 5, 3, 1, -3], [6, -11, -1, -5, 6], [-11, 8, -11, 5, -14], [-1, 5, 0, 0, -1], [-1, -2, 2, -7, 7], [-5, 7, -6, 11, 4], [-6, 1, -4, -5, -3], [-7, 5, -11, 4, -15], [1, -1, 1, -4, 11], [-5, 8, -2, 16, -5], [-1, 9, -11, 3, -7], [11, 6, -2, 0, -3], [7, -2, 5, -8, 14], [-11, 3, -10, 7, -5], [-5, -2, 0, 1, -9], [-15, 4, -1, 12, -3], [0, 5, 0, 3, 6], [6, 0, 2, 5, -7], [4, -9, 9, -8, 4], [0, -7, 10, 2, 0], [7, -8, 1, -14, 1], [-15, 1, -7, 7, -7], [3, -7, 1, -2, 4], [-15, 9, -6, -2, -11], [-6, -3, -14, 3, 5], [6, -1, 1, 12, -6], [-1, -11, 5, 1, -2], [-7, -7, 6, 6, -2], [-9, 8, -4, 6, -4], [2, 0, -6, 4, -3], [9, -15, -3, -5, 8], [0, 2, -1, 14, -3], [0, -13, 6, -13, -3], [-6, -2, 2, 0, -8], [1, 4, -6, 8, -12], [15, -15, 9, 0, 14], [1, -4, 3, 3, -13], [2, -15, 4, -6, 6], [-8, -15, 0, 2, 2], [5, -11, 2, -2, 10], [9, 1, 8, -13, 9], [7, 1, 11, -11, -4], [-2, -6, 11, -10, 11], [4, -7, 13, -11, 4], [-11, 7, -11, -12, -3], [-21, 12, -20, 11, -22], [4, 3, 5, -1, 14], [4, -1, 6, 17, -4], [-2, -4, 5, -16, -2], [3, -14, 17, -14, 15], [14, -5, -7, 2, 5], [-3, 6, 9, 6, 1], [2, 7, -6, -2, 8], [-6, 14, -13, 12, -5], [7, -2, -4, -5, -4], [7, -14, 14, -10, 16], [-10, 2, 9, -5, 16], [16, -5, 9, 10, 1], [-9, 0, 3, 14, -1], [-1, 18, -1, 8, -3], [7, 11, 8, -3, -2], [1, -12, -4, -6, -1], [5, 10, 0, 5, -3], [-6, 0, -16, 13, 0], [5, 5, -2, -2, -9], [13, -10, -6, -6, -14], [7, 5, -13, 2, -11], [5, -10, 11, -23, -4], [-10, 12, 6, 3, -2], [-5, -10, 14, -8, 10], [10, 11, -7, -8, 7], [-3, 1, -17, -6, -1], [-19, 16, -3, 21, -11], [-33, 10, -12, 14, -16], [-12, -9, 5, -16, -4], [11, -15, 15, -15, 25], [-3, 11, 5, -15, -1], [16, -5, 13, -5, 1], [-8, 31, -17, 23, -17], [-2, -4, -13, 8, -6], [-8, -1, 3, -6, 17], [16, -10, 11, -8, 6], [7, -11, 5, -21, -7], [16, -11, 24, -17, 14], [0, 9, 9, -3, 4], [-1, -3, 4, 18, -6], [3, 0, -3, 7, 5], [-3, 0, -24, 7, -6], [-10, -22, 9, -14, 16], [6, 0, 4, 18, -13], [4, 20, -9, 15, 2], [5, 16, -21, 14, 4], [-1, 1, -16, 3, -2], [-11, 3, -14, 24, 1], [-5, 17, -15, 12, -8], [-6, 5, 11, -2, -8], [-10, -3, -2, -12, 15], [9, -16, 28, -3, 17], [14, 3, 10, -10, 1], [11, -8, 26, -2, -3], [5, 8, 14, -7, -3], [-6, 25, -25, 15, -20], [-14, 30, -12, 19, -26], [-3, -7, 3, 0, 5], [19, -12, 1, -1, -6], [-2, 1, -4, 19, -2], [-36, 10, -27, 21, -24], [15, -5, 1, -18, -5], [-16, 12, -3, 13, -22], [-3, 11, -6, -2, 3], [-3, 23, -8, -1, -6], [18, -26, 18, -24, 18], [-13, 10, 17, 3, 0], [18, -20, 8, 3, 17], [-11, -14, -6, 8, 0], [0, 4, 12, -5, 21], [38, -14, 2, -6, 10], [-15, -5, -1, -3, -18], [-5, 25, -14, 6, -17], [19, -23, 7, -17, 19], [8, -18, 33, -12, 6], [20, -6, -1, -24, 6], [1, 9, -26, -8, -1], [-10, -3, -1, -2, -19], [-6, 7, -6, -3, 5], [-19, 26, 5, 10, -2], [14, -14, -8, 1, -12], [-11, -12, 15, -14, -14], [-3, -4, 15, -3, 28], [28, 4, 7, -5, 19], [19, 1, 28, -15, -4], [17, -33, 16, -22, 29], [6, 5, -1, -18, -3], [-5, 1, -12, 7, -15], [-11, -2, 10, -14, 28], [-15, 23, 13, -1, -9], [-12, 25, -25, 15, -8], [-10, 2, -8, 6, -9], [-12, -16, 30, -4, 15], [2, 2, -15, -6, -16], [1, -12, 5, -29, 21], [23, 1, 21, -4, -18], [-2, 22, -33, 12, -12], [1, -7, 9, -1, -19], [-12, -4, -16, -2, -6], [22, -17, 13, 7, 16], [-9, 31, -10, 0, -15], [22, -15, -11, 8, 5], [10, -8, 20, -17, 8], [-5, 1, -17, -1, 21], [8, 6, -13, -6, 1], [13, -5, 34, -2, 9], [9, -24, 25, -21, 19], [-16, 0, -22, 5, 2], [-23, -2, -6, 4, 15], [-12, 6, -1, 0, -16], [1, 11, 11, 6, 10], [-27, 11, 5, 5, -14], [-17, 20, -6, -6, -9], [17, -15, 12, -4, 22], [27, -17, 7, -17, 9], [-10, -32, 21, -18, 17], [-31, 21, -11, -3, -23], [4, 16, -6, 1, -21], [10, 14, 12, -13, 9], [-30, 2, 2, 6, 5], [1, -1, 6, 6, -14], [-25, 25, -4, 29, -15], [-1, -10, 4, -4, 17], [-17, -5, -19, 21, -24], [-14, 16, -30, 20, 1], [1, 20, -11, 13, -9], [1, -8, 22, -9, 18], [-11, -3, -13, 22, 9], [33, -15, 2, -4, 19], [4, -31, 24, -25, 4], [3, -17, 8, -14, 8], [-28, 25, -4, 16, -5], [27, -21, 13, -32, 26], [5, 1, -4, 16, 18], [-1, 15, -19, 8, -19], [-15, 22, 4, 2, 8], [-24, -4, 1, 2, -20], [-10, 3, -11, 11, -31], [-14, 6, 18, 18, 3], [-20, 16, -19, -6, -26], [13, -18, 2, -8, -6], [-40, 8, -4, 13, -15], [-6, -12, 3, 12, -27], [17, -12, 2, -10, -1], [-40, 19, -20, 27, -17], [-28, -17, 11, -7, 5], [-32, -7, -7, 27, -13], [34, -25, 20, -10, 38], [19, -25, -2, -7, 0], [7, -22, 12, -5, 14], [22, -5, 1, -2, 22], [-6, -15, -11, 13, 11], [7, -11, 10, -26, 5], [18, -18, 12, 12, 15], [-25, 20, -10, 35, -13], [11, 7, 5, 5, 3], [16, -11, 37, -20, 40], [-8, -5, 12, -10, 13], [-16, 10, 3, 3, -8], [42, -4, 19, 2, 0], [22, -6, 26, -17, 22], [-8, 8, -20, -1, 1], [1, 17, -11, -19, 18], [-41, 13, -23, 14, -23], [11, 1, -28, 5, -20], [6, 0, 24, -10, 2], [-33, 12, -22, -3, -33], [-2, 10, -14, 20, -3], [-3, 10, 12, -23, -9], [10, -8, -25, -1, -16], [-9, 13, 9, -8, 13], [-3, -4, 29, -9, -8], [-25, 6, -7, 1, -14], [14, -23, -5, 9, 20], [0, 5, 2, -13, -14], [-18, 2, -16, 30, -22], [9, 15, -6, -19, -1], [7, -1, 16, -23, 1], [23, -30, 9, 1, 12], [44, -15, 2, 11, 3], [6, -13, -1, 8, -9], [-18, -11, 6, -14, -23], [7, 15, -39, 10, -26], [28, -28, 40, -31, 30], [12, -8, 4, -4, 17], [27, -7, 17, -21, 21], [2, -17, -16, 11, -23], [-19, 12, -24, 28, -34], [4, 6, -19, -13, 5], [-37, 22, -3, 8, -17], [-3, 14, -25, 6, -2], [35, -12, -6, -10, -16], [-29, 0, 5, 27, -12], [-7, 23, -28, -16, 3], [29, 2, -10, 6, -10], [3, -10, -1, -4, -15], [-36, 34, -14, 16, -35], [14, -23, 4, -5, -2], [38, -28, 46, -29, 42], [-25, -4, -17, 5, 17], [10, 17, -21, 19, 6], [-33, 39, -13, 47, -13], [-26, -23, 5, 1, 0], [43, 9, -1, -2, 20], [23, 10, 6, 1, 22], [20, -32, 8, -28, 12], [22, -4, 27, -6, 17], [14, -10, -18, 2, 13], [-23, -3, -3, 21, 0], [7, 23, -20, 25, -14], [1, 21, -13, 19, -2], [28, -4, 15, -21, -7], [19, -30, 8, -36, 0], [-56, 38, -32, 14, -49], [38, -6, 29, -27, 29], [-41, 5, -8, -18, -20], [57, 2, 21, -30, 17], [5, 36, -13, 10, 1], [22, 10, -5, 25, -24], [-44, 0, -8, -4, 11], [-41, 27, 7, 13, -9], [-20, 34, -24, 48, -2], [-39, 8, -7, -8, -27], [29, -16, -7, 10, -10], [16, -35, -9, 6, 8], [-19, 0, -19, 28, 9], [40, -27, 13, 3, 13], [-17, -19, 28, 5, 8], [-11, 47, -4, 20, -19], [39, -23, 35, -31, 37], [11, -12, 1, -6, 33], [-7, -24, 11, 8, 6], [-3, -17, 0, -6, 19], [-1, 34, 2, -21, 0], [-6, 6, -31, -3, -2], [-6, -5, -1, 17, -24], [-14, -6, -28, 30, 5], [-4, 22, 2, 33, 7], [-46, 27, -34, 6, -23], [-6, -43, 25, -15, 0], [3, -29, 25, -11, 4], [-15, 12, -9, -6, 32], [1, -1, 30, 11, 10], [7, -21, -4, 18, 6], [20, -9, 25, 4, 19], [-30, -23, -18, 31, -15], [27, -6, 8, 1, -4], [6, -36, -9, 6, 10], [-16, -10, 15, -18, 12], [-61, 13, -40, 52, -20], [-7, 1, 0, 23, -33], [8, -3, -17, 19, 9], [-7, 20, 29, -1, -8], [23, 29, -7, 8, -11], [7, 11, -22, 4, -16], [-16, 4, 3, -19, 12], [16, -26, 48, -13, 7], [-6, 43, -22, 24, -21], [-28, -11, -2, -12, -31], [-24, 7, 16, 14, -8], [35, -7, 37, -13, 3], [-2, 13, -2, 10, 30], [-34, 33, 10, 30, 0], [10, 5, 8, 34, -19], [3, 2, 21, 10, -2], [4, 0, 2, 22, -3], [53, -24, 42, -24, 47], [-21, -8, -26, 10, -21], [-14, 11, 31, -7, 38], [26, -21, -8, 7, 15], [5, -1, -22, -17, -8], [14, 7, 4, -20, 25], [-8, -19, -3, -10, 11], [19, -11, 17, -5, 3], [-17, 8, 28, 11, -10], [2, -23, 20, -32, 37], [31, -16, 32, -41, 28], [-15, 21, -41, -26, -8], [-9, 34, 1, -23, -4], [11, -1, -35, 15, 2], [21, -44, 13, -30, 37], [-2, -28, -16, -12, 9], [-19, -21, 0, -11, 9], [-17, 32, -25, 37, -33], [31, 12, 6, 18, -28], [-7, 25, -8, -11, 15], [22, -23, 18, -47, 28], [-15, 13, 1, -29, 4], [-32, 9, 12, 10, -12], [-13, -2, 16, 5, -10], [3, -33, 17, 23, -8], [17, -29, 2, -37, 18], [-2, -1, 23, -3, 3], [33, 27, -12, 10, -11], [11, -2, 8, 41, -8], [-10, 16, 32, 1, -10], [-19, -29, 13, -5, 19], [-59, 22, -39, 48, -15], [20, 14, 7, 7, 15], [27, -37, 1, -8, 11], [29, -27, 39, -15, 4], [42, -10, -17, -7, -11], [-14, -7, -2, 12, -27], [3, -12, 27, -11, 30], [56, -12, -7, -12, -15], [-7, 10, -7, -3, -11], [-23, 25, -29, 48, 14], [-38, 16, 3, 19, 24], [-27, -13, -7, 14, -3], [13, -18, -7, -29, 20], [-29, 8, -26, 9, 13], [3, -42, 26, -2, 3], [-38, -20, -3, 2, 8], [-2, 11, 16, 34, -11], [13, -26, 35, -23, 45], [-8, -20, 25, -21, 21], [65, -20, 23, -26, 22], [53, -13, -6, -4, 5], [-31, 56, -21, 20, -30], [-23, 17, 4, 4, -22], [28, 5, 28, -38, -6], [-5, -29, 33, 0, 19], [5, 21, 1, -40, 19], [-25, 9, 2, 26, 18], [7, 22, -12, -22, -2], [21, 17, 5, -23, 0], [5, -27, 11, -18, 12], [-1, 18, 18, -32, 4], [-17, 43, 4, 10, 2], [-33, 9, 3, -10, 23], [3, 19, -49, 1, -13], [-2, 26, -21, 9, 3], [8, 36, -15, -18, 18], [-32, 31, -5, 3, -6], [-30, 31, -62, 20, -34], [8, -17, 31, -8, 27], [-1, -12, 18, -14, -13], [7, 13, 35, -2, -9], [13, 1, -7, 7, 8], [10, 22, -10, -8, 17], [44, -32, 14, -15, 14], [-40, 4, -29, 23, 8], [51, -13, 32, -27, 43], [0, 22, 1, 6, 26], [21, 5, -22, 9, -29], [33, 1, 25, 5, 6], [-10, 6, -27, 26, -30], [-28, 21, -20, -10, -13], [-48, 20, 3, 18, 2], [-50, -10, -22, 15, -4], [-61, -11, -4, 11, -16], [4, -17, -6, -15, 26]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1045_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1045_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_1045_2_a_d(:prec:=5)
    chi := MakeCharacter_1045_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_1045_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_1045_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1045_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, -10, -15, -3, 3, 1]>],Snew);
    return Vf;
end function;