
// Make newform 1200.4.f.g in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1200_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1200_f_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_1200_4_f_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_1200_4_f_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, 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_1200_f();"
function MakeCharacter_1200_f()
    N := 1200;
    order := 2;
    char_gens := [751, 901, 401, 577];
    v := [2, 2, 2, 1];
    // 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_1200_f_Hecke();"
function MakeCharacter_1200_f_Hecke(Kf)
    N := 1200;
    order := 2;
    char_gens := [751, 901, 401, 577];
    char_values := [[1, 0], [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, 0], [0, -3], [0, 0], [0, 8], [-20, 0], [0, 22], [0, 14], [76, 0], [0, -56], [154, 0], [-160, 0], [0, 162], [-390, 0], [0, -388], [0, -544], [0, -210], [-380, 0], [-794, 0], [0, -148], [840, 0], [0, 858], [144, 0], [0, -316], [-1098, 0], [0, -994], [-834, 0], [0, -1672], [0, -732], [970, 0], [0, 1938], [0, -528], [-636, 0], [0, -1754], [-2508, 0], [-1486, 0], [-2120, 0], [0, 1850], [0, 1172], [0, -1656], [0, -2666], [1132, 0], [-2866, 0], [-1888, 0], [0, 1282], [0, -350], [-3400, 0], [-4652, 0], [0, -4016], [0, 2316], [-94, 0], [0, -4230], [2064, 0], [4562, 0], [-2532, 0], [0, -3522], [0, 2232], [-2806, 0], [-4848, 0], [0, -7790], [-118, 0], [0, 6508], [0, -8770], [0, -4292], [9464, 0], [0, 9578], [0, 186], [492, 0], [0, -2290], [0, -6092], [-5766, 0], [0, -9374], [-3528, 0], [0, 7616], [0, 3406], [-12284, 0], [0, -5424], [-3486, 0], [0, 3626], [5874, 0], [12662, 0], [6396, 0], [8286, 0], [4112, 0], [0, 5330], [11272, 0], [0, -14196], [5886, 0], [0, 7526], [8502, 0], [0, -12672], [0, 16540], [8864, 0], [0, 3688], [16140, 0], [1580, 0], [0, -15000], [-20486, 0], [7706, 0], [0, 3932], [-23930, 0], [0, 11468], [0, 11498], [0, -16988], [17366, 0], [24860, 0], [0, 26302], [0, 7812], [0, 7986], [-21048, 0], [1738, 0], [0, 18576], [0, -13602], [0, -19578], [12308, 0], [8600, 0], [6978, 0], [0, 7668], [0, -15384], [0, -2186], [-1524, 0], [-4242, 0], [0, 24354], [0, 322], [0, 7932], [-20684, 0], [25222, 0], [-23678, 0], [8432, 0], [0, 8312], [0, -26298], [16956, 0], [0, 17880], [-22032, 0], [0, -11534], [38250, 0], [-19330, 0], [0, -40674], [0, 7004], [0, -12198], [25734, 0], [-15668, 0], [-34450, 0], [0, 38792], [0, -20460], [-5542, 0], [25240, 0], [0, -37330], [0, 3894], [20324, 0], [0, -6288], [0, 24650], [9426, 0], [0, 9316], [0, 6968], [0, -35964], [-47888, 0], [-12760, 0], [25054, 0], [0, -28282], [-30634, 0], [0, 48572], [0, 12906], [0, -5880], [-55444, 0], [0, -32050], [0, -29880], [-5216, 0], [0, -6750], [-52178, 0], [0, 37678], [-15356, 0], [-11546, 0], [9224, 0], [0, -9958], [-5872, 0], [-39690, 0], [-13828, 0], [-7874, 0], [0, 13688], [-17206, 0], [0, -39376], [-12924, 0], [0, 48446], [0, -4250], [0, 7712], [-13774, 0], [0, -5702], [0, 532], [61702, 0], [-55136, 0], [0, 62594], [0, 7260], [-428, 0], [-38810, 0], [0, -47668], [0, -3782], [31506, 0], [0, -13530], [0, 50174], [0, -41032], [-22710, 0], [80336, 0], [0, -32366], [45150, 0], [-44844, 0], [0, 61754], [-65504, 0], [0, 83188], [8358, 0], [-76820, 0], [0, 19598], [-10802, 0], [0, -45656], [0, 59892], [54968, 0], [-58182, 0], [0, -9920], [-78158, 0], [0, -18024], [0, 67430], [12062, 0], [7816, 0], [-27842, 0], [0, -80640], [0, 91356], [83474, 0], [0, 36490], [66592, 0], [0, 52904], [-61300, 0], [0, -13322], [82092, 0], [1792, 0], [-85606, 0], [0, -54564], [0, -48960], [-13554, 0], [0, -1074], [-72988, 0], [-103896, 0], [0, 93796], [-29668, 0], [0, -55336], [93290, 0], [0, -19982], [89800, 0], [0, -28784], [119396, 0], [-5612, 0], [0, 85952], [0, 41178], [95810, 0], [0, 63144], [37606, 0], [0, -39242], [11532, 0], [90158, 0], [0, -61836], [0, 29442], [0, 80982], [0, 47792], [0, 29356], [32322, 0], [0, 13382], [0, 2270], [-134084, 0], [8362, 0], [3690, 0], [0, 14988], [0, 42590], [28502, 0], [0, 117340], [0, -141558], [-105088, 0], [0, -7634], [0, 45256], [0, 85844], [78874, 0], [136922, 0], [-85548, 0], [0, -7824], [106632, 0], [0, 25912], [57022, 0], [0, -51260], [-127184, 0], [0, 64946], [0, 106674], [71400, 0], [-156834, 0], [129494, 0], [0, 77628], [0, 15018], [17580, 0], [0, 39958], [89754, 0], [-96224, 0], [0, 61422], [-137212, 0], [0, -171924], [0, 15642], [0, 94602], [-158320, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1200_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1200_4_f_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_1200_4_f_g(:prec:=2)
    chi := MakeCharacter_1200_f();
    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_1200_4_f_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_1200_4_f_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1200_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![64, 0, 1]>,<11,R![20, 1]>],Snew);
    return Vf;
end function;