
// Make newform 105.4.a.c in Magma, downloaded from the LMFDB on 23 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_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_105_4_a_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 := [-4, -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 := [[-3, -1], [-3, 0], [-5, 0], [-7, 0], [-8, -10], [18, -22], [-6, 28], [100, -26], [64, 56], [26, -84], [156, 18], [-78, 24], [30, 140], [216, -68], [92, 108], [-90, 214], [164, 36], [522, -252], [-408, 28], [392, -330], [478, -178], [160, 88], [700, -264], [382, -728], [-322, 146], [210, 476], [1008, -224], [820, -60], [-554, -252], [-1142, 414], [-296, -16], [636, 896], [-750, -326], [-556, -290], [-2782, 304], [1952, 308], [514, 1222], [-1232, 1564], [2948, -700], [-1246, -836], [-1240, 1110], [-214, 296], [1144, 722], [2170, -336], [206, -526], [2652, -110], [2940, -1416], [304, 268], [3508, -792], [-806, 2932], [-1662, -34], [-4328, -778], [5706, -916], [-644, -268], [-3686, 472], [2112, -2836], [970, -828], [1700, 54], [3130, -1000], [-2766, 3428], [-748, 1848], [-5902, 1332], [-4476, 1692], [-4896, 2728], [-1738, 2834], [7406, -802], [-1740, -1032], [2138, -3616], [2516, -784], [5858, 116], [-1870, -1780], [5480, 1794], [-136, 372], [-2662, -420], [5260, -1292], [6260, 196], [194, -1072], [-2894, -470], [818, -728], [11722, -2032], [12484, -80], [-1042, 4368], [11552, -1754], [-6098, 4906], [-1980, 2634], [9252, -2688], [4970, -1232], [-4206, 4876], [5770, -1712], [-5792, -2896], [-11972, 2888], [-5048, -2336], [-10768, 2176], [7472, 1750], [-6092, 3380], [13716, 2884], [-10198, 3676], [9310, 1836], [-3292, -2740], [-14530, -1184], [-16744, -1228], [3310, 886], [-20836, -1416], [-15734, 2792], [9476, 3164], [2942, -4542], [-5596, 3584], [4346, -10228], [-12944, 8602], [-1646, -6988], [1088, -6564], [10834, -9528], [6994, 4054], [-17444, -2994], [-13376, 1000], [578, -6612], [-1396, 3988], [-1892, -4660], [9270, 5746], [7592, -1750], [11250, 4468], [-5950, 10940], [2234, -1844], [3852, 1608], [-19492, -1718], [12986, -308], [18662, -10792], [640, -2788], [-11800, -4852], [25730, 4290], [-8428, 12388], [19304, -4852], [-21400, 8816], [18618, 2580], [-4618, -2456], [-15118, 916], [4402, -860], [-29004, 3676], [21730, -12], [-32238, 6476], [-24348, -3482], [6050, -348], [26600, -104], [-7508, 10572], [-14006, 9232], [-7648, -9148], [8890, 6594], [22834, -3272], [1540, -254], [20640, 8128], [32034, -4240], [-7986, 6976], [-38952, -2468], [-30084, 11996], [-9440, -2196], [26920, 730], [7456, -12344], [-32226, -1128], [-23602, 3158], [1226, -21280], [-19484, -14064], [15266, -16922], [8616, 18736], [1548, -3252], [8778, -21250], [-14252, 1620], [40888, -6040], [26282, 8538]];
    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_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_105_4_a_c(: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_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_105_4_a_c( : 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, 7, 1]>],Snew);
    return Vf;
end function;