
// Make newform 1764.4.k.g 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_g();".
// 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_g();".
// 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, -4], [0, 0], [-20, 20], [4, 0], [-24, 24], [0, 44], [0, 72], [38, 0], [184, -184], [0, 30], [-216, 0], [-164, 0], [0, -520], [-146, 146], [-460, 460], [0, 628], [-556, 556], [-592, 0], [1024, -1024], [0, 104], [-324, 0], [0, -896], [920, 0], [-1108, 1108], [0, 1448], [0, 1316], [86, -86], [-1778, 0], [-928, 0], [0, -1404], [-1370, 1370], [-516, 0], [0, 1390], [-136, 136], [-148, 148], [0, 1212], [1976, 0], [0, 2692], [-2580, 2580], [2036, 0], [0, 3960], [-2, 2], [-3774, 0], [-3560, 3560], [-2692, 0], [-4528, 0], [3652, -3652], [0, -4804], [0, 2758], [-6528, 0], [-56, 56], [4900, 0], [0, 6784], [-4544, 4544], [4052, -4052], [0, -2752], [-4366, 4366], [-7734, 0], [-4052, 4052], [-3420, 0], [-7324, 0], [4192, -4192], [0, -6840], [0, 6630], [0, 6868], [-7378, 0], [-2676, 2676], [5124, 0], [-4560, 4560], [0, 3656], [-1616, 1616], [0, -2734], [-1380, 0], [0, -6888], [-2046, 2046], [0, 3116], [0, 2958], [7944, -7944], [4084, 0], [-6306, 0], [-11824, 11824], [4504, 0], [0, 13056], [0, 132], [-4866, 0], [0, -10106], [-18036, 0], [5288, 0], [0, -15164], [-7896, 7896], [-2920, 2920], [7932, 0], [0, 2004], [4496, 0], [0, -12620], [-18008, 18008], [0, 13292], [0, 8570], [-1916, 0], [19926, -19926], [-4244, 4244], [0, -22794], [-14028, 14028], [8368, -8368], [52, 0], [0, -5808], [-10464, 10464], [-1184, 0], [0, 13152], [18334, -18334], [8122, 0], [5980, -5980], [12528, 0], [20798, -20798], [1932, 0], [8424, -8424], [0, -17750], [27580, 0], [-9292, 9292], [11486, 0], [0, -7116], [-7612, 7612], [0, -21572], [1702, 0], [0, -6370], [0, 8808], [-17768, 0], [0, -5564], [17564, -17564], [38280, 0], [0, -36192], [-14, 0], [0, 26504], [-40184, 0], [35340, -35340], [-14852, 14852], [19788, 0], [16986, -16986], [26596, 0], [0, -34898], [-12928, 12928], [43164, 0], [-41228, 41228], [-1368, 0], [-5276, 0], [-840, 840], [0, -26028], [0, -29448], [0, -25866], [9472, 0], [49372, 0], [0, 11160], [-22708, 22708], [16192, 0], [0, 46320], [0, 2280], [-49056, 0], [24876, -24876], [0, 23428], [6678, 0], [15544, 0], [0, 31124], [50062, -50062], [-328, 328], [20872, -20872], [-46924, 46924], [13522, 0], [0, -33612], [-22812, 22812], [37764, 0], [0, -19680], [1242, -1242], [-4424, 4424], [47696, 0], [13220, 0], [27618, -27618], [4240, 0], [0, 53380], [0, -16944], [-34780, 0], [-32670, 0], [0, -48144], [-47888, 47888], [17524, -17524], [45754, -45754], [9516, -9516], [0, -42118], [-71336, 71336], [0, 20720], [22636, 0], [0, 56212], [0, -37212], [-37684, 37684], [-46864, 46864], [-47182, 47182], [0, -7066], [80992, 0], [0, 768], [44950, -44950], [58936, 0], [0, 65444], [67040, -67040], [-13428, 0], [55932, -55932], [0, 46120], [0, 65508], [-38618, 0], [53300, -53300], [0, 65586], [-90732, 0], [-41816, 0], [0, -84876], [13376, -13376], [0, -39112], [-71320, 71320], [-58200, 58200], [0, 6336], [-101146, 0], [0, -43330], [-14400, 0], [0, -7614], [0, 99712], [34956, 0], [-22990, 0], [0, 23552], [-47056, 47056], [0, -13680], [0, 71292], [-90378, 90378], [-33884, 33884], [-25736, 0], [17050, -17050], [-37212, 0], [25184, -25184], [0, -9416], [0, 52718], [-37748, 0], [-19280, 0], [-4348, 4348], [0, -88436], [-27856, 27856], [0, 89482], [59232, 0], [0, 2472], [-48064, 0], [53748, -53748], [-53740, 53740], [-5960, 0], [-92346, 0], [0, 110768], [-54192, 54192], [-75520, 0], [81612, -81612], [0, 98044], [89038, -89038], [-71756, 71756], [-4620, 0], [0, -32536], [-89504, 89504], [-53156, 0], [-120556, 120556], [62940, 0], [-36168, 36168], [0, 6740], [-15222, 0], [-51360, 0], [-71212, 0], [-101566, 101566], [0, -142500], [96428, -96428], [368, -368], [0, -143584], [-109280, 0], [0, -5392], [0, 51580], [-52678, 52678], [0, 117350], [0, 108372], [53488, -53488], [-30080, 30080], [153896, 0], [154588, 0], [0, 153244], [0, -44784], [69838, -69838], [2418, 0], [-79640, 79640], [-32208, 0], [81558, -81558], [74708, -74708], [0, 85014], [-78068, 0], [43830, 0], [-43820, 43820], [0, 9664], [-19508, 0], [0, -163572], [-82468, 0], [0, -4952], [0, -83574], [60056, -60056]];
    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_g();".
// 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_g(: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_g();".
// 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_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1764_k();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![16, 4, 1]>,<11,R![400, 20, 1]>,<13,R![-4, 1]>],Snew);
    return Vf;
end function;