
// Make newform 105.4.a.d in Magma, downloaded from the LMFDB on 19 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_d();".
// 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_d();".
// 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;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [1, 1];
    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_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 := [[-2, -1], [3, 0], [-5, 0], [-7, 0], [-46, 2], [4, 38], [-22, -44], [-54, -26], [-160, 20], [-118, -12], [-30, -102], [102, -24], [22, 80], [68, -128], [200, 168], [8, -86], [-232, 36], [-342, -84], [368, -164], [-370, 138], [212, 122], [-204, 484], [304, 84], [-666, 112], [-1224, 86], [34, 632], [-736, 256], [568, -312], [1318, -336], [-572, 618], [-1180, -628], [-956, -208], [968, -386], [-1982, 478], [2014, -104], [48, 608], [96, -758], [-348, 64], [-996, -76], [190, -1220], [674, -126], [-1386, -712], [-1558, 206], [2266, 552], [684, 398], [-2614, -350], [2184, 516], [2748, -1172], [1456, 1068], [2850, -140], [2212, 794], [-2674, -310], [2182, -1396], [-2200, 356], [-498, -656], [-2612, 992], [-3326, 648], [-274, -186], [1334, 2216], [1918, -1732], [2780, 1008], [4142, 996], [-6432, -84], [-9784, -56], [888, -586], [-936, -1774], [-264, -2244], [6642, -976], [924, 1940], [-2358, 1484], [-1350, -3388], [-3982, 414], [-2860, 468], [-3310, -540], [-5772, 376], [-740, -332], [1134, -2944], [-3636, -4490], [1570, 5608], [-7198, -3040], [-12828, 1336], [-5362, -3264], [-1274, -1478], [-7032, 1678], [-3366, 1314], [15468, -12], [4570, 2152], [950, 5644], [6690, -2432], [6548, 620], [9632, -916], [-12144, 2824], [-5636, -6380], [490, -7742], [3116, 1304], [-388, -7508], [-5150, 4408], [-722, -3504], [4680, 6068], [-3194, -1136], [-3432, -2044], [-9264, 4714], [10652, 168], [-190, -1936], [-444, 1808], [-2272, -522], [5436, 7088], [-8718, -2452], [-4158, -2114], [854, -1300], [4028, 2796], [14662, 3912], [-12612, 2674], [7930, 1254], [-13860, -1436], [2318, -9708], [-5240, -9020], [4220, -4588], [-14884, -7058], [-16482, 6134], [3962, 10420], [4542, -2500], [-33866, -308], [1644, 5844], [-22130, -1070], [15610, 1924], [18534, -5512], [23108, -4492], [-14820, 860], [1436, 4590], [25564, -4736], [8340, 4544], [708, -12100], [-20790, -3252], [13558, -7664], [4438, -44], [20622, -4748], [28712, -4868], [10078, 11916], [-3242, 3980], [18914, -5090], [-10702, 1884], [-11588, 8716], [-11144, -11928], [-7074, 14824], [-18372, 1244], [-8420, -4050], [-4818, -392], [2766, 10978], [-26872, 7972], [-11138, 4040], [-5506, -7640], [-18904, 7372], [-14264, 12272], [-9728, 8460], [-1854, -6794], [-29868, -428], [654, -2880], [-16476, 1250], [-31782, -6112], [-5132, -1188], [9508, -1502], [10880, 5824], [-2568, -2436], [6940, -18694], [-19568, 11712], [-20596, 13844], [-7372, -14730]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_105_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-1, 4, 1]>],Snew);
    return Vf;
end function;