
// Make newform 3192.2.a.i in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3192_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_3192_2_a_i();".
// 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_3192_2_a_i();".
// 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_3192_a();"
function MakeCharacter_3192_a()
    N := 3192;
    order := 1;
    char_gens := [799, 1597, 2129, 913, 1009];
    v := [1, 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_3192_a_Hecke(Kf)
    return MakeCharacter_3192_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], [2], [-1], [4], [-6], [6], [1], [8], [-6], [0], [-2], [6], [-4], [4], [2], [12], [-10], [8], [0], [10], [-12], [16], [6], [-10], [-6], [16], [-12], [-2], [6], [4], [16], [-6], [-4], [-2], [-4], [14], [12], [-16], [18], [-4], [-6], [24], [2], [6], [8], [8], [16], [20], [14], [18], [-8], [-2], [0], [-2], [24], [18], [8], [-26], [22], [-12], [26], [-28], [-28], [-6], [18], [-32], [2], [-20], [-18], [6], [-24], [24], [-10], [16], [24], [-18], [-18], [-26], [-34], [0], [30], [-24], [-2], [16], [4], [-18], [10], [-30], [-8], [-16], [-28], [-4], [12], [-20], [28], [34], [-10], [-28], [-34], [16], [-42], [44], [-10], [4], [-30], [-24], [6], [-24], [6], [8], [-26], [-6], [4], [48], [6], [-44], [-12], [14], [20], [-46], [-6], [-38], [-36], [-36], [-18], [-26], [-20], [48], [14], [-44], [-8], [-20], [-10], [6], [-14], [26], [-4], [34], [-6], [44], [-2], [24], [-12], [42], [-24], [-50], [38], [36], [0], [6], [-58], [-4], [40], [-8], [-32], [-8], [46], [34], [18], [4], [22], [-32], [-20], [54], [0], [-20], [-10], [-6], [-6], [-4], [-22], [32], [-38], [8], [30], [60], [54], [-16], [-10], [-48], [-12], [-58], [54], [-24], [10], [-58], [-28], [-22], [4], [-50], [12], [-48], [-30], [-28], [6], [2], [-34], [30], [-36], [-54], [56], [-22], [14], [48], [50], [-56], [-44], [-30], [-68], [-46], [34], [48], [20], [-56], [-34], [-8], [30], [48], [6], [70], [-24], [6], [48], [68], [70], [54], [0], [-24], [-20], [46], [36], [36], [14], [-20], [-12], [66], [-42], [20], [48], [4], [-28], [-24], [-18], [-34], [-4], [32], [-28], [64], [-24], [-66], [54], [8], [6], [50], [60], [-10], [-28], [-70], [-30], [12], [-52], [70], [18], [-42], [-76], [-22], [14], [56], [14], [26], [16], [74], [-24], [-58], [-16], [60], [46], [58], [-48], [-64], [56], [44], [10], [52], [40], [2], [10], [16], [-50], [38], [-64], [38], [28], [-50], [-14], [-40], [34], [68], [-52], [-14], [-22], [16], [36], [-76], [50], [36], [-54], [-72], [38], [-4], [62], [70], [16], [40], [-66], [56], [-16], [-38], [-62], [-12], [90], [-62], [-76], [50], [-66], [56], [-76], [-40], [54], [-82], [90], [-64], [64], [44], [-56], [70], [-66], [-94], [8], [-10], [-54], [42], [28], [-86], [20], [70], [-64], [56], [-86], [68], [2], [-50], [-8], [-58], [6], [60], [-48], [-86], [-64], [-26], [54], [24], [-36], [20], [82], [10], [-4], [-70], [4], [-60], [56], [18], [80], [-18], [84], [48], [66], [-74], [86], [-30], [-6], [-56], [2], [-44], [0], [32], [-98], [4], [40], [34], [10], [12], [-20], [-72], [-70], [0], [-42], [40], [-66], [-30], [18], [-76], [54], [-102], [-16], [-98], [-50], [36], [-24], [46], [78], [44], [68], [-38], [42], [-40], [-16], [-42], [48], [-86], [-62], [72], [84], [102], [18], [76], [-30], [52], [-84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3192_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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