
// Make newform 504.3.g.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_504_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_504_g_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_504_3_g_b();".
// 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_504_3_g_b();".
// 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 := [256, -64, -32, -8, 24, -2, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [-1, 0, 1, 0, 0, 0, 0, 0], [-256, 32, 32, -12, -22, -4, 3, -1], [128, 32, 32, -4, 6, -4, 5, 1], [0, 96, 0, -12, -6, 12, 3, -1], [512, -96, -96, -28, 50, -4, -1, -5], [-640, -96, 128, 164, -38, -12, 3, 7]];
    Rf_basisdens := [1, 1, 1, 128, 128, 128, 128, 128];
    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_504_g();"
function MakeCharacter_504_g()
    N := 504;
    order := 2;
    char_gens := [127, 253, 281, 73];
    v := [1, 1, 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_504_g_Hecke();"
function MakeCharacter_504_g_Hecke(Kf)
    N := 504;
    order := 2;
    char_gens := [127, 253, 281, 73];
    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, -1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [-1, 1, -1, -1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [5, -3, 0, 1, 0, 0, 2, 1], [3, 1, -1, 3, -4, 1, 0, 0], [10, 0, 0, -4, -4, 0, 0, 0], [7, 0, 0, -1, -1, 0, 0, 0], [0, -3, -3, 0, -1, 5, 0, -2], [4, 0, 6, 4, 0, 2, 0, 2], [0, -6, 6, 0, 6, 6, 0, 0], [4, 4, 2, 4, -4, 14, 0, 2], [-14, -14, -2, 0, -2, -2, 4, 4], [3, 7, 4, -1, -4, 4, 6, -1], [-8, 16, -4, -8, 0, -4, 0, 4], [6, 2, 4, 6, -4, -4, 0, 2], [-7, 14, 8, -7, -13, 8, 12, -2], [-5, -3, 3, -5, 8, -19, 0, 0], [35, -7, -4, -9, -6, -4, -6, 1], [-16, -8, 8, -16, 24, 0, 0, 0], [-10, -12, 0, 4, 0, 0, 8, 4], [12, -28, 16, 12, 8, -8, 0, -4], [-7, 26, 8, 1, -1, 8, 4, -6], [70, -10, 2, 12, 6, 2, 12, 4], [10, 34, 10, -4, -6, 10, 4, -8], [-7, 35, -23, -7, -20, -25, 0, 4], [-16, -2, 2, -16, 18, 10, 0, 0], [-83, -9, 0, 1, -2, 0, 6, 3], [12, 4, -10, 12, -20, -22, 0, -2], [-20, 29, 5, 16, 19, 5, -6, -8], [-20, -5, -1, -20, 21, 23, 0, -2], [57, -26, -8, 1, 3, -8, -4, 6], [-22, -6, -6, 0, 6, -6, -12, 0], [-97, -20, -8, -25, -21, -8, -8, 4], [2, -46, 4, 2, 16, 20, 0, -14], [-40, 21, -27, -40, 15, 13, 0, -2], [7, -27, -21, 7, -12, -11, 0, -16], [19, 47, 8, -25, -20, 8, -10, -13], [-24, 0, -36, -24, 0, 4, 0, -12], [-7, -45, 21, -7, 36, 11, 0, -8], [-19, 27, 12, -7, -14, 12, 14, -5], [35, -3, 15, 35, -24, 33, 0, 4], [-20, -4, 16, -20, 32, -56, 0, 4], [20, -5, -5, 32, 37, -5, -10, 0], [22, 34, -4, 22, -36, -28, 0, 10], [-32, -62, -10, -32, 46, -34, 0, -24], [-61, -19, -4, -9, -10, -4, 2, 5], [-40, 54, -18, -40, 10, 30, 0, 12], [25, -20, -8, -15, -11, -8, -8, 4], [1, -85, 13, 1, 36, 19, 0, -24], [-54, -30, -6, -48, -50, -6, 4, 8], [-36, -17, 11, -36, 49, 19, 0, -2], [98, 34, 10, 20, 18, 10, 4, -8], [113, 86, 8, -55, -37, 8, -36, -26], [78, -20, -20, -24, -4, -20, -40, 0], [8, -60, 12, 8, 20, -76, 0, -16], [53, -33, 33, 53, -20, -1, 0, 0], [-16, -50, -10, -16, 26, -58, 0, -20], [-6, 106, 8, -6, -24, 0, 0, 38], [-146, 48, 0, 16, 32, 0, -32, -16], [55, 28, 16, -41, -53, 16, 24, -4], [-23, 3, -39, -23, -4, -41, 0, -12], [87, 58, 16, 31, 29, 16, 4, -14], [-48, -100, 4, -48, 84, 12, 0, -32], [-50, -10, 26, 16, -22, 26, 76, 12], [14, -30, 48, 14, 28, -80, 0, 6], [83, -57, 12, 31, -4, 12, 70, 23], [96, 61, -11, -32, 3, -11, -70, -24], [-67, 53, 20, 17, 8, 20, 18, -11], [-11, -5, -55, -11, -24, 87, 0, -20], [-70, 58, 10, -96, -90, 10, -12, -16], [64, -95, 41, 64, -5, 33, 0, -18], [-8, -122, -10, -8, 42, -42, 0, -44], [78, -106, 52, 78, -8, -12, 0, -18], [51, -25, -28, -65, -36, -28, -58, -1], [32, 68, -32, 32, -76, -128, 0, 12], [-56, 0, -30, -56, 36, 158, 0, -10], [57, 7, 29, 57, -40, 3, 0, 12], [8, -77, -29, 104, 117, -29, -26, 16], [130, -124, -16, -64, -84, -16, 40, 36], [-239, -28, -40, -31, 13, -40, -88, -4], [-22, -30, -24, -22, 16, 32, 0, -18], [-4, 153, -27, -4, -65, 45, 0, 42], [30, -36, -36, -92, -56, -36, -72, 0], [8, 218, -14, 8, -90, -6, 0, 68], [-155, 23, -4, 65, 78, -4, -26, -9], [178, -134, -38, 40, 46, -38, -12, 32], [20, -61, 11, 128, 93, 11, 70, 24], [-29, -147, -57, -29, 40, -71, 0, -68], [-68, 76, -40, -68, 16, -64, 0, 12], [-23, -32, 16, -15, -47, 16, 64, 16], [-32, 138, -138, -32, -106, -42, 0, 0], [64, -1, -17, 64, -75, 55, 0, -6], [-443, -5, 4, -15, -22, 4, 14, 3], [-165, -79, -40, 103, 130, -40, -54, 13], [-8, 126, -114, -8, -110, 6, 0, 4], [15, -55, -5, 15, 0, -155, 0, -20], [326, -44, 16, 40, 4, 16, 72, 20], [-137, -194, -32, 39, 17, -32, 44, 54], [-106, -146, 80, -106, 208, -8, 0, -22], [-317, -109, -16, 151, 136, -16, 30, 31], [-62, 14, -44, -62, 28, -180, 0, -10], [-95, 82, 16, 49, 55, 16, -12, -22], [-32, -77, -29, -96, -83, -29, -26, 16], [-237, 167, 20, 95, 124, 20, -58, -49], [-50, -34, -58, -24, 42, -58, -132, -8], [609, -100, 8, 9, -35, 8, 88, 36], [-530, 160, 64, -16, -48, 64, 64, -32], [-144, 28, -52, -144, 100, 84, 0, -8], [-230, 74, 14, -76, -70, 14, -12, -20], [48, 136, -64, 48, -136, 96, 0, 24], [-24, 164, -62, -24, -72, -74, 0, 34], [-520, -69, 3, 72, 45, 3, 54, 24], [-57, 0, 24, 103, 71, 24, 64, 8], [136, 100, -28, 136, -188, -20, 0, 24], [160, -17, 31, -8, -55, 31, 94, 16], [319, 62, 32, -81, -103, 32, 44, -10], [-40, -46, -26, -40, 38, -18, 0, -24], [44, 128, 46, 44, -56, -22, 0, 58], [189, -19, 8, 249, 232, 8, 34, 9], [-21, 121, -37, -21, -44, 69, 0, 28], [242, -80, 40, 80, 0, 40, 160, 40], [5, -13, 25, 5, 16, 327, 0, 4], [429, -17, 16, 113, 86, 16, 54, 11], [7, -254, -32, 95, 53, -32, 84, 74], [8, -180, 114, 8, 128, -90, 0, -22], [-180, 372, -126, -180, -28, 78, 0, 82], [128, 20, 88, 128, -76, 168, 0, 36], [-176, 128, -188, -176, 8, -4, 0, -20], [191, -231, 159, 191, -8, -63, 0, -24], [723, 23, 8, 7, 4, 8, 6, -5], [56, 137, 25, 56, -85, -191, 0, 54], [-20, -137, 35, -20, 89, -149, 0, -34], [56, -444, 138, 56, 184, 142, 0, -102], [-70, -82, -22, 64, 66, -22, -4, 20], [426, 38, 38, 52, 14, 38, 76, 0], [-145, -103, 7, -145, 184, 233, 0, -32], [-457, 304, 88, -17, -33, 88, 32, -72], [21, 247, -175, 21, -220, -113, 0, 24], [-204, 41, -31, -256, -201, -31, -110, -24], [-857, -44, -32, -57, -29, -32, -56, 4], [-142, 370, -76, -142, -32, 100, 0, 98], [104, -264, 120, 104, 64, -32, 0, -48], [189, -85, -88, 33, 122, -88, -178, -1], [-149, 125, -77, -149, 56, -211, 0, 16], [88, -46, -98, 88, -138, 230, 0, -48], [153, -173, 173, 153, 20, 163, 0, 0], [-266, -60, -24, -56, -44, -24, -24, 12], [351, -162, -48, -25, -15, -48, -20, 38], [92, -112, 244, 92, 108, 44, 0, 44], [32, 88, 26, 32, -44, -90, 0, 38], [-534, 54, 54, -24, -78, 54, 108, 0], [-789, -135, -72, -185, -134, -72, -102, 21], [-264, -68, 32, -264, 308, -168, 0, -12], [-85, -183, -12, 191, 146, -12, 90, 57], [-100, 459, -129, -100, -139, 103, 0, 110], [96, -40, 16, 96, -72, -248, 0, -8], [250, 164, 20, 180, 208, 20, -56, -48], [-130, 412, 88, -100, -80, 88, -40, -108], [7, 205, -25, 7, -92, 73, 0, 60], [77, -99, -72, -119, -56, -72, -126, 9], [870, 124, 28, -48, -44, 28, -8, -32], [-88, -13, 43, -88, 121, -349, 0, 10], [-535, -354, -48, 121, 67, -48, 108, 102], [170, -206, -62, 144, 158, -62, -28, 48], [200, -10, 226, 200, -46, -6, 0, 72], [60, -232, -20, 60, 4, 68, 0, -84], [41, 247, -55, 41, -160, 231, 0, 64]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_504_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_504_3_g_b();".
// 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_504_3_g_b(:prec:=8)
    chi := MakeCharacter_504_g();
    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_504_3_g_b();".
// 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_504_3_g_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_504_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![369664, 0, 66944, 0, 4164, 0, 108, 0, 1]>],Snew);
    return Vf;
end function;