
// Make newform 392.3.g.i in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_392_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_392_g_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_3_g_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_392_3_g_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
        poly := [2, -10, 20, -21, 13, -3, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [3, -9, 10, -2, 1, 0], [-2, 10, -10, 11, -2, 1], [8, -20, 21, -13, 3, -1], [20, -69, 90, -70, 15, -6], [21, -71, 90, -70, 15, -6]];
    Rf_basisdens := [1, 1, 1, 1, 1, 1];
    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_g();"
function MakeCharacter_392_g()
    N := 392;
    order := 2;
    char_gens := [295, 197, 297];
    v := [1, 1, 2];
    // 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_g_Hecke();"
function MakeCharacter_392_g_Hecke(Kf)
    N := 392;
    order := 2;
    char_gens := [295, 197, 297];
    char_values := [[-1, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 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 := 3;
    raw_aps := [[0, 0, 0, 1, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, -1, 1, 1, -1], [0, 0, 0, 0, 0, 0], [-6, -3, 0, 0, 0, 0], [0, 0, -5, 5, -2, 2], [5, 2, -1, -1, 0, 0], [16, 1, 4, 4, 0, 0], [0, 0, 5, -5, 0, -7], [0, 0, 3, -3, -8, 2], [0, 0, 6, -6, 0, 9], [0, 0, 4, -4, -9, -1], [26, 18, 1, 1, 0, 0], [-20, 10, -10, -10, 0, 0], [0, 0, 2, -2, -16, -5], [0, 0, -20, 20, 3, -7], [-12, 9, 5, 5, 0, 0], [0, 0, 10, -10, -15, 25], [-70, 1, 3, 3, 0, 0], [0, 0, -22, 22, 6, -8], [15, 18, -12, -12, 0, 0], [0, 0, -5, 5, -16, 25], [50, -12, 14, 14, 0, 0], [13, 4, -36, -36, 0, 0], [0, -8, -15, -15, 0, 0], [0, 0, -21, 21, -5, 1], [0, 0, 31, -31, 2, -5], [0, 35, 8, 8, 0, 0], [0, 0, -1, 1, 19, 11], [-60, -20, 5, 5, 0, 0], [0, 0, -10, 10, -46, 28], [-14, -25, 45, 45, 0, 0], [-65, 42, 9, 9, 0, 0], [-22, 40, 0, 0, 0, 0], [0, 0, 63, -63, -37, 77], [0, 0, -6, 6, -20, -49], [0, 0, -26, 26, 11, -37], [40, -13, 25, 25, 0, 0], [0, 0, -65, 65, 22, -48], [0, 0, 22, -22, -49, 5], [94, 53, 14, 14, 0, 0], [0, 0, 34, -34, -6, 76], [0, 0, 40, -40, -20, -35], [-55, -60, -71, -71, 0, 0], [0, 0, 1, -1, 88, 2], [0, 0, 84, -84, -34, 81], [154, 0, -10, -10, 0, 0], [0, 0, -36, 36, 46, 32], [-100, 21, 4, 4, 0, 0], [0, 0, -9, 9, 47, 59], [-55, -40, -1, -1, 0, 0], [0, 0, -61, 61, 42, -44], [-79, 64, -72, -72, 0, 0], [6, -48, 17, 17, 0, 0], [5, 34, 59, 59, 0, 0], [0, 0, -81, 81, -18, 15], [0, 0, -64, 64, -29, 19], [0, 0, 1, -1, 18, -21], [0, 0, -40, 40, -69, 9], [-76, -8, 49, 49, 0, 0], [-30, -9, 2, 2, 0, 0], [0, 0, 16, -16, 20, -64], [-60, 74, 40, 40, 0, 0], [0, 0, -71, 71, 24, 21], [-205, -102, -56, -56, 0, 0], [0, 0, -106, 106, -3, -25], [134, 17, -7, -7, 0, 0], [-50, 62, -135, -135, 0, 0], [-180, 75, -71, -71, 0, 0], [0, 0, -45, 45, -24, 50], [395, -52, 75, 75, 0, 0], [0, 0, -130, 130, -26, -75], [0, 0, -15, 15, 162, -43], [0, 0, 26, -26, 65, 23], [-26, 4, 87, 87, 0, 0], [0, 0, -84, 84, -130, -9], [0, 0, -63, 63, 37, -37], [0, 0, 29, -29, 131, -73], [119, -110, 3, 3, 0, 0], [33, -118, 33, 33, 0, 0], [-284, 62, -140, -140, 0, 0], [0, 0, 5, -5, -26, -50], [0, 0, 114, -114, -62, 11], [-20, 176, -85, -85, 0, 0], [0, 0, -66, 66, -54, 91], [330, -31, -18, -18, 0, 0], [-126, -186, -133, -133, 0, 0], [265, 58, 150, 150, 0, 0], [0, 0, 153, -153, -144, 42], [0, 0, -132, 132, 28, -180], [-280, 141, -167, -167, 0, 0], [0, 0, 159, -159, -212, 101], [0, 0, 63, -63, 82, -29], [-138, -196, 91, 91, 0, 0], [92, 11, -179, -179, 0, 0], [0, 0, -16, 16, -214, 72], [0, 0, 29, -29, -167, 51], [-179, 120, 58, 58, 0, 0], [480, 77, -50, -50, 0, 0], [0, 0, 309, -309, -165, 181], [510, 236, 115, 115, 0, 0], [0, 0, 225, -225, -77, 183], [-150, 39, -179, -179, 0, 0], [-431, 198, -52, -52, 0, 0], [282, -103, -32, -32, 0, 0], [305, -196, 6, 6, 0, 0], [700, 26, -44, -44, 0, 0], [195, 92, 129, 129, 0, 0], [0, 0, -189, 189, -104, -101], [-212, -36, -53, -53, 0, 0], [0, 0, -58, 58, 182, 95], [0, 0, 29, -29, 223, -81], [-350, 42, -3, -3, 0, 0], [486, 199, 109, 109, 0, 0], [0, 0, 146, -146, -114, 24], [-121, -52, 52, 52, 0, 0], [180, -102, -14, -14, 0, 0], [0, 0, 59, -59, -32, 153], [0, 0, -42, 42, 77, -85], [482, -176, -52, -52, 0, 0], [0, 0, -312, 312, 103, 7], [70, 26, -111, -111, 0, 0], [0, 0, -83, 83, -181, -5], [680, 179, 46, 46, 0, 0], [-362, -265, 67, 67, 0, 0], [0, 0, 44, -44, 224, -84], [0, 0, 56, -56, 211, 79], [0, 0, -283, 283, -158, 123], [0, 0, -102, 102, -30, -56], [0, 0, -156, 156, 163, -247], [44, 215, 244, 244, 0, 0], [0, 0, 144, -144, -134, 324], [0, 0, -150, 150, 20, -235], [0, 0, 43, -43, -84, -14], [-339, 214, -81, -81, 0, 0], [606, -162, 19, 19, 0, 0], [0, 0, 279, -279, -305, 133], [-720, -167, 111, 111, 0, 0], [0, 0, 209, -209, 128, 262], [-71, -10, -66, -66, 0, 0], [-644, -186, 38, 38, 0, 0], [0, 0, 171, -171, -207, -31], [0, 0, 523, -523, -252, 211], [-920, -174, -122, -122, 0, 0], [0, 0, -3, 3, -39, 193], [0, 0, -140, 140, 164, 40], [0, 0, -215, 215, -62, -238], [675, -158, 46, 46, 0, 0], [718, 75, 60, 60, 0, 0], [0, 0, 259, -259, -44, 109], [0, 0, 65, -65, -169, -47], [-444, -222, -48, -48, 0, 0], [290, 100, -98, -98, 0, 0], [0, 0, 124, -124, -128, -53], [-180, -145, 245, 245, 0, 0], [0, 0, -62, 62, 436, -228], [0, 0, 74, -74, -206, 141], [-109, -46, -248, -248, 0, 0], [-470, -384, -172, -172, 0, 0], [0, 0, -298, 298, 213, -477], [-610, -307, -103, -103, 0, 0], [320, -204, 203, 203, 0, 0], [0, 0, 210, -210, -2, 4], [466, 295, 175, 175, 0, 0], [1085, 152, 157, 157, 0, 0], [0, 0, -244, 244, -224, -219], [0, 0, -227, 227, 316, -313], [0, 0, -234, 234, 251, -285]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_392_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_392_3_g_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_392_3_g_i(:prec:=6)
    chi := MakeCharacter_392_g();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_3_g_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_392_3_g_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_392_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![-35, -19, 3, 1]>],Snew);
    return Vf;
end function;