
// Make newform 30.3.d.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_30_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_30_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_30_3_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_30_3_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 := [9, 0, -4, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, -1, 0, 1], [-2, 0, 1, 0]];
    Rf_basisdens := [1, 1, 3, 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_30_d();"
function MakeCharacter_30_d()
    N := 30;
    order := 2;
    char_gens := [11, 7];
    v := [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_30_d_Hecke();"
function MakeCharacter_30_d_Hecke(Kf)
    N := 30;
    order := 2;
    char_gens := [11, 7];
    char_values := [[-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 := 3;
    raw_aps := [[0, 0, -1, 0], [1, 1, -1, 1], [0, 0, 0, -1], [2, -6, 3, 0], [0, 0, 6, 0], [-10, 0, 0, 0], [0, 0, -12, -6], [8, 12, -6, 0], [0, 0, -3, 6], [0, 0, 0, -12], [8, 0, 0, 0], [-22, 24, -12, 0], [0, 0, 24, 6], [14, -18, 9, 0], [0, 0, -15, 30], [0, 0, -12, -6], [0, 0, -36, 12], [-16, -24, 12, 0], [-82, -18, 9, 0], [0, 0, 30, -12], [50, 24, -12, 0], [-28, -12, 6, 0], [0, 0, 9, 6], [0, 0, 24, 12], [74, -24, 12, 0], [0, 0, -72, 24], [74, 42, -21, 0], [0, 0, -21, -6], [20, 72, -36, 0], [0, 0, 72, -42], [-106, -18, 9, 0], [0, 0, -42, -72], [0, 0, 96, -18], [56, -84, 42, 0], [0, 0, -72, -78], [56, -24, 12, 0], [-10, 72, -36, 0], [74, -54, 27, 0], [0, 0, 93, 54], [0, 0, -96, 66], [0, 0, 84, -84], [38, -144, 72, 0], [0, 0, -18, 60], [-130, -24, 12, 0], [0, 0, 36, 54], [92, -36, 18, 0], [-172, 72, -36, 0], [14, 54, -27, 0], [0, 0, -87, -42], [98, 144, -72, 0], [0, 0, -84, 30], [0, 0, -252, -24], [104, 216, -108, 0], [0, 0, -54, -72], [0, 0, -216, -6], [0, 0, -111, 54], [0, 0, 288, 54], [-400, -48, 24, 0], [302, -72, 36, 0], [0, 0, 144, -102], [170, 18, -9, 0], [0, 0, 240, 78], [74, -54, 27, 0], [0, 0, 234, 84], [-274, 144, -72, 0], [0, 0, -132, 66], [-28, -264, 132, 0], [98, 144, -72, 0], [0, 0, 141, -66], [206, -192, 96, 0], [0, 0, 24, -270], [0, 0, 156, 24], [-22, 306, -153, 0], [-454, -24, 12, 0], [-88, -108, 54, 0], [0, 0, 303, 42], [0, 0, -24, -258], [-214, 0, 0, 0], [0, 0, 96, -264], [-460, -72, 36, 0], [0, 0, -240, 108], [224, 312, -156, 0], [0, 0, -378, 108], [146, 216, -108, 0], [428, -108, 54, 0], [0, 0, 27, 114], [0, 0, 120, 186], [-130, -432, 216, 0], [0, 0, -240, -96], [110, -354, 177, 0], [0, 0, 183, -102], [0, 0, 444, 48], [-178, 54, -27, 0], [0, 0, 66, 360], [80, 36, -18, 0], [0, 0, 171, 90], [0, 0, 96, -156], [0, 0, -192, 120], [350, 390, -195, 0], [-262, 48, -24, 0], [-262, -30, 15, 0], [0, 0, -240, 150], [0, 0, -195, 198], [0, 0, 120, -6], [-220, 48, -24, 0], [-226, 480, -240, 0], [0, 0, 201, -42], [0, 0, 216, 18], [0, 0, -384, -72], [680, -216, 108, 0], [-826, 6, -3, 0], [302, -336, 168, 0], [0, 0, 540, 138], [128, -132, 66, 0], [-304, 504, -252, 0], [0, 0, 96, -90], [-514, 54, -27, 0], [0, 0, 147, 114], [0, 0, 348, -30], [0, 0, -300, 108], [-520, 24, -12, 0], [602, -264, 132, 0], [0, 0, -156, -378], [0, 0, -93, -294], [932, 0, 0, 0], [0, 0, 264, 438], [110, -240, 120, 0], [0, 0, -228, -96], [-634, -306, 153, 0], [-334, 216, -108, 0], [-880, 36, -18, 0], [0, 0, 159, -102], [920, 168, -84, 0], [-370, -312, 156, 0], [0, 0, 24, 240], [-514, -144, 72, 0], [0, 0, -564, 78], [914, -366, 183, 0], [0, 0, -228, -102], [0, 0, -192, 372], [-124, -72, 36, 0], [0, 0, 96, -90], [-190, 450, -225, 0], [0, 0, -537, -102], [68, 168, -84, 0], [0, 0, -288, 240], [-58, 720, -360, 0], [0, 0, -696, 126], [-376, 60, -30, 0], [0, 0, -249, -30], [662, 264, -132, 0], [0, 0, 216, 198], [386, -798, 399, 0], [0, 0, -177, -342], [926, 366, -183, 0], [0, 0, 462, 156], [-268, -324, 162, 0], [0, 0, -24, -474], [-370, -24, 12, 0], [0, 0, -816, 216], [0, 0, 141, 630], [0, 0, 672, -114], [-1438, 258, -129, 0], [0, 0, -786, -24], [0, 0, 84, -378], [0, 0, -525, -126], [-280, -960, 480, 0], [446, 288, -144, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_30_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_30_3_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_30_3_d_a(:prec:=4)
    chi := MakeCharacter_30_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_30_3_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_30_3_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_30_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;