
// Make newform 1.12.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1_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_1_12_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_1_12_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_1_a();"
function MakeCharacter_1_a()
    N := 1;
    order := 1;
    char_gens := [];
    v := [];
    // 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_1_a_Hecke(Kf)
    return MakeCharacter_1_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 := 12;
    raw_aps := [[-24], [252], [4830], [-16744], [534612], [-577738], [-6905934], [10661420], [18643272], [128406630], [-52843168], [-182213314], [308120442], [-17125708], [2687348496], [-1596055698], [-5189203740], [6956478662], [-15481826884], [9791485272], [1463791322], [38116845680], [-29335099668], [-24992917110], [75013568546], [81742959102], [-225755128648], [90241258356], [73482676310], [-85146862638], [-262717201024], [631528759932], [-297198746214], [596793577940], [-1115433620850], [-824447297848], [1315116754406], [-357832759588], [2754833892216], [-950387449578], [1681384224780], [-996774496018], [2762403350592], [5442387685442], [-2876091504354], [728391402200], [-6793168439188], [7334863021472], [-1359839565924], [-11824411223170], [-17563353448518], [-7139577462960], [-231306909358], [12983053545252], [23961192565506], [-24273728464488], [25837706543670], [-3767932360528], [-16418932005874], [21035722907082], [16713176326532], [-23926858987458], [15311092828556], [49875160575912], [-99480832756438], [83369248359366], [-63584021925868], [121001428335986], [-155661561078204], [-25643022194650], [24909815245602], [157584150853560], [-177901220129584], [-55161734023378], [146463116322980], [231449571733632], [-149871571611810], [208110680273846], [-133407937691598], [-206167580638390], [73403515193820], [171111932338622], [-71775829446768], [99881248225682], [-29031220908760], [328369848718692], [-612368143631550], [303483032911706], [-729307946668938], [122188164073712], [-617380683662484], [1050837984850080], [-219909971761864], [-483863128068108], [-108877719272500], [506588355787752], [85753393288710], [927574652509722], [-21818651341228], [-1695266465052058], [752144751323996], [187489032934806], [244971285916812], [1352425875836970], [1432228477996772], [-877659324192574], [-2434250501782764], [-303318212278158], [-1701984261329400], [2339216023326602], [-2496072486898144], [2473009012516862], [24336777156666], [4225450805148020], [-4263261111420568], [1008295928583042], [303982416160892], [3435828424131096], [-1185387156769098], [-2265102374509140], [-5330121520762738], [4741198635421922], [-1413071638641474], [-3031157627077068], [-2747313442193908], [5727465442629702], [698326457818910], [9709787093595120], [2464686886685576], [7912852398754982], [-8406936179151460], [1362869794471992], [6817220120892752], [-667048975014994], [-7744079729695638], [2524111226919170], [-11145268112466978], [13227104255899436], [2302477758246246], [5604717670694010], [-5085164185024588], [279110773799022], [-13526517861603928], [272543929352676], [18045917610367430], [-7961834707368360], [-14982644001390898], [-22256130175851894], [5442366024533060], [10810987426382112], [-28102437759471274], [4222089494633682], [516091995437132], [5719060466578536], [-84377784300844], [-11009058285505488], [-4863513801242680], [3575343976258530], [38637331724206586], [-34899727878407658], [-28512291118415604], [40033434235820202], [18495304308397016], [-21487665696030828], [-8738799717564174], [11892426157508232], [23440857788319392], [-21400415987399554]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1_12_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_1_12_a_a(:prec:=1)
    chi := MakeCharacter_1_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 12));
    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_1_12_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_1_12_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;