
// Make newform 17.3.e.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_17_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_17_e_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_3_e_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_3_e_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, 0, 0, 0, 0, 0, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_e();"
function MakeCharacter_17_e()
    N := 17;
    order := 16;
    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_e_Hecke();"
function MakeCharacter_17_e_Hecke(Kf)
    N := 17;
    order := 16;
    char_gens := [3];
    char_values := [[0, 1, 0, 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 := [[-1, 1, 1, 0, -1, -1, 1, -1], [-1, -1, 0, -1, 1, -1, 1, 0], [2, 2, -2, -2, 2, 2, 0, 0], [1, -1, 1, -1, 1, -5, -5, 1], [-1, -1, 3, -6, -6, 3, -1, -1], [2, 10, 0, 10, 2, 2, 0, -2], [0, 0, 0, 0, 0, 0, 0, 17], [0, -7, 6, 0, -6, 7, 0, -4], [-7, -7, -3, 5, -5, 3, 7, 7], [6, 8, -16, 16, -8, -6, -2, -2], [3, -3, 5, -5, -5, -5, -5, 5], [4, 4, 12, -2, -16, 16, 2, -12], [6, 5, -5, -6, 14, 6, 6, 14], [-29, 22, -29, -2, 14, 0, -14, 2], [24, -8, 2, -8, 24, 22, 0, -22], [-4, 18, 16, 0, -16, -18, 4, 2], [-6, 0, 6, -10, 15, -42, 15, -10], [-20, -12, -12, -20, -8, 36, -36, 8], [0, -33, 4, 26, 42, 26, 4, -33], [5, 5, -43, -35, -11, 11, 35, 43], [6, -6, 25, 25, -6, 6, -26, 26], [-17, 41, -3, -21, -21, -3, 41, -17], [-33, 20, 42, 54, 42, 20, -33, 0], [20, -46, 0, 46, -20, -29, -8, -29], [6, 13, 30, -30, -13, -6, -22, -22], [0, -28, -2, -50, -24, -50, -2, -28], [20, -4, -54, 28, 0, -28, 54, 4], [0, 43, -9, -9, 43, 0, 51, -51], [12, -20, 20, -12, 92, 32, 32, 92], [-28, -39, -16, -32, -32, -16, -39, -28], [78, 0, -78, 10, -48, 26, -48, 10], [13, 7, 34, -34, -7, -13, 69, 69], [-52, 30, 47, 42, 0, -42, -47, -30], [-73, -73, 99, 6, 7, -7, -6, -99], [-50, 48, -34, 48, -50, 64, 0, -64], [-38, -52, -24, 6, -24, -52, -38, 0], [6, -2, 0, 2, -6, 80, -30, 80], [-47, -63, -63, -47, 30, 13, -13, -30], [51, -51, 65, -81, -43, -43, -81, 65], [-24, -24, -16, 94, -36, 36, -94, 16], [56, 0, 56, -67, -62, 0, 62, 67], [48, 100, -12, -34, -34, -12, 100, 48], [-32, 58, 0, -58, 32, 74, 14, 74], [46, -131, -26, -126, 126, 26, 131, -46], [88, -22, -22, 88, 50, -48, 48, -50], [-5, -81, -123, 123, 81, 5, 1, 1], [-15, -35, 35, 15, -21, -102, -102, -21], [-46, 44, 66, 0, -66, -44, 46, 144], [-99, 65, -82, 61, -61, 82, -65, 99], [-80, 0, 80, 36, 38, -192, 38, 36], [55, 70, 214, -214, -70, -55, -70, -70], [194, 128, -52, 20, 0, -20, 52, -128], [16, 16, -8, 60, 139, -139, -60, 8], [56, 86, 54, 86, 56, -97, 0, 97], [-90, -128, -111, 0, 111, 128, 90, -8], [-94, 0, 94, -80, -14, 210, -14, -80], [0, 0, 70, 46, -102, -102, 46, 70], [0, 72, -122, -142, 50, -142, -122, 72], [82, 58, 20, 20, 58, 82, 142, -142], [-8, -4, -8, 169, -88, 0, 88, -169], [56, 93, -41, 133, 133, -41, 93, 56], [-70, 88, 0, -88, 70, -6, 18, -6], [12, -10, -80, 150, 0, -150, 80, 10], [-5, 31, 217, 217, 31, -5, 23, -23], [136, -136, 136, -136, -67, 152, 152, -67], [174, -162, -12, 6, 6, -12, -162, 174], [143, 0, -143, 76, -94, 74, -94, 76], [-142, 142, 235, 34, 90, 90, 34, 235], [117, -88, 88, -117, 9, -89, -89, 9], [-192, -162, -192, -88, 112, 0, -112, 88], [-36, -105, -284, -105, -36, -64, 0, 64], [-118, 32, -188, 0, 188, -32, 118, -144], [-211, 109, 109, -211, 125, -57, 57, -125], [0, -228, 80, -226, -212, -226, 80, -228], [303, -279, -33, -33, -279, 303, 44, -44], [206, -22, 206, -42, 232, 0, -232, 42], [164, 90, -54, 68, -54, 90, 164, 0], [24, -16, -154, 76, -76, 154, 16, -24], [77, 116, 116, 77, 60, 256, -256, -60], [124, 4, 72, -127, 0, 127, -72, -4], [-225, 56, 171, 59, 59, 171, 56, -225], [62, -192, -200, -192, 62, 104, 0, -104], [-73, -73, 233, 191, -191, -233, 73, 73], [-98, 0, 98, 19, -122, 396, -122, 19], [-367, 367, -85, -67, 107, 107, -67, -85], [-154, -303, -10, 142, 0, -142, 10, 303], [101, 412, -412, -101, -48, -140, -140, -48], [50, 101, -266, 172, -266, 101, 50, 0], [52, -200, -34, 0, 34, 200, -52, -66], [-14, 170, 0, -170, 14, -234, -196, -234], [-22, 0, 22, -33, -36, 104, -36, -33], [-149, -149, -87, -197, -259, 259, 197, 87], [-163, 273, -15, -105, -105, -15, 273, -163], [-182, -223, 28, -184, 28, -223, -182, 0], [-135, -25, 217, -120, 120, -217, 25, 135], [111, -109, -109, 111, -315, -253, 253, 315], [0, 72, -146, 102, 198, 102, -146, 72], [110, -294, -74, -101, -101, -74, -294, 110], [330, 195, 290, 195, 330, -126, 0, 126], [156, -156, -248, -182, 36, 36, -182, -248], [-297, -297, -9, 170, 107, -107, -170, 9], [-92, -130, 232, -130, -92, -90, 0, 90], [-50, 524, 47, 0, -47, -524, 50, -174], [177, 0, -177, 296, -268, -104, -268, 296], [171, -92, -92, 171, 35, 327, -327, -35], [0, 90, 180, -114, -176, -114, 180, 90], [16, 314, 16, 132, 276, 0, -276, -132], [159, -20, 68, -420, 68, -20, 159, 0], [154, 228, 0, -228, -154, -58, -148, -58], [364, -89, 188, -148, 148, -188, 89, -364], [-15, 87, -327, 327, -87, 15, 501, 501], [-478, -56, -140, 200, 0, -200, 140, 56], [-202, -178, -86, -86, -178, -202, 49, -49], [-32, -221, 221, 32, 229, 145, 145, 229], [-172, 14, -142, 0, 142, -14, 172, 424], [-346, -294, 270, -270, 294, 346, 25, 25], [125, -125, 327, 43, 180, 180, 43, 327], [222, 386, 52, -176, 0, 176, -52, -386], [-394, 32, -32, 394, -42, 408, 408, -42], [-60, 20, -510, 20, -60, 140, 0, -140], [202, -154, -4, 570, -4, -154, 202, 0], [-136, -260, -260, -136, -180, -9, 9, 180], [-106, 106, 320, -554, -300, -300, -554, 320], [261, 261, 479, -3, 24, -24, 3, -479], [-174, -1, 345, -13, -13, 345, -1, -174], [168, -76, 0, 76, -168, -236, -484, -236], [-142, 294, 90, -90, -294, 142, -202, -202], [47, -333, -137, -137, -333, 47, 381, -381], [182, 18, -54, 18, 182, 26, 0, -26], [326, -200, -350, 0, 350, 200, -326, -170], [178, 0, -178, -196, 66, -4, 66, -196], [161, -355, -81, 81, 355, -161, 301, 301], [-113, -113, -381, 35, 307, -307, -35, 381], [-104, -256, -104, -142, -48, 0, 48, 142], [-241, -70, 172, -70, -241, -174, 0, 174], [299, -474, 0, 474, -299, 58, -476, 58], [220, 0, -220, -362, 546, -120, 546, -362], [143, 438, 175, 175, 438, 143, -319, 319], [158, -208, 224, -302, 224, -208, 158, 0], [421, 10, 10, 421, 54, 86, -86, -54], [56, 291, 153, -153, -291, -56, -347, -347], [458, 520, -212, -212, 520, 458, 110, -110], [309, 323, -323, -309, 173, 65, 65, 173], [-567, -213, 108, 351, 351, 108, -213, -567], [264, -98, -46, -98, 264, -474, 0, 474], [-125, 21, -25, -117, 117, 25, -21, 125], [-284, -284, 118, -130, -166, 166, 130, -118], [-580, -240, 240, 580, -132, 413, 413, -132], [-35, -274, -35, 206, 430, 0, -430, -206], [-406, -38, 496, -38, -406, 276, 0, -276], [14, 212, 212, 14, -54, 184, -184, 54], [-222, 222, -222, -202, -249, -249, -202, -222], [0, -46, 374, 271, -86, 271, 374, -46], [-51, -51, 147, 317, -631, 631, -317, -147], [554, 107, 203, 631, -631, -203, -107, -554], [417, 309, 309, 417, 461, -43, 43, -461], [-318, -372, 438, -158, 0, 158, -438, 372], [220, 700, -11, 280, 280, -11, 700, 220], [-320, -116, -336, 0, 336, 116, 320, -812], [-564, -614, 472, 194, -194, -472, 614, 564], [154, -479, 147, -147, 479, -154, 407, 407], [-176, 119, 440, -482, 0, 482, -440, -119], [-70, 332, -610, -472, -610, 332, -70, 0], [349, 298, 94, 0, -94, -298, -349, 586], [188, 0, -188, 456, -27, -124, -27, 456], [265, -265, 75, -761, 133, 133, -761, 75], [-753, 203, 57, 57, 203, -753, -65, 65], [-112, -198, 146, 652, 652, 146, -198, -112]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_17_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_17_3_e_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_3_e_a(:prec:=8)
    chi := MakeCharacter_17_e();
    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_17_3_e_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_3_e_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_17_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![961, -248, -192, -120, 64, 72, 32, 8, 1]>],Snew);
    return Vf;
end function;