
// Make newform 1856.2.a.g in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1856_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_1856_2_a_g();".
// 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_1856_2_a_g();".
// 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_1856_a();"
function MakeCharacter_1856_a()
    N := 1856;
    order := 1;
    char_gens := [639, 581, 321];
    v := [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_1856_a_Hecke(Kf)
    return MakeCharacter_1856_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], [-1], [1], [0], [5], [-1], [-6], [-4], [-6], [1], [9], [0], [-8], [1], [9], [9], [-14], [-10], [4], [-6], [-4], [-17], [-6], [0], [-4], [0], [2], [-12], [1], [-18], [16], [4], [8], [14], [-5], [-12], [0], [5], [10], [-14], [-10], [-21], [-24], [12], [22], [-20], [1], [-2], [22], [-16], [15], [22], [1], [-13], [-3], [5], [-6], [11], [10], [15], [-6], [18], [-11], [24], [17], [18], [-7], [-22], [34], [25], [-18], [-5], [-12], [7], [24], [8], [32], [-1], [3], [10], [30], [10], [18], [-40], [-2], [36], [-26], [-26], [22], [-14], [-15], [-33], [-12], [-29], [10], [-25], [-3], [39], [28], [-16], [8], [30], [-3], [-6], [4], [-34], [-42], [3], [23], [-4], [23], [-31], [-24], [-35], [-14], [38], [28], [2], [48], [3], [-26], [1], [-10], [-4], [40], [23], [-49], [18], [-16], [-36], [11], [-24], [-20], [-18], [42], [22], [24], [-38], [-26], [-44], [16], [-33], [32], [33], [6], [-47], [50], [17], [11], [46], [-39], [-46], [-4], [33], [-44], [27], [-6], [-38], [-22], [-47], [-7], [27], [-39], [52], [-7], [5], [54], [28], [29], [-24], [0], [-47], [38], [-46], [38], [14], [20], [-6], [-1], [18], [35], [-47], [-62], [-19], [0], [-13], [58], [-56], [-62], [-8], [5], [51], [-31], [-40], [48], [10], [-28], [58], [21], [-16], [40], [50], [-40], [-40], [0], [51], [-25], [-6], [-9], [11], [32], [-18], [-61], [-27], [8], [-59], [-68], [-12], [0], [-30], [-42], [4], [18], [51], [-12], [-50], [-4], [32], [1], [-24], [-44], [32], [-8], [4], [-26], [0], [38], [8], [-36], [5], [-69], [-34], [66], [52], [-50], [10], [-26], [14], [26], [11], [38], [-33], [-9], [-80], [16], [-44], [-28], [51], [-42], [-7], [-64], [-11], [42], [62], [18], [-55], [-64], [-6], [-52], [49], [25], [62], [-11], [56], [-28], [-51], [-16], [7], [-36], [-38], [6], [6], [28], [-30], [-28], [16], [18], [36], [68], [-23], [-13], [38], [45], [-77], [38], [-65], [61], [-3], [4], [12], [18], [46], [56], [61], [-4], [81], [17], [-61], [-84], [83], [70], [-24], [78], [-38], [20], [31], [28], [82], [-19], [18], [39], [25], [18], [-49], [-33], [48], [38], [-52], [-49], [5], [52], [3], [-92], [-4], [47], [-24], [26], [-94], [-42], [-20], [46], [-14], [22], [-20], [-46], [-75], [-42], [-4], [-45], [-62], [-78], [22], [6], [2], [-6], [10], [-3], [-64], [6], [50], [-17], [9], [-69], [-26], [17], [-32], [-48], [-2], [-42], [73], [14], [-76], [26], [-86], [-67], [36], [-45], [-86], [-1], [5], [20], [-71], [-3], [-78], [-72], [60], [16], [-49], [-39], [-32], [-22], [61], [-57], [-25], [57], [-31], [56], [-64], [2], [26], [-82], [-102], [88], [-3], [-29], [22], [57], [-6], [-19], [-48], [-22], [81], [16], [10], [40], [50], [95], [-70], [48], [-97], [-90], [-22], [19], [31], [47], [57], [26], [-20], [66], [-29]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1856_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1856_2_a_g();".
// 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_1856_2_a_g(:prec:=1)
    chi := MakeCharacter_1856_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_1856_2_a_g();".
// 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_1856_2_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1856_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![-1, 1]>,<17,R![6, 1]>],Snew);
    return Vf;
end function;