
// Make newform 171.3.p.d in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_171_p();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_171_p_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_171_3_p_d();".
// 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_171_3_p_d();".
// 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, -7, 50, 5, 8, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [-56, 7, -50, 64, -8, 1], [-385, 2750, 344, 440, -55, 56], [534, 3634, 561, 550, -36, 70], [-1430, 3683, 211, 605, -92, 77]];
    Rf_basisdens := [1, 1, 393, 393, 393, 393];
    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_171_p();"
function MakeCharacter_171_p()
    N := 171;
    order := 6;
    char_gens := [20, 154];
    v := [6, 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_171_p_Hecke();"
function MakeCharacter_171_p_Hecke(Kf)
    N := 171;
    order := 6;
    char_gens := [20, 154];
    char_values := [[1, 0, 0, 0, 0, 0], [0, 0, 0, -1, 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 := [[1, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0], [3, -1, 0, 2, 2, 1], [-1, 0, -3, 0, 2, -2], [-6, 0, -5, 0, 2, -2], [4, 0, 0, 2, -4, 0], [16, 0, 0, 15, 2, 1], [14, -5, -3, 13, 2, 5], [4, -1, -1, 5, 2, 4], [0, -9, 9, 0, -13, 0], [17, -2, -1, 42, -4, -4], [-3, 2, 1, -24, 9, 9], [-3, 10, 20, 4, 0, 1], [-22, 8, 0, -18, -8, -4], [-12, 1, 1, 13, -6, -12], [8, 4, -4, 4, 12, 0], [-2, -11, -22, -20, 0, -22], [-30, -13, -13, -30, -15, -30], [48, -13, 13, 24, -8, 0], [10, -4, -8, -10, 0, 0], [25, -2, 0, 14, 22, 11], [26, 10, 20, 8, 0, 34], [-12, 0, 37, 0, -13, 13], [-6, 24, -24, -3, -9, 0], [-29, -2, -4, -17, 0, -46], [-70, -23, -23, 40, -35, -70], [22, -40, -20, 24, 10, 10], [82, -28, -14, 116, 24, 24], [-54, -26, -52, -8, 0, -62], [-7, -16, -8, -46, 16, 16], [-52, 4, -4, -26, -54, 0], [-116, -55, 0, -85, -62, -31], [-44, -46, -46, 107, -22, -44], [-52, -43, -43, -108, -26, -52], [-43, 41, 0, -52, 18, 9], [-79, -46, -23, -154, -2, -2], [190, 24, 0, 164, 52, 26], [-48, 0, -37, 0, 20, -20], [8, 16, -16, 4, 10, 0], [66, 56, 112, -58, 0, 8], [-128, 46, 23, -218, -19, -19], [-8, -25, 25, -4, -87, 0], [104, 0, -4, 0, -28, 28], [-46, 0, 0, 17, 0, -29], [245, 0, -33, 0, 13, -13], [80, 80, 80, 136, 40, 80], [-58, -2, -4, 15, 0, -43], [97, 75, 150, -65, 0, 32], [32, 86, 43, -46, 55, 55], [140, 0, -74, 0, 22, -22], [-115, -138, 0, -36, -158, -79], [-122, 0, 4, 0, -38, 38], [-212, -32, 32, -106, -65, 0], [42, -45, -45, 45, 21, 42], [172, -64, 64, 86, 7, 0], [-3, 47, 0, 23, -52, -26], [-82, -68, -136, 40, 0, -42], [-109, -17, 0, -157, 96, 48], [81, 0, 9, 0, 49, -49], [-204, 36, -36, -102, 85, 0], [-48, 5, 0, 9, -114, -57], [27, 94, 47, 76, -11, -11], [-82, -35, -70, 142, 0, 60], [133, 0, 49, 0, -44, 44], [-154, -214, -214, -90, -77, -154], [-212, -88, 88, -106, -170, 0], [-2, 2, 1, 94, -49, -49], [-57, 52, 104, 131, 0, 74], [312, -47, 0, 238, 148, 74], [272, 0, 102, 0, 6, -6], [-201, 0, 68, 0, 46, -46], [-169, 99, 0, -189, 40, 20], [116, 123, 123, 251, 58, 116], [8, 228, 114, -120, 68, 68], [140, -4, -2, -62, 171, 171], [-157, -15, -30, 63, 0, -94], [-116, -100, -100, -376, -58, -116], [-21, -173, 0, 122, -286, -143], [41, -4, -8, -79, 0, -38], [-68, 14, -14, -34, 121, 0], [-60, 0, 194, 0, -44, 44], [41, 27, 54, -120, 0, -79], [-276, 48, -48, -138, 216, 0], [-106, -72, 72, -53, -87, 0], [273, 3, 6, -259, 0, 14], [302, 121, 121, -269, 151, 302], [365, -64, -32, 704, 13, 13], [5, 0, 56, 0, 109, -109], [-296, -34, 0, -340, 88, 44], [28, 0, 158, 0, -94, 94], [566, 0, 77, 0, -112, 112], [-64, -106, -106, -250, -32, -64], [83, -386, -193, 178, -6, -6], [376, 134, 0, 362, 28, 14], [-156, 151, 0, -225, 138, 69], [-136, 93, 93, -279, -68, -136], [-272, -34, 34, -136, -184, 0], [443, -4, -2, 314, 286, 286], [108, 76, -76, 54, 282, 0], [204, 440, 440, 100, 102, 204], [296, -130, 130, 148, 142, 0], [-40, 8, 8, 212, -20, -40], [424, 298, 149, 250, 299, 299], [-250, -252, -126, -210, -145, -145], [308, 0, 189, 0, -51, 51], [-277, 0, -262, 0, 30, -30], [366, 124, 0, 328, 76, 38], [66, -12, -12, 0, 33, 66], [434, -29, 29, 217, 56, 0], [377, 236, 118, 624, 65, 65], [-271, 10, 5, -270, -136, -136], [-186, -70, 0, -182, -8, -4], [176, -152, -152, 469, 88, 176], [512, 0, 214, 0, -248, 248], [-76, -383, -383, -79, -38, -76], [-49, -52, -104, 326, 0, 277], [-428, 87, 0, -359, -138, -69], [112, 0, 78, 0, -94, 94], [30, 0, -62, 0, 74, -74], [-40, 196, -196, -20, 88, 0], [248, 193, -193, 124, 173, 0], [354, -36, -18, 226, 241, 241], [-618, 164, 82, -796, -220, -220], [248, 16, 8, 136, 180, 180], [-126, 0, -108, 0, -148, 148], [-281, -201, 0, -66, -430, -215], [-18, -203, -203, 84, -9, -18], [68, 54, 0, -18, 172, 86], [178, 348, 0, 4, 348, 174], [-193, 0, 407, 0, -101, 101], [-412, -23, 0, -360, -104, -52], [27, -149, -298, -233, 0, -206], [-4, 78, -78, -2, -178, 0], [-82, -166, 0, 52, -268, -134], [329, 0, -398, 0, 36, -36], [-394, -480, -480, -35, -197, -394], [116, -239, 239, 58, -287, 0], [-10, -198, -99, 122, -71, -71], [-465, 122, 61, -844, -43, -43], [-389, 0, 322, 0, -191, 191], [-20, 68, -68, -10, -174, 0], [164, 380, 380, -298, 82, 164], [12, 300, 300, 238, 6, 12], [-590, -205, 205, -295, -159, 0], [-530, 24, 12, -820, -120, -120], [835, 207, 414, -129, 0, 706], [-778, 68, 0, -840, 124, 62], [-91, -324, -648, 15, 0, -76], [494, 195, 195, -107, 247, 494], [-197, 122, 61, 38, -216, -216], [-213, -45, -90, 146, 0, -67], [-1299, 0, -162, 0, -63, 63], [-90, 271, 271, 557, -45, -90], [348, -96, 96, 174, 122, 0], [-928, -87, -174, 630, 0, -298], [241, -210, -105, 894, -206, -206], [-252, 0, -394, 0, 50, -50], [523, -34, 0, 269, 508, 254], [-22, -58, -58, -668, -11, -22], [-216, 102, -102, -108, 32, 0], [650, -268, 0, 740, -180, -90], [-817, -248, -496, 94, 0, -723], [1154, -174, 0, 1100, 108, 54], [310, 119, 238, -310, 0, 0], [415, -584, -292, 490, 170, 170], [-600, 166, 332, 724, 0, 124], [-218, 82, 164, 530, 0, 312], [642, 237, 237, -152, 321, 642]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_171_p_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_171_3_p_d();".
// 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_171_3_p_d(:prec:=6)
    chi := MakeCharacter_171_p();
    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_171_3_p_d();".
// 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_171_3_p_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_171_p();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![27, -63, 40, 21, -4, -3, 1]>,<5,R![15876, -5544, 2188, -164, 48, -2, 1]>],Snew);
    return Vf;
end function;