
// Make newform 1386.2.a.d in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1386_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_1386_2_a_d();".
// 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_1386_2_a_d();".
// 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_1386_a();"
function MakeCharacter_1386_a()
    N := 1386;
    order := 1;
    char_gens := [155, 199, 1135];
    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_1386_a_Hecke(Kf)
    return MakeCharacter_1386_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], [0], [2], [-1], [-1], [-4], [2], [-6], [-2], [-2], [-8], [-2], [-2], [-2], [2], [6], [0], [4], [0], [10], [-2], [-12], [-12], [12], [-2], [-6], [0], [-4], [-8], [-4], [20], [-12], [0], [2], [-22], [-12], [-2], [-8], [-8], [22], [-20], [14], [-10], [14], [18], [-8], [10], [16], [-20], [-10], [18], [28], [-30], [12], [12], [-24], [6], [-20], [12], [2], [-6], [6], [26], [2], [22], [30], [16], [30], [-4], [8], [-36], [32], [-32], [-4], [16], [2], [18], [-18], [0], [10], [4], [-26], [-32], [-34], [-8], [24], [36], [-22], [-18], [16], [12], [20], [32], [-12], [-4], [0], [-10], [4], [-22], [-40], [18], [38], [-4], [30], [10], [-10], [-48], [-18], [30], [26], [-4], [-8], [36], [-40], [-48], [-12], [16], [42], [10], [-20], [-10], [-26], [26], [-36], [-24], [30], [-6], [-50], [-16], [-4], [6], [-24], [40], [38], [-30], [-2], [30], [-22], [-22], [-18], [38], [18], [-16], [-28], [-38], [-18], [-52], [2], [-44], [10], [4], [0], [-32], [-20], [-28], [18], [-12], [12], [42], [-14], [-16], [38], [20], [-16], [32], [42], [16], [48], [-62], [-46], [36], [-34], [-32], [22], [8], [40], [26], [2], [0], [-8], [0], [-52], [46], [58], [34], [-6], [52], [-24], [2], [52], [-38], [-44], [44], [-38], [-20], [-36], [-22], [-46], [-30], [48], [-14], [16], [22], [14], [12], [-66], [32], [12], [6], [20], [38], [-38], [8], [-28], [-36], [34], [32], [-42], [42], [-22], [60], [4], [-24], [64], [-12], [28], [-56], [42], [-32], [-52], [-58], [-10], [40], [-66], [-24], [24], [30], [-66], [-36], [-10], [-12], [-62], [0], [-46], [-18], [60], [40], [-32], [-54], [-24], [-36], [-34], [-2], [-26], [26], [36], [-58], [46], [-58], [-34], [-44], [28], [-36], [80], [-48], [36], [-30], [40], [-50], [-10], [-74], [4], [18], [-40], [-26], [-16], [0], [-16], [34], [-36], [-12], [-40], [-12], [-8], [-2], [54], [-26], [22], [24], [-18], [42], [-40], [-54], [-4], [-44], [2], [72], [-26], [20], [78], [14], [22], [32], [84], [-64], [-46], [-56], [-86], [90], [-36], [0], [-10], [-70], [-36], [24], [-18], [4], [60], [-18], [30], [50], [38], [58], [-80], [-78], [-30], [-40], [-24], [36], [54], [36], [50], [-16], [92], [-2], [36], [10], [-66], [-90], [36], [26], [60], [38], [-32], [-18], [-60], [86], [40], [36], [-66], [-42], [-58], [70], [44], [-24], [-6], [70], [52], [-74], [-66], [56], [94], [-26], [-60], [-4], [90], [-38], [-44], [-34], [-72], [20], [-44], [2], [80], [54], [48], [44], [-46], [98], [42], [-22], [-44], [-4], [38], [-62], [-14], [-40], [18], [-26], [2], [-2], [-86], [4], [32], [-30], [-6], [32], [104], [56], [-58], [12], [0], [48], [-48], [6], [40], [-2], [-54], [-28], [-56], [18], [-54], [-48], [10], [-2], [78], [-76], [80], [-8], [104], [58], [12], [34], [76], [-10], [-66], [-48], [66], [68], [-68]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1386_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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