
// Make newform 17.4.b.a in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_17_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_17_b_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_17_4_b_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_17_4_b_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 := [1072, 0, 74, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [40, 0, 1, 0], [0, 46, 0, 1]];
    Rf_basisdens := [1, 1, 6, 6];
    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_17_b();"
function MakeCharacter_17_b()
    N := 17;
    order := 2;
    char_gens := [3];
    v := [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_17_b_Hecke();"
function MakeCharacter_17_b_Hecke(Kf)
    N := 17;
    order := 2;
    char_gens := [3];
    char_values := [[-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 := [[-1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, -1], [0, -1, 0, 1], [0, 7, 0, 0], [42, 0, -14, 0], [21, -8, 14, 1], [-28, 0, 0, 0], [0, -7, 0, -7], [0, 0, 0, 7], [0, -7, 0, -1], [0, -28, 0, -7], [0, -20, 0, 14], [-92, 0, -76, 0], [224, 0, 28, 0], [-98, 0, -92, 0], [-364, 0, 140, 0], [0, 64, 0, -21], [-28, 0, -64, 0], [0, -21, 0, 7], [0, -48, 0, 56], [0, 77, 0, 21], [-756, 0, 84, 0], [518, 0, -42, 0], [0, 28, 0, -22], [210, 0, 378, 0], [-168, 0, 0, 0], [0, -63, 0, -42], [0, -28, 0, 49], [0, -140, 0, -56], [784, 0, -568, 0], [0, -107, 0, -54], [734, 0, -90, 0], [0, -329, 0, 20], [-1282, 0, -160, 0], [-1576, 0, 512, 0], [-714, 0, 644, 0], [0, -217, 0, 182], [0, 15, 0, 77], [0, 260, 0, -77], [-1324, 0, -104, 0], [0, 124, 0, -89], [3808, 0, -36, 0], [0, 336, 0, -154], [0, -644, 0, -77], [0, 169, 0, -63], [0, 637, 0, 70], [-1232, 0, 1260, 0], [0, -273, 0, 90], [-3038, 0, -294, 0], [0, -196, 0, -28], [1952, 0, 484, 0], [0, 476, 0, 202], [3724, 0, 616, 0], [-3290, 0, 126, 0], [1864, 0, -392, 0], [0, -72, 0, 107], [2352, 0, -1764, 0], [0, 868, 0, 35], [-2478, 0, 1280, 0], [0, 7, 0, -204], [2814, 0, -2240, 0], [-4116, 0, -252, 0], [0, 567, 0, 181], [0, -1120, 0, -78], [0, -224, 0, -105], [-4060, 0, -1100, 0], [0, 1484, 0, -28], [0, -749, 0, 154], [6566, 0, 532, 0], [-2870, 0, 1456, 0], [3304, 0, -1156, 0], [0, -1161, 0, -329], [930, 0, -230, 0], [0, 917, 0, 252], [-11200, 0, -840, 0], [1106, 0, -3718, 0], [0, 700, 0, -225], [0, 588, 0, 602], [-4606, 0, 3388, 0], [0, 889, 0, -820], [-1870, 0, 3018, 0], [0, 301, 0, -819], [7966, 0, 1806, 0], [0, -1245, 0, -301], [-3764, 0, -2352, 0], [0, -112, 0, -238], [4438, 0, 566, 0], [-9002, 0, 1316, 0], [-288, 0, -3968, 0], [-700, 0, 2296, 0], [0, -867, 0, 525], [0, -1309, 0, 819], [-2996, 0, 4416, 0], [0, 2205, 0, 490], [0, -189, 0, -1037], [-8778, 0, 336, 0], [0, 1716, 0, 630], [-4564, 0, 4928, 0], [0, -1820, 0, 385], [0, 1029, 0, -630], [16858, 0, -3038, 0], [15708, 0, 2716, 0], [5130, 0, -5060, 0], [0, -2107, 0, 924], [-14714, 0, 3598, 0], [-15988, 0, 728, 0], [4242, 0, 6748, 0], [3464, 0, 6784, 0], [0, -380, 0, -124], [0, 665, 0, 419], [16590, 0, -3312, 0], [0, -84, 0, 392], [0, 411, 0, -1062], [-7560, 0, -3004, 0], [0, -2380, 0, 126], [0, 505, 0, -252], [2968, 0, -4732, 0], [0, 3640, 0, 245], [4620, 0, -3116, 0], [-434, 0, -2240, 0], [0, 3248, 0, -742], [0, -3012, 0, -775], [0, -1491, 0, 756], [0, -2029, 0, -672], [-54, 0, 98, 0], [0, 2912, 0, 595], [0, 1023, 0, -777], [1400, 0, 7476, 0], [24598, 0, -2464, 0], [4724, 0, -3744, 0], [0, -4179, 0, -497], [0, -1505, 0, 371], [24226, 0, 666, 0], [-6930, 0, -378, 0], [2422, 0, -994, 0], [5698, 0, 5530, 0], [0, 2595, 0, -404], [17206, 0, -1904, 0], [0, 2912, 0, -938], [0, 2731, 0, 1134], [0, -532, 0, 805], [0, -175, 0, -1911], [0, -2415, 0, 2058], [-1498, 0, -3500, 0], [0, 1559, 0, -887], [0, -1584, 0, 247], [0, -448, 0, 1742], [-2268, 0, 11620, 0], [-34384, 0, -3256, 0], [0, 1484, 0, -2247], [0, 3612, 0, -1212], [-5964, 0, -5608, 0], [0, 2681, 0, -267], [0, -1393, 0, 1624], [0, -1757, 0, -1351], [11304, 0, -2484, 0], [0, 5460, 0, -722], [-30590, 0, 2156, 0], [0, 876, 0, 399], [0, -2205, 0, -1428], [25606, 0, -554, 0], [-11208, 0, 1548, 0], [-3500, 0, -13580, 0], [-13678, 0, -372, 0], [0, -1253, 0, 839], [0, 2583, 0, 847], [0, -7468, 0, 231]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_17_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_17_4_b_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_17_4_b_a(:prec:=4)
    chi := MakeCharacter_17_b();
    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_17_4_b_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_17_4_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_17_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;