
// Make newform 18.3.d.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_18_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_18_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_18_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_18_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 := [4, 0, -2, 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, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2, 2];
    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_18_d();"
function MakeCharacter_18_d()
    N := 18;
    order := 6;
    char_gens := [11];
    v := [5];
    // 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_18_d_Hecke();"
function MakeCharacter_18_d_Hecke(Kf)
    N := 18;
    order := 6;
    char_gens := [11];
    char_values := [[0, 0, 1, 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, 1, 0, 0], [1, -2, -2, 1], [-6, 0, 3, 0], [1, 3, -1, -6], [3, -3, 3, 0], [0, 6, -5, 6], [6, 0, -12, -6], [-10, -12, 0, 6], [6, -3, -3, 3], [3, 6, 3, 0], [0, -9, 19, -9], [32, 12, 0, -6], [-42, 18, 21, -18], [-23, -9, 23, 18], [9, 21, 9, 0], [30, 0, -60, -30], [42, -39, -21, 39], [31, -18, -31, 36], [0, -9, -53, -9], [-30, 0, 60, -24], [-52, 36, 0, -18], [7, 15, -7, -30], [-63, -15, -63, 0], [-30, 0, 60, 66], [7, 42, -7, -84], [-3, 108, -3, 0], [0, 27, 37, 27], [-60, 0, 120, -48], [-16, -108, 0, 54], [18, -132, -9, 132], [-34, -36, 0, 18], [138, 81, -69, -81], [111, -30, 111, 0], [0, 39, -11, 39], [6, 78, -3, -78], [175, -45, -175, 90], [0, -36, 109, -36], [-58, 156, 0, -78], [-198, -87, 99, 87], [9, -60, 9, 0], [-96, 0, 192, -84], [140, 84, 0, -42], [-3, 9, -3, 0], [0, -90, -125, -90], [24, 0, -48, 84], [80, 60, 0, -30], [0, -21, 103, -21], [-203, -45, 203, 90], [27, -87, 27, 0], [0, 108, -143, 108], [48, 0, -96, -48], [-18, -51, 9, 51], [-143, -24, 143, 48], [48, 0, -96, 96], [-54, -24, 27, 24], [231, 45, 231, 0], [30, 0, -60, 6], [-34, -252, 0, 126], [49, 0, -49, 0], [-105, 114, -105, 0], [0, -81, 145, -81], [-174, 96, 87, -96], [110, -36, 0, 18], [-186, -63, 93, 63], [397, 12, -397, -24], [-165, -126, -165, 0], [-245, -93, 245, 186], [0, 12, -335, 12], [414, -159, -207, 159], [-107, 84, 107, -168], [-105, -102, -105, 0], [36, 0, -72, -252], [-77, -45, 77, 90], [0, -138, -113, -138], [-328, -132, 0, 66], [-366, 297, 183, -297], [-39, 72, -39, 0], [-124, 108, 0, -54], [390, 54, -195, -54], [0, -24, 385, -24], [-306, -195, 153, 195], [-269, -6, 269, 12], [276, 0, -552, 156], [344, 108, 0, -54], [139, 171, -139, -342], [81, 489, 81, 0], [-54, 0, 108, 450], [-401, -126, 401, 252], [-69, 78, -69, 0], [0, -93, 283, -93], [-60, 0, 120, -516], [279, -123, 279, 0], [-190, 132, 0, -66], [690, 249, -345, -249], [0, 243, -35, 243], [342, 0, -684, -216], [390, -378, -195, 378], [168, 0, -336, -60], [488, -324, 0, 162], [-274, 192, 0, -96], [-41, 207, 41, -414], [-318, 0, 636, 102], [-666, 21, 333, -21], [-3, 180, -3, 0], [0, -33, 7, -33], [-88, 36, 0, -18], [-291, 45, -291, 0], [78, 0, -156, -150], [-966, 33, 483, -33], [751, -18, -751, 36], [0, 183, 745, 183], [56, 468, 0, -234], [-42, 162, 21, -162], [175, -9, -175, 18], [-58, 84, 0, -42], [-159, 24, -159, 0], [0, -81, -593, -81], [36, 0, -72, 252], [-750, -84, 375, 84], [-21, -279, -21, 0], [0, -252, -395, -252], [-137, -174, 137, 348], [33, -288, 33, 0], [180, 0, -360, 576], [-707, 135, 707, -270], [-366, 0, 732, -102], [349, -336, -349, 672], [180, 0, -360, 144], [643, -81, -643, 162], [0, 270, 55, 270], [272, 540, 0, -270], [594, -375, -297, 375], [0, -429, 241, -429], [866, -144, 0, 72], [-738, -222, 369, 222], [0, 204, 157, 204], [234, 0, -468, 90], [0, -189, -359, -189], [126, 858, -63, -858], [6, 0, -12, 174], [74, -540, 0, 270], [87, -774, 87, 0], [0, 15, 1579, 15], [-444, 0, 888, -132], [980, -84, 0, 42], [-27, -771, -27, 0], [-77, 162, 77, -324], [495, -42, 495, 0], [0, 291, -245, 291], [528, 0, -1056, -276], [0, 282, -317, 282], [-510, 0, 1020, -462], [-838, -444, 0, 222], [174, 513, -87, -513], [-335, -345, 335, 690], [99, 777, 99, 0], [38, 1044, 0, -522], [279, 102, 279, 0], [896, -348, 0, 174], [330, 456, -165, -456], [-225, 273, -225, 0], [342, 0, -684, -90], [0, 243, -629, 243], [-90, 0, 180, 576], [-30, -1092, 15, 1092], [15, -567, 15, 0], [650, 180, 0, -90], [-275, -396, 275, 792]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_18_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_18_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_18_3_d_a(:prec:=4)
    chi := MakeCharacter_18_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_18_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_18_3_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_18_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;