
// Make newform 40.2.d.a in Magma, downloaded from the LMFDB on 26 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_40_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_40_d_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_40_2_d_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_40_2_d_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, 0, -1, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [0, 1, -1, 0]];
    Rf_basisdens := [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_40_d();"
function MakeCharacter_40_d()
    N := 40;
    order := 2;
    char_gens := [31, 21, 17];
    v := [2, 1, 2];
    // 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_40_d_Hecke();"
function MakeCharacter_40_d_Hecke(Kf)
    N := 40;
    order := 2;
    char_gens := [31, 21, 17];
    char_values := [[1, 0, 0, 0], [-1, 0, 0, 0], [1, 0, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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, 1, 0], [-1, 1, 1, -1], [0, 0, -1, 0], [-1, 1, -1, 1], [0, 0, -2, 0], [2, -2, 0, 2], [0, 2, -2, 2], [2, -2, 4, 2], [-1, -3, 3, -3], [-4, 4, 0, -4], [-2, -2, 2, -2], [0, 0, -2, 0], [-2, 2, -2, 2], [-1, 1, -7, -1], [5, -1, 1, -1], [-2, 2, 8, -2], [2, -2, -4, 2], [4, -4, -2, 4], [-1, 1, 9, -1], [2, 2, -2, 2], [4, 2, -2, 2], [-8, 4, -4, 4], [1, -1, 3, 1], [2, 4, -4, 4], [-4, -6, 6, -6], [-4, 4, -4, -4], [7, 5, -5, 5], [-1, 1, 1, -1], [-4, 4, 10, -4], [-6, -4, 4, -4], [15, 1, -1, 1], [8, -8, -6, 8], [-2, 4, -4, 4], [-2, 2, -4, -2], [8, -8, 6, 8], [-2, -6, 6, -6], [-4, 4, -10, -4], [3, -3, 5, 3], [-15, 3, -3, 3], [0, 0, 2, 0], [-2, 2, -12, -2], [0, 0, 16, 0], [-2, 10, -10, 10], [4, -2, 2, -2], [2, -2, -16, 2], [12, -8, 8, -8], [-12, 12, 6, -12], [-11, 3, -3, 3], [-11, 11, -9, -11], [0, 0, -4, 0], [12, -10, 10, -10], [-20, 0, 0, 0], [6, 6, -6, 6], [-4, 4, 18, -4], [-2, 0, 0, 0], [-3, -5, 5, -5], [4, -4, -2, 4], [-2, -10, 10, -10], [0, 0, 2, 0], [14, -2, 2, -2], [5, -5, -1, 5], [8, -8, -2, 8], [15, -15, 1, 15], [14, 10, -10, 10], [-18, 8, -8, 8], [2, -2, 12, 2], [0, 0, -14, 0], [-6, -8, 8, -8], [5, -5, 7, 5], [0, 0, -28, 0], [6, 4, -4, 4], [12, 4, -4, 4], [15, -7, 7, -7], [16, -16, 2, 16], [14, -14, -12, 14], [9, 7, -7, 7], [-12, 12, -14, -12], [-14, 14, 8, -14], [6, -8, 8, -8], [6, -10, 10, -10], [-6, 6, 8, -6], [8, -8, 14, 8], [-18, -2, 2, -2], [-16, -2, 2, -2], [20, 12, -12, 12], [-15, 15, -5, -15], [-6, -10, 10, -10], [-6, -12, 12, -12], [4, -4, -4, 4], [-13, 1, -1, 1], [5, -5, -17, 5], [-8, 8, -8, 8], [-9, 9, -9, 9], [4, -4, -10, 4], [-2, 2, 28, -2], [-9, 5, -5, 5], [8, -8, 12, 8], [-30, -8, 8, -8], [-3, 3, -17, -3], [4, -4, -20, 4], [1, -1, 35, 1], [-12, 12, -6, -12], [11, -11, -3, 11], [-10, -2, 2, -2], [-12, 12, -14, -12], [-2, 24, -24, 24], [-3, 3, -9, -3], [-2, 20, -20, 20], [0, -20, 20, -20], [22, -2, 2, -2], [5, 15, -15, 15], [-6, 6, 16, -6], [-24, 2, -2, 2], [-10, 10, -16, -10], [-6, -10, 10, -10], [-10, 6, -6, 6], [-7, 7, 27, -7], [15, 1, -1, 1], [-14, 14, -12, -14], [10, -10, 0, 10], [-8, 8, -22, -8], [16, -2, 2, -2], [6, -6, 28, 6], [15, -15, 9, 15], [0, 0, 18, 0], [4, -4, 26, 4], [12, -12, 8, 12], [12, -8, 8, -8], [-5, -11, 11, -11], [-4, 4, -42, -4], [-10, 10, 12, -10], [-15, 15, -15, 15], [2, 14, -14, 14], [-4, 4, -26, -4], [22, -16, 16, -16], [6, 4, -4, 4], [6, -6, -12, 6], [-9, 9, 1, -9], [-22, 22, 12, -22], [10, 8, -8, 8], [4, -4, -22, 4], [-12, 12, 14, -12], [3, -7, 7, -7], [-7, 7, 11, -7], [-4, 4, 22, -4], [4, 12, -12, 12], [-6, 6, -32, -6], [-6, -8, 8, -8], [-14, 14, 4, -14], [39, 5, -5, 5], [16, -16, 26, 16], [6, 2, -2, 2], [19, -19, 5, 19], [19, -19, 19, -19], [7, -7, 17, 7], [-18, -18, 18, -18], [-32, -4, 4, -4], [-14, -2, 2, -2], [-20, 14, -14, 14], [0, 0, -32, 0], [7, -7, 9, 7], [-38, 12, -12, 12], [25, -5, 5, -5], [-4, 4, 30, -4], [-28, 2, -2, 2], [-47, -1, 1, -1], [38, 2, -2, 2], [18, -18, 20, 18]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_40_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_40_2_d_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_40_2_d_a(:prec:=4)
    chi := MakeCharacter_40_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_40_2_d_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_40_2_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_40_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;