
// Make newform 18.12.a.b in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_18_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_18_12_a_b();".
// 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_18_12_a_b();".
// 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_18_a();"
function MakeCharacter_18_a()
    N := 18;
    order := 1;
    char_gens := [11];
    v := [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_18_a_Hecke(Kf)
    return MakeCharacter_18_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 := [[-32], [0], [5280], [-49036], [414336], [-522982], [9499968], [13053944], [58755840], [-117142944], [142907156], [718521806], [-668055360], [141575864], [729235200], [4917225312], [1408015104], [-3223327018], [-2358681328], [-22245092352], [-28036594330], [-20685045676], [37818604416], [11288711808], [-115724393266], [61178322144], [176207004476], [-87778619136], [9078532058], [1716667008], [99672265244], [755891672064], [514820281152], [-217973684080], [1001961885984], [1155276731444], [-2149984970218], [2087431331960], [49782365952], [-363256473696], [-4115864560896], [-1476064510558], [1145047187712], [4611791776130], [-331732603296], [3847360645676], [-4765294960624], [-674458785652], [-9046256380800], [5212225392146], [-18717164638848], [5071316819712], [-20297071469650], [-2670975027072], [17953713422592], [6141661980672], [25664387615712], [-15314962826932], [20948143757666], [-1024747784448], [14189057224928], [-33724005477024], [-30529277777392], [-98533777562112], [10397329462490], [-1634194566816], [-88655695845664], [22585651842878], [108733850759040], [-25263460108138], [116788194238080], [62047780540416], [-136092307564588], [-141263688722914], [173435900730680], [-184328055724800], [79499862082848], [87318688569158], [-177759435141696], [-126723892860298], [417469571258496], [-66632395275886], [-185688665775360], [-331842402000286], [51146202752492], [93913533187968], [81488234244672], [-489053641592218], [-853551643220448], [423617180683868], [-643574531389824], [216255703563264], [709675692410756], [-993021630928896], [-1040653004094496], [26301829501440], [822717728781792], [-918768015125568], [-578310465610312], [573475853459018], [-720999553592920], [302539762233504], [-22584568317312], [-1791610464745536], [-2688090201660016], [-1093335636046894], [2034376637150208], [1586599047570816], [-883346957043456], [276844275123530], [1004533355344796], [175795262364494], [866386973080320], [-4481652939216160], [-1067485140115900], [984911085390144], [-70039876134232], [3095146490759424], [-4273699084845024], [-2329388338306560], [1823175122148062], [3371957784188402], [-3260340901084128], [1787822537533056], [2482461579304184], [-1809318128279328], [101905450540754], [4322472732393984], [2029587271074500], [-7801105875427414], [-959859502889200], [-1439408722632960], [8648084156725700], [-4646001696963550], [4025782572173376], [-243691176377854], [-3772349169952032], [-8964825233131576], [7857396302074656], [-9920384264477376], [-3160558609762552], [1953510792267360], [13221154783149092], [-20544005049576192], [13378549385517194], [12949600690624512], [494214240086654], [8079975480471744], [18800319121149320], [-13092849297877248], [8260792270583174], [26010928732264704], [-22937142742002952], [-24111818443881984], [-26540976674767000], [-23481714276061440], [4237632699706532], [-20734736794276416], [-7698496179338134], [37101870010441248], [-28601958648106752], [7655738941644480], [-26048374834466116], [15646524664639104], [-26255182046359872], [35202681664052736], [-40827108565867228], [-37429078848578482]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_18_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_18_12_a_b();".
// 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_18_12_a_b(:prec:=1)
    chi := MakeCharacter_18_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_18_12_a_b();".
// 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_18_12_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_18_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([<5,R![-5280, 1]>],Snew);
    return Vf;
end function;