
// Make newform 1800.2.a.r in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1800_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_1800_2_a_r();".
// 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_1800_2_a_r();".
// 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_1800_a();"
function MakeCharacter_1800_a()
    N := 1800;
    order := 1;
    char_gens := [1351, 901, 1001, 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_1800_a_Hecke(Kf)
    return MakeCharacter_1800_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 := 2;
    raw_aps := [[0], [0], [0], [2], [-1], [4], [-5], [1], [2], [8], [10], [-6], [3], [4], [-4], [-6], [-8], [10], [-1], [12], [3], [6], [13], [9], [-14], [-6], [4], [15], [14], [9], [-6], [12], [-13], [9], [-8], [2], [-14], [7], [12], [-8], [19], [2], [10], [-11], [2], [-28], [-1], [-4], [12], [4], [-6], [-24], [-7], [-15], [-30], [-30], [16], [-6], [8], [2], [-1], [2], [13], [-14], [-22], [-12], [-27], [19], [-17], [-18], [10], [18], [20], [10], [-5], [-6], [-30], [4], [-21], [-11], [3], [-12], [34], [5], [16], [21], [39], [1], [-12], [-36], [28], [-22], [-38], [-36], [-4], [-36], [-34], [35], [-35], [-32], [-19], [28], [32], [-17], [-20], [-13], [-3], [-39], [-30], [43], [0], [30], [18], [44], [10], [6], [-12], [-12], [34], [21], [24], [-6], [32], [3], [-19], [28], [-28], [14], [8], [36], [12], [-30], [24], [32], [3], [13], [0], [-52], [-42], [-18], [44], [38], [28], [-3], [-20], [16], [-22], [3], [41], [-48], [4], [-30], [5], [-28], [12], [-28], [-34], [2], [-5], [-4], [-40], [39], [0], [31], [-17], [-6], [38], [-12], [29], [16], [-35], [10], [34], [35], [-16], [-39], [7], [58], [-56], [34], [-28], [27], [-58], [41], [4], [-20], [26], [41], [-7], [-40], [47], [9], [-1], [-14], [23], [22], [-45], [-54], [-11], [10], [-18], [-44], [32], [3], [36], [-26], [58], [20], [-30], [8], [-62], [-2], [56], [3], [-10], [33], [58], [-5], [66], [-10], [-34], [-20], [-25], [-66], [27], [-50], [1], [-56], [44], [-35], [34], [-45], [50], [-7], [-24], [18], [45], [0], [-55], [-12], [-37], [19], [-14], [4], [13], [16], [22], [-68], [-5], [16], [-48], [5], [-30], [-37], [-32], [-75], [-40], [75], [6], [69], [46], [73], [-48], [-28], [-53], [37], [50], [-30], [-51], [-34], [-4], [43], [-33], [-16], [2], [-4], [-13], [-40], [-34], [3], [60], [30], [-8], [4], [55], [8], [62], [24], [0], [30], [-34], [12], [-51], [81], [28], [18], [2], [48], [-4], [3], [-39], [-54], [-16], [-65], [-57], [73], [12], [46], [54], [38], [26], [-66], [-9], [-8], [4], [-26], [-49], [-72], [-65], [29], [44], [-67], [-36], [-62], [38], [18], [-65], [20], [70], [36], [44], [-82], [-26], [-1], [-11], [-24], [80], [-66], [55], [-30], [8], [15], [2], [20], [-70], [-52], [28], [-89], [72], [42], [-29], [18], [-70], [68], [-46], [53], [36], [59], [-46], [-44], [26], [53], [-14], [12], [-21], [19], [96], [56], [5], [12], [-55], [4], [36], [48], [-34], [39], [-74], [-37], [-57], [98], [70], [95], [-48], [-87], [48], [62], [-32], [0], [96], [14], [-5], [-84], [-39], [19], [-50], [77], [68], [-33], [89], [30], [96], [75], [52], [39], [-96], [-48], [-62], [6], [4], [9], [-65], [-48], [-59], [-20], [31], [-30], [26], [10], [-81], [42], [-24], [22], [-58], [77], [69], [24], [-24], [-3], [53], [-38]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1800_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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