
// Make newform 441.4.a.m in Magma, downloaded from the LMFDB on 06 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_441_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_441_4_a_m();".
// 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_441_4_a_m();".
// 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_441_a();"
function MakeCharacter_441_a()
    N := 441;
    order := 1;
    char_gens := [344, 199];
    v := [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_441_a_Hecke(Kf)
    return MakeCharacter_441_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 := [[5], [0], [0], [0], [68], [0], [0], [0], [40], [166], [0], [450], [0], [-180], [0], [-590], [0], [0], [-740], [-688], [0], [-1384], [0], [0], [0], [0], [0], [1580], [-54], [670], [-2000], [0], [-3110], [0], [-814], [-2952], [0], [1780], [0], [0], [2084], [0], [4072], [-4590], [2210], [0], [5868], [0], [0], [0], [4730], [7376], [0], [0], [0], [-7520], [0], [0], [7310], [-4342], [0], [0], [0], [0], [0], [6970], [10908], [-3330], [4100], [0], [0], [8104], [0], [-13970], [11916], [0], [10526], [0], [-1598], [0], [0], [15262], [8608], [0], [0], [-18580], [2686], [8010], [0], [-8440], [0], [0], [21240], [-20372], [-7236], [0], [0], [0], [0], [15878], [12980], [-20470], [0], [26906], [-6788], [0], [0], [0], [24736], [0], [0], [15010], [-30550], [0], [-26192], [-8878], [0], [0], [-27050], [1804], [0], [-33570], [0], [34060], [0], [4198], [12546], [0], [0], [0], [-25324], [-25160], [-2448], [-34830], [0], [0], [0], [0], [0], [-37354], [0], [43538], [-46240], [23980], [0], [0], [0], [0], [0], [20200], [-6550], [0], [30060], [0], [52740], [39632], [21744], [0], [0], [0], [-48820], [-29290], [52040], [0], [37490], [0], [57528], [0], [-6046], [0], [-18356], [0], [-64192], [29990], [0], [0], [-53548], [-66082], [0], [0], [53440], [0], [4930], [0], [73120], [0], [-66330], [0], [-63866], [0], [0], [77180], [-79812], [0], [14180], [0], [-49698], [-12710], [0], [0], [72266], [0], [0], [0], [0], [0], [0], [-68980], [29174], [0], [92990], [0], [91800], [0], [0], [0], [81720], [0], [320], [8230], [24642], [0], [-23746], [94320], [0], [74034], [0], [105376], [0], [108148], [-72630], [0], [55512], [-76442], [0], [0], [0], [87890], [8636], [0], [-96980], [0], [0], [112086], [0], [0], [0], [0], [-18884], [-125960], [-116730], [0], [126320], [0], [0], [69844], [45362], [0], [0], [0], [-112880], [134860], [0], [0], [0], [0], [130954], [0], [37060], [-142690], [0], [-98460], [0], [142704], [0], [0], [31540], [66726], [32202], [0], [0], [-143008], [0], [0], [0], [97088], [-95310], [-151790], [0], [0], [165002], [0], [115610], [0], [-75850], [0], [0], [0], [0], [0], [0], [174550], [153736], [-94180], [19532], [-154530], [24260], [0], [48424], [-174690], [0], [54094], [-172958], [-158940], [96080], [0], [0], [-124168], [0], [27634], [0], [-152010], [0], [-89640], [-190570], [0], [177284], [0], [161600], [189950], [-45162], [199750], [0], [0], [210548], [0], [-13606], [0], [0], [0], [67790], [-43430], [0], [-219168], [-195610], [-162724], [0], [-206820], [0], [0], [0], [-122470], [-34358], [0], [-147026], [0], [0], [0], [237550], [-226760], [172510], [0], [-75280], [-100004], [0], [-61290], [0], [20680], [250938], [30308], [0], [60520], [21586], [0], [221510], [0], [120272], [0], [0], [0], [0], [263030], [-122400], [81310], [0], [0], [178488], [0], [38180], [0], [110126], [0], [-280436], [0], [279032], [231030], [0], [0], [242692], [203278], [0], [-273730], [-261560], [0], [0], [0], [-288650], [16798], [0], [0], [0], [-288850], [-147100], [-261252], [-101050], [0], [226616], [0], [0], [0], [10246], [0], [-287240], [0], [0], [0], [39100], [-214006], [0], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_441_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_441_4_a_m();".
// 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_441_4_a_m(:prec:=1)
    chi := MakeCharacter_441_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_441_4_a_m();".
// 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_441_4_a_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_441_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-5, 1]>,<5,R![0, 1]>,<13,R![0, 1]>],Snew);
    return Vf;
end function;