
// Make newform 144.3.q.d in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_144_q();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_144_q_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_144_3_q_d();".
// 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_144_3_q_d();".
// 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 := [4, 0, -2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 4, 0, 0], [0, 0, 1, 0], [0, 0, 0, 2]];
    Rf_basisdens := [1, 1, 2, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_144_q();"
function MakeCharacter_144_q()
    N := 144;
    order := 6;
    char_gens := [127, 37, 65];
    v := [6, 6, 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_144_q_Hecke();"
function MakeCharacter_144_q_Hecke(Kf)
    N := 144;
    order := 6;
    char_gens := [127, 37, 65];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 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 := 3;
    raw_aps := [[0, 0, 0, 0], [3, 0, 0, 0], [1, -1, 1, 0], [0, 1, 3, 1], [6, 2, -3, -2], [-7, 1, 7, -2], [8, 0, -16, 2], [-2, -4, 0, 2], [-5, 1, -5, 0], [2, -3, -1, 3], [-37, 1, 37, -2], [-30, 4, 0, -2], [-23, 0, -23, 0], [0, 4, -5, 4], [-58, -3, 29, 3], [32, 0, -64, -8], [3, -2, 3, 0], [0, -1, -31, -1], [11, 10, -11, -20], [24, 0, -48, -2], [10, 12, 0, -6], [0, -7, 43, -7], [22, -14, -11, 14], [24, 0, -48, 6], [0, -2, 121, -2], [18, -3, -9, 3], [67, -7, -67, 14], [-40, 0, 80, 6], [34, -4, 0, 2], [17, -24, 17, 0], [86, 20, 0, -10], [35, 8, 35, 0], [-142, -12, 71, 12], [3, -10, -3, 20], [89, -11, 89, 0], [0, 7, -85, 7], [65, 15, -65, -30], [-114, -12, 0, 6], [99, 3, 99, 0], [50, 3, -25, -3], [-56, 0, 112, -30], [-78, -4, 0, 2], [-122, -39, 61, 39], [-119, 10, 119, -20], [40, 0, -80, 18], [62, 0, 0, 0], [-149, -2, 149, 4], [0, -13, -29, -13], [198, -48, -99, 48], [161, 5, -161, -10], [112, 0, -224, -20], [11, 11, 11, 0], [0, 4, 321, 4], [-120, 0, 240, 34], [1, 64, 1, 0], [246, 29, -123, -29], [-32, 0, 64, -40], [70, -4, 0, 2], [0, 3, -55, 3], [178, 32, -89, -32], [83, -10, -83, 20], [-223, 15, -223, 0], [-210, 52, 0, -26], [-21, -51, -21, 0], [0, -6, -255, -6], [34, -15, -17, 15], [0, -2, 35, -2], [81, -16, -81, 32], [-229, -48, -229, 0], [0, -15, 393, -15], [274, -10, -137, 10], [48, 0, -96, 60], [0, 41, 139, 41], [25, 15, -25, -30], [406, -48, 0, 24], [-45, 117, -45, 0], [-206, -47, 103, 47], [-46, -108, 0, 54], [153, -18, 153, 0], [33, 58, -33, -116], [-149, 78, -149, 0], [0, -27, 97, -27], [-320, 0, 640, 16], [-278, 84, 0, -42], [0, 27, -357, 27], [566, -56, -283, 56], [-216, 0, 432, 10], [0, 56, -23, 56], [-318, 49, 159, -49], [203, 33, -203, -66], [-136, 0, 272, -114], [-746, 7, 373, -7], [-58, 68, 0, -34], [-13, 30, -13, 0], [531, -8, -531, 16], [232, 0, -464, -30], [57, 63, 57, 0], [-384, 0, 768, 32], [566, -16, 0, 8], [138, 96, 0, -48], [0, -28, -589, -28], [96, 0, -192, 88], [-101, -74, -101, 0], [258, 142, -129, -142], [-165, 56, 165, -112], [26, -164, 0, 82], [-906, 20, 453, -20], [264, 0, -528, -14], [-29, 159, -29, 0], [0, -8, -583, -8], [419, -45, -419, 90], [-910, -52, 0, 26], [137, 72, 137, 0], [0, 30, 835, 30], [-202, 12, 0, -6], [258, -80, -129, 80], [-933, -18, 933, 36], [464, 0, -928, -44], [-207, -99, -207, 0], [198, 12, -99, -12], [-287, -7, 287, 14], [0, 70, -103, 70], [-526, 49, 263, -49], [-200, 0, 400, -178], [0, 42, -325, 42], [656, 0, -1312, -12], [0, 55, 265, 55], [-208, 0, 416, -52], [0, -33, 843, -33], [-359, 53, 359, -106], [358, -48, 0, 24], [211, 55, 211, 0], [627, -29, -627, 58], [410, -176, 0, 88], [201, -20, 201, 0], [49, 60, -49, -120], [128, 0, -256, 144], [-1373, 4, 1373, -8], [233, 59, 233, 0], [-344, 0, 688, -118], [-322, -156, 0, 78], [1154, 75, -577, -75], [-229, -21, 229, 42], [424, 0, -848, 10], [82, -220, 0, 110], [-1082, -69, 541, 69], [0, 81, 577, 81], [-366, -156, 183, 156], [219, -16, -219, 32], [368, 0, -736, 140], [969, 13, -969, -26], [488, 0, -976, -70], [-338, -44, 0, 22], [571, -103, 571, 0], [0, 12, -581, 12], [-618, -109, 309, 109], [-298, -132, 0, 66], [-1230, 2, 615, -2], [-438, 204, 0, -102], [-95, 9, -95, 0], [230, -48, -115, 48], [-120, 0, 240, -174], [-85, 11, 85, -22], [352, 0, -704, -72], [337, 96, 337, 0], [-234, 93, 117, -93], [-1354, -124, 0, 62], [0, -45, -679, -45]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_144_q_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_144_3_q_d();".
// 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_144_3_q_d(:prec:=4)
    chi := MakeCharacter_144_q();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_144_3_q_d();".
// 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_144_3_q_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_144_q();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![841, 174, -17, -6, 1]>],Snew);
    return Vf;
end function;