
// Make newform 1764.4.k.o in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1764_k();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1764_k_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_1764_4_k_o();".
// 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_1764_4_k_o();".
// 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_1764_k();"
function MakeCharacter_1764_k()
    N := 1764;
    order := 3;
    char_gens := [883, 785, 1081];
    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_1764_k_Hecke();"
function MakeCharacter_1764_k_Hecke(Kf)
    N := 1764;
    order := 3;
    char_gens := [883, 785, 1081];
    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], [0, 0], [0, 18], [0, 0], [36, -36], [10, 0], [-18, 18], [0, -100], [0, 72], [234, 0], [-16, 16], [0, 226], [90, 0], [452, 0], [0, -432], [414, -414], [684, -684], [0, 422], [-332, 332], [360, 0], [26, -26], [0, -512], [-1188, 0], [0, 630], [1054, 0], [-558, 558], [0, 8], [0, 1764], [-1622, 1622], [1134, 0], [-592, 0], [0, 1908], [954, -954], [-2564, 0], [0, -738], [2440, -2440], [-2554, 2554], [0, 820], [1944, 0], [0, 1242], [1116, -1116], [-1070, 0], [0, -576], [1342, -1342], [-1422, 0], [872, -872], [1340, 0], [-4880, 0], [-2700, 2700], [0, 254], [0, 4410], [3888, 0], [5138, -5138], [4788, 0], [0, 5886], [2232, -2232], [666, -666], [0, -5536], [-2126, 2126], [2934, 0], [2036, -2036], [2286, 0], [-1244, 0], [-1224, 1224], [0, 1898], [0, -9162], [0, 4348], [7154, 0], [-1836, 1836], [-5894, 0], [-11106, 11106], [0, 13176], [-6112, 6112], [0, 13618], [692, 0], [0, 8064], [12654, -12654], [0, -106], [0, -4014], [3914, -3914], [4428, 0], [-15490, 0], [6768, -6768], [-1298, 0], [0, -2248], [0, -9612], [-162, 0], [0, -1370], [-15354, 0], [-13024, 0], [0, 14436], [-12096, 12096], [-6056, 6056], [-7524, 0], [0, -5276], [4968, 0], [0, -10998], [8838, -8838], [0, 22436], [0, 4762], [-6004, 0], [-5274, 5274], [12420, -12420], [0, -21366], [-21140, 21140], [3266, -3266], [17028, 0], [0, -9522], [-10296, 10296], [3382, 0], [0, -20656], [22114, -22114], [-19962, 0], [-604, 604], [152, 0], [4194, -4194], [7252, 0], [6696, -6696], [0, 28422], [19908, 0], [14318, -14318], [30050, 0], [0, -22158], [-3132, 3132], [0, -20932], [21834, 0], [0, -12446], [0, 12528], [-11576, 0], [0, -29338], [-2540, 2540], [18792, 0], [0, -4832], [-20818, 0], [0, -12042], [-13058, 0], [11826, -11826], [11996, -11996], [6966, 0], [-40806, 40806], [17980, 0], [0, -12834], [37864, -37864], [-42516, 0], [45638, -45638], [17496, 0], [-32174, 0], [38934, -38934], [0, 29780], [0, -48096], [0, -21302], [-7470, 0], [764, 0], [0, -32328], [36316, -36316], [13392, 0], [0, -38072], [0, 12798], [-34874, 0], [-17190, 17190], [0, 40284], [-15498, 0], [37160, 0], [0, -18468], [10386, -10386], [-44136, 44136], [28432, -28432], [-39778, 39778], [-31726, 0], [0, 19458], [-2988, 2988], [-31142, 0], [0, -46728], [-42074, 42074], [-25984, 25984], [-28278, 0], [-50956, 0], [26190, -26190], [19000, 0], [0, -3274], [0, -38672], [-29844, 0], [-48994, 0], [0, 21222], [51120, -51120], [59490, -59490], [37018, -37018], [53036, -53036], [0, 23878], [-42624, 42624], [0, 38402], [11772, 0], [0, 38404], [0, -28854], [66924, -66924], [17478, -17478], [-42002, 42002], [0, -20870], [-41022, 0], [0, -64440], [6534, -6534], [-3104, 0], [0, 43406], [-22366, 22366], [-19836, 0], [35082, -35082], [0, -38224], [0, -6228], [-52506, 0], [63908, -63908], [0, -44594], [-50850, 0], [77912, 0], [0, 49932], [-6264, 6264], [0, -30790], [-34976, 34976], [9774, -9774], [0, 3816], [12618, 0], [0, 46018], [-66296, 0], [0, 50850], [0, -44768], [35100, 0], [18926, 0], [0, 76086], [67968, -67968], [0, 10376], [0, 31140], [8938, -8938], [69500, -69500], [63920, 0], [1818, -1818], [32284, 0], [49968, -49968], [0, 13490], [0, -3618], [48492, 0], [-115848, 0], [18108, -18108], [0, 109268], [-100216, 100216], [0, 17578], [-38862, 0], [0, 14328], [-18800, 0], [-42156, 42156], [77020, -77020], [-70416, 0], [51014, 0], [0, -56322], [-117864, 117864], [-84122, 0], [77274, -77274], [0, -59652], [102898, -102898], [-54412, 54412], [73710, 0], [0, -31318], [40528, -40528], [-75276, 0], [-95842, 95842], [-101510, 0], [-82818, 82818], [0, -4948], [102042, 0], [-40662, 0], [14132, 0], [78894, -78894], [0, 99926], [121348, -121348], [134858, -134858], [0, -15344], [-39890, 0], [0, 51704], [0, 130932], [-4070, 4070], [0, 103462], [0, 89028], [-50688, 50688], [-41672, 41672], [7272, 0], [-74942, 0], [0, -141148], [0, 29808], [51022, -51022], [-71838, 0], [-83656, 83656], [-11934, 0], [112806, -112806], [-71964, 71964], [0, -65430], [-87804, 0], [61526, 0], [152586, -152586], [0, 60560], [44190, 0], [0, 42444], [-106028, 0], [0, -109414], [0, 73926], [23488, -23488]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1764_k_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1764_4_k_o();".
// 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_1764_4_k_o(:prec:=2)
    chi := MakeCharacter_1764_k();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1764_4_k_o();".
// 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_1764_4_k_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1764_k();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![324, -18, 1]>,<11,R![1296, -36, 1]>,<13,R![-10, 1]>],Snew);
    return Vf;
end function;