
// Make newform 588.4.i.j in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_588_i();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_588_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_588_4_i_j();".
// 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_588_4_i_j();".
// 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 := [2304, 48, 49, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [2304, -49, 49, -1], [97, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2352, 49];
    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_588_i();"
function MakeCharacter_588_i()
    N := 588;
    order := 3;
    char_gens := [295, 197, 493];
    v := [3, 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_588_i_Hecke();"
function MakeCharacter_588_i_Hecke(Kf)
    N := 588;
    order := 3;
    char_gens := [295, 197, 493];
    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], [3, 0, -3, 0], [0, -1, 6, 0], [0, 0, 0, 0], [-1, 7, -6, 7], [0, 0, 0, -5], [52, -4, -48, -4], [0, -3, 35, 0], [0, 20, -48, 0], [143, 0, 0, -11], [171, 20, -191, 20], [0, -45, 25, 0], [72, 0, 0, 18], [362, 0, 0, -3], [0, -36, -90, 0], [-243, -9, 252, -9], [113, -53, -60, -53], [0, 40, -286, 0], [-94, 77, 17, 77], [778, 0, 0, 44], [634, -53, -581, -53], [0, 62, -761, 0], [755, 0, 0, -101], [0, 42, -1008, 0], [-295, 0, 0, 29], [2, 184, -186, 184], [0, -111, -457, 0], [0, -207, -72, 0], [-46, -31, 77, -31], [872, 0, 0, -110], [1233, 0, 0, -106], [0, 335, 6, 0], [-278, 2, 276, 2], [182, 0, 0, -181], [0, 78, 2268, 0], [-451, -301, 752, -301], [1052, -402, -650, -402], [0, 96, -1484, 0], [2374, 0, 0, -4], [0, -490, 312, 0], [-876, 246, 630, 246], [-2664, 0, 0, -209], [0, -18, 3402, 0], [2203, 378, -2581, 378], [-1334, 0, 0, -202], [-628, 132, 496, 132], [-2062, 0, 0, 378], [-1457, 0, 0, 609], [-5793, -57, 5850, -57], [0, 107, 1193, 0], [0, 434, 3066, 0], [-588, 0, 0, -618], [-553, 903, -350, 903], [543, 0, 0, -327], [0, 336, -4374, 0], [-5778, 54, 5724, 54], [-6061, 379, 5682, 379], [0, 55, 1712, 0], [5488, -393, -5095, -393], [-3590, 0, 0, 722], [-158, -971, 1129, -971], [825, 0, 0, -177], [-76, 0, 0, 143], [-8152, -194, 8346, -194], [0, -526, -2425, 0], [0, -735, 4248, 0], [0, -543, 307, 0], [-2871, 0, 0, 164], [-2940, -372, 3312, -372], [-1042, 0, 0, 200], [-6312, -582, 6894, -582], [0, -326, 6996, 0], [-967, 840, 127, 840], [0, 883, -203, 0], [2584, 0, 0, 169], [0, 274, 6780, 0], [106, -1516, 1410, -1516], [0, 385, -925, 0], [0, -540, -3564, 0], [5807, -450, -5357, -450], [-1968, 0, 0, 726], [3774, 0, 0, -1225], [444, -174, -270, -174], [-6686, 0, 0, 1359], [0, 1471, -256, 0], [0, 791, -4836, 0], [-2896, 0, 0, -626], [0, 598, -10157, 0], [4246, 0, 0, -346], [6736, 0, 0, 1081], [0, -406, 1986, 0], [7126, -490, -6636, -490], [-7829, 468, 7361, 468], [6837, 0, 0, -249], [0, 397, -12767, 0], [4152, 0, 0, -354], [0, -1671, -8118, 0], [4158, -2322, -1836, -2322], [0, 3, -3067, 0], [0, 491, 2239, 0], [6252, 0, 0, -760], [4747, 1007, -5754, 1007], [12949, 221, -13170, 221], [0, 1446, -13590, 0], [30, -797, 767, -797], [-3473, 1744, 1729, 1744], [5225, 0, 0, 2359], [0, 1194, -10566, 0], [-3786, -426, 4212, -426], [-3467, 0, 0, 156], [0, -116, -16927, 0], [-7212, -1790, 9002, -1790], [-6902, 0, 0, 1856], [-9206, 151, 9055, 151], [-13555, 0, 0, -1845], [-7714, -1550, 9264, -1550], [8626, 0, 0, 1527], [9030, -264, -8766, -264], [0, -2091, 342, 0], [2056, 0, 0, -1324], [5266, 1417, -6683, 1417], [7277, 0, 0, -582], [0, 1581, 7020, 0], [22197, 303, -22500, 303], [0, -2001, -4303, 0], [-21841, 0, 0, 49], [0, 984, 19882, 0], [0, 552, 17442, 0], [19529, 0, 0, -166], [0, 397, 16805, 0], [-17274, 2503, 14771, 2503], [-9306, 0, 0, 4176], [0, 1814, 13813, 0], [7770, 0, 0, 2744], [0, -1208, 3360, 0], [-4573, 0, 0, 1166], [-1150, 2020, -870, 2020], [-18114, 1178, 16936, 1178], [14607, 0, 0, 1305], [-32504, -262, 32766, -262], [-19186, 0, 0, 3698], [0, -553, 15270, 0], [19592, -3928, -15664, -3928], [-11355, 0, 0, -1263], [-11960, -491, 12451, -491], [-24364, 0, 0, 3328], [20338, 0, 0, 615], [-23034, -2886, 25920, -2886], [0, 2094, -10648, 0], [0, -2852, 8058, 0], [0, -686, 18094, 0], [-24462, 0, 0, 1926], [-22960, 0, 0, 297], [0, -1518, 15714, 0], [-13644, -3245, 16889, -3245], [-28026, 0, 0, 3330], [0, -2183, 21271, 0], [0, -6132, -6498, 0], [-17435, 0, 0, -5004], [5729, -3221, -2508, -3221], [0, -14, -6306, 0], [-9470, 0, 0, 2426], [9683, 0, 0, -3606], [0, -3129, -8244, 0], [-35394, 2868, 32526, 2868], [28572, -1104, -27468, -1104], [7129, 3408, -10537, 3408], [-2218, -3039, 5257, -3039]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_588_i_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_588_4_i_j();".
// 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_588_4_i_j(:prec:=4)
    chi := MakeCharacter_588_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_588_4_i_j();".
// 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_588_4_i_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_588_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![324, 198, 139, -11, 1]>],Snew);
    return Vf;
end function;