
// Make newform 960.4.f.p in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_960_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_960_f_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_960_4_f_p();".
// 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_960_4_f_p();".
// 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 := [100, 0, 21, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 31, 0, 1], [0, -33, 0, -3], [320, -1, 30, -1]];
    Rf_basisdens := [1, 10, 10, 10];
    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_960_f();"
function MakeCharacter_960_f()
    N := 960;
    order := 2;
    char_gens := [511, 901, 641, 577];
    v := [2, 2, 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_960_f_Hecke();"
function MakeCharacter_960_f_Hecke(Kf)
    N := 960;
    order := 2;
    char_gens := [511, 901, 641, 577];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [-1, 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 := 4;
    raw_aps := [[0, 0, 0, 0], [0, 0, 1, 0], [-1, 1, 2, -1], [0, 3, 1, 0], [-20, 1, 1, -2], [0, -3, -13, 0], [0, 5, -17, 0], [-32, -4, -4, 8], [0, 4, 8, 0], [-166, -7, -7, 14], [-28, -2, -2, 4], [0, -27, 51, 0], [-202, 2, 2, -4], [0, -48, 20, 0], [0, 46, 30, 0], [0, -41, -67, 0], [92, -1, -1, 2], [-154, 16, 16, -32], [0, 30, -122, 0], [48, 30, 30, -60], [0, -54, 222, 0], [140, -50, -50, 100], [0, 104, -164, 0], [-582, -24, -24, 48], [0, -120, 128, 0], [530, -31, -31, 62], [0, 33, 515, 0], [0, 44, 360, 0], [-298, 34, 34, -68], [0, -271, 51, 0], [0, -45, -303, 0], [-1396, 59, 59, -118], [0, -157, 217, 0], [32, -6, -6, 12], [-194, -83, -83, 166], [-212, -30, -30, 60], [0, -273, -7, 0], [0, 90, -486, 0], [0, -32, -4, 0], [0, -419, -481, 0], [2940, 15, 15, -30], [-3358, -2, -2, 4], [2120, 74, 74, -148], [0, 174, 226, 0], [0, -131, 839, 0], [3436, -18, -18, 36], [-1232, -178, -178, 356], [0, -123, 847, 0], [0, 32, 244, 0], [-142, -134, -134, 268], [0, -253, 1305, 0], [-728, -86, -86, 172], [-1730, 116, 116, -232], [-348, -105, -105, 210], [0, 59, -95, 0], [0, 100, -24, 0], [2502, 279, 279, -558], [-3100, -266, -266, 532], [0, 129, 1999, 0], [2806, 28, 28, -56], [0, 498, 1138, 0], [0, -365, -1159, 0], [0, 1044, 480, 0], [-1400, -326, -326, 652], [0, -210, -286, 0], [0, 1111, 965, 0], [-1048, -420, -420, 840], [0, 66, 2326, 0], [0, -460, -1632, 0], [-3286, -92, -92, 184], [0, -397, -3207, 0], [168, 276, 276, -552], [0, -861, -191, 0], [0, 1833, -385, 0], [-1640, -550, -550, 1100], [0, -950, -94, 0], [9758, -199, -199, 398], [0, 981, 1155, 0], [4994, -346, -346, 692], [3622, 14, 14, -28], [7628, 461, 461, -922], [5534, 142, 142, -284], [-2808, 468, 468, -936], [0, 1710, -1566, 0], [5420, -530, -530, 1060], [0, -292, 2728, 0], [1374, 438, 438, -876], [0, 852, 1364, 0], [-5782, -25, -25, 50], [0, -57, -2035, 0], [0, -76, -2240, 0], [-15040, 230, 230, -460], [0, 1305, 1467, 0], [-10276, 179, 179, -358], [-12392, 56, 56, -112], [0, 1462, -1258, 0], [7498, 481, 481, -962], [8058, 642, 642, -1284], [0, -1998, 1650, 0], [2122, -522, -522, 1044], [0, 672, -316, 0], [0, -395, 7767, 0], [0, 404, 1536, 0], [-10202, -74, -74, 148], [-5000, 356, 356, -712], [0, 0, 5088, 0], [0, -796, 1120, 0], [0, -823, -2165, 0], [-12584, 172, 172, -344], [2950, -334, -334, 668], [0, -75, 7727, 0], [0, -2889, -3471, 0], [0, -427, 2767, 0], [5792, -546, -546, 1092], [3908, 10, 10, -20], [-9602, 742, 742, -1484], [0, 4062, 1510, 0], [0, -3110, 2402, 0], [0, 445, -5209, 0], [-3260, -1115, -1115, 2230], [-9842, 780, 780, -1560], [0, -1254, -58, 0], [0, 2719, 3549, 0], [0, -1480, 2044, 0], [9760, 176, 176, -352], [16326, -9, -9, 18], [-19598, 294, 294, -588], [-18024, -528, -528, 1056], [0, 1497, -4037, 0], [0, 663, 1385, 0], [3592, 884, 884, -1768], [0, 2998, 1110, 0], [-30940, -206, -206, 412], [0, -1863, 6183, 0], [-1654, -1522, -1522, 3044], [-19166, -742, -742, 1484], [0, 805, 7631, 0], [0, 1434, 4330, 0], [0, -3749, -55, 0], [-7110, -450, -450, 900], [-18808, -450, -450, 900], [-4066, -1153, -1153, 2306], [0, 951, 6269, 0], [0, 1040, 4468, 0], [-5690, -450, -450, 900], [-17320, 200, 200, -400], [0, -3165, -3179, 0], [0, -5071, 3475, 0], [-34520, 50, 50, -100], [0, -2192, -6580, 0], [0, -2937, 10481, 0], [-16194, 528, 528, -1056], [0, -5418, -5250, 0], [0, 2056, -4060, 0], [0, -3036, -8080, 0], [17648, -400, -400, 800], [-20, 630, 630, -1260], [7198, 1126, 1126, -2252], [0, -1404, 4956, 0], [10522, 1003, 1003, -2006], [0, -3892, 8872, 0], [0, 2933, 4383, 0], [0, 2535, -9043, 0], [9828, -153, -153, 306], [0, 2105, 8043, 0], [0, -1886, 12458, 0], [17468, 1330, 1330, -2660], [0, 1947, 4333, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_960_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_960_4_f_p();".
// 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_960_4_f_p(:prec:=4)
    chi := MakeCharacter_960_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_960_4_f_p();".
// 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_960_4_f_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_960_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![129600, 0, 756, 0, 1]>,<11,R![72, 42, 1]>],Snew);
    return Vf;
end function;