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


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