
// Make newform 392.4.i.k in Magma, downloaded from the LMFDB on 28 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_392_i();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_392_i_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_392_4_i_k();".
// 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_392_4_i_k();".
// 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
        poly := [4, 0, 2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2, 2];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_392_i();"
function MakeCharacter_392_i()
    N := 392;
    order := 3;
    char_gens := [295, 197, 297];
    v := [3, 3, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_392_i_Hecke();"
function MakeCharacter_392_i_Hecke(Kf)
    N := 392;
    order := 3;
    char_gens := [295, 197, 297];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 4;
    raw_aps := [[0, 0, 0, 0], [0, 1, 0, 0], [0, 10, 0, 10], [0, 0, 0, 0], [-54, 0, -54, 0], [0, 0, 0, -16], [0, -23, 0, 0], [0, -45, 0, -45], [0, 0, 28, 0], [282, 0, 0, 0], [0, 194, 0, 0], [0, 0, 146, 0], [0, 0, 0, -241], [10, 0, 0, 0], [0, -358, 0, -358], [-598, 0, -598, 0], [0, -407, 0, 0], [0, 330, 0, 330], [-916, 0, -916, 0], [420, 0, 0, 0], [0, 497, 0, 0], [0, 0, -292, 0], [0, 0, 0, -815], [0, 315, 0, 315], [0, 0, 0, 417], [0, 130, 0, 0], [0, -1334, 0, -1334], [0, 0, -1340, 0], [538, 0, 538, 0], [2068, 0, 0, 0], [-944, 0, 0, 0], [0, 321, 0, 321], [1300, 0, 1300, 0], [0, 0, 0, 1977], [0, 0, -2190, 0], [-1184, 0, -1184, 0], [0, -600, 0, 0], [0, 0, -1822, 0], [0, 0, 0, -1082], [0, 1236, 0, 1236], [3580, 0, 3580, 0], [0, 0, 0, 2064], [0, 0, -484, 0], [800, 0, 800, 0], [-1686, 0, 0, 0], [0, -78, 0, 0], [3804, 0, 0, 0], [0, 0, 0, 944], [0, -473, 0, 0], [0, 708, 0, 708], [0, 0, 680, 0], [1028, 0, 0, 0], [0, 1991, 0, 0], [0, 0, 0, 2761], [0, -4935, 0, -4935], [-7580, 0, -7580, 0], [0, 632, 0, 0], [0, 3920, 0, 3920], [-3446, 0, -3446, 0], [-5216, 0, 0, 0], [0, -519, 0, 0], [0, 0, 0, -586], [0, 0, 0, 6047], [0, 6040, 0, 0], [0, 1809, 0, 1809], [0, 0, 3170, 0], [0, 0, 6610, 0], [8866, 0, 0, 0], [1046, 0, 1046, 0], [0, 0, 0, -7200], [0, -2719, 0, 0], [0, 0, -1904, 0], [0, 8188, 0, 0], [0, 0, 50, 0], [8830, 0, 0, 0], [0, -2770, 0, -2770], [1278, 0, 1278, 0], [0, -1328, 0, -1328], [0, 0, -114, 0], [0, 11565, 0, 0], [0, 0, 0, 4727], [1014, 0, 0, 0], [-3036, 0, -3036, 0], [0, 0, 0, 10525], [0, 4452, 0, 4452], [0, 0, -10380, 0], [-4898, 0, 0, 0], [0, 0, 1464, 0], [0, 0, 0, -1900], [13760, 0, 0, 0], [0, 10383, 0, 10383], [0, 4862, 0, 0], [-3276, 0, -3276, 0], [-15956, 0, 0, 0], [0, 0, -10108, 0], [0, 0, 0, -2764], [0, 2512, 0, 2512], [0, -10775, 0, 0], [0, 12145, 0, 12145], [0, 0, 13666, 0], [-5650, 0, 0, 0], [11430, 0, 11430, 0], [0, -9087, 0, 0], [0, 0, 3770, 0], [3370, 0, 3370, 0], [0, 1503, 0, 0], [0, 0, 0, 7845], [0, -13781, 0, -13781], [23952, 0, 23952, 0], [0, 0, 0, 7915], [0, -8908, 0, -8908], [10230, 0, 10230, 0], [9046, 0, 0, 0], [0, -6837, 0, 0], [-20036, 0, 0, 0], [-9690, 0, -9690, 0], [0, 0, 0, 3049], [0, -9150, 0, 0], [0, 0, 21910, 0], [-23322, 0, 0, 0], [0, 21250, 0, 0], [6820, 0, 0, 0], [0, 11748, 0, 11748], [-11140, 0, -11140, 0], [0, 7265, 0, 7265], [12326, 0, 0, 0], [0, 0, -7982, 0], [0, 10778, 0, 10778], [0, 0, 0, -19146], [0, -12426, 0, -12426], [10758, 0, 10758, 0], [27648, 0, 0, 0], [0, 0, 4236, 0], [7850, 0, 0, 0], [0, -6701, 0, -6701], [0, 0, 0, 4275], [0, -10106, 0, 0], [0, -11687, 0, 0], [0, 0, 0, -2192], [21488, 0, 21488, 0], [0, 0, 0, -451], [0, 0, 6534, 0], [-38632, 0, -38632, 0], [-10564, 0, 0, 0], [0, 23838, 0, 0], [0, 0, 0, -18502], [0, 0, 0, -31680], [0, 27835, 0, 0], [0, 10525, 0, 10525], [0, 0, 8252, 0], [0, 0, -19190, 0], [0, 0, 0, -8085], [40580, 0, 0, 0], [0, 19966, 0, 19966], [15236, 0, 15236, 0], [-38568, 0, 0, 0], [0, 0, -24032, 0], [0, 5327, 0, 5327], [0, 0, 0, -36423], [0, -33490, 0, 0], [0, 0, -10986, 0], [-36938, 0, 0, 0], [-32460, 0, 0, 0], [0, 11711, 0, 11711], [-14460, 0, -14460, 0], [0, 17654, 0, 0], [8480, 0, 8480, 0], [0, -8562, 0, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_392_i_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_392_4_i_k();".
// 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_392_4_i_k(:prec:=4)
    chi := MakeCharacter_392_i();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_392_4_i_k();".
// 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_392_4_i_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_392_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![4, 0, 2, 0, 1]>,<5,R![40000, 0, 200, 0, 1]>],Snew);
    return Vf;
end function;