
// Make newform 22.3.d.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_22_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_22_d_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_22_3_d_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_22_3_d_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 := [16, 0, -8, 0, 4, 0, -2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2, 2, 4, 4, 8, 8];
    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_22_d();"
function MakeCharacter_22_d()
    N := 22;
    order := 10;
    char_gens := [13];
    v := [7];
    // 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_22_d_Hecke();"
function MakeCharacter_22_d_Hecke(Kf)
    N := 22;
    order := 10;
    char_gens := [13];
    char_values := [[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, 1, 0, 0, 0, 0, 0, 0], [1, -1, -2, 1, 2, -1, -1, 2], [0, -2, -1, 0, -3, 0, -1, -2], [-5, 1, 2, -1, 1, 0, 4, -2], [-1, 2, 4, 3, -2, 4, -4, -3], [1, -4, 5, 0, -4, -10, 2, 10], [6, 8, -1, -6, 3, 6, -5, -8], [-3, 11, -4, -5, -4, 11, -3, 0], [-16, -16, 0, 12, -6, -8, 6, 4], [17, 6, -18, -6, 19, 0, -36, -4], [3, 1, -3, -2, 0, 4, 14, 2], [3, 2, -6, 6, 3, -4, 0, 2], [25, -8, -12, 8, -12, -8, 25, 0], [-14, 0, 28, 4, -24, -10, 4, 4], [-13, 9, 6, 1, -6, -11, 13, 2], [-57, 6, 57, -12, 0, 0, 34, -12], [15, -13, -20, -33, 15, 20, 0, -10], [10, -6, 9, 34, 5, -34, -19, 6], [12, 4, 0, -14, -4, 2, 4, 10], [0, 19, -3, -7, -33, -7, -3, 19], [-9, -22, 2, 22, 5, 0, 4, 26], [59, -17, 15, 0, 44, 24, -22, -24], [-30, -39, -1, 33, -15, -33, 31, 39], [-30, 48, 0, -6, -66, 24, 66, -42], [1, -12, -1, 24, 0, -8, -12, 16], [-37, -6, 43, 0, -80, 30, 40, -30], [-9, -9, -56, 35, -9, -44, 0, 22], [-13, 9, 52, -1, 52, 9, -13, 0], [26, 0, -52, -8, 34, -38, -18, -8], [87, -6, 30, 2, -30, 2, -87, 4], [78, 67, -105, -9, 39, 9, 27, -67], [22, 0, -44, -22, -66, 0, -110, -22], [0, -50, -33, -30, 31, -30, -33, -50], [-15, -41, 28, 41, -41, 0, 56, -46], [-158, 10, 73, -62, -79, 62, 85, -10], [23, 7, 74, 33, 74, 7, 23, 0], [63, -24, -144, 6, 144, 12, -63, 12], [89, 91, -89, -182, 0, 130, -132, -52], [35, 29, 51, 0, -16, -8, 8, 8], [-9, 122, -10, -18, -10, 122, -9, 0], [-43, -121, 90, 93, -90, -65, 43, 186], [0, 58, -137, 32, 125, 32, -137, 58], [59, -5, -68, 31, 59, -36, 0, 18], [-150, 120, 163, -64, -75, 64, -13, -120], [54, 0, -108, -46, 132, 26, 24, -46], [-168, 36, 0, -66, -160, 18, 160, 30], [31, -117, 115, 0, -84, 4, 42, -4], [85, 39, -2, -83, 2, 127, -85, -166], [-159, 47, 82, -47, -5, 0, 164, 28], [-27, -76, 27, 152, 0, 12, 44, 164], [77, 12, -95, 0, 172, -38, -86, 38], [95, -129, -14, 13, -14, -129, 95, 0], [6, 0, -12, 72, 10, -2, -2, 72], [151, -17, -151, 34, 0, -116, 32, -82], [39, -40, 88, -164, 39, 124, 0, -62], [2, 0, -4, 80, -68, 58, -72, 80], [0, -60, 65, 40, -5, 40, 65, -60], [-91, -61, -78, 61, 247, 0, -156, 108], [-243, -64, -11, 0, -232, 12, 116, -12], [-46, -84, 163, 124, -23, -124, -117, 84], [3, -67, -20, 19, -20, -67, 3, 0], [141, 70, -62, -70, -17, 0, -124, -36], [-158, 0, 316, 138, -82, -116, 234, 138], [-115, 111, 162, -27, -162, -57, 115, -54], [0, 76, 97, -18, 49, -18, 97, 76], [49, -12, -49, 24, 0, 32, -60, 56], [-34, -24, 0, -18, 72, -12, -72, 42], [63, -116, -12, 116, -39, 0, -24, 42], [154, 21, 151, -19, 77, 19, -305, -21], [-25, -32, 20, -258, 20, -32, -25, 0], [-216, -124, 0, 142, -176, -62, 176, -18], [149, -81, -154, 81, 159, 0, -308, -132], [-109, 87, -66, 7, -109, 80, 0, -40], [-60, 0, 120, -38, 10, -112, 130, -38], [0, -73, -319, -35, 287, -35, -319, -73], [-53, 125, 53, -250, 0, 48, 222, -202], [191, 36, 148, 128, 191, -92, 0, 46], [32, -220, 0, 186, -72, -110, 72, 34], [0, -10, -85, -36, -155, -36, -85, -10], [215, 210, 115, 0, 100, 40, -50, -40], [304, 48, 0, -188, 118, 24, -118, 140], [133, -118, -206, 72, 206, -26, -133, 144], [-135, -13, -51, 0, -84, -310, 42, 310], [-83, 358, 114, -42, -83, 400, 0, -200], [218, 0, -436, -110, 294, -36, -142, -110], [-381, 23, 368, -105, -368, 187, 381, -210], [13, 0, -13, 0, 0, 240, 144, 240], [10, 238, -327, -118, 5, 118, 317, -238], [186, 0, -372, 38, 80, -282, -292, 38], [-88, -268, 0, 178, 196, -134, -196, 90], [0, 19, -13, -61, 517, -61, -13, 19], [-174, -127, -81, -107, -87, 107, 255, 127], [335, -69, -92, 39, 92, -9, -335, 78], [269, -33, 16, 33, -301, 0, 32, 90], [-91, -315, 34, 97, -91, -412, 0, 206], [285, 227, 196, -173, 196, 227, 285, 0], [-223, 44, 348, 36, -348, -116, 223, 72], [-19, 146, -298, 58, -19, 88, 0, -44], [522, -233, -139, 405, 261, -405, -383, 233], [541, 88, -67, 0, 608, 68, -304, -68], [-333, -15, -188, 127, -188, -15, -333, 0], [-109, -56, -28, 56, 165, 0, -56, 118], [-101, -307, -429, 0, 328, -20, -164, 20], [-167, -142, 278, -232, 278, -142, -167, 0], [-18, 0, 36, 94, 62, 424, 98, 94], [0, -10, 531, -102, -353, -102, 531, -10], [3, 363, 270, 67, 3, 296, 0, -148], [100, 0, -200, 74, -186, 40, -386, 74], [0, 29, -97, 301, -41, 301, -97, 29], [-295, 302, 334, -302, -373, 0, 668, -20], [-95, 259, 189, 0, -284, 186, 142, -186], [177, -90, 34, 308, 34, -90, 177, 0], [74, -272, 0, -66, -270, -136, 270, 338], [-9, 15, -228, 159, 228, -333, 9, 318], [407, -169, -32, -197, 407, 28, 0, -14], [699, 100, -344, -170, 344, 240, -699, -340], [0, -15, -449, 37, 355, 37, -449, -15], [-605, 27, 605, -54, 0, 24, -4, -30], [5, -196, 116, -316, 5, 120, 0, -60], [190, 0, -380, -190, 378, -188, -2, -190], [120, 44, 0, 76, -94, 22, 94, -120], [-161, 482, -433, 0, 272, 166, -136, -166], [-642, 330, 433, -336, -321, 336, 209, -330], [-34, 0, 0, -78, 412, 0, -412, 78], [503, 155, -503, -310, 0, 32, -244, -278], [-71, 144, -188, 372, -188, 144, -71, 0], [0, -206, 195, -230, 15, -230, 195, -206], [-391, -243, 156, 221, -391, -464, 0, 232], [-78, 104, 0, 102, -476, 52, 476, -206], [441, 64, -156, -64, -129, 0, -312, 312], [-85, 95, 383, 0, -468, 236, 234, -236], [610, 385, -409, 77, 305, -77, -201, -385], [377, -125, 22, 133, -22, -141, -377, 266], [207, 42, -207, -84, 0, 76, 362, -8], [155, 162, 311, 0, -156, -288, 78, 288], [-384, 0, 768, 34, -442, -268, 326, 34], [-399, -266, 142, -16, -142, 298, 399, -32], [362, 43, 225, -115, 181, 115, -587, -43], [0, -188, 101, 294, -127, 294, 101, -188], [746, 114, -519, -250, 373, 250, -227, -114], [-325, 273, -364, -589, -364, 273, -325, 0], [-585, -390, 238, 390, 109, 0, 476, 170], [-495, -11, 495, 22, 0, -242, 410, -220], [-133, -131, -405, 0, 272, -304, -136, 304], [375, -32, -804, 188, 375, -220, 0, 110], [217, -511, -288, 247, 288, 17, -217, 494], [322, -204, -89, -88, 161, 88, -233, 204], [-54, 0, 108, -346, 8, -290, 116, -346], [718, 336, 0, -110, 560, 168, -560, -226], [0, -35, 517, -153, -531, -153, 517, -35], [409, 18, -262, 312, -262, 18, 409, 0], [272, -172, 0, -162, -452, -86, 452, 334], [-407, 121, -280, 87, 280, -295, 407, 174], [47, 285, -356, -285, 665, 0, -712, -448], [0, -173, 25, 73, 147, 73, 25, -173], [-419, -101, 419, 202, 0, 174, 152, 376], [38, 439, 111, -705, 19, 705, -149, -439], [0, 6, -501, -86, -489, -86, -501, 6], [203, 30, 407, 0, -204, 248, 102, -248], [-1122, 404, 707, -274, -561, 274, 415, -404], [-242, 336, 0, 274, -260, 168, 260, -610], [-543, -234, 514, 234, -485, 0, 1028, 260], [350, 0, -700, -228, 520, 514, -180, -228], [-455, 321, 8, -281, -8, 241, 455, -562], [-43, 60, 43, -120, 0, -230, -82, -350], [-187, 33, -1024, 209, -187, -176, 0, 88], [-686, 496, 0, -538, -700, 248, 700, 42], [187, 56, -136, -56, 85, 0, -272, 46]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_22_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_22_3_d_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_22_3_d_a(:prec:=8)
    chi := MakeCharacter_22_d();
    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_22_3_d_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_22_3_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_22_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;