
// Make newform 1856.2.a.m in Magma, downloaded from the LMFDB on 23 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_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_1856_2_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_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], [3], [2], [3], [5], [-4], [0], [0], [1], [9], [-8], [-2], [11], [-7], [-9], [-4], [12], [-12], [2], [-4], [3], [16], [2], [-14], [0], [-10], [14], [-1], [-6], [16], [4], [-12], [-8], [-11], [-2], [18], [15], [-18], [14], [-22], [-19], [24], [2], [14], [26], [3], [-2], [-18], [26], [-9], [20], [1], [-3], [29], [-19], [12], [-25], [2], [-29], [-24], [-6], [7], [-24], [-15], [-8], [-17], [-12], [-30], [-21], [30], [3], [-8], [-15], [28], [-6], [-24], [13], [11], [-18], [-6], [20], [-12], [8], [-8], [-12], [6], [38], [-10], [-24], [-21], [-17], [14], [-15], [16], [-9], [-29], [-29], [-16], [8], [22], [34], [-13], [26], [20], [28], [-32], [-25], [-1], [22], [-9], [-37], [-28], [-5], [18], [28], [34], [12], [6], [25], [-14], [1], [-30], [8], [4], [-7], [37], [-26], [-8], [24], [37], [20], [40], [20], [50], [-32], [14], [30], [20], [8], [-2], [45], [40], [-45], [-28], [17], [2], [-3], [41], [-46], [-17], [18], [22], [-3], [44], [39], [54], [-14], [30], [-33], [-9], [-33], [-23], [20], [-3], [-3], [46], [-8], [25], [12], [54], [-1], [-22], [-10], [12], [-42], [16], [-40], [35], [30], [-1], [-5], [-2], [-23], [-8], [57], [24], [-28], [-36], [10], [-3], [45], [-45], [40], [28], [-30], [-40], [30], [9], [-20], [18], [64], [-50], [-54], [12], [9], [7], [6], [-21], [-23], [-6], [-10], [35], [35], [48], [45], [-16], [-12], [8], [-50], [28], [-62], [-42], [39], [-56], [-26], [-48], [-32], [25], [6], [24], [16], [-20], [0], [-6], [8], [-36], [26], [6], [41], [-3], [6], [46], [-20], [2], [-26], [-24], [-24], [46], [11], [-8], [39], [-21], [-64], [22], [-28], [70], [33], [34], [9], [-8], [51], [14], [-32], [-52], [-9], [40], [72], [28], [-9], [67], [46], [-11], [-32], [40], [53], [76], [13], [60], [64], [6], [-6], [-24], [-58], [-36], [-76], [-64], [22], [46], [41], [9], [-64], [45], [69], [14], [37], [37], [23], [-86], [-20], [64], [74], [64], [3], [60], [9], [-53], [-23], [38], [-55], [28], [-18], [2], [-72], [-74], [-53], [44], [38], [-47], [60], [-23], [-31], [2], [-49], [-45], [-86], [-50], [-4], [-33], [-57], [-4], [-19], [28], [84], [-79], [40], [66], [-54], [-14], [20], [40], [2], [76], [-48], [14], [-57], [-52], [-52], [31], [-74], [68], [-2], [-6], [40], [-68], [68], [21], [-46], [92], [78], [-19], [-15], [-57], [30], [7], [-6], [0], [-38], [50], [79], [34], [-14], [-30], [-4], [-57], [76], [-65], [88], [63], [85], [-40], [-63], [61], [-28], [-46], [-14], [-16], [-63], [35], [4], [-2], [-9], [25], [-23], [45], [-67], [92], [36], [-10], [-14], [2], [-46], [-76], [69], [77], [68], [-73], [96], [-61], [-36], [94], [39], [-38], [8], [82], [10], [39], [102], [-52], [-45], [-66], [22], [107], [69], [-49], [-13], [28], [-16], [60], [-77]];
    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_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_1856_2_a_m(: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_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_1856_2_a_m( : 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![-3, 1]>,<17,R![4, 1]>],Snew);
    return Vf;
end function;