
// Make newform 1870.2.a.f in Magma, downloaded from the LMFDB on 20 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1870_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_1870_2_a_f();".
// 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_1870_2_a_f();".
// 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_1870_a();"
function MakeCharacter_1870_a()
    N := 1870;
    order := 1;
    char_gens := [1497, 1531, 1431];
    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_1870_a_Hecke(Kf)
    return MakeCharacter_1870_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 := [[-1], [1], [-1], [0], [1], [-3], [1], [3], [4], [-3], [-7], [8], [-4], [-2], [-9], [-3], [-11], [-1], [-12], [-7], [7], [0], [-6], [-1], [-9], [4], [16], [0], [-1], [-13], [-1], [-8], [2], [-14], [-20], [22], [-2], [20], [-14], [8], [-12], [18], [18], [18], [20], [-9], [10], [9], [-3], [-26], [-3], [-24], [-22], [-20], [-24], [1], [-27], [-10], [-4], [5], [17], [19], [30], [0], [-26], [-6], [-17], [15], [35], [20], [-14], [18], [-4], [22], [-4], [17], [38], [-2], [-2], [31], [14], [-12], [16], [32], [-16], [-18], [10], [-8], [38], [23], [-14], [9], [18], [-27], [14], [-20], [-22], [12], [4], [6], [3], [-31], [44], [-31], [10], [-12], [-2], [24], [24], [-2], [46], [-23], [-33], [0], [0], [12], [-20], [-19], [0], [29], [14], [19], [12], [21], [-10], [-6], [11], [-11], [15], [-34], [-11], [-48], [-5], [11], [-30], [-5], [-26], [-37], [-30], [-10], [-18], [-45], [-44], [-8], [-2], [43], [-34], [13], [-31], [4], [-22], [-12], [14], [20], [-33], [-20], [-44], [-40], [16], [45], [3], [38], [32], [1], [22], [24], [25], [-18], [-16], [54], [-9], [52], [-33], [-58], [26], [6], [50], [-41], [29], [14], [39], [10], [-32], [-64], [28], [34], [10], [24], [4], [-64], [-46], [1], [1], [10], [25], [-46], [-30], [-18], [-45], [11], [-6], [59], [-2], [7], [20], [2], [4], [-48], [6], [67], [13], [26], [-26], [0], [-25], [-70], [-53], [-57], [38], [-5], [68], [-55], [34], [-12], [58], [-12], [-69], [-43], [-15], [-8], [47], [-14], [8], [35], [62], [-4], [-26], [-32], [64], [38], [-25], [52], [20], [30], [-29], [21], [58], [-8], [-40], [-77], [25], [60], [-67], [26], [41], [45], [-53], [-28], [-4], [-76], [44], [-42], [-46], [6], [3], [4], [42], [75], [37], [2], [-15], [20], [-38], [32], [-2], [-33], [38], [-24], [-27], [-57], [-72], [-59], [10], [58], [-68], [42], [15], [-32], [-56], [-19], [75], [9], [-18], [-72], [-10], [35], [20], [-69], [-80], [-4], [-56], [15], [77], [11], [40], [-70], [54], [-35], [12], [39], [28], [70], [22], [-54], [-65], [33], [-73], [-32], [-26], [41], [50], [-65], [38], [-16], [-51], [69], [-58], [-9], [-24], [4], [-74], [58], [-8], [30], [-74], [-36], [-80], [-78], [-64], [8], [-9], [48], [37], [-58], [6], [-18], [22], [46], [15], [-52], [76], [-2], [-60], [44], [-44], [6], [-36], [-40], [70], [57], [8], [36], [84], [0], [-58], [84], [2], [5], [-68], [-69], [60], [54], [12], [-22], [74], [-10], [49], [-50], [33], [74], [-31], [0], [38], [15], [50], [6], [-10], [-94], [-23], [12], [-8], [76], [69], [-42], [-2], [-70], [-70], [86], [4], [58], [-94], [42], [43], [-91], [84], [64], [-45], [-30], [22], [-14], [-77], [21], [-76], [-8], [4], [-57], [-40], [-18], [-7], [-73], [42], [-4], [86], [73], [1], [-11], [84], [-44], [49], [-80]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1870_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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