
// Make newform 117.4.q.c in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_117_q();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_117_q_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_117_4_q_c();".
// 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_117_4_q_c();".
// 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, -1, 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_117_q();"
function MakeCharacter_117_q()
    N := 117;
    order := 6;
    char_gens := [92, 28];
    v := [6, 5];
    // 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_117_q_Hecke();"
function MakeCharacter_117_q_Hecke(Kf)
    N := 117;
    order := 6;
    char_gens := [92, 28];
    char_values := [[1, 0], [0, 1]];
    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 := [[2, 2], [0, 0], [8, -16], [26, -13], [13, 13], [-39, 52], [0, 27], [-102, 51], [57, -57], [-69, 69], [42, -84], [-23, -23], [227, 227], [0, 85], [-198, 396], [-426, 0], [22, -11], [0, 17], [95, 95], [-674, 337], [-580, 1160], [-1244, 0], [-246, 492], [-177, -177], [1426, -713], [1959, -1959], [1856, 0], [-255, 255], [352, -704], [0, 411], [2243, -2243], [372, 0], [1374, -687], [0, 2545], [-1506, 753], [50, -100], [-1534, 0], [-1886, 943], [-939, -939], [0, -873], [-1287, 1287], [2, 0], [0, -2841], [-2451, -2451], [-1589, -1589], [0, 1685], [-1681, 1681], [-2365, -2365], [-506, 253], [-104, 208], [5778, 0], [-1074, 2148], [2378, -1189], [0, 4491], [5451, -5451], [-783, 783], [0, -5085], [-765, -765], [0, 3421], [468, -936], [-7177, 7177], [-10754, 5377], [2758, -5516], [-6192, 0], [-770, 0], [4652, -9304], [-6094, 3047], [8278, 0], [0, 6867], [7017, 7017], [-3353, -3353], [774, -1548], [-3665, 3665], [0, -5371], [-6645, -6645], [-2794, 1397], [9858, 0], [-10070, 5035], [4379, 4379], [4970, -2485], [5397, -5397], [-4192, 8384], [281, 281], [0, -12139], [-461, 461], [-12156, 0], [342, -171], [353, 353], [15158, -7579], [482, -964], [-14496, 0], [5137, 5137], [5490, -2745], [-1635, 1635], [8334, -16668], [0, 12687], [3319, 3319], [-6054, 0], [14803, -14803], [12396, -24792], [-13516, 0], [-1669, -1669], [0, 11583], [12879, -12879], [-11636, 0], [-7108, 14216], [9033, 9033], [14908, -29816], [-1668, 0], [-13699, 13699], [0, 23173], [9593, 9593], [-32786, 16393], [3606, -7212], [25842, -12921], [0, 19827], [-9758, 4879], [2949, -2949], [12039, -12039], [0, 3363], [5865, 5865], [18169, -18169], [-9042, 0], [14390, -7195], [-2493, -2493], [18270, 0], [1882, -941], [0, 9831], [-15464, 0], [7284, -14568], [-9401, -9401], [-6239, -6239], [13615, -13615], [-5551, 5551], [-11642, 5821], [17181, 17181], [-32026, 16013], [-36430, 18215], [0, 1455], [1659, -1659], [-2542, 5084], [16571, 16571], [0, -15779], [-4246, 8492], [-17371, 17371], [-34034, 17017], [-1680, 3360], [5346, 0], [24244, 0], [18926, -37852], [-5246, 2623], [-20517, 20517], [-23852, 0], [38757, -38757], [39071, -39071], [53040, 0], [0, -367], [34566, -17283], [42166, 0], [-20220, 40440], [1501, 1501], [0, 10623], [-11662, 23324], [0, -2325], [-19017, -19017], [24654, -49308], [4843, -4843], [0, -10943]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_117_q_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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