
// Make newform 3.18.a.a in Magma, downloaded from the LMFDB on 24 June 2026.
// To make the character of type GrpDrchElt, type "MakeCharacter_3_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_3_18_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_3_18_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_3_a();"
function MakeCharacter_3_a()
    N := 3;
    order := 1;
    char_gens := [2];
    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_3_a_Hecke( : Kf := Kf)
    return MakeCharacter_3_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]];
    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 := 18;
    raw_aps := [[204], [-6561], [-163554], [-20846560], [817372356], [299589758], [-44775606078], [78748651964], [-704672009160], [-163793785242], [1049860831400], [-19805735857210], [14660035932090], [116038864682564], [-176606594594112], [152863496635230], [-262797291296124], [-1358552281482562], [444863620615292], [-4003270764790968], [924832535317130], [14747307742797080], [26422963268810172], [-38883748080645126], [-25374394856250238], [-74421564908619954], [206557922943767024], [-223373368675933452], [-266379304714075906], [-67515704202938478], [-211177462460441128], [1022021861572822908], [-1707781957964514246], [1053125016780983156], [-1419479306521068498], [-864829675310196208], [4232857445406150974], [-4499455532334671284], [-2891312782791032040], [-5475509214196050138], [-24605858219218326900], [20952841351383653510], [-6037986899066582784], [13260469717966295042], [-26279116477095299634], [-20677413638054754448], [103174950609075180140], [-72521942489423724712], [9297662158936815180], [98759459723101521686], [35328361496526635274], [-46855681133071575696], [-255943599060295956334], [206943519310626427092], [467477313658400521218], [126367052036080792056], [-118588851269902775466], [433191330459998702696], [426563234552020726886], [-997106511862312556646], [-485988385982618346652], [1481935928740669598286], [3296978568996845708], [-2791719230696261032344], [1831260515031346454906], [1744069766593882048998], [2035453970667801361172], [-3474848182639799279854], [-1599580896477732826188], [1654977413075612696798], [8345678711667798798978], [-348877492132908239064], [-6367263662274085306984], [-7788647409311196040906], [-3177686764250450971180], [6697970058736566782976], [-11070021367051768391394], [-14391476332663303108402], [8632995092120504159778], [1625080537290617100026], [19376141134134058489164], [-27378331313220457781386], [-35408329478230462492320], [12520004702932987970546], [-30589578605826940352800], [-14926688848573368544812], [4931731301855187436722], [66784703536659344977130], [-35859312431471576577018], [-21795374216591268184216], [-10186196761389776884452], [67489901500217639538480], [21507536648733038250752], [13683271543943792674548], [-13634755876823187883636], [61780758384976946439768], [-91955263557445406741274], [-176715565502045448239910], [-239600760656205333615676], [16176551116729815527150], [246036971325391544561900], [-86141357431268060575098], [278425827602627954423772], [-452461488443358438796086], [76395687471041755224356], [240302408327871425280002], [-119662844821732427772972], [-298533885682185185268366], [-269094021699661380167208], [30492201973656202843226], [-558818288821187742267208], [-634889959878904768587418], [236657938342128342686634], [323770414202051421919316], [-143355421806326602932688], [466874749916709323962290], [-489782421308380228990516], [-1094241747549594067084728], [518955202002457794510246], [401366970745387425205260], [1726548978723714009807542], [-961197338707666904566750], [-620817377265090200793954], [-270132206989193123931324], [-516536893308302124419524], [595182040750749110119398], [1041411567959391869263094], [1572230980486265979602592], [3531429975477450235167824], [2940364418779261040868494], [901115564635577309533436], [-3555722116085987904926856], [1884755976943948019220536], [-1470311405007636730880986], [-1011461633776204811712726], [-4176737297570600156663038], [-3394515670674966710443314], [-7126538140577047208646052], [5988121391184358277218998], [-2160520286360767973821350], [-722228577560502623164348], [10714511811231444701436414], [-8717051185989327698609008], [-3385642006215906483484188], [1770791343059942542389710], [-616728136593721310028120], [8042410581934283530316726], [171826657424672952206058], [5306129899003254383936180], [-15697440142866311428436256], [-5172448938650564360203090], [18102047615007965313770034], [-12210643343656764275719876], [-15917501785737235612743672], [-5230096775460034864433212], [14692620852521133137000592], [3025320187266337755185840], [-33184898137353847102433166], [-4730588720925890491566934], [-29305880047914496434844890], [21221995537850520786241164], [36284612247043229052430218], [22750776172707338347293392], [25538479598196440848240740], [32519538652044545480599842], [-10030141936642171116592920], [-32176497689623780575815608], [24821312325376839839763494]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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