
// Make newform 14.3.d.a in Magma, downloaded from the LMFDB on 25 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_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_14_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_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 := 3;
    raw_aps := [[0, 1, 0, 0], [-2, -1, -1, 1], [-1, -2, 1, -4], [3, 4, 2, 5], [0, -3, -9, -3], [6, -4, 12, -2], [-10, -2, -5, 2], [1, 1, -1, 2], [15, -9, 15, 0], [12, 0, 0, -6], [-14, 15, -7, -15], [-31, -24, -31, 0], [-2, 20, -4, 10], [-2, 0, 0, -6], [29, 1, -29, 2], [0, -12, 39, -12], [-26, -25, -13, 25], [-7, -32, 7, -64], [0, 45, 29, 45], [-6, 0, 0, 30], [106, 16, 53, -16], [55, 15, 55, 0], [-68, 8, -136, 4], [-63, 24, 63, 48], [22, -52, 44, -26], [-18, 18, -9, -18], [51, 13, -51, 26], [33, -57, 33, 0], [0, 78, -35, 78], [60, 0, 0, -18], [-154, 0, 0, -66], [-81, -3, 81, -6], [0, -30, -75, -30], [0, -56, 0, -28], [-33, 42, -33, 0], [0, -15, -113, -15], [-126, -48, -63, 48], [-65, 111, -65, 0], [-10, -152, -20, -76], [-27, -48, 27, -96], [0, -27, 147, -27], [-16, 104, -32, 52], [-159, 63, -159, 0], [0, -66, 61, -66], [48, 0, 0, -162], [298, -37, 149, 37], [118, 0, 0, 174], [156, 120, 312, 60], [90, 63, 45, -63], [79, 90, -79, 180], [27, -174, 27, 0], [-36, 0, 0, -162], [-22, 28, -11, -28], [-190, 136, -380, 68], [87, 102, -87, 204], [0, 165, 201, 165], [-230, 44, -115, -44], [-17, -19, 17, -38], [0, -240, -61, -240], [-72, 0, 0, 330], [-94, -93, -47, 93], [-8, -256, -16, -128], [-80, -96, -160, -48], [230, 7, 115, -7], [17, -52, -17, -104], [465, -12, 465, 0], [-85, 99, -85, 0], [-188, 0, 0, -54], [0, 201, 93, 201], [98, 28, 196, 14], [-442, -14, -221, 14], [-63, 171, -63, 0], [-418, -109, -209, 109], [1, 180, 1, 0], [364, 0, 0, 102], [133, 101, -133, 202], [0, -114, 9, -114], [-37, 126, 37, 252], [-117, -378, -117, 0], [486, 46, 243, -46], [-184, -176, -368, -88], [-4, 0, 0, -114], [0, -291, 183, -291], [2, 36, 4, 18], [21, 45, -21, 90], [-171, -45, -171, 0], [-60, 0, 0, 138], [-7, 108, -7, 0], [346, 236, 692, 118], [-46, 0, 0, -198], [399, -39, -399, -78], [718, 47, 359, -47], [0, 21, 241, 21], [-12, 0, 0, -546], [23, -105, 23, 0], [28, 56, 56, 28], [-329, 38, 329, 76], [466, -184, 233, 184], [-27, -223, 27, -446], [-677, 126, -677, 0], [68, 0, 0, 246], [0, 114, -423, 114], [246, -285, 123, 285], [-123, 408, -123, 0], [0, 117, -397, 117], [-6, 268, -3, -268], [0, 672, 0, 336], [-413, 38, 413, 76], [0, 369, 9, 369], [-74, -660, -148, -330], [185, -115, -185, -230], [0, 246, 749, 246], [-696, 0, 0, -186], [-450, 251, -225, -251], [182, 0, 0, -402], [0, -60, -1077, -60], [308, -336, 616, -168], [-90, -33, -45, 33], [357, 24, 357, 0], [-18, 0, 0, 222], [386, 128, 193, -128], [364, 0, 0, 186], [183, -174, -183, -348], [0, -279, 399, -279], [-229, 237, 229, 474], [-942, 0, 0, -72], [149, -588, 149, 0], [-185, -187, 185, -374], [-136, 408, -272, 204], [29, -216, -29, -432], [0, -63, -109, -63], [510, 0, 0, -330], [305, 375, 305, 0], [520, 0, 0, 294], [247, 398, -247, 796], [-98, -140, -196, -70], [-154, -146, -77, 146], [214, 47, 107, -47], [270, -684, 540, -342], [0, -96, 627, -96], [396, -432, 792, -216], [507, -54, 507, 0], [0, -615, 161, -615], [-654, 0, 0, 570], [206, -178, 103, 178], [-508, 544, -1016, 272], [-682, 44, -1364, 22], [98, 484, 49, -484], [97, 173, -97, 346], [-525, -417, -525, 0], [703, 282, 703, 0], [312, -264, 624, -132], [-542, 0, 0, -18], [-583, 49, 583, 98], [0, -111, 217, -111], [1278, 0, 0, -102], [-1063, -429, -1063, 0], [5, -392, -5, -784], [-24, 1024, -48, 512], [-1646, -28, -823, 28], [-33, -525, -33, 0], [996, 0, 0, -42], [1298, 0, 0, 54], [-161, 101, 161, 202], [0, -162, -771, -162], [634, 215, 317, -215], [0, -903, -599, -903], [842, -60, 421, 60]];
    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_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_14_3_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, 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_14_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_14_3_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_14_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;