
// Make newform 10.5.c.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_10_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_10_c_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_10_5_c_a();".
// 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_10_5_c_a();".
// 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, 0, 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_10_c();"
function MakeCharacter_10_c()
    N := 10;
    order := 4;
    char_gens := [7];
    v := [3];
    // 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_10_c_Hecke();"
function MakeCharacter_10_c_Hecke(Kf)
    N := 10;
    order := 4;
    char_gens := [7];
    char_values := [[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 := 5;
    raw_aps := [[-2, -2], [9, -9], [-15, 20], [29, 29], [-118, 0], [69, -69], [-271, -271], [0, 280], [269, -269], [0, 680], [202, 0], [-651, -651], [1682, 0], [1089, -1089], [1269, 1269], [-611, 611], [0, 1160], [-5598, 0], [-751, -751], [6442, 0], [-2951, 2951], [0, 10560], [-6231, 6231], [0, -14480], [-7311, -7311], [-878, 0], [10429, -10429], [-4711, -4711], [0, 22040], [-2111, 2111], [5909, 5909], [-6358, 0], [20409, 20409], [0, -9400], [0, 13800], [-18998, 0], [-16371, -16371], [20009, -20009], [1549, 1549], [2789, -2789], [0, -2600], [-44398, 0], [-14678, 0], [42849, -42849], [-10971, -10971], [0, -38160], [72842, 0], [-30891, 30891], [-54911, -54911], [0, -50280], [-2391, 2391], [0, -17760], [-28238, 0], [121002, 0], [-72431, -72431], [-14771, 14771], [0, 89720], [68202, 0], [18549, 18549], [2322, 0], [-91711, 91711], [-4851, 4851], [42849, 42849], [-72278, 0], [18249, -18249], [25149, 25149], [-54038, 0], [8529, 8529], [-56551, -56551], [0, -22520], [-44511, 44511], [0, -9680], [-14971, -14971], [-13811, 13811], [0, 251080], [-86091, 86091], [0, -75000], [29149, 29149], [-45918, 0], [0, 78720], [0, -14760], [221282, 0], [212522, 0], [145409, -145409], [0, -299440], [240609, -240609], [0, -82480], [-188151, -188151], [-326158, 0], [-218731, 218731], [59249, 59249], [0, 273440], [-123651, -123651], [198442, 0], [0, 269240], [109869, -109869], [0, -211000], [297282, 0], [-25071, 25071], [-142478, 0], [291009, 291009], [-83091, -83091], [43449, -43449], [0, -270560], [57482, 0], [195889, 195889], [-404631, -404631], [-210991, 210991], [0, 300560], [367442, 0], [146469, 146469], [160989, -160989], [320409, 320409], [0, 341160], [-390998, 0], [-585038, 0], [-31911, 31911], [-280931, -280931], [523989, -523989], [0, -404360], [-5278, 0], [-332111, 332111], [578309, 578309], [-349311, 349311], [282762, 0], [270242, 0], [0, -297800], [0, 913760], [-417651, -417651], [394549, -394549], [0, 109880], [-466451, 466451], [1010922, 0], [313269, 313269], [142082, 0], [0, 13280], [-782211, 782211], [201409, 201409], [-36291, -36291], [0, -71600], [-103318, 0], [-157438, 0], [791309, -791309], [-889671, -889671], [0, -618280], [0, -821360], [-698291, 698291], [144489, 144489], [0, -943480], [438149, -438149], [281469, 281469], [876722, 0], [-327431, 327431], [-477171, -477171], [1112089, 1112089], [-883958, 0], [0, 1240400], [0, 1227440], [-1070471, -1070471], [558642, 0], [-191711, -191711], [-630231, 630231], [-345491, -345491], [1083082, 0], [-146751, -146751], [466909, -466909], [-901238, 0], [152149, 152149]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_10_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_10_5_c_a();".
// 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_10_5_c_a(:prec:=2)
    chi := MakeCharacter_10_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 5));
    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_10_5_c_a();".
// 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_10_5_c_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_10_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<3,R![162, -18, 1]>],Snew);
    return Vf;
end function;