
// Make newform 40.6.a.c in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_40_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_40_6_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_40_6_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_40_a();"
function MakeCharacter_40_a()
    N := 40;
    order := 1;
    char_gens := [31, 21, 17];
    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_40_a_Hecke(Kf)
    return MakeCharacter_40_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 := 6;
    raw_aps := [[0], [-2], [-25], [-62], [-144], [-654], [-1190], [556], [2182], [-1578], [9660], [-3534], [7462], [-7114], [-28294], [-13046], [-37092], [39570], [-56734], [45588], [11842], [94216], [-31482], [-94054], [23714], [-129674], [136846], [-193190], [-120046], [-152646], [107906], [-233072], [356082], [312204], [27498], [-136908], [406714], [-13642], [-203438], [127242], [-94684], [-517018], [-412300], [-771654], [-190238], [132072], [928704], [421494], [991962], [-266946], [960314], [-492696], [56078], [1967920], [-971910], [-154770], [1023714], [-1147756], [-2496758], [1695398], [-2123954], [992722], [487522], [-444116], [47242], [694058], [82168], [-727934], [2022978], [4408582], [1069650], [-32968], [3640810], [-3173110], [1604980], [1989254], [-5164950], [937586], [-5636574], [4061374], [-976108], [-1627062], [-4279004], [-3201950], [5092456], [-5435514], [-2990074], [8017594], [2584622], [6142606], [-1592702], [863592], [-8207142], [8933936], [1119596], [3681774], [-6734826], [441370], [-11729970], [744158], [-3248006], [-9944462], [3893742], [-11195138], [-844040], [-5133782], [9761562], [966226], [-7900000], [10312598], [-9707670], [-11056750], [8311738], [11545124], [-8202620], [-5396946], [-13389618], [6482538], [14490714], [6590804], [-3252326], [3866546], [1238562], [-13137642], [-12384104], [-9789518], [12225742], [-13505304], [11727122], [-11651206], [-12680828], [-197370], [-13328196], [-3861222], [-8317558], [27635778], [17884210], [21569082], [-10306022], [372378], [-19479456], [-469318], [17862182], [9425602], [-14862190], [-4711696], [16268482], [-29266702], [-33106164], [-15805178], [-42683414], [-3573970], [-16847090], [-8367918], [-25723022], [34210828], [2440336], [13436118], [7965290], [9080254], [-32176910], [5338074], [37152170], [-10986536], [-26505422], [-47572626], [32214908], [38707242]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_40_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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