
// Make newform 2240.4.a.f in Magma, downloaded from the LMFDB on 27 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2240_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_2240_4_a_f();".
// 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_2240_4_a_f();".
// 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_2240_a();"
function MakeCharacter_2240_a()
    N := 2240;
    order := 1;
    char_gens := [1471, 1541, 897, 1921];
    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_2240_a_Hecke(Kf)
    return MakeCharacter_2240_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], [-7], [5], [7], [33], [43], [111], [70], [42], [225], [-88], [34], [432], [178], [411], [708], [-480], [-812], [-596], [432], [-358], [425], [-972], [960], [-709], [438], [-1033], [-906], [1915], [-558], [-1744], [318], [2496], [-1370], [-2490], [137], [3274], [-902], [-3969], [1713], [3660], [1708], [-2073], [-3688], [-3276], [2360], [-2657], [47], [-6051], [-3080], [-3288], [-1755], [2], [5418], [-2154], [3882], [-570], [3332], [394], [3267], [-677], [8613], [3499], [5682], [4097], [4674], [-10172], [-9394], [-4566], [6730], [3027], [2760], [6131], [-9632], [9700], [1212], [-4305], [1609], [-13503], [470], [11700], [6163], [5187], [2882], [9830], [5178], [-4545], [-484], [1368], [14852], [-7521], [8850], [-14614], [-15237], [9565], [-7263], [7230], [4962], [-13772], [3193], [-22916], [-15096], [-7932], [-12990], [-20252], [-24379], [2304], [6837], [-8925], [20342], [-27439], [-6842], [-10494], [-22970], [6347], [13602], [-5807], [-19104], [-3822], [-30555], [-18632], [-15568], [-31821], [25188], [2428], [-11187], [-22655], [-9750], [-17584], [-20657], [15505], [-1548], [8417], [-4376], [-16878], [830], [15603], [12589], [5769], [3945], [1618], [-23217], [15032], [12654], [3400], [16830], [-25022], [-2094], [4300], [-7428], [-33446], [20592], [47248], [-16824], [-8066], [-21168], [10685], [5820], [-1429], [-16932], [45804], [-41508], [39566], [40188], [-17214], [12657], [38072], [35269], [-36835], [-25242], [-54840], [50398], [26367], [26282], [35930], [36900], [36133], [42783], [7292], [-28100], [-7864], [53568], [-25562], [15846], [26472], [22290], [17944], [1033], [-24055], [42852], [38162], [59058], [-22637], [11028], [72204], [-19203], [19127], [59128], [-49329], [-75873], [-47190], [-18538], [-36146], [-67930], [-3510], [-16851], [33950], [-17232], [-10170], [-35012], [11936], [-14922], [-38968], [-37491], [65910], [-66688], [-11374], [67872], [-32214], [-25902], [37798], [-12490], [9285], [-96298], [-8811], [73690], [50307], [49755], [65711], [-71412], [35368], [51910], [45632], [75657], [-40907], [72111], [21260], [-77352], [20115], [29682], [60648], [-66512], [-32173], [82690], [-17523], [-50610], [-75184], [44898], [-22025], [56502], [61234], [-82398], [-82644], [25760], [12858], [-51675], [22138], [59524], [15219], [22421], [-24298], [20034], [-56180], [-22817], [-74454], [-36650], [-134865], [82602], [-11972], [16458], [-60212], [-88976], [-64573], [5780], [46046], [55307], [-90996], [-80570], [132617], [-20382], [87807], [-152143], [86976], [50218], [-10091], [-15213], [29522], [71874], [117020], [-96660], [-62697], [141639], [-48828], [-73512], [140218], [103740], [49502], [-96237], [660], [61009], [1127], [-131766], [-95785], [89568], [164563], [59366], [1914], [157840], [-15240], [78568], [-108768], [-113475], [149982], [-151922], [9126], [154280], [-47430], [-122613], [-34153], [170325], [54718], [-68524], [-94842], [-30688], [-45618], [44732], [105955], [156868], [-86844], [-113832], [56338], [98664], [45290], [-4917], [176668], [-6831], [-110405], [141522], [-59398], [-106189], [-57602], [182016], [-52110], [-13633], [-10842], [-6300], [217258], [-25976], [169722], [81489], [-145712], [-142684], [13383], [-208573], [115855], [-138618], [-141870], [-25302], [-55794], [83862], [32164], [205872], [103836], [-203595], [75004], [-143098], [-17271], [205202], [37397], [-172947], [53260], [95712], [103725], [-114118], [98854], [78000], [10647], [185717], [-212970], [18641], [18618], [96822], [-161404], [-234474], [248740], [219567], [-102733], [-11981], [26188], [-154179], [62345], [-127347], [138975], [-115796], [-210333], [16112], [254000], [123300], [-176447], [-41322], [-160400], [56652], [-56734], [214611], [-135660], [-117898], [25054], [-171873], [210868], [-225060], [183467], [-212046], [192618], [-277292], [-170494], [-209112], [-170085], [-14524], [37746], [147327], [272715], [-182846], [135486], [84780], [122177], [101409], [5088], [-163710], [-281972], [-225060]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2240_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2240_4_a_f();".
// 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_2240_4_a_f(:prec:=1)
    chi := MakeCharacter_2240_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_2240_4_a_f();".
// 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_2240_4_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![7, 1]>,<11,R![-33, 1]>],Snew);
    return Vf;
end function;