
// Make newform 105.4.a.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_105_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_105_4_a_g();".
// 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_105_4_a_g();".
// 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 := [-10, -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_105_a();"
function MakeCharacter_105_a()
    N := 105;
    order := 1;
    char_gens := [71, 22, 31];
    v := [1, 1, 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;

function MakeCharacter_105_a_Hecke(Kf)
    return MakeCharacter_105_a();
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, 1], [3, 0], [-5, 0], [7, 0], [32, -2], [2, -10], [26, -12], [-60, -2], [32, -48], [170, 12], [52, -38], [-134, 80], [62, -108], [-248, 100], [-164, 140], [462, 58], [220, 76], [-398, -84], [-64, -228], [112, 86], [182, -38], [920, -8], [-228, -224], [230, 336], [-474, 278], [602, 348], [-48, 192], [356, 340], [1150, -252], [-718, -254], [816, -520], [452, -72], [-534, 134], [-1260, 390], [-110, -344], [632, 36], [-94, -614], [-1968, -316], [-964, 300], [-3038, 436], [-880, 574], [-3198, -184], [2752, -318], [-4598, -48], [-1434, -370], [1460, 714], [1812, -712], [832, -4], [2116, 416], [-3310, 1148], [-1318, 354], [1280, -586], [-2198, 1172], [372, -1924], [2506, 152], [-6288, 436], [-2750, 1396], [4652, 830], [-3214, -368], [2802, -1124], [-468, 1176], [482, -1220], [-4724, 28], [3152, 952], [-2338, 1398], [-794, 1298], [812, 2776], [8506, 976], [-1564, -1296], [-6950, 1036], [-7678, -1148], [3680, -2654], [5816, -2092], [4722, 1548], [7940, -236], [8012, -228], [2210, -2600], [2946, -3210], [7202, -1464], [-7990, -208], [4220, -2008], [12382, 912], [-9608, 2486], [-6298, -418], [10780, -2798], [-7708, -2256], [410, 480], [1826, -2060], [42, 2656], [5752, -3224], [-6884, 3520], [-2920, 3728], [-4024, 3272], [-5768, -322], [13820, 1300], [-6788, 3084], [3410, -3988], [3742, 4148], [-10948, 2108], [3742, 1360], [-11104, -700], [12966, -1382], [6452, -3080], [-150, -2152], [5612, 12], [-5354, -1130], [-15044, 528], [-6038, 2788], [17640, -3766], [4642, -5924], [8016, 1740], [1162, 4544], [12826, 1834], [-15460, 1350], [-4568, 7848], [-1598, 132], [1492, 7732], [16276, -5548], [-738, -2658], [-640, 1090], [-10358, -6164], [-5598, 1716], [2506, 3556], [-25268, 3704], [-4948, -3662], [682, 5916], [-27930, 136], [13120, 1692], [3096, -4164], [-12558, 3726], [2140, -4700], [-7448, -7596], [15392, -304], [4786, -5596], [-18498, 4304], [-14750, 7884], [-17998, -3092], [-3444, 8060], [16946, -5588], [20210, 6212], [612, -8322], [-16718, -6364], [24592, 160], [-20804, -4900], [-9630, 352], [12560, -1020], [6922, -4322], [39506, -840], [22500, -3670], [-20688, 3928], [-17574, 888], [4422, -4792], [-10528, 1420], [-18404, 5676], [-11224, 8060], [36672, 1194], [40520, -440], [36230, -4736], [5926, 4210], [-2358, -2400], [-5404, -8416], [17402, -14598], [-22664, -3472], [-9708, -8092], [-734, 13794], [34532, 4500], [-12288, -11576], [-15094, 15318]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_105_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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