
// Make newform 1764.4.k.m in Magma, downloaded from the LMFDB on 25 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_m();".
// 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_m();".
// 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, 8], [0, 0], [-40, 40], [12, 0], [58, -58], [0, 26], [0, -64], [62, 0], [252, -252], [0, -26], [6, 0], [416, 0], [0, 396], [-450, 450], [-274, 274], [0, -576], [476, -476], [448, 0], [-158, 158], [0, 936], [530, 0], [0, 390], [-214, 0], [-1432, 1432], [0, 764], [0, 324], [1334, -1334], [-1798, 0], [-384, 0], [0, 1814], [1666, -1666], [-1126, 0], [0, 2694], [2648, -2648], [-556, 556], [0, 328], [-4268, 0], [0, 3476], [2268, -2268], [276, 0], [0, -3000], [-3278, 3278], [2362, 0], [-1036, 1036], [3524, 0], [1336, 0], [-1290, 1290], [0, 5524], [0, 6314], [3960, 0], [-7018, 7018], [-2394, 0], [0, 2766], [7968, -7968], [2900, -2900], [0, 2640], [1522, -1522], [4534, 0], [4834, -4834], [-4656, 0], [7238, 0], [-1096, 1096], [0, -3818], [0, 1998], [0, 7936], [2766, 0], [8352, -8352], [5924, 0], [-2226, 2226], [0, 3880], [-2584, 2584], [0, -10534], [-4472, 0], [0, -2468], [-1046, 1046], [0, 2124], [0, 11598], [-2770, 2770], [9438, 0], [5550, 0], [-3000, 3000], [-12926, 0], [0, -408], [0, -14452], [-10258, 0], [0, 5498], [-16316, 0], [8944, 0], [0, 9422], [-13820, 13820], [13264, -13264], [5940, 0], [0, 8252], [4704, 0], [0, 10788], [14586, -14586], [0, 26], [0, -11214], [-5424, 0], [-17618, 17618], [3562, -3562], [0, 2838], [360, -360], [22018, -22018], [1454, 0], [0, -13818], [6696, -6696], [-10010, 0], [0, 2880], [-6522, 6522], [-6614, 0], [5266, -5266], [3344, 0], [-4882, 4882], [15898, 0], [-6132, 6132], [0, -24198], [-17456, 0], [-656, 656], [-18214, 0], [0, -30252], [-10836, 10836], [0, 9578], [-12442, 0], [0, 25174], [0, -34188], [5204, 0], [0, -32880], [3912, -3912], [16008, 0], [0, -9960], [12378, 0], [0, -34670], [10898, 0], [25808, -25808], [-21054, 21054], [-24276, 0], [21526, -21526], [12806, 0], [0, 13214], [-32248, 32248], [14316, 0], [25168, -25168], [9356, 0], [-2372, 0], [-11694, 11694], [0, -20506], [0, -28136], [0, 37070], [-6198, 0], [-31876, 0], [0, 132], [-38244, 38244], [7008, 0], [0, -36664], [0, -45510], [3838, 0], [16832, -16832], [0, -40928], [24070, 0], [-17152, 0], [0, 32910], [-6822, 6822], [48420, -48420], [49216, -49216], [35264, -35264], [-52930, 0], [0, 10860], [20284, -20284], [-29716, 0], [0, 5088], [19830, -19830], [-66276, 66276], [-14078, 0], [53988, 0], [-24166, 24166], [-2672, 0], [0, -16672], [0, -6456], [70838, 0], [26050, 0], [0, -45082], [-19904, 19904], [-64092, 64092], [-24870, 24870], [34922, -34922], [0, 50910], [-61428, 61428], [0, 39394], [15012, 0], [0, -25756], [0, 49324], [51324, -51324], [-2690, 2690], [-8654, 8654], [0, 68702], [25146, 0], [0, 47544], [6726, -6726], [-68196, 0], [0, -50704], [-9030, 9030], [44010, 0], [-31344, 31344], [0, -17356], [0, 25320], [48306, 0], [-38434, 38434], [0, 9538], [-75224, 0], [56232, 0], [0, 34526], [-31080, 31080], [0, -45442], [56432, -56432], [71602, -71602], [0, -59992], [1942, 0], [0, 20702], [-4024, 0], [0, -16302], [0, 21848], [-15942, 0], [-25678, 0], [0, 79414], [66472, -66472], [0, 77760], [0, 52872], [-47706, 47706], [92926, -92926], [55312, 0], [-19254, 19254], [-19946, 0], [-28128, 28128], [0, 83014], [0, -6090], [37908, 0], [-65696, 0], [95072, -95072], [0, -28870], [-49024, 49024], [0, -52230], [-35926, 0], [0, 58092], [89424, 0], [73222, -73222], [-80240, 80240], [33552, 0], [51886, 0], [0, 56670], [36240, -36240], [-30394, 0], [-27440, 27440], [0, -34036], [30430, -30430], [-53874, 53874], [-67016, 0], [0, 55934], [-43000, 43000], [15656, 0], [-120584, 120584], [63632, 0], [-34134, 34134], [0, 41038], [26946, 0], [-58310, 0], [22228, 0], [139986, -139986], [0, -14388], [39616, -39616], [-107886, 107886], [0, 82576], [-119698, 0], [0, 139728], [0, -115648], [80706, -80706], [0, -103630], [0, -68418], [-100968, 100968], [-106296, 106296], [-55380, 0], [116416, 0], [0, -15414], [0, 79424], [141726, -141726], [-111494, 0], [115148, -115148], [-21870, 0], [5526, -5526], [61950, -61950], [0, 6822], [80314, 0], [61958, 0], [-56364, 56364], [0, -124288], [110748, 0], [0, 25770], [73050, 0], [0, 67246], [0, 37818], [-8128, 8128]];
    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_m();".
// 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_m(: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_m();".
// 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_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1764_k();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![64, -8, 1]>,<11,R![1600, 40, 1]>,<13,R![-12, 1]>],Snew);
    return Vf;
end function;