
// Make newform 160.3.h.a in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_160_h();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_160_h_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_h_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_160_3_h_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 := [1, 0, 14, 0, 9, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [-7, 0, 10, 0, 2, 0], [0, -76, 0, -40, 0, -4], [23, 64, 20, 50, 2, 6], [21, -64, 30, -50, 4, -6], [0, -88, 0, -100, 0, -12]];
    Rf_basisdens := [1, 5, 5, 5, 5, 5];
    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_h();"
function MakeCharacter_160_h()
    N := 160;
    order := 2;
    char_gens := [31, 101, 97];
    v := [1, 2, 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_160_h_Hecke();"
function MakeCharacter_160_h_Hecke(Kf)
    N := 160;
    order := 2;
    char_gens := [31, 101, 97];
    char_values := [[-1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 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], [-1, -1, 0, 0, 0, 0], [0, 0, 0, 0, -1, 0], [-2, 0, 0, -1, -1, 0], [-2, -2, 1, -2, 2, 0], [0, 0, 0, 0, 0, -1], [1, 1, -4, 1, -1, -1], [0, 0, -1, 0, 0, 2], [12, 2, 0, -3, -3, 0], [10, 8, 0, 0, 0, 0], [2, 2, -4, 2, -2, -2], [-1, -1, -8, -1, 1, 2], [-13, -5, 0, -1, -1, 0], [13, 1, 0, 6, 6, 0], [-42, 8, 0, -1, -1, 0], [-8, -8, 8, -8, 8, -3], [0, 0, -15, 0, 0, -2], [-17, -1, 0, 7, 7, 0], [-47, 13, 0, 2, 2, 0], [-6, -6, -2, -6, 6, 2], [-1, -1, 4, -1, 1, 3], [-6, -6, 24, -6, 6, -2], [33, -3, 0, -14, -14, 0], [10, -8, 0, -16, -16, 0], [5, 5, -12, 5, -5, -1], [58, 8, 0, -16, -16, 0], [-20, 2, 0, -3, -3, 0], [-61, -21, 0, -20, -20, 0], [39, 7, 0, 7, 7, 0], [12, 12, 0, 12, -12, -8], [20, 26, 0, -3, -3, 0], [-12, -12, 3, -12, 12, -10], [2, 2, -8, 2, -2, -4], [6, 6, -31, 6, -6, 8], [7, 23, 0, -17, -17, 0], [12, 12, -26, 12, -12, -8], [-3, -3, 32, -3, 3, 6], [-141, -21, 0, 20, 20, 0], [16, -34, 0, 1, 1, 0], [13, 13, 8, 13, -13, 10], [14, 14, -29, 14, -14, 12], [10, -72, 0, 16, 16, 0], [-20, -20, 8, -20, 20, -12], [-13, -13, -20, -13, 13, 1], [-6, -6, 32, -6, 6, -1], [34, 34, 14, 34, -34, -6], [-12, -12, 39, -12, 12, 14], [-72, 54, 0, -31, -31, 0], [123, 35, 0, -4, -4, 0], [-54, 24, 0, 0, 0, 0], [31, 31, -28, 31, -31, -5], [-6, -6, 0, -6, 6, -2], [139, -29, 0, 7, 7, 0], [-6, -6, -27, -6, 6, -4], [-28, -28, 56, -28, 28, -12], [18, -20, 0, 3, 3, 0], [111, -65, 0, 7, 7, 0], [6, 6, -32, 6, -6, -14], [35, 35, 24, 35, -35, 2], [-213, -13, 0, -33, -33, 0], [-389, -13, 0, -12, -12, 0], [-19, -19, 32, -19, 19, -20], [253, 1, 0, -2, -2, 0], [12, 12, 46, 12, -12, 24], [10, 10, -72, 10, -10, 8], [14, 14, 0, 14, -14, 5], [-44, -44, 13, -44, 44, -14], [-26, -26, 16, -26, 26, -26], [165, 41, 0, 14, 14, 0], [-118, -56, 0, -48, -48, 0], [-18, -18, 24, -18, 18, 34], [20, 20, -54, 20, -20, 24], [108, 34, 0, 21, 21, 0], [31, 31, -72, 31, -31, -18], [34, 34, 21, 34, -34, 4], [116, 122, 0, 45, 45, 0], [-281, -9, 0, 47, 47, 0], [-54, -54, 64, -54, 54, 21], [266, -24, 0, 16, 16, 0], [179, 91, 0, -17, -17, 0], [26, 26, 103, 26, -26, -8], [439, 71, 0, 15, 15, 0], [6, 6, 104, 6, -6, -14], [3, 3, -92, 3, -3, 13], [52, 52, -98, 52, -52, 16], [351, -17, 0, -8, -8, 0], [-293, -77, 0, -9, -9, 0], [80, 80, -16, 80, -80, -26], [-470, 8, 0, -16, -16, 0], [-306, -112, 0, -25, -25, 0], [-361, -25, 0, 56, 56, 0], [40, 40, -88, 40, -40, 24], [-62, 124, 0, -45, -45, 0], [16, 16, -131, 16, -16, 6], [-86, -86, 75, -86, 86, 0], [252, 18, 0, -35, -35, 0], [42, 136, 0, 0, 0, 0], [-350, 80, 0, 48, 48, 0], [-281, -89, 0, 48, 48, 0], [-230, -40, 0, -80, -80, 0], [-225, -161, 0, -16, -16, 0], [25, 25, -48, 25, -25, 22], [99, 59, 0, 4, 4, 0], [131, -117, 0, -65, -65, 0], [-26, -26, 61, -26, 26, 32], [-78, -78, -24, -78, 78, 22], [-523, 89, 0, -34, -34, 0], [12, 12, 152, 12, -12, 24], [26, 26, -126, 26, -26, -54], [-229, 67, 0, -33, -33, 0], [54, -184, 0, -17, -17, 0], [14, 14, -24, 14, -14, 59], [13, 13, -44, 13, -13, 13], [-40, -40, 61, -40, 40, -50], [52, 52, -18, 52, -52, 32], [347, -13, 0, 39, 39, 0], [53, 73, 0, -58, -58, 0], [366, 208, 0, 63, 63, 0], [78, 78, -56, 78, -78, 9], [26, 26, 51, 26, -26, -16], [463, 207, 0, 31, 31, 0], [-87, -87, 4, -87, 87, -37], [-72, -72, -96, -72, 72, 9], [-111, -99, 0, 106, 106, 0], [64, 64, -9, 64, -64, -14], [119, 311, 0, 55, 55, 0], [-118, 120, 0, 16, 16, 0], [34, 34, -120, 34, -34, -42], [-214, -12, 0, 75, 75, 0], [-65, -65, -200, -65, 65, -4], [22, 22, 31, 22, -22, -20], [342, -216, 0, 7, 7, 0], [-76, -76, -60, -76, 76, 36], [-35, -35, 216, -35, 35, 48], [-438, 40, 0, -112, -112, 0], [2, -16, 0, -48, -48, 0], [32, 32, -40, 32, -32, -15], [569, 37, 0, -118, -118, 0], [-48, -48, 216, -48, 48, -45], [418, 144, 0, -80, -80, 0], [98, 98, 49, 98, -98, -36], [399, 47, 0, -177, -177, 0], [-184, -58, 0, 41, 41, 0], [-343, -59, 0, -126, -126, 0], [-769, -17, 0, 87, 87, 0], [-100, -100, -14, -100, 100, 48], [24, 24, -208, 24, -24, -51], [-108, -108, 144, -108, 108, -66], [72, 72, -183, 72, -72, 22], [114, 268, 0, -133, -133, 0], [27, 27, -48, 27, -27, -92], [547, -37, 0, 55, 55, 0], [-441, 183, 0, -56, -56, 0], [502, 40, 0, -41, -41, 0], [1195, 67, 0, 68, 68, 0], [58, 58, 8, 58, -58, 78], [26, 26, 50, 26, -26, 10], [75, 323, 0, 7, 7, 0], [-45, -45, 92, -45, 45, -17], [-390, 120, 0, 48, 48, 0], [401, -243, 0, -14, -14, 0], [-24, -24, -16, -24, 24, 18], [88, 22, 0, 25, 25, 0], [-184, -184, 69, -184, 184, 30], [27, 27, 4, 27, -27, 13], [-846, 44, 0, 3, 3, 0], [200, 200, -56, 200, -200, 8], [-72, -72, 280, -72, 72, 1]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_160_h_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_160_3_h_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_160_3_h_a(:prec:=6)
    chi := MakeCharacter_160_h();
    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_h_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_160_3_h_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_160_h();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![8, -16, 2, 1]>],Snew);
    return Vf;
end function;