
// Make newform 1050.4.g.n in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1050_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1050_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_1050_4_g_n();".
// 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_1050_4_g_n();".
// 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, 0, 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_1050_g();"
function MakeCharacter_1050_g()
    N := 1050;
    order := 2;
    char_gens := [701, 127, 451];
    v := [2, 1, 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_1050_g_Hecke();"
function MakeCharacter_1050_g_Hecke(Kf)
    N := 1050;
    order := 2;
    char_gens := [701, 127, 451];
    char_values := [[1, 0], [-1, 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, 2], [0, -3], [0, 0], [0, -7], [-8, 0], [0, 42], [0, -2], [124, 0], [0, -76], [-254, 0], [-72, 0], [0, 398], [462, 0], [0, -212], [0, -264], [0, 162], [772, 0], [30, 0], [0, -764], [-236, 0], [0, -418], [-552, 0], [0, -1036], [-30, 0], [0, -1190], [1370, 0], [0, -464], [0, -2136], [1226, 0], [0, -338], [0, 2088], [-292, 0], [0, 818], [2156, 0], [2850, 0], [1672, 0], [0, 446], [0, -2708], [0, 896], [0, -4034], [3480, 0], [-2898, 0], [2652, 0], [0, -146], [0, -2546], [2536, 0], [-1300, 0], [0, -2576], [0, -1836], [1874, 0], [0, -3730], [-2004, 0], [-646, 0], [1260, 0], [0, 5910], [0, -2988], [1318, 0], [-5640, 0], [0, 6446], [4930, 0], [0, 6260], [0, 2310], [0, 196], [-6736, 0], [0, -394], [0, -6714], [692, 0], [0, -1566], [0, -5328], [-11326, 0], [0, 2130], [-3044, 0], [0, 12416], [0, 7442], [-100, 0], [0, -8080], [5482, 0], [0, 10446], [-11334, 0], [-8594, 0], [-10500, 0], [-12066, 0], [4332, 0], [0, 1918], [7992, 0], [0, -3184], [-11426, 0], [0, -16934], [-17038, 0], [0, 13592], [0, 8612], [-7432, 0], [0, -6616], [17040, 0], [2948, 0], [0, -17304], [-4650, 0], [16854, 0], [0, 124], [5382, 0], [0, 17460], [0, -9514], [0, -3988], [-11346, 0], [-8436, 0], [0, 2098], [0, 9436], [0, 1314], [8940, 0], [16058, 0], [0, 3936], [0, -174], [0, 16018], [3068, 0], [24656, 0], [7594, 0], [0, 3724], [0, 3792], [0, -24702], [20144, 0], [-2522, 0], [0, 10414], [0, -22230], [0, -18192], [8108, 0], [-5794, 0], [1954, 0], [32016, 0], [0, -23072], [0, -31782], [24396, 0], [0, 32604], [-7680, 0], [0, 366], [29374, 0], [38990, 0], [0, 20470], [0, 29916], [0, 4914], [-34250, 0], [-41804, 0], [30862, 0], [0, -10576], [0, -10680], [1178, 0], [5600, 0], [0, 826], [0, 45918], [-42380, 0], [0, 26524], [0, 20614], [-23730, 0], [0, 9028], [0, 37200], [0, 23988], [15276, 0], [10760, 0], [52890, 0], [0, -6118], [-32230, 0], [0, -18544], [0, -25930], [0, 8192], [-54444, 0], [0, -25446], [0, -33192], [11024, 0], [0, -40714], [-28226, 0], [0, -48330], [-44456, 0], [61054, 0], [-3900, 0], [0, 32166], [-27984, 0], [11242, 0], [7548, 0], [-22730, 0], [0, -16688], [24946, 0], [0, -2136], [39676, 0], [0, 62690], [0, -17386], [0, 62140], [27078, 0], [0, 2022], [0, 36052], [-32346, 0], [65280, 0], [0, 13718], [0, 70824], [-28484, 0], [-5150, 0], [0, -14736], [0, 27386], [16610, 0], [0, -24646], [0, -77658], [0, -33840], [58122, 0], [17472, 0], [0, -36706], [-76410, 0], [73164, 0], [0, 35922], [-9872, 0], [0, 52704], [-25482, 0], [30140, 0], [0, -75118], [-18046, 0], [0, -8472], [0, -23980], [27792, 0], [-74086, 0], [0, 29056], [24526, 0], [0, -62084], [0, 42530], [-7778, 0], [35840, 0], [42414, 0], [0, 95264], [0, 68796], [56866, 0], [0, 49250], [-106196, 0], [0, 54336], [20208, 0], [0, 11754], [-25828, 0], [-14096, 0], [-36870, 0], [0, -52556], [0, 27312], [-79018, 0], [0, -56622], [-38880, 0], [46760, 0], [0, -48712], [-75388, 0], [0, 2872], [-39990, 0], [0, 28490], [92160, 0], [0, -24360], [-12140, 0], [-1532, 0], [0, 9084], [0, -125018], [-44658, 0], [0, -3564], [120638, 0], [0, 56702], [-19984, 0], [3758, 0], [0, 9420], [0, 14378], [0, -94582], [0, 22184], [0, 94376], [-68878, 0], [0, -34846], [0, -107298], [-109132, 0], [-27414, 0], [-42490, 0], [0, -75196], [0, -44022], [43710, 0], [0, 69404], [0, -51378], [-120224, 0], [0, -47894], [0, 18912], [0, -69560], [45690, 0], [90250, 0], [-139452, 0], [0, 22752], [-83616, 0], [0, -40824], [-69034, 0], [0, 9732], [95500, 0], [0, -94674], [0, 17950], [-26864, 0], [93450, 0], [13110, 0], [0, -47028], [0, 56190], [101948, 0], [0, 21418], [-131346, 0], [-114784, 0], [0, -161154], [13956, 0], [0, -67620], [0, -32650], [0, -55266], [41336, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1050_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1050_4_g_n();".
// 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_1050_4_g_n(:prec:=2)
    chi := MakeCharacter_1050_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(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_1050_4_g_n();".
// 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_1050_4_g_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1050_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<11,R![8, 1]>,<13,R![1764, 0, 1]>],Snew);
    return Vf;
end function;