
// Make newform 14.5.d.a in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_14_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_14_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_14_5_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_14_5_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, 2, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2, 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_14_d();"
function MakeCharacter_14_d()
    N := 14;
    order := 6;
    char_gens := [3];
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_14_d_Hecke();"
function MakeCharacter_14_d_Hecke(Kf)
    N := 14;
    order := 6;
    char_gens := [3];
    char_values := [[1, 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 := 5;
    raw_aps := [[0, 1, 0, 0], [-6, 2, -3, -2], [9, 2, -9, 4], [-35, 0, -56, 0], [0, -6, 27, -6], [8, 72, 16, 36], [306, 32, 153, -32], [5, 90, -5, 180], [-243, -240, -243, 0], [810, 0, 0, -24], [-182, 180, -91, -180], [-223, 270, -223, 0], [72, -416, 144, -208], [586, 0, 0, 648], [117, -316, -117, -632], [0, 774, -1377, 774], [4122, -146, 2061, 146], [1281, -738, -1281, -1476], [0, -1674, 2531, -1674], [4698, 0, 0, 456], [-5758, -900, -2879, 900], [397, -972, 397, 0], [2448, -200, 4896, -100], [-2079, 564, 2079, 1128], [216, 2448, 432, 1224], [1458, 1602, 729, -1602], [-4211, 1800, 4211, 3600], [-15579, -1758, -15579, 0], [0, -54, -14105, -54], [-9558, 0, 0, 264], [938, 0, 0, 1080], [-7587, -1446, 7587, -2892], [0, -3924, 13959, -3924], [-896, 7560, -1792, 3780], [28809, -4914, 28809, 0], [0, -5616, 13723, -5616], [-16926, -5490, -8463, 5490], [-4883, 5886, -4883, 0], [-8712, 608, -17424, 304], [1377, -8730, -1377, -17460], [0, 12966, -16821, 12966], [-21824, 6696, -43648, 3348], [-5211, -2268, -5211, 0], [0, -18360, -617, -18360], [-28134, 0, 0, 9432], [43146, -6408, 21573, 6408], [1210, 0, 0, -648], [8832, 10944, 17664, 5472], [20682, 15162, 10341, -15162], [-19079, -1854, 19079, -3708], [45225, -3492, 45225, 0], [18738, 0, 0, 1536], [51426, -7560, 25713, 7560], [-5256, -14824, -10512, -7412], [46089, 5736, -46089, 11472], [0, -8784, -64989, -8784], [47106, -13178, 23553, 13178], [-36387, 9756, 36387, 19512], [0, 486, -8497, 486], [-32454, 0, 0, -43368], [-105142, 3798, -52571, -3798], [51696, -152, 103392, -76], [73552, -5832, 147104, -2916], [-87174, 2072, -43587, -2072], [5713, 1620, -5713, 3240], [2673, 29514, 2673, 0], [8909, 28890, 8909, 0], [50170, 0, 0, 27000], [0, -16566, -113805, -16566], [-12600, 1512, -25200, 756], [-40302, 4424, -20151, -4424], [34965, 25728, 34965, 0], [46906, 6084, 23453, -6084], [-80015, -39258, -80015, 0], [-11774, 0, 0, -30672], [-38619, 12604, 38619, 25208], [0, -10602, 56295, -10602], [-70423, -6930, 70423, -13860], [-149607, -40656, -149607, 0], [-100238, 46332, -50119, -46332], [94320, 63704, 188640, 31852], [145274, 0, 0, -60264], [0, 27348, 91179, 27348], [18632, -6192, 37264, -3096], [95781, -11448, -95781, -22896], [24165, -20646, 24165, 0], [-258822, 0, 0, -25320], [112721, -92124, 112721, 0], [-15480, -19256, -30960, -9628], [-49270, 0, 0, 6264], [-2835, -48366, 2835, -96732], [30042, -50228, 15021, 50228], [0, 96768, -134045, 96768], [-285822, 0, 0, -336], [-18931, 117558, -18931, 0], [-22176, -63056, -44352, -31528], [74025, -9194, -74025, -18388], [19458, -37796, 9729, 37796], [122645, -49050, -122645, -98100], [26233, -4158, 26233, 0], [-5182, 0, 0, 129168], [0, -132198, -63369, -132198], [7290, -2118, 3645, 2118], [-151551, -99636, -151551, 0], [0, 96498, -225301, 96498], [-5534, 5904, -2767, -5904], [299376, 66024, 598752, 33012], [92169, -39392, -92169, -78784], [0, -124968, -29133, -124968], [21592, -11808, 43184, -5904], [-204875, -27108, 204875, -54216], [0, 62694, -164185, 62694], [2538, 0, 0, 84264], [267674, 78822, 133837, -78822], [336938, 0, 0, -41256], [0, 94800, -413073, 94800], [234080, -68040, 468160, -34020], [-331398, 65376, -165699, -65376], [203553, -99390, 203553, 0], [-216486, 0, 0, 193080], [-410078, -29502, -205039, 29502], [348250, 0, 0, -89640], [108729, 71490, -108729, 142980], [0, 20442, -71253, 20442], [-33331, 38466, 33331, 76932], [283986, 0, 0, -164928], [-109231, 249102, -109231, 0], [-129123, -40196, 129123, -80392], [-98704, -140400, -197408, -70200], [107777, -11250, -107777, -22500], [0, -32130, -580645, -32130], [222858, 0, 0, 126360], [-441979, -1620, -441979, 0], [-481766, 0, 0, 161784], [99081, -167348, -99081, -334696], [392, 73584, 784, 36792], [341586, -64318, 170793, 64318], [848538, -93222, 424269, 93222], [-242568, 196104, -485136, 98052], [0, -85788, 313119, -85788], [333504, 206856, 667008, 103428], [636201, 4062, 636201, 0], [0, 227880, 434483, 227880], [-435942, 0, 0, -157320], [-1182830, 54774, -591415, -54774], [133200, -361360, 266400, -180680], [-336536, 288504, -673072, 144252], [-477918, 49124, -238959, -49124], [147765, -2322, -147765, -4644], [-386883, -301284, -386883, 0], [-501047, -69606, -501047, 0], [-619056, -152832, -1238112, -76416], [176362, 0, 0, 252936], [-648315, -28504, 648315, -57008], [0, -116478, 622531, -116478], [326106, 0, 0, -378072], [692309, 59400, 692309, 0], [-29151, 210224, 29151, 420448], [519424, -98064, 1038848, -49032], [976770, 9142, 488385, -9142], [-1101411, -148890, -1101411, 0], [-429462, 0, 0, 12264], [-271366, 0, 0, -117720], [-3843, 156286, 3843, 312572], [0, -39216, 1114263, -39216], [-995094, 69136, -497547, -69136], [0, -616140, 67219, -616140], [53570, -143478, 26785, 143478]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_14_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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