
// Make newform 392.4.i.b in Magma, downloaded from the LMFDB on 19 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_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_392_4_i_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
        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 := 4;
    raw_aps := [[0, 0], [-4, 4], [0, -2], [0, 0], [44, -44], [-22, 0], [50, -50], [0, 44], [0, 56], [198, 0], [-160, 160], [0, 162], [198, 0], [52, 0], [0, 528], [242, -242], [-668, 668], [0, 550], [-188, 188], [728, 0], [154, -154], [0, 656], [-236, 0], [0, 714], [478, 0], [1566, -1566], [0, -968], [0, 780], [1994, -1994], [-942, 0], [1408, 0], [0, -2692], [-1626, 1626], [684, 0], [0, -302], [-1352, 1352], [3142, -3142], [0, -3036], [264, 0], [0, -2826], [-3084, 3084], [2418, 0], [0, 960], [-2882, 2882], [1086, 0], [88, -88], [-3476, 0], [-928, 0], [156, -156], [0, -1634], [0, 902], [1616, 0], [4818, -4818], [2140, 0], [0, 770], [7400, -7400], [-2794, 2794], [0, 8624], [1874, -1874], [3338, 0], [7172, -7172], [-5214, 0], [-396, 0], [-4056, 4056], [0, 2154], [0, 7386], [0, 1132], [-3342, 0], [-2244, 2244], [6522, 0], [-11230, 11230], [0, -1848], [7120, -7120], [0, -6350], [-7900, 0], [0, 10368], [-8830, 8830], [0, 9878], [0, 13134], [906, -906], [5412, 0], [-4642, 0], [-656, 656], [-9490, 0], [0, 5544], [0, -7652], [-446, 0], [0, -1562], [-10582, 0], [-10768, 0], [0, -9876], [-352, 352], [15176, -15176], [-8844, 0], [0, -19404], [-16488, 0], [0, -12954], [10970, -10970], [0, -16940], [0, -198], [-15268, 0], [-20854, 20854], [-19316, 19316], [0, -7018], [-24420, 24420], [23234, -23234], [10604, 0], [0, -13838], [3960, -3960], [5942, 0], [0, -3040], [2530, -2530], [-19206, 0], [10996, -10996], [-6680, 0], [-6274, 6274], [-9084, 0], [-23656, 23656], [0, 6762], [15276, 0], [11054, -11054], [-21278, 0], [0, 8926], [-8116, 8116], [0, -11764], [-4698, 0], [0, -24638], [0, 16624], [-30216, 0], [0, -3322], [14692, -14692], [28600, 0], [0, 29616], [2894, 0], [0, 14762], [7678, 0], [27390, -27390], [19756, -19756], [-38854, 0], [14278, -14278], [716, 0], [0, 23538], [6616, -6616], [27236, 0], [12070, -12070], [42024, 0], [-2414, 0], [-37686, 37686], [0, 40644], [0, 18656], [0, 13002], [-49490, 0], [1100, 0], [0, -14104], [12716, -12716], [-39632, 0], [0, -5704], [0, 8162], [55110, 0], [16374, -16374], [0, -8460], [-20502, 0], [-36520, 0], [0, 20244], [-50034, 50034], [37128, -37128], [-27808, 27808], [-28514, 28514]];
    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_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_392_4_i_b(:prec:=2)
    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_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_392_4_i_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_392_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![16, 4, 1]>,<5,R![4, 2, 1]>],Snew);
    return Vf;
end function;