
// Make newform 12.8.a.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_12_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_12_8_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_12_8_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_12_a();"
function MakeCharacter_12_a()
    N := 12;
    order := 1;
    char_gens := [7, 5];
    v := [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_12_a_Hecke(Kf)
    return MakeCharacter_12_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 := 8;
    raw_aps := [[0], [-27], [-378], [-832], [-2484], [14870], [-22302], [-16300], [-115128], [157086], [-16456], [-149266], [-241110], [-443188], [922752], [-697626], [870156], [2067062], [-1680748], [-1070280], [-2403334], [2301512], [4708692], [4143690], [-1622974], [1093878], [-15633232], [-2370276], [2127062], [-21709134], [-10299832], [-39882348], [-25741206], [26240924], [69812982], [27133520], [-10985674], [-84392380], [59546664], [-21215682], [-95093244], [-131731570], [-58046976], [-69208702], [-53384778], [71913392], [205462820], [51250760], [117075780], [-195800386], [-130949190], [172545552], [105072818], [133753788], [297590274], [339540552], [-26808786], [-349721416], [-434817874], [-127669014], [278115116], [-22638474], [819496964], [924456024], [-323652982], [712782558], [-165095428], [-809760526], [-444349476], [-948805546], [438524226], [-500653224], [-1612286392], [95502782], [351477692], [-1211203584], [-14301306], [1831525094], [-314244414], [139489994], [-2317475772], [-1059349090], [-1705866912], [2713203218], [1667776832], [-796843332], [-988996878], [756748250], [-2542154274], [1357453496], [3655508724], [1225965456], [-4963269664], [258972444], [3605462276], [-2251397592], [-5030588322], [862266762], [-2600591284], [-378390682], [1359800996], [3330220446], [-6376901580], [2874924954], [-226451860], [603279746], [-67990212], [-5801749038], [5055194664], [-5970609622], [4037118824], [-833837554], [8212701402], [5437517756], [6207966512], [-331503246], [-637326652], [-7368211656], [9875028222], [-12176678268], [-11103238402], [-3496625950], [6407435718], [14821699788], [1912256468], [-11565009954], [5174664926], [-9979145568], [-5251051504], [12121110662], [-18585976300], [-14514706872], [-4069906648], [5688347822], [-12639217158], [-14052996862], [17452108854], [15718025396], [-18076565394], [21474488394], [20550834380], [-5113034874], [-1964546224], [-8459612244], [3791631878], [-5752045224], [-15460436866], [-29190764934], [15185040860], [11933542944], [22554358022], [-6336641070], [-9725272876], [-33635283912], [-12468142516], [18936300528], [-12497107408], [-7354274958], [13619907674], [10293257790], [-23914232316], [28325173338], [-36498238000], [-28808671572], [-49380579774], [51139901016], [-57478408552], [19812669902]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_12_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_12_8_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_12_8_a_a(:prec:=1)
    chi := MakeCharacter_12_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_12_8_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_12_8_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_12_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![378, 1]>],Snew);
    return Vf;
end function;