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