
// Make newform 160.3.p.f in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_160_p();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_160_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_160_3_p_f();".
// 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_160_3_p_f();".
// 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 := [40, -12, 1, 16, -3, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [-3140, 757, 1100, -681, 100, 7], [-760, 122, 510, -1, -75, 17], [6, 7, 92, -11, -24, 9], [-1880, 3829, -940, -677, 400, -61], [560, 594, 280, -107, -15, 39]];
    Rf_basisdens := [1, 890, 445, 178, 890, 445];
    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_160_p();"
function MakeCharacter_160_p()
    N := 160;
    order := 4;
    char_gens := [31, 101, 97];
    v := [4, 4, 3];
    // 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_160_p_Hecke();"
function MakeCharacter_160_p_Hecke(Kf)
    N := 160;
    order := 4;
    char_gens := [31, 101, 97];
    char_values := [[1, 0, 0, 0, 0, 0], [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 := [[0, 0, 0, 0, 0, 0], [0, 0, -1, 0, 0, 0], [1, 0, 1, 0, 1, 0], [2, 0, 0, 2, 2, -1], [-4, -2, -1, 0, 2, 1], [-3, 2, -4, 3, 0, 0], [-3, 0, 0, -3, -2, -6], [0, 2, -4, -4, 2, -4], [10, -2, -3, -10, 0, 0], [0, 4, 2, 0, 4, 2], [-36, 2, 1, 0, -2, -1], [17, 0, 0, 17, 8, -6], [18, 3, 1, 0, -3, -1], [28, 4, 11, -28, 0, 0], [14, 0, 0, 14, -10, -9], [-19, -4, -2, 19, 0, 0], [0, -10, 0, -60, -10, 0], [-38, 3, 5, 0, -3, -5], [12, 0, 0, 12, -4, 13], [-52, -6, 17, 0, 6, -17], [5, 20, 0, -5, 0, 0], [0, 8, 4, -16, 8, 4], [40, -16, -5, -40, 0, 0], [0, 8, 4, 16, 8, 4], [-35, 0, 0, -35, 20, 20], [-30, -6, 22, 0, 6, -22], [18, 6, -27, -18, 0, 0], [48, 0, 0, 48, 16, -31], [0, -11, 9, -40, -11, 9], [57, -18, -14, -57, 0, 0], [-18, 0, 0, -18, 14, 11], [44, -18, 31, 0, 18, -31], [-103, 0, 0, -103, 8, 4], [0, 6, 28, 68, 6, 28], [0, -1, 3, -64, -1, 3], [-20, 10, 5, 0, -10, -5], [105, 0, 0, 105, 10, -20], [0, 32, -17, 0, 0, 0], [10, 0, 0, 10, -30, 11], [17, 32, -34, -17, 0, 0], [0, -30, 20, -20, -30, 20], [-6, -32, -16, 0, 32, 16], [156, 18, -11, 0, -18, 11], [101, 26, -42, -101, 0, 0], [13, 0, 0, 13, 12, 6], [0, -4, -12, 168, -4, -12], [44, 22, 11, 0, -22, -11], [-82, 18, 13, 82, 0, 0], [12, 0, 0, 12, 4, -31], [0, 24, -38, -56, 24, -38], [-99, -14, -22, 99, 0, 0], [0, 8, -16, 64, 8, -16], [-46, 35, 49, 0, -35, -49], [220, -10, -45, 0, 10, 45], [41, 0, 0, 41, 34, 22], [-70, -10, -79, 70, 0, 0], [0, 3, 19, 184, 3, 19], [108, -46, -3, 0, 46, 3], [-47, 0, 0, -47, 2, 56], [114, -21, 33, 0, 21, -33], [4, -44, 23, -4, 0, 0], [9, 14, 52, -9, 0, 0], [-76, 0, 0, -76, 68, -19], [188, -6, 17, 0, 6, -17], [-103, -18, 46, 103, 0, 0], [117, 0, 0, 117, -2, 24], [-116, 22, -69, 0, -22, 69], [121, 0, 0, 121, -56, 112], [-56, 0, 0, -56, -32, 61], [0, -48, -74, 168, -48, -74], [-95, 80, -80, 95, 0, 0], [0, 44, 92, -168, 44, 92], [6, 0, 0, 6, -58, -61], [-175, 0, 10, 175, 0, 0], [0, 6, -32, 308, 6, -32], [30, 34, 13, -30, 0, 0], [0, -27, 117, -24, -27, 117], [-99, 0, 0, -99, -36, -78], [-102, 20, 60, 0, -20, -60], [0, -37, 45, -192, -37, 45], [0, 50, 40, -260, 50, 40], [418, 23, -79, 0, -23, 79], [-180, 50, 5, 0, -50, -5], [189, 44, 52, -189, 0, 0], [0, -44, -52, 168, -44, -52], [4, 148, 7, -4, 0, 0], [0, 57, 11, -104, 57, 11], [129, 0, 0, 129, 26, -102], [-30, -14, -82, 0, 14, 82], [462, -6, -31, -462, 0, 0], [388, 0, 0, 388, 44, 9], [0, 16, -12, -432, 16, -12], [-222, 0, 0, -222, 82, -145], [-340, -10, -45, 0, 10, 45], [0, 10, 80, 140, 10, 80], [-198, 126, -99, 198, 0, 0], [0, 48, -26, 392, 48, -26], [-30, 14, -118, 0, -14, 118], [92, -84, -161, -92, 0, 0], [-14, -6, 22, 0, 6, -22], [-308, 0, 0, -308, 36, -79], [49, 0, 0, 49, 56, 38], [376, -72, 79, -376, 0, 0], [0, -75, 27, -672, -75, 27], [-516, -18, 71, 0, 18, -71], [337, 0, 0, 337, 58, 94], [-176, 0, 0, -176, 8, 109], [-31, -96, 232, 31, 0, 0], [0, 4, -108, -328, 4, -108], [98, 35, 153, 0, -35, -153], [-74, 0, 0, -74, -194, 143], [-251, 154, -8, 251, 0, 0], [-235, 0, 0, -235, -10, 150], [0, -98, 56, -44, -98, 56], [-484, -22, -111, 0, 22, 111], [-30, -69, 121, 0, 69, -121], [320, 136, 75, -320, 0, 0], [66, 0, 0, 66, -14, -177], [525, 60, 130, -525, 0, 0], [0, 26, 128, -52, 26, 128], [-142, 23, -39, 0, -23, 39], [125, 60, -180, -125, 0, 0], [-339, 0, 0, -339, -36, 22], [108, -12, -85, -108, 0, 0], [-148, 6, 43, 0, -6, -43], [-678, 11, 133, 0, -11, -133], [0, 36, -82, -304, 36, -82], [0, -120, -80, -320, -120, -80], [-294, 0, 0, -294, -6, 223], [57, 72, 286, -57, 0, 0], [0, -6, 52, 732, -6, 52], [170, 22, 1, -170, 0, 0], [-196, 162, 21, 0, -162, -21], [345, 0, 0, 345, 90, -80], [394, 36, -132, 0, -36, 132], [0, 116, -92, 536, 116, -92], [501, -294, 88, -501, 0, 0], [-620, 0, 0, -620, -140, 29], [605, 0, 0, 605, -10, -160], [0, 160, -20, 208, 160, -20], [60, -90, -125, 0, 90, 125], [-86, -125, -51, 0, 125, 51], [-326, -114, 213, 326, 0, 0], [-104, 0, 0, -104, -48, -19], [0, -125, 107, -552, -125, 107], [0, -4, 168, 328, -4, 168], [-795, 10, 80, 795, 0, 0], [-727, 0, 0, -727, -88, 76], [0, 6, -152, 548, 6, -152], [150, -62, -263, -150, 0, 0], [-135, 0, 0, -135, -150, -140], [-190, 67, -183, 0, -67, 183], [-24, 72, -257, 24, 0, 0], [-246, 0, 0, -246, 58, -249], [-32, 0, 0, -32, 96, -39], [732, -94, 33, 0, 94, -33], [0, 244, -68, 632, 244, -68], [0, -145, -99, -456, -145, -99], [-131, 0, 0, -131, -114, -242], [-422, -120, 40, 0, 120, -40], [-348, 0, 0, -348, 148, 45], [-271, 134, -58, 271, 0, 0], [-6, 0, 0, -6, -94, -293], [716, -2, 159, 0, 2, -159], [-243, 0, 0, -243, -12, -196], [-318, -130, 225, 318, 0, 0], [76, -142, -31, 0, 142, 31], [-347, 0, 0, -347, 62, 276]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_160_p_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_160_3_p_f();".
// 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_160_3_p_f(:prec:=6)
    chi := MakeCharacter_160_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_160_3_p_f();".
// 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_160_3_p_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_160_p();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![128, -224, 196, -16, 0, 0, 1]>,<13,R![125000, -125000, 62500, -4000, 98, 14, 1]>],Snew);
    return Vf;
end function;