
// Make newform 1805.2.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1805_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_1805_2_a_b();".
// 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_1805_2_a_b();".
// 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_1805_a();"
function MakeCharacter_1805_a()
    N := 1805;
    order := 1;
    char_gens := [362, 1446];
    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_1805_a_Hecke(Kf)
    return MakeCharacter_1805_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], [2], [-1], [-4], [3], [-2], [6], [0], [0], [3], [7], [-8], [6], [-4], [6], [6], [15], [5], [-2], [3], [8], [-5], [12], [15], [-8], [15], [16], [0], [-11], [-6], [-2], [12], [-12], [-16], [3], [-17], [2], [-10], [0], [0], [9], [-2], [-15], [16], [-18], [-19], [7], [4], [24], [-7], [6], [-3], [-5], [-15], [18], [-18], [21], [11], [8], [-6], [14], [-24], [34], [24], [-10], [-24], [4], [16], [12], [14], [12], [24], [-4], [4], [37], [-30], [15], [8], [33], [-23], [-3], [19], [15], [-8], [13], [-12], [9], [-22], [-9], [-16], [0], [15], [-2], [-15], [20], [-42], [18], [45], [-26], [-37], [4], [12], [-6], [-39], [-7], [-16], [-36], [12], [0], [31], [-2], [2], [36], [-4], [-19], [-9], [-34], [18], [24], [12], [-5], [4], [-48], [18], [-37], [6], [-31], [15], [14], [32], [35], [24], [7], [-10], [6], [29], [-30], [34], [30], [-27], [43], [-3], [-22], [42], [-26], [0], [14], [-36], [-7], [-18], [52], [9], [8], [-36], [-8], [-21], [8], [27], [-46], [9], [-18], [12], [-16], [12], [12], [36], [-8], [-22], [7], [-54], [-3], [-2], [21], [-22], [-56], [18], [23], [-30], [28], [-22], [62], [-3], [34], [-24], [6], [-51], [-20], [-14], [58], [27], [-62], [-48], [-17], [-9], [12], [-18], [-22], [-40], [12], [54], [-18], [37], [-56], [-14], [-12], [66], [47], [-18], [-27], [-20], [-28], [9], [-40], [66], [12], [-50], [-22], [-30], [18], [-24], [-29], [16], [-21], [-64], [42], [-22], [-48], [-60], [-8], [-33], [-34], [1], [13], [-42], [26], [18], [-61], [-36], [-33], [33], [-54], [20], [-16], [46], [6], [-24], [38], [0], [25], [-60], [2], [15], [-12], [10], [0], [57], [-73], [-68], [30], [-46], [22], [-72], [65], [-26], [-18], [-5], [-9], [-33], [16], [36], [34], [34], [44], [59], [-74], [8], [-30], [-14], [-26], [69], [-60], [23], [6], [22], [-22], [48], [-28], [54], [23], [-27], [57], [-18], [-36], [-15], [-50], [-57], [-8], [72], [48], [14], [-4], [66], [-19], [30], [83], [-62], [-24], [-65], [0], [-46], [66], [-75], [57], [-38], [60], [-65], [-57], [51], [-76], [78], [76], [-88], [87], [-26], [60], [-17], [28], [76], [-72], [30], [5], [42], [17], [-42], [20], [-42], [-26], [12], [-7], [62], [-26], [36], [81], [64], [30], [39], [-79], [82], [27], [-18], [-83], [10], [-63], [46], [67], [-90], [12], [51], [-36], [-60], [-82], [15], [60], [-84], [20], [-8], [-42], [46], [-89], [-48], [1], [66], [9], [-88], [-28], [51], [-9], [-16], [-69], [22], [-3], [78], [38], [-36], [-29], [-24], [41], [32], [-34], [84], [-14], [-54], [9], [68], [75], [-32], [-71], [15], [-20], [-87], [70], [6], [94], [60], [42], [8], [-40], [-39], [40], [12], [-14], [60], [54], [-31], [-58], [63], [-39], [28], [-42], [-36], [-27], [46], [48], [-9], [-26], [66], [6], [87], [-19], [9]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1805_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1805_2_a_b();".
// 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_1805_2_a_b(:prec:=1)
    chi := MakeCharacter_1805_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_1805_2_a_b();".
// 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_1805_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1805_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![0, 1]>,<3,R![-2, 1]>],Snew);
    return Vf;
end function;