
// Make newform 240.3.l.d in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_240_l();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_240_l_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_240_3_l_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_240_3_l_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 := [864, -96, 168, -136, 49, 20, -2, -4, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [8424, 3264, -6528, -866, 691, 27, -43, -1], [-2124, 4668, 243, -523, -230, 85, 7, -2], [-33096, 11472, -18728, -3692, 1955, 1215, -447, 17], [56712, 2136, -16734, -4816, 2725, 115, -151, 21], [3912, 35496, 16366, -6186, -2045, 375, 319, -69], [804, -1788, 507, -232, -145, 30, 28, -8], [-5916, 13212, -8413, 5943, 1680, -385, -287, 82]];
    Rf_basisdens := [1, 3720, 3100, 18600, 18600, 18600, 1860, 9300];
    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_240_l();"
function MakeCharacter_240_l()
    N := 240;
    order := 2;
    char_gens := [31, 181, 161, 97];
    v := [2, 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_240_l_Hecke();"
function MakeCharacter_240_l_Hecke(Kf)
    N := 240;
    order := 2;
    char_gens := [31, 181, 161, 97];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0], [0, 0, -1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [-1, 0, 0, -1, 1, -1, 1, 1], [-2, -1, -2, 1, 2, 2, -1, 1], [-4, 1, -2, 1, -2, 2, -1, -1], [0, -2, -1, -2, 0, 0, 0, 1], [2, 1, -1, -3, 2, 0, 3, 2], [3, -1, 2, -4, -3, -3, -4, 0], [0, 1, 3, 1, 0, 0, -9, 2], [-16, 1, 1, -1, 0, 6, 1, -2], [0, -1, -2, 3, -2, -6, -3, 1], [0, -5, -9, -5, 0, 0, -13, -4], [45, -1, 3, -2, 3, -1, 2, 1], [-1, 1, 4, 2, 1, 1, -2, 4], [-4, 6, 0, 10, 4, 4, -4, -2], [0, 3, -2, 3, 0, 0, -19, -5], [8, -7, 15, 7, 0, 2, -7, -8], [-31, 3, -11, 6, -9, -1, -6, -1], [-6, 4, -4, 10, 6, 6, -2, -2], [-2, 2, -2, 2, -4, 8, -2, -4], [-14, 1, -5, 1, -2, -4, -1, 2], [3, 3, 13, 0, -3, -3, -40, 7], [8, 2, 26, -6, -8, -8, 2, 16], [6, 8, -16, -4, -4, 4, 4, 4], [0, 1, -15, 1, 0, 0, -13, -16], [11, -4, -2, 15, -11, -9, -15, -5], [9, 9, 23, 0, -9, -9, -16, 5], [-16, -7, 13, 3, 4, -6, -3, -2], [-4, -10, -27, -6, 4, 4, 0, -13], [37, 12, -24, -7, -5, 5, 7, 7], [-2, -19, -20, -17, 2, 2, -15, 1], [-12, -4, -1, 8, 12, 12, -14, 15], [-6, 1, -13, 21, -22, 0, -21, -10], [-16, -8, -6, 8, 16, 16, 6, 18], [-16, -1, 9, 1, 0, 14, -1, -8], [-40, 11, -16, -21, 10, 2, 21, 15], [51, 15, -19, -20, 5, 17, 20, 9], [11, 9, 22, -2, -11, -11, 18, 2], [24, -4, 6, -28, -24, -24, -6, -14], [0, -11, -26, -11, 0, 0, 19, -15], [-170, 14, -30, 18, -32, 28, -18, -16], [10, -10, 10, -20, -10, -10, 28, 10], [206, -18, 44, -14, 32, -16, 14, 6], [4, -2, 2, -6, -4, -4, 96, 0], [2, -5, 19, -17, 22, -4, 17, 8], [-68, -3, -5, -5, 8, -30, 5, 16], [41, 0, 8, -23, 23, -7, 23, 15], [-21, -29, -49, -8, 21, 21, -32, 1], [6, -6, 36, -14, 20, 28, 14, -10], [8, 6, -29, -2, -8, -8, 12, -43], [-12, 0, -18, 12, 12, 12, 36, -6], [-192, -1, -7, 33, -32, 14, -33, -24], [10, 23, 22, 13, -10, -10, 3, -11], [4, 14, -13, 10, -4, -4, 28, -31], [15, 27, 30, 12, -15, -15, 36, -12], [-24, -24, -58, 0, 24, 24, 58, -10], [-76, 3, 1, -31, 28, -14, 31, 24], [-124, -15, 16, 1, 14, -42, -1, 13], [0, 25, -1, 25, 0, 0, -55, -26], [219, -3, 1, 38, -35, 25, -38, -33], [8, 16, 42, 8, -8, -8, 26, 18], [-173, -15, 47, -6, 21, 13, 6, -11], [-2, 16, 24, 18, 2, 2, 46, 10], [62, -12, 40, 28, -16, 48, -28, -44], [-36, -26, -38, 10, 36, 36, -108, 24], [-100, 9, -21, 31, -40, 34, -31, -28], [-42, -20, 70, 0, 20, 40, 0, -30], [15, 15, 25, 0, -15, -15, -40, -5], [-142, -20, 66, -16, 36, 16, 16, -10], [-52, -20, -57, 32, 52, 52, 6, 15], [-12, 10, 6, 22, 12, 12, 98, 8], [43, 6, 2, -43, 37, -9, 43, 29], [108, 33, -50, -43, 10, 22, 43, 27], [86, -27, 51, -27, 54, -60, 27, 30], [-15, -41, -60, -26, 15, 15, -94, -4], [8, -34, -78, -42, -8, -8, -60, -52], [100, 21, -16, -7, -14, 66, 7, -19], [56, 0, 74, -56, -56, -56, -80, 18], [164, 27, -47, -51, 24, -10, 51, 44], [-36, -47, -24, -11, 36, 36, -37, 59], [8, 33, -85, 15, -48, 10, -15, 4], [10, -14, -24, -24, -10, -10, 160, -20], [-26, -24, 72, 4, 20, 28, -4, -28], [-450, -5, 3, 3, 2, -16, -3, 4], [1, -43, -79, -44, -1, -1, -140, -37], [-16, 1, -27, 17, 16, 16, 145, -12], [10, -16, 46, -60, 76, -48, 60, 46], [32, -9, 1, -41, -32, -32, -195, -22], [209, 12, -30, -11, -1, -11, 11, 17], [-27, -3, -27, 24, 27, 27, -32, 3], [28, -48, 6, -76, -28, -28, -12, 26], [345, -34, 94, 11, 23, 29, -11, -37], [10, 35, -36, 25, -10, -10, -73, -81], [438, -11, 19, 45, -34, 28, -45, -42], [45, 43, 38, -2, -45, -45, 34, -50], [8, -27, -123, -35, -8, -8, -117, -104], [-64, -30, -26, 34, 64, 64, -2, 68], [5, -37, 57, 26, 11, -45, -26, -9], [-254, -16, 0, 8, 8, -72, -8, 24], [-233, 5, 15, -46, 41, 9, 46, 21], [-8, 62, 32, 70, 8, 8, -36, -22], [31, 21, -9, -10, -31, -31, 114, -61], [8, 15, 41, 7, -8, -8, -237, 18], [272, -43, 115, -33, 76, -18, 33, 4], [138, 30, -64, -26, -4, -4, 26, 30], [35, 45, -13, 10, -35, -35, -74, -93], [60, -32, 39, -92, -60, -60, 242, 11], [16, 30, 86, 14, -16, -16, -270, 40], [208, -3, 35, 35, -32, 90, -35, -64], [205, -4, -22, -15, 19, -79, 15, 45], [72, -5, -30, 67, -62, -18, -67, -27], [-32, -38, -107, -6, 32, 32, -92, -37], [290, 13, -21, -39, 26, -16, 39, 34], [292, -39, 65, -21, 60, -86, 21, 34], [-32, -35, -153, -3, 32, 32, 201, -86], [-55, 9, -7, -72, 63, -41, 72, 61], [13, -23, -6, -36, -13, -13, 228, 4], [84, 32, 114, -52, -84, -84, -198, -2], [0, 69, 46, 69, 0, 0, -125, -23], [16, -31, 43, -1, 32, -70, 1, 20], [70, -28, 48, 16, 12, -28, -16, -8], [28, 36, 158, 8, -28, -28, 154, 94], [-71, 39, -29, 110, 71, 71, 170, 3], [232, -15, 23, 11, 4, -18, -11, -4], [56, 60, 104, 4, -56, -56, 6, -12], [194, 32, -22, -20, -12, 96, 20, -22], [4, 28, 104, 24, -4, -4, 160, 72], [469, 14, -62, 39, -53, -15, -39, -5], [304, -37, 54, 31, 6, -46, -31, -11], [-202, 57, -105, 73, -130, 148, -73, -82], [-23, -1, -10, 22, 23, 23, -110, 14], [-12, -7, 97, -45, 52, 114, 45, -38], [236, -19, -12, 29, -10, -90, -29, 21], [8, -42, 22, -50, -8, -8, -6, 56], [286, 24, -68, -56, 32, -72, 56, 76], [-52, -78, -90, -26, 52, 52, 284, 40], [-717, -27, 81, -2, 29, 25, 2, -25], [76, 42, 98, -34, -76, -76, -72, -20], [104, 50, 108, -54, -104, -104, -54, -46], [4, -67, 155, 67, 0, 42, -67, -88], [-104, -82, -292, 22, 104, 104, 188, -106], [-329, 0, 30, 7, -7, 67, -7, -37], [29, 31, 213, 2, -29, -29, 190, 153], [-680, 13, -35, -73, 60, -78, 73, 82], [-128, -74, -190, 54, 128, 128, -30, 12], [-504, 51, -82, -29, -22, 62, 29, 9], [76, -16, 175, -92, -76, -76, 102, 115], [490, 85, -165, -95, 10, 0, 95, 90], [-23, -17, -28, 6, 23, 23, 74, 12], [-176, 75, -156, -45, -30, 18, 45, 51], [-128, 5, -59, 133, 128, 128, 129, 64], [-65, -45, 77, 68, -23, -3, -68, -55], [-95, -35, -82, 60, 95, 95, -332, 48], [-95, 13, -89, 28, -41, -85, -28, 35], [-94, 22, 44, 116, 94, 94, -28, 116], [-890, 13, -65, 17, -30, -48, -17, 22], [-16, -13, 55, 3, 16, 16, 299, 84], [870, -68, 174, -52, 120, -44, 52, 14], [-8, -93, -33, -85, 8, 8, 17, 68], [23, 55, -121, 32, -23, -23, -64, -199], [92, 126, 155, 34, -92, -92, 160, -63], [191, -12, 142, -181, 193, 43, 181, 63], [-90, -61, -242, 29, 90, 90, 171, -91], [-32, 84, 1, 116, 32, 32, -26, -51], [37, -85, -46, -122, -37, -37, -14, 2], [-228, -89, 193, 93, -4, 34, -93, -108], [756, 33, -66, -15, -18, 18, 15, 15]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_240_l_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_240_3_l_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_240_3_l_d(:prec:=8)
    chi := MakeCharacter_240_l();
    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_240_3_l_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_240_3_l_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_240_l();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<7,R![-1944, -872, -82, 8, 1]>],Snew);
    return Vf;
end function;