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


// To make the character of type GrpDrchElt, type "MakeCharacter_80_a();"
function MakeCharacter_80_a()
    N := 80;
    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_80_a_Hecke(Kf)
    return MakeCharacter_80_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], [-24], [25], [172], [-132], [-946], [-222], [-500], [-3564], [2190], [-2312], [-11242], [1242], [-20624], [-6588], [-21066], [-7980], [16622], [-1808], [24528], [20474], [46240], [51576], [-110310], [-78382], [141942], [436], [-232968], [-174850], [182994], [122452], [241908], [277098], [193540], [140550], [-433952], [-555922], [66616], [205692], [433854], [489180], [719462], [185928], [-591406], [449478], [-157160], [-253052], [-1073444], [626832], [-116650], [-743046], [-978720], [-1132798], [-905652], [1939938], [805476], [-858690], [383608], [2010758], [202602], [221536], [-322506], [-1443008], [-171312], [-1026886], [752958], [-1994132], [-987022], [-2206008], [2741870], [-2389566], [279480], [2476372], [2745254], [1189060], [-3257604], [1983510], [4974158], [-1342638], [-1094230], [954060], [-1593898], [2646648], [3723554], [2583400], [-7562064], [4307730], [-2243542], [1656702], [2891596], [6526992], [5962320], [-2991908], [1204188], [-9205460], [3359556], [-2537010], [-9315798], [5028016], [134222], [-605648], [-7062402], [10302936], [1047690], [-14076452], [1626818], [-6960888], [-11363886], [-14868840], [-1236118], [12449812], [-8734906], [12549498], [14665780], [196288], [-11159598], [2251576], [-8053188], [-416466], [-13172100], [-1694938], [-8916046], [-14289402], [5333136], [-698252], [17987982], [-13946410], [-6222720], [7767292], [24208334], [-12684980], [-19763244], [9017608], [-1125562], [22574682], [-632350], [-12586746], [-21579248], [-30976002], [4249290], [-3423332], [31057062], [31190356], [8284872], [-18168850], [10274280], [6285974], [15405018], [-14352620], [-13327764], [32484638], [15460002], [1694776], [2872572], [-39542168], [-11317752], [-8513480], [-7545870], [-18450022], [6750462], [-6456768], [-39664806], [34301452], [5771148], [7087458], [-45936204], [45029848], [-23736442]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_80_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_80_6_a_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_80_6_a_a(:prec:=1)
    chi := MakeCharacter_80_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_80_6_a_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_80_6_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_80_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![24, 1]>],Snew);
    return Vf;
end function;