
// Make newform 140.4.e.c in Magma, downloaded from the LMFDB on 06 February 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_140_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_140_e_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_140_4_e_c();".
// 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_140_4_e_c();".
// 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 := [9, 0, 0, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 0, 7, 0], [0, 3, 0, 1], [0, 9, 0, -4]];
    Rf_basisdens := [1, 3, 3, 3];
    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_140_e();"
function MakeCharacter_140_e()
    N := 140;
    order := 2;
    char_gens := [71, 57, 101];
    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_140_e_Hecke();"
function MakeCharacter_140_e_Hecke(Kf)
    N := 140;
    order := 2;
    char_gens := [71, 57, 101];
    char_values := [[1, 0, 0, 0], [-1, 0, 0, 0], [1, 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]];
    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, 0, 1, 0], [1, 1, 0, 1], [0, -1, 0, 0], [24, 0, -2, -4], [0, -2, 23, 0], [0, 2, 24, 0], [44, 0, 1, 2], [0, 2, -2, 0], [70, 0, -2, -4], [52, 0, 2, 4], [0, 26, -48, 0], [90, 0, -22, -44], [0, -28, -94, 0], [0, 4, -70, 0], [0, -66, 50, 0], [-284, 0, 23, 46], [-234, 0, 23, 46], [0, 16, -288, 0], [-486, 0, 2, 4], [0, -94, -98, 0], [-482, 0, -50, -100], [0, -48, -287, 0], [826, 0, 24, 48], [0, 102, -20, 0], [582, 0, 47, 94], [0, -248, 94, 0], [0, 208, 192, 0], [-170, 0, 46, 92], [0, -58, 524, 0], [0, 182, -530, 0], [236, 0, 47, 94], [0, -88, 812, 0], [-52, 0, -71, -142], [-366, 0, -140, -280], [-768, 0, 104, 208], [0, -282, -745, 0], [0, 132, -914, 0], [0, 240, 286, 0], [0, -6, -1075, 0], [1076, 0, -4, -8], [-3310, 0, -5, -10], [670, 0, -186, -372], [0, -46, -616, 0], [0, -106, -1342, 0], [-1452, 0, 70, 140], [-1932, 0, -192, -384], [0, 148, -456, 0], [0, 336, -189, 0], [-4366, 0, 55, 110], [0, 364, 860, 0], [1408, 0, -196, -392], [1878, 0, 138, 276], [716, 0, 167, 334], [0, 54, 450, 0], [0, 288, 1388, 0], [190, 0, -31, -62], [2220, 0, 0, 0], [0, -610, -1538, 0], [-162, 0, 144, 288], [0, 848, -643, 0], [0, 570, -965, 0], [0, -152, 1723, 0], [8412, 0, -120, -240], [0, 1010, 536, 0], [0, -502, 422, 0], [1844, 0, 522, 1044], [0, -1210, 104, 0], [0, 1056, -1582, 0], [-762, 0, 121, 242], [0, 606, 1654, 0], [-3544, 0, 344, 688], [0, 752, -1368, 0], [0, -418, -762, 0], [2600, 0, -294, -588], [0, 108, -3914, 0], [-1626, 0, 50, 100], [0, -298, -1125, 0], [5424, 0, 468, 936], [-2410, 0, -562, -1124], [-956, 0, 431, 862], [-8246, 0, 436, 872], [-6568, 0, -628, -1256], [0, 490, 1364, 0], [1672, 0, 468, 936], [0, -1848, -2212, 0], [7786, 0, 480, 960], [0, -1526, 2504, 0], [-4886, 0, 123, 246], [0, 640, -872, 0], [0, 1304, 1057, 0], [-14856, 0, -234, -468], [0, 266, 3990, 0], [-11976, 0, -232, -464], [1256, 0, 340, 680], [0, -1532, -1152, 0], [2146, 0, -985, -1970], [21618, 0, 78, 156], [0, -1328, 5359, 0], [13982, 0, -184, -368], [0, 1652, 4938, 0], [0, 1270, 2070, 0], [0, -856, -1719, 0], [-1416, 0, 188, 376], [-9256, 0, 962, 1924], [0, -122, -610, 0], [0, 1568, -6593, 0], [0, -42, 410, 0], [402, 0, -386, -772], [-10906, 0, -132, -264], [0, -716, -492, 0], [0, -2118, -380, 0], [0, -86, 3560, 0], [1484, 0, -1177, -2354], [-6354, 0, -1162, -2324], [-22592, 0, 292, 584], [0, -8, 2009, 0], [0, 2148, 2338, 0], [0, 198, -7972, 0], [-4980, 0, -482, -964], [23218, 0, 565, 1130], [0, -280, -6332, 0], [0, 1742, -3553, 0], [0, -1976, -1832, 0], [15124, 0, 967, 1934], [-21550, 0, 570, 1140], [-2622, 0, -706, -1412], [-836, 0, -134, -268], [0, -232, 5382, 0], [0, -2734, -761, 0], [14300, 0, 950, 1900], [0, -642, 4086, 0], [17392, 0, -148, -296], [0, -1602, 3980, 0], [10822, 0, -550, -1100], [-13846, 0, -1386, -2772], [0, -2874, 5133, 0], [0, -1536, 11089, 0], [0, 3482, 7885, 0], [17684, 0, 40, 80], [660, 0, -1933, -3866], [9018, 0, -1636, -3272], [0, -192, 12160, 0], [0, 1996, 11368, 0], [-11662, 0, 1097, 2194], [11008, 0, 742, 1484], [0, 4082, -3539, 0], [0, -1790, -2944, 0], [4260, 0, 1543, 3086], [0, 3040, 5532, 0], [0, 398, 996, 0], [4722, 0, -1780, -3560], [0, 4600, -2012, 0], [0, -732, 9270, 0], [0, -6872, 572, 0], [-25600, 0, -344, -688], [-5258, 0, -1534, -3068], [-18390, 0, 254, 508], [0, 338, -18910, 0], [39386, 0, -407, -814], [0, 3336, -3726, 0], [0, 3828, 2932, 0], [0, -290, -5534, 0], [-3948, 0, 217, 434], [0, 2304, 468, 0], [0, -2708, -7590, 0], [-20978, 0, 282, 564], [0, 1682, -3357, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_140_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_140_4_e_c();".
// 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_140_4_e_c(:prec:=4)
    chi := MakeCharacter_140_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_140_4_e_c();".
// 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_140_4_e_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_140_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![6, 0, 1]>],Snew);
    return Vf;
end function;