
// Make newform 171.3.p.c in Magma, downloaded from the LMFDB on 23 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_c();".
// 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_c();".
// 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 := [43, -77, 94, -35, 20, -3, 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], [6, -1, 1, 0, 0, 0], [11, -56, 34, -26, 5, -2], [52, -87, 98, -10, 9, 1], [-7, 9, -2, 1, 0, 0]];
    Rf_basisdens := [1, 1, 1, 23, 23, 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_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], [1, 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 := [[0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0], [0, 2, -1, 2, 1, -1], [-3, 1, 1, 1, 2, 0], [8, 1, -3, 1, 2, 0], [-1, -4, -4, -3, -4, -2], [0, -8, -2, 8, -4, -2], [-5, -5, 3, -7, -2, -2], [-14, -3, 0, -17, 3, 3], [12, -2, -8, -14, -2, -4], [13, 5, 1, 31, 0, 2], [-1, -12, 4, -14, 0, 8], [-24, 0, 2, -12, -2, -2], [0, -6, -1, -39, -3, -1], [18, 0, 0, 18, 0, 4], [4, -9, 18, -13, -9, 9], [8, 0, -1, 4, 11, 1], [19, -10, 0, 9, 10, 0], [3, 13, -18, 10, 13, -9], [-36, 0, -4, -18, -30, 4], [0, -40, -8, 3, -20, -8], [-6, 0, 11, -3, -1, -11], [80, 24, 6, 24, 48, 0], [10, -9, -30, -19, -9, -15], [-60, 0, 0, -30, -14, 0], [126, -14, 0, 112, 14, -8], [33, -47, 3, 19, 0, 6], [-46, 24, -2, -68, 0, -4], [60, 0, 6, 30, -16, -6], [30, -25, -7, 35, 0, -14], [-64, 6, -28, 70, 6, -14], [0, -32, -34, -50, -16, -34], [-146, 0, 0, -146, 0, 18], [-149, 7, 0, -142, -7, -3], [0, 22, 29, -32, 11, 29], [8, 70, -22, 86, 0, -44], [0, 36, 14, 15, 18, 14], [55, -5, 11, -5, -10, 0], [-88, 11, 14, 99, 11, 7], [-40, 0, 6, -20, -100, -6], [-50, -35, -35, -135, 0, -70], [10, 12, 36, 2, 12, 18], [114, 3, 31, 3, 6, 0], [-170, 0, -6, -85, -40, 6], [82, 29, 5, 29, 58, 0], [-1, -29, 0, -30, 29, -67], [34, 0, -5, 17, -17, 5], [-190, 0, 7, -95, -29, -7], [-2, -13, -31, -17, 0, -62], [5, -10, 24, -10, -20, 0], [0, -72, 70, -50, -36, 70], [-166, 17, 69, 17, 34, 0], [9, 16, -32, 7, 16, -16], [-202, 6, 0, -196, -6, -46], [72, 41, -26, -31, 41, -13], [0, 72, -10, 78, 36, -10], [132, 0, -7, 66, -105, 7], [0, -24, -8, -158, -12, -8], [300, 14, -32, 14, 28, 0], [-40, -103, -78, -63, -103, -39], [0, -12, -34, -6, -6, -34], [202, -34, -2, 370, 0, -4], [-108, 0, -22, -54, -120, 22], [92, 81, -21, 81, 162, 0], [-92, 32, 0, -60, -32, 30], [-96, -163, -70, -67, -163, -35], [35, -151, 13, -81, 0, 26], [-110, 0, -2, -55, -80, 2], [0, 138, -15, 260, 69, -15], [-373, -8, 30, -8, -16, 0], [-192, 27, -23, 27, 54, 0], [0, 44, 58, -192, 22, 58], [91, -49, 0, 42, 49, 81], [-114, -16, -58, -244, 0, -116], [243, -3, 9, 483, 0, 18], [208, 0, 3, 104, -67, -3], [-176, 57, 0, -119, -57, 9], [0, 64, 22, 275, 32, 22], [-12, 0, -19, -6, 115, 19], [-18, -140, -12, -122, -140, -6], [384, -5, 79, -5, -10, 0], [-212, 0, -16, -106, 106, 16], [-30, -84, 16, -54, -84, 8], [-121, -42, 176, 79, -42, 88], [106, 0, -61, 53, -11, 61], [336, -4, 0, 332, 4, 82], [8, -22, -76, -6, 0, -152], [381, -24, -74, -24, -48, 0], [0, -186, 11, -276, -93, 11], [267, 87, 91, 87, 174, 0], [-168, 28, -128, 28, 56, 0], [-74, 108, 0, 34, -108, 18], [212, -36, 40, 388, 0, 80], [0, -64, -118, -190, -32, -118], [0, -202, 3, -491, -101, 3], [32, -80, 0, -48, 80, 62], [10, 94, 116, 84, 94, 58], [-10, 191, 11, 171, 0, 22], [109, 87, -30, -22, 87, -15], [-193, 168, 0, -25, -168, -64], [75, -41, -146, -116, -41, -73], [-62, -96, 0, -158, 96, -78], [-14, 16, 62, -12, 0, 124], [156, 106, -26, 418, 0, -52], [-343, -43, 45, -43, -86, 0], [-428, 116, -70, 116, 232, 0], [0, 226, -33, 462, 113, -33], [86, -51, 0, 35, 51, -7], [112, -155, -130, -267, -155, -65], [-85, -232, 70, -402, 0, 140], [-135, -73, -61, -343, 0, -122], [0, -232, -14, -552, -116, -14], [558, 85, 0, 643, -85, -53], [-109, -55, -15, -55, -110, 0], [59, -163, 0, -104, 163, -175], [148, 0, -64, 74, 112, 64], [0, 182, 87, 259, 91, 87], [232, -44, -190, -44, -88, 0], [-370, -36, 114, -36, -72, 0], [352, -19, -26, -371, -19, -13], [-540, -100, -80, 440, -100, -40], [299, 22, 20, 620, 0, 40], [-22, 220, 44, 176, 0, 88], [-54, 39, 57, -69, 0, 114], [362, -52, -24, -52, -104, 0], [0, 54, 49, 298, 27, 49], [-27, 156, 0, 129, -156, 202], [0, -86, 247, -64, -43, 247], [0, 110, 93, 763, 55, 93], [-180, 82, 100, 82, 164, 0], [0, 138, -75, -463, 69, -75], [-48, 0, 35, -24, -59, -35], [-215, -415, 30, -200, -415, 15], [0, -24, 10, 25, -12, 10], [-730, 11, 231, 11, 22, 0], [687, 20, 0, 707, -20, -140], [-6, 218, 156, 224, 218, 78], [-19, 247, 145, 209, 0, 290], [174, -284, -30, 64, 0, -60], [404, 201, -129, 201, 402, 0], [116, 330, -28, 214, 330, -14], [-56, 274, 0, 218, -274, -192], [774, -34, 0, 740, 34, 202], [56, 156, 164, 100, 156, 82], [-233, 204, 74, -262, 0, 148], [20, 0, -107, 10, -257, 107], [0, 236, -22, 77, 118, -22], [-364, 0, -30, -182, 146, 30], [429, 115, 0, 544, -115, -41], [-234, -66, -114, -534, 0, -228], [-522, 0, -34, -261, 226, 34], [-236, 29, -229, 29, 58, 0], [767, -181, 0, 586, 181, 53], [300, 331, 198, 31, 331, 99], [928, 0, 86, 464, -50, -86], [-292, 440, -44, -144, 0, -88], [205, -143, -91, -143, -286, 0], [0, 10, -133, -878, 5, -133], [-697, 118, 0, -579, -118, -148], [384, 280, -96, -104, 280, -48], [0, 120, 134, 654, 60, 134], [-100, 0, -9, -50, 219, 9], [0, 194, -37, 461, 97, -37], [348, 0, 110, 174, -6, -110], [-62, -224, -148, -348, 0, -296], [-592, 0, -59, -296, 31, 59], [1242, 0, -145, 621, -33, 145], [45, 62, 0, 107, -62, -122]];
    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_c();".
// 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_c(: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_c();".
// 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_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_171_p();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![3, 21, 46, -21, -4, 3, 1]>,<5,R![29584, 6880, 2288, 184, 56, 4, 1]>],Snew);
    return Vf;
end function;