
// Make newform 8085.2.a.b in Magma, downloaded from the LMFDB on 10 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8085_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_8085_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_8085_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_8085_a();"
function MakeCharacter_8085_a()
    N := 8085;
    order := 1;
    char_gens := [2696, 4852, 1816, 3676];
    v := [1, 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_8085_a_Hecke(Kf)
    return MakeCharacter_8085_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 := [[-2], [-1], [-1], [0], [1], [6], [7], [5], [-1], [-5], [8], [-2], [-12], [-11], [-8], [-11], [5], [-7], [-2], [12], [-4], [-10], [1], [-15], [-3], [-2], [1], [-12], [0], [9], [-7], [-2], [-22], [0], [-10], [2], [17], [-6], [-8], [6], [0], [-22], [-8], [-6], [-12], [-20], [22], [-9], [-23], [20], [-6], [5], [18], [-12], [-18], [4], [25], [3], [-22], [-18], [-14], [-29], [-18], [-12], [31], [-22], [-3], [13], [-32], [25], [6], [-15], [17], [29], [5], [-34], [30], [-38], [12], [10], [15], [-23], [-8], [26], [5], [-36], [0], [33], [38], [-6], [-28], [10], [-22], [27], [-25], [11], [35], [23], [16], [12], [3], [38], [-4], [15], [-18], [2], [42], [6], [-10], [3], [22], [-6], [18], [-30], [-3], [-18], [-19], [-48], [-1], [15], [28], [-1], [-13], [-36], [-42], [-23], [-35], [25], [47], [-14], [-10], [-6], [17], [-22], [-42], [-35], [6], [-28], [-18], [30], [-12], [-3], [14], [48], [30], [-15], [36], [42], [-20], [39], [23], [33], [4], [-43], [-2], [12], [30], [50], [-28], [-42], [23], [34], [-47], [13], [-7], [-14], [-33], [22], [-30], [-14], [5], [28], [7], [19], [30], [10], [2], [12], [46], [-10], [-52], [-52], [-6], [17], [-1], [5], [-37], [31], [20], [-52], [-14], [14], [32], [43], [18], [-24], [-48], [34], [7], [16], [-45], [-7], [47], [-10], [25], [-8], [0], [14], [55], [-12], [13], [23], [4], [-58], [0], [58], [-37], [-42], [28], [-1], [-68], [40], [-60], [-26], [-33], [-10], [-4], [-20], [-68], [-63], [14], [-20], [22], [7], [-39], [37], [-30], [44], [30], [-12], [44], [33], [56], [55], [31], [70], [32], [-57], [30], [44], [38], [48], [-12], [-40], [21], [25], [-3], [-28], [-8], [62], [-71], [8], [35], [76], [2], [-10], [60], [-17], [-11], [74], [-42], [-22], [41], [80], [-78], [56], [43], [80], [-68], [28], [81], [52], [12], [-47], [2], [62], [24], [-12], [-10], [40], [12], [42], [-46], [48], [14], [60], [-52], [26], [0], [52], [-74], [-42], [10], [-21], [-63], [-72], [43], [10], [-40], [-51], [-24], [-60], [-3], [-36], [8], [-85], [40], [-33], [26], [-45], [-32], [-32], [18], [-26], [44], [88], [-60], [71], [-12], [-66], [-68], [38], [5], [36], [2], [12], [-25], [6], [-2], [-28], [-16], [3], [20], [-8], [-81], [25], [38], [28], [68], [12], [23], [28], [-78], [46], [40], [-9], [0], [-42], [-22], [49], [3], [18], [48], [45], [-53], [-26], [27], [79], [-8], [-48], [-100], [-36], [35], [3], [-82], [60], [-33], [-4], [-50], [-38], [-27], [19], [-32], [-12], [95], [-84], [-73], [-13], [24], [-48], [5], [-94], [60], [-73], [-58], [79], [0], [-50], [57], [-13], [55], [69], [103], [82], [30], [-7], [-72], [2], [-59], [15], [-64], [-52], [-36], [-98], [-22], [43], [55], [-43], [-58], [36], [-60], [22], [-37], [20], [11], [42], [-61], [-15], [98], [90]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8085_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_8085_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_8085_2_a_b(:prec:=1)
    chi := MakeCharacter_8085_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_8085_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_8085_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_8085_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![2, 1]>,<13,R![-6, 1]>,<17,R![-7, 1]>],Snew);
    return Vf;
end function;