
// Make newform 112.3.s.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_112_s();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_112_s_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_112_3_s_b();".
// 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_112_3_s_b();".
// 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 := [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, 1, 0], [0, 4, 0, 1], [0, 2, 0, -1]];
    Rf_basisdens := [1, 2, 2, 2];
    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_112_s();"
function MakeCharacter_112_s()
    N := 112;
    order := 6;
    char_gens := [15, 85, 17];
    v := [6, 6, 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_112_s_Hecke();"
function MakeCharacter_112_s_Hecke(Kf)
    N := 112;
    order := 6;
    char_gens := [15, 85, 17];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [1, 1, 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], [2, 1, 0, 1], [-1, 1, -2, 2], [-3, -2, -3, 2], [0, 9, 2, -1], [6, 12, -2, 0], [-10, -5, 0, -2], [-1, 1, -1, 1], [-15, -15, 3, 3], [12, 0, -2, 4], [14, 7, 0, -15], [-31, -31, -8, -8], [-2, -4, 10, 0], [2, 0, 2, -4], [-29, 29, -1, 1], [0, 39, -8, 4], [26, 13, 0, 25], [-7, 7, -32, 32], [0, -29, -30, 15], [6, 0, -10, 20], [106, 53, 0, 16], [-55, -55, -5, -5], [68, 136, -4, 0], [-63, 63, 24, -24], [22, 44, -26, 0], [-18, -9, 0, 18], [-51, 51, -13, 13], [-33, -33, 19, 19], [0, -35, 52, -26], [60, 0, -6, 12], [154, 0, 22, -44], [81, -81, 3, -3], [0, -75, -20, 10], [0, 0, 28, 0], [-33, -33, 14, 14], [0, 113, 10, -5], [-126, -63, 0, -48], [65, 65, -37, -37], [10, 20, 76, 0], [-27, 27, -48, 48], [0, -147, 18, -9], [-16, -32, 52, 0], [159, 159, -21, -21], [0, 61, -44, 22], [48, 0, -54, 108], [-298, -149, 0, 37], [-118, 0, -58, 116], [-156, -312, -60, 0], [-90, -45, 0, -63], [79, -79, 90, -90], [27, 27, -58, -58], [36, 0, 54, -108], [-22, -11, 0, 28], [190, 380, -68, 0], [87, -87, 102, -102], [0, -201, -110, 55], [-230, -115, 0, 44], [17, -17, 19, -19], [0, -61, -160, 80], [-72, 0, 110, -220], [94, 47, 0, 93], [-8, -16, -128, 0], [80, 160, 48, 0], [-230, -115, 0, -7], [17, -17, -52, 52], [465, 465, -4, -4], [85, 85, -33, -33], [-188, 0, -18, 36], [0, -93, -134, 67], [98, 196, 14, 0], [-442, -221, 0, -14], [63, 63, -57, -57], [418, 209, 0, 109], [1, 1, 60, 60], [-364, 0, -34, 68], [-133, 133, -101, 101], [0, 9, -76, 38], [-37, 37, 126, -126], [-117, -117, -126, -126], [486, 243, 0, 46], [184, 368, 88, 0], [-4, 0, -38, 76], [0, -183, 194, -97], [2, 4, 18, 0], [-21, 21, -45, 45], [171, 171, 15, 15], [-60, 0, 46, -92], [-7, -7, 36, 36], [346, 692, 118, 0], [46, 0, 66, -132], [-399, 399, 39, -39], [-718, -359, 0, -47], [0, -241, -14, 7], [12, 0, 182, -364], [-23, -23, 35, 35], [-28, -56, -28, 0], [-329, 329, 38, -38], [466, 233, 0, -184], [27, -27, 223, -223], [-677, -677, 42, 42], [-68, 0, -82, 164], [0, -423, 76, -38], [-246, -123, 0, 285], [-123, -123, 136, 136], [0, 397, -78, 39], [-6, -3, 0, 268], [0, 0, -336, 0], [-413, 413, 38, -38], [0, -9, -246, 123], [-74, -148, -330, 0], [-185, 185, 115, -115], [0, 749, 164, -82], [-696, 0, -62, 124], [450, 225, 0, -251], [-182, 0, 134, -268], [0, -1077, -40, 20], [-308, -616, 168, 0], [90, 45, 0, 33], [357, 357, 8, 8], [18, 0, -74, 148], [386, 193, 0, 128], [364, 0, 62, -124], [183, -183, -174, 174], [0, -399, 186, -93], [229, -229, -237, 237], [-942, 0, -24, 48], [149, 149, -196, -196], [185, -185, 187, -187], [136, 272, -204, 0], [29, -29, -216, 216], [0, 109, 42, -21], [-510, 0, 110, -220], [-305, -305, -125, -125], [520, 0, 98, -196], [247, -247, 398, -398], [-98, -196, -70, 0], [-154, -77, 0, -146], [-214, -107, 0, -47], [270, 540, -342, 0], [0, 627, -64, 32], [-396, -792, 216, 0], [507, 507, -18, -18], [0, -161, 410, -205], [654, 0, -190, 380], [206, 103, 0, -178], [508, 1016, -272, 0], [-682, -1364, 22, 0], [98, 49, 0, 484], [-97, 97, -173, 173], [525, 525, 139, 139], [703, 703, 94, 94], [312, 624, -132, 0], [542, 0, 6, -12], [583, -583, -49, 49], [0, -217, 74, -37], [-1278, 0, 34, -68], [1063, 1063, 143, 143], [5, -5, -392, 392], [-24, -48, 512, 0], [-1646, -823, 0, -28], [33, 33, 175, 175], [996, 0, -14, 28], [-1298, 0, -18, 36], [161, -161, -101, 101], [0, -771, -108, 54], [-634, -317, 0, -215], [0, 599, 602, -301], [842, 421, 0, -60]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_112_s_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_112_3_s_b();".
// 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_112_3_s_b(:prec:=4)
    chi := MakeCharacter_112_s();
    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_112_3_s_b();".
// 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_112_3_s_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_112_s();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![9, 18, 9, -6, 1]>],Snew);
    return Vf;
end function;