
// Make newform 342.4.g.h in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_342_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_342_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_342_4_g_h();".
// 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_342_4_g_h();".
// 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 := [147, 0, 169, 0, 26, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [7, 13, 0, 1, 0, 0], [119, 0, 40, 0, 2, 0], [-119, 0, 116, 0, 10, 0], [119, 431, -116, 43, -10, 2], [-119, 179, -40, 43, -2, 2]];
    Rf_basisdens := [1, 14, 7, 7, 14, 14];
    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_342_g();"
function MakeCharacter_342_g()
    N := 342;
    order := 3;
    char_gens := [191, 325];
    v := [3, 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_342_g_Hecke();"
function MakeCharacter_342_g_Hecke(Kf)
    N := 342;
    order := 3;
    char_gens := [191, 325];
    char_values := [[1, 0, 0, 0, 0, 0], [-1, 1, 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 := 4;
    raw_aps := [[0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 3, -1, 0, 0, -1], [2, 0, -1, 0, 0, 0], [15, 0, 0, -1, 0, 0], [1, -1, 0, 0, 0, 6], [0, -30, -7, 0, 1, -7], [10, -6, 4, 0, -1, -3], [3, -3, 0, -2, -2, -9], [36, -36, 0, -1, -1, -15], [-28, 0, 24, -1, 0, 0], [77, 0, -17, -3, 0, 0], [0, -222, 20, 0, 2, 20], [0, 28, -21, 0, 2, -21], [102, -102, 0, 6, 6, 10], [-435, 435, 0, 1, 1, -18], [0, 81, -9, 0, 0, -9], [157, -157, 0, -4, -4, -32], [244, -244, 0, -1, -1, -12], [0, -18, 19, 0, -11, 19], [0, 217, 66, 0, 4, 66], [0, 172, 30, 0, -3, 30], [-846, 0, -37, 5, 0, 0], [543, -543, 0, -11, -11, -34], [0, -320, 13, 0, 1, 13], [-324, 324, 0, -11, -11, 9], [-892, 0, 33, 4, 0, 0], [528, 0, 13, 15, 0, 0], [0, 478, -1, 0, -7, -1], [117, 0, -50, -9, 0, 0], [-26, 26, 0, -20, -20, 46], [0, 210, 29, 0, -5, 29], [-282, 282, 0, -15, -15, -193], [1906, -1906, 0, -24, -24, -37], [0, -555, -235, 0, 10, -235], [-1402, 0, -96, -18, 0, 0], [0, 577, 123, 0, 17, 123], [182, 0, -136, 11, 0, 0], [-1077, 1077, 0, -24, -24, 99], [0, -288, 65, 0, -5, 65], [2217, 0, 233, 32, 0, 0], [-2192, 2192, 0, -19, -19, 17], [1449, 0, -40, 15, 0, 0], [0, -269, 159, 0, 19, 159], [-2757, 0, 82, 9, 0, 0], [694, -694, 0, 9, 9, 222], [0, 112, 155, 0, -28, 155], [0, -338, 254, 0, 31, 254], [2307, 0, -248, -19, 0, 0], [-2329, 0, -100, 14, 0, 0], [0, -972, 211, 0, -13, 211], [-1443, 0, 32, -25, 0, 0], [865, -865, 0, 34, 34, -36], [864, -864, 0, 72, 72, 298], [483, -483, 0, 27, 27, -386], [0, -738, -307, 0, -89, -307], [0, 315, 232, 0, 65, 232], [0, 2776, 40, 0, -8, 40], [3056, 0, 143, -95, 0, 0], [-705, 705, 0, 54, 54, -409], [0, 532, 392, 0, 80, 392], [6534, 0, 196, -8, 0, 0], [0, -4562, -331, 0, 17, -331], [-2775, 0, -133, -48, 0, 0], [394, -394, 0, -51, -51, -489], [-3219, 3219, 0, 20, 20, 223], [-5464, 0, 93, 6, 0, 0], [0, -2501, -663, 0, 107, -663], [0, -4263, 140, 0, 5, 140], [2885, 0, 310, -120, 0, 0], [-885, 0, -321, -60, 0, 0], [0, 2268, 244, 0, -28, 244], [9622, -9622, 0, -11, -11, 228], [6032, 0, 615, -3, 0, 0], [-7558, 0, -673, -30, 0, 0], [0, 165, -736, 0, -37, -736], [-2235, 2235, 0, 73, 73, 218], [0, -1949, 230, 0, -80, 230], [0, -4503, -272, 0, 63, -272], [-2570, 2570, 0, 101, 101, -505], [-7815, 0, 446, -73, 0, 0], [0, -2738, 255, 0, -51, 255], [-7398, 7398, 0, -16, -16, 472], [2137, -2137, 0, 77, 77, 511], [0, 5320, 627, 0, 30, 627], [1260, -1260, 0, -127, -127, -177], [2388, 0, 467, 135, 0, 0], [-7495, 0, 707, 35, 0, 0], [0, 939, 130, 0, 43, 130], [-2572, 0, 215, 112, 0, 0], [2922, 0, 556, 194, 0, 0], [7626, -7626, 0, -65, -65, -491], [11102, 0, -682, 88, 0, 0], [0, 5574, 857, 0, 135, 857], [0, -1460, 76, 0, -233, 76], [-10584, 10584, 0, 47, 47, 661], [3786, -3786, 0, 62, 62, 754], [7227, 0, 447, -10, 0, 0], [3514, -3514, 0, -139, -139, 1114], [835, -835, 0, 43, 43, 977], [12196, -12196, 0, -182, -182, -363], [5190, -5190, 0, 131, 131, 1797], [-2820, 0, -733, -43, 0, 0], [-2262, 0, -540, -182, 0, 0], [-11302, 0, -1252, -115, 0, 0], [3854, 0, -627, 21, 0, 0], [0, 18513, 275, 0, 88, 275], [2121, -2121, 0, -7, -7, 1752], [4575, -4575, 0, -171, -171, 570], [7595, 0, 1599, 83, 0, 0], [-14422, 0, 652, -107, 0, 0], [0, 3460, 1129, 0, -167, 1129], [-3417, 3417, 0, -210, -210, -315], [2942, 0, 1278, -205, 0, 0], [-12458, 12458, 0, 82, 82, -19], [0, -12648, -648, 0, 102, -648], [0, 1816, 562, 0, -35, 562], [-18912, 0, 979, -33, 0, 0], [-5772, 0, 1511, -223, 0, 0], [3279, -3279, 0, 3, 3, -260], [706, -706, 0, 296, 296, 1508], [14831, 0, 879, -189, 0, 0], [-6498, 0, 794, -54, 0, 0], [-11355, 0, -250, 69, 0, 0], [260, 0, 16, -64, 0, 0], [0, -7323, 1173, 0, -64, 1173], [-797, 797, 0, 250, 250, -1658], [0, -5457, 1979, 0, -208, 1979], [0, 8584, -254, 0, 493, -254], [5912, 0, 241, 371, 0, 0], [0, 12184, -1916, 0, 53, -1916], [0, 10959, 232, 0, -131, 232], [14272, -14272, 0, 292, 292, -715], [0, -7145, -1230, 0, -202, -1230], [-13461, 0, -1536, -153, 0, 0], [6289, -6289, 0, -214, -214, -2062], [8724, -8724, 0, 94, 94, -484], [2006, 0, -1518, 311, 0, 0], [-14736, 0, -331, 407, 0, 0], [-7899, 0, 1043, -310, 0, 0], [-28790, 28790, 0, 202, 202, 640], [-21240, 21240, 0, 195, 195, -711], [5362, -5362, 0, -396, -396, -594], [3156, -3156, 0, 463, 463, 1893], [20165, 0, -1220, 20, 0, 0], [0, -8085, 3780, 0, 179, 3780], [0, 20647, 1347, 0, -137, 1347], [0, 17442, 92, 0, 76, 92], [-8564, 8564, 0, -69, -69, -212], [-4866, 0, 1172, 344, 0, 0], [0, 11299, 1877, 0, 117, 1877], [4767, 0, -1151, -62, 0, 0], [22336, -22336, 0, 32, 32, 311], [11043, -11043, 0, 240, 240, -2305], [0, 19852, -2376, 0, -216, -2376], [-18858, 0, 1320, -114, 0, 0], [-45460, 0, 501, -42, 0, 0], [0, -8127, -1477, 0, -402, -1477], [9547, -9547, 0, 145, 145, 4289], [10416, -10416, 0, -190, -190, -666], [0, 1038, -2069, 0, 65, -2069], [0, -4005, -1283, 0, 424, -1283], [0, 18208, 1374, 0, 199, 1374], [0, -7890, -1368, 0, -100, -1368], [2880, 0, -104, -198, 0, 0], [0, -24243, -602, 0, -55, -602], [0, 19486, 1860, 0, -277, 1860], [-4115, 4115, 0, 239, 239, 317]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_342_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_342_4_g_h();".
// 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_342_4_g_h(:prec:=6)
    chi := MakeCharacter_342_g();
    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_342_4_g_h();".
// 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_342_4_g_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_342_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![254016, -48384, 14256, -48, 196, -10, 1]>],Snew);
    return Vf;
end function;