
// Make newform 720.4.a.v in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_720_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_720_4_a_v();".
// 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_720_4_a_v();".
// 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_720_a();"
function MakeCharacter_720_a()
    N := 720;
    order := 1;
    char_gens := [271, 181, 641, 577];
    v := [1, 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_720_a_Hecke(Kf)
    return MakeCharacter_720_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 := 4;
    raw_aps := [[0], [0], [5], [-4], [72], [-6], [-38], [-52], [152], [78], [-120], [-150], [-362], [484], [280], [670], [696], [222], [4], [96], [178], [632], [-612], [-994], [1634], [-890], [524], [932], [446], [786], [-716], [-808], [1770], [924], [3198], [3384], [-3302], [-2252], [-184], [2646], [-608], [2246], [-3848], [2058], [3838], [1992], [-4764], [-4092], [468], [-5586], [1058], [6840], [-6430], [-6352], [-1422], [7224], [-3186], [256], [-5942], [-3202], [-3940], [-1826], [-6580], [-5728], [-1742], [-8746], [2564], [-4166], [-9444], [-9218], [4698], [-6056], [8228], [5954], [-5284], [9832], [222], [12098], [5958], [1930], [4744], [1614], [9296], [-3494], [12584], [-12852], [-14458], [-4310], [-5338], [-1156], [5948], [6888], [-2892], [4096], [-11060], [-9648], [10062], [7966], [-7668], [6590], [4700], [15766], [22788], [3358], [11444], [-10622], [-6588], [11362], [1624], [-14950], [-8244], [6698], [-22670], [10060], [-10240], [-13218], [23412], [-15264], [-1482], [-18920], [-24218], [890], [-29250], [14580], [-23668], [-32402], [-30626], [13440], [24820], [21986], [-4420], [34560], [24792], [-2166], [10622], [29826], [-6386], [-3516], [25030], [-7962], [34668], [-250], [6388], [3932], [-25906], [-9944], [-14630], [-478], [-24132], [15776], [-33542], [-22858], [-2764], [6216], [18884], [-15232], [-7744], [-22266], [16202], [53494], [-2332], [-15414], [-35012], [11360], [24586], [8832], [22912], [-10974], [3170], [-38706], [-38200], [60662], [-9800], [-26078], [-8408], [-16290], [37788], [-41970], [15492], [58742], [-68836], [-21752], [-8678], [26058], [-36888], [-27490], [-38182], [20836], [-37526], [-45672], [-7190], [-52884], [-8140], [-45786], [-39716], [70154], [-24494], [69994], [31554], [-17728], [-56074], [9160], [-59054], [-35950], [-6248], [-33882], [-3416], [-34876], [7086], [-40828], [-38038], [40910], [4508], [72940], [60544], [71722], [23444], [68230], [-77824], [-69978], [86606], [-8072], [-3714], [-39092], [91436], [3454], [-59094], [33024], [-32284], [-42600], [6842], [51796], [55456], [47326], [-110628], [57024], [-63086], [-12786], [49736], [78224], [-42060], [28180], [-105468], [84686], [-26630], [20160], [3524], [2544], [-53100], [-45008], [83554], [70974], [-60904], [-12694], [92326], [4248], [8854], [69644], [75630], [-31246], [-91420], [-58220], [-71042], [69650], [-119086], [20348], [-29146], [117190], [-23380], [-58978], [-50842], [129708], [114274], [107624], [55866], [-80292], [86604], [-45714], [22266], [-46536], [93648], [-11800], [-448], [-53138], [-135628], [41640], [26130], [-21394], [-145336], [112734], [-32570], [100572], [18738], [-45688], [-98702], [-128074], [-22888], [67278], [-45672], [109756], [117634], [56822], [-120496], [58044], [55332], [-173134], [44548], [14166], [8096], [-145006], [-43952], [-112074], [-41386], [-135700], [-43712], [-78038], [-35872], [-175416], [-56838], [-95010], [-185396], [14514], [-40482], [157212], [-135374], [-54942], [30452], [128308], [88296], [82622], [94054], [-18298], [68408], [-151508], [38492], [142900], [164070], [129546], [151834], [-140364], [-93902], [-104958], [-3386], [95176], [173686], [-166240], [136958], [-38956], [-116344], [161646], [55300], [74698], [-124002], [116212], [168326], [-50126], [141936], [-5192], [-112286], [192200], [-123070], [135270], [-78984], [-99080], [44228], [-130342], [-84570], [115228], [134858], [-6624], [-205172], [143768], [-7130], [152872], [-98654], [-19200], [-44736], [61650], [220086], [208010], [-143274], [-13718], [23868], [170250], [-50300], [-3320], [-1208], [-17430], [83396], [5920], [-229758], [150622], [41768], [76148], [98936], [5658], [102368], [48134], [-104588], [155374], [90262], [83106], [19428], [-2854], [-24042], [71504], [36514], [220998], [-28108], [-109992], [56994], [125934], [82532], [164876], [-216718], [-108626], [170016], [-187460], [218322], [-149736], [-179242], [-308894], [278568], [-61040], [32722], [-283338], [189020], [-128682], [-154636], [84216]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_720_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_720_4_a_v();".
// 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_720_4_a_v(:prec:=1)
    chi := MakeCharacter_720_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_720_4_a_v();".
// 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_720_4_a_v( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_720_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![4, 1]>,<11,R![-72, 1]>],Snew);
    return Vf;
end function;