
// Make newform 1008.3.m.e in Magma, downloaded from the LMFDB on 27 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1008_m();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1008_m_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_1008_3_m_e();".
// 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_1008_3_m_e();".
// 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, -2, -1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-5, 0, 0, 2], [4, 12, 4, -4], [12, 4, -4, -4]];
    Rf_basisdens := [1, 1, 1, 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_1008_m();"
function MakeCharacter_1008_m()
    N := 1008;
    order := 2;
    char_gens := [127, 757, 785, 577];
    v := [1, 2, 2, 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_1008_m_Hecke();"
function MakeCharacter_1008_m_Hecke(Kf)
    N := 1008;
    order := 2;
    char_gens := [127, 757, 785, 577];
    char_values := [[-1, 0, 0, 0], [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+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], [2, 0, 0, 0], [0, -1, 0, 0], [0, 4, 0, 1], [2, 0, -1, 0], [-14, 0, 1, 0], [0, 8, 0, 1], [0, 4, 0, -3], [14, 0, 2, 0], [0, 0, 0, -4], [26, 0, -2, 0], [10, 0, -1, 0], [0, 8, 0, 4], [0, 24, 0, 4], [-10, 0, 2, 0], [0, 8, 0, 3], [-6, 0, -1, 0], [0, 0, 0, -8], [0, 28, 0, -3], [58, 0, 2, 0], [0, 0, 0, 14], [0, 32, 0, -3], [-38, 0, -3, 0], [-22, 0, 4, 0], [-38, 0, -2, 0], [0, 32, 0, -8], [0, 20, 0, -21], [-70, 0, 2, 0], [62, 0, -4, 0], [0, 32, 0, 0], [0, -16, 0, 17], [-10, 0, -10, 0], [0, 32, 0, 21], [22, 0, 6, 0], [0, 8, 0, -6], [42, 0, -7, 0], [0, 16, 0, 30], [0, 8, 0, -36], [186, 0, 8, 0], [0, -20, 0, 17], [-78, 0, -5, 0], [0, 12, 0, -39], [-78, 0, -8, 0], [102, 0, -6, 0], [0, 40, 0, -38], [0, -104, 0, 10], [0, -104, 0, 16], [0, -104, 0, -1], [2, 0, -5, 0], [262, 0, 2, 0], [0, -44, 0, 5], [-326, 0, 0, 0], [0, -120, 0, 9], [-38, 0, 5, 0], [0, 68, 0, 31], [82, 0, 6, 0], [0, 16, 0, -10], [122, 0, 4, 0], [70, 0, 10, 0], [0, -72, 0, -7], [-262, 0, -14, 0], [0, 24, 0, -11], [0, -24, 0, 34], [-30, 0, -2, 0], [342, 0, -14, 0], [0, -200, 0, 12], [226, 0, 12, 0], [0, -132, 0, -13], [-278, 0, -3, 0], [-350, 0, 15, 0], [0, 44, 0, -33], [0, -88, 0, -8], [122, 0, 28, 0], [0, 72, 0, 32], [0, 16, 0, 2], [-2, 0, -22, 0], [106, 0, 21, 0], [358, 0, -6, 0], [-150, 0, -12, 0], [0, 8, 0, 13], [-70, 0, 20, 0], [0, -196, 0, -21], [-446, 0, -18, 0], [0, -72, 0, 80], [0, -276, 0, 1], [558, 0, 16, 0], [162, 0, 24, 0], [-758, 0, 0, 0], [0, -16, 0, -66], [0, 88, 0, 27], [0, -104, 0, -60], [0, 88, 0, -10], [0, -20, 0, -19], [0, 8, 0, -86], [0, 96, 0, -8], [466, 0, 14, 0], [-542, 0, -11, 0], [0, 176, 0, -33], [106, 0, -32, 0], [0, -80, 0, 74], [-266, 0, -10, 0], [0, 168, 0, -75], [294, 0, 30, 0], [0, 32, 0, 102], [-238, 0, 8, 0], [0, -8, 0, 65], [2, 0, 47, 0], [0, 300, 0, -7], [-94, 0, -8, 0], [0, 104, 0, 52], [-38, 0, -6, 0], [550, 0, -34, 0], [0, -160, 0, 47], [0, 192, 0, -102], [134, 0, 26, 0], [0, -8, 0, -109], [0, 296, 0, -4], [718, 0, 2, 0], [0, 148, 0, -47], [-150, 0, -17, 0], [-62, 0, -28, 0], [-102, 0, -12, 0], [0, 188, 0, -79], [0, -240, 0, -13], [478, 0, -6, 0], [618, 0, -10, 0], [0, 80, 0, -78], [0, -112, 0, -68], [178, 0, 13, 0], [0, 416, 0, -26], [0, 124, 0, 39], [0, 104, 0, -18], [810, 0, 2, 0], [298, 0, -5, 0], [-94, 0, -6, 0], [650, 0, -10, 0], [0, 192, 0, 143], [-1094, 0, -16, 0], [-610, 0, -20, 0], [0, 96, 0, -145], [1094, 0, -6, 0], [0, 288, 0, 90], [0, 132, 0, -33], [-430, 0, 21, 0], [0, 280, 0, 76], [-806, 0, 1, 0], [10, 0, -57, 0], [0, -232, 0, 47], [0, -332, 0, 55], [-566, 0, 14, 0], [-174, 0, -65, 0], [0, 216, 0, 110], [0, -56, 0, -184], [0, -488, 0, 34], [0, -308, 0, 83], [0, -104, 0, -60], [314, 0, -33, 0], [146, 0, 36, 0], [354, 0, 48, 0], [0, 372, 0, 121], [1614, 0, 12, 0], [0, 464, 0, -4], [0, -448, 0, 17], [70, 0, -18, 0], [0, 32, 0, -10], [0, -344, 0, -48], [586, 0, -1, 0], [594, 0, -8, 0], [842, 0, -28, 0], [0, -188, 0, -97], [970, 0, -39, 0], [0, -356, 0, -39], [674, 0, 12, 0], [0, 264, 0, -26], [-446, 0, 79, 0], [0, -352, 0, 96], [46, 0, 34, 0], [0, 336, 0, 82], [90, 0, -73, 0], [0, -240, 0, 4], [0, 32, 0, 23], [106, 0, -38, 0], [-206, 0, -5, 0], [0, -348, 0, 101], [826, 0, 44, 0], [-566, 0, 0, 0], [0, -176, 0, 91], [-1214, 0, -32, 0], [0, -736, 0, -30], [-1174, 0, -14, 0], [0, -92, 0, -109], [0, 392, 0, 14], [-726, 0, 80, 0], [0, 340, 0, 59], [-1726, 0, -9, 0], [-654, 0, 16, 0], [1338, 0, -30, 0], [458, 0, 17, 0], [0, 280, 0, 98], [774, 0, -58, 0], [0, -440, 0, 98], [850, 0, 81, 0], [346, 0, -22, 0], [0, 192, 0, -43], [1066, 0, -54, 0], [0, -152, 0, 66], [0, 4, 0, -135], [1742, 0, -20, 0], [0, -392, 0, 27], [754, 0, 76, 0], [178, 0, 4, 0], [0, -440, 0, -26], [0, 192, 0, 97], [0, -216, 0, 250], [-606, 0, 34, 0], [0, -56, 0, -276], [-830, 0, -43, 0], [0, 292, 0, -161], [-658, 0, -94, 0], [970, 0, -58, 0], [0, 208, 0, 170], [-226, 0, -60, 0], [0, -232, 0, 218], [0, 496, 0, -63], [-262, 0, 22, 0], [666, 0, 85, 0], [0, -44, 0, 209], [0, 192, 0, 50], [0, -132, 0, -113], [-262, 0, 102, 0], [0, 632, 0, 11], [0, 88, 0, 172], [1278, 0, -52, 0], [0, 536, 0, -39], [0, 656, 0, 116], [1898, 0, 8, 0], [-1418, 0, 46, 0], [0, -4, 0, 197], [0, 192, 0, 196], [0, 236, 0, 147], [0, -208, 0, -197], [0, 408, 0, -160], [10, 0, 108, 0], [1898, 0, -7, 0], [0, 40, 0, -108], [0, -656, 0, -114], [0, 360, 0, -53], [0, 384, 0, 94], [0, 356, 0, -165], [202, 0, -64, 0], [-1790, 0, -57, 0], [0, 252, 0, -159], [826, 0, 42, 0], [74, 0, -94, 0], [0, 196, 0, -365], [-406, 0, 38, 0], [0, 424, 0, -241], [234, 0, 2, 0], [706, 0, -130, 0], [0, 368, 0, -140], [0, 716, 0, -133], [402, 0, -35, 0], [570, 0, 71, 0], [258, 0, 105, 0], [0, -96, 0, -215], [-858, 0, 54, 0], [946, 0, 71, 0], [0, -656, 0, 168], [270, 0, 90, 0], [1754, 0, 33, 0], [0, 632, 0, -40], [-358, 0, -82, 0], [0, 536, 0, 150], [1018, 0, -102, 0], [0, -368, 0, -166], [0, 68, 0, -163], [2506, 0, -24, 0], [-1694, 0, 64, 0], [0, 200, 0, 231], [0, 144, 0, -96], [0, -112, 0, 186], [0, 496, 0, 2], [202, 0, -153, 0], [0, 272, 0, 187], [0, -500, 0, -197], [994, 0, -8, 0], [-2170, 0, 62, 0], [0, 352, 0, -40], [1914, 0, 1, 0], [-602, 0, 14, 0], [0, -16, 0, -27], [-1850, 0, 46, 0], [0, -392, 0, -169], [-854, 0, 80, 0], [954, 0, 32, 0], [0, 424, 0, -120], [1378, 0, -66, 0], [0, -328, 0, -379], [0, 32, 0, 189], [1106, 0, -14, 0], [62, 0, 66, 0], [0, -704, 0, -290]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1008_m_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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