
// Make newform 392.6.i.e in Magma, downloaded from the LMFDB on 25 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_6_i_e();".
// 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_6_i_e();".
// 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 := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [1, 0], [0, -1]];
    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 := 6;
    raw_aps := [[0, 0], [20, -20], [0, -74], [0, 0], [-124, 124], [-478, 0], [-1198, 1198], [0, 3044], [0, -184], [-3282, 0], [-5728, 5728], [0, -10326], [8886, 0], [-9188, 0], [0, 23664], [-11686, 11686], [16876, -16876], [0, -18482], [15532, -15532], [-31960, 0], [-4886, 4886], [0, -44560], [-67364, 0], [0, 71994], [-48866, 0], [51606, -51606], [0, 180424], [0, 65700], [112706, -112706], [-23502, 0], [-94592, 0], [0, 70292], [-277290, 277290], [130308, 0], [0, 401530], [75976, -75976], [-394322, 394322], [0, 11724], [551928, 0], [0, 432894], [-559620, 559620], [-604710, 0], [0, 409152], [-540866, 540866], [-629898, 0], [283048, -283048], [142756, 0], [-889696, 0], [1143156, -1143156], [0, -695786], [0, 347126], [-1642960, 0], [-1167438, 1167438], [790612, 0], [0, -129790], [-70888, 70888], [1790174, -1790174], [0, -1773616], [275450, -275450], [594170, 0], [1092428, -1092428], [-333654, 0], [-1059972, 0], [-1336488, 1336488], [0, 1644186], [0, 1723698], [0, -2749628], [-3414894, 0], [-730764, 730764], [2297490, 0], [-1170718, 1170718], [0, -3886536], [933040, -933040], [0, 392218], [-4729300, 0], [0, 1897344], [3722954, -3722954], [0, 3338078], [0, -4274898], [-2573190, 2573190], [-5268276, 0], [-973354, 0], [-3557360, 3557360], [1954958, 0], [0, -3296808], [0, 5058196], [2127298, 0], [0, -289130], [-2668702, 0], [7586192, 0], [0, -1419612], [-1884064, 1884064], [6013880, -6013880], [4292316, 0], [0, -1345092], [-202008, 0], [0, 9783438], [-10483030, 10483030], [0, 6210172], [0, -5080878], [3346868, 0], [-7003774, 7003774], [-12981884, 12981884], [0, -1899418], [1662996, -1662996], [8773442, -8773442], [-5183932, 0], [0, 8498578], [-11247096, 11247096], [3464390, 0], [0, -999712], [-9813398, 9813398], [-5347446, 0], [-6827684, 6827684], [-3609704, 0], [13385342, -13385342], [9911148, 0], [-17835928, 17835928], [0, 4323234], [19785828, 0], [15777158, -15777158], [6787618, 0], [0, -14994218], [-11558044, 11558044], [0, -220156], [4789326, 0], [0, -4268918], [0, -16195952], [-6534264, 0], [0, 13161710], [14234764, -14234764], [-21583544, 0], [0, -18659376], [-2566810, 0], [0, -25958566], [-5532674, 0], [8329398, -8329398], [-13652252, 13652252], [8546258, 0], [-7584842, 7584842], [-6184732, 0], [0, 2781018], [-16389464, 16389464], [22951148, 0], [-3501362, 3501362], [-5296680, 0], [20294842, 0], [-4827846, 4827846], [0, -13021044], [0, 39238688], [0, -13462206], [917710, 0], [24548804, 0], [0, -16146280], [20336132, -20336132], [10772560, 0], [0, -41856568], [0, 29984546], [-14240202, 0], [-41454594, 41454594], [0, 15407868], [-20632806, 0], [11872424, 0], [0, 1532220], [-17432082, 17432082], [2232696, -2232696], [-22250080, 22250080], [53266198, -53266198]];
    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_6_i_e();".
// 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_6_i_e(:prec:=2)
    chi := MakeCharacter_392_i();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_6_i_e();".
// 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_6_i_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_392_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![400, -20, 1]>],Snew);
    return Vf;
end function;