
// Make newform 648.4.i.m in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_648_i();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_648_i_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_648_4_i_m();".
// 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_648_4_i_m();".
// 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, -3, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-9, -2, 2, 1], [3, 10, 2, -2], [-33, 10, 2, 7]];
    Rf_basisdens := [1, 6, 1, 2];
    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_648_i();"
function MakeCharacter_648_i()
    N := 648;
    order := 3;
    char_gens := [487, 325, 569];
    v := [3, 3, 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_648_i_Hecke();"
function MakeCharacter_648_i_Hecke(Kf)
    N := 648;
    order := 3;
    char_gens := [487, 325, 569];
    char_values := [[1, 0, 0, 0], [1, 0, 0, 0], [-1, -1, 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 := 4;
    raw_aps := [[0, 0, 0, 0], [0, 0, 0, 0], [-4, -4, 0, -1], [0, 12, -1, 1], [0, 1, 0, 0], [-16, -16, 0, 4], [-28, 0, -4, 0], [92, 0, 2, 0], [-46, -46, 0, 4], [0, -168, -2, 2], [-188, -188, 0, 5], [174, 0, -4, 0], [156, 156, 0, -10], [0, 40, 14, -14], [0, -114, 8, -8], [76, 0, -13, 0], [-340, -340, 0, 24], [0, -56, 32, -32], [-176, -176, 0, -34], [908, 0, -12, 0], [-287, 0, 0, 0], [0, -680, -32, 32], [0, -391, -32, 32], [120, 0, 18, 0], [0, -169, -8, 8], [0, -828, 9, -9], [56, 56, 0, 52], [693, 0, -56, 0], [46, 0, 76, 0], [732, 732, 0, 70], [-164, 0, 23, 0], [1003, 1003, 0, 104], [0, -1296, -2, 2], [-1416, -1416, 0, 100], [-2132, -2132, 0, 31], [0, -1468, 113, -113], [764, 764, 0, -72], [1236, 0, 120, 0], [390, 390, 0, -144], [0, 3252, 19, -19], [-1197, 0, 56, 0], [2016, 0, 16, 0], [0, -840, 196, -196], [499, 499, 0, 40], [1092, 0, -245, 0], [1564, 0, -129, 0], [-3028, -3028, 0, -34], [0, -2192, -8, 8], [0, 1740, -24, 24], [1682, 1682, 0, 8], [-3128, 0, -50, 0], [2878, 2878, 0, 164], [0, 1950, -248, 248], [-1168, 0, 336, 0], [3940, 3940, 0, 56], [0, 2158, -132, 132], [560, 0, 226, 0], [68, 0, 185, 0], [0, -3008, 24, -24], [0, 284, 116, -116], [1420, 1420, 0, 178], [624, 624, 0, -134], [-8148, 0, 4, 0], [-1110, -1110, 0, -84], [0, -2427, 96, -96], [0, 36, 449, -449], [0, 1952, 134, -134], [5694, 5694, 0, 56], [-2655, -2655, 0, -304], [0, 1434, 156, -156], [0, -460, -526, 526], [-3462, 0, -24, 0], [0, -8564, 193, -193], [-7628, -7628, 0, -276], [1856, 0, 12, 0], [-10044, -10044, 0, -108], [0, 5668, -409, 409], [-5152, 0, 36, 0], [7104, 7104, 0, -132], [-9279, -9279, 0, -128], [612, 612, 0, -512], [0, 3584, -360, 360], [7346, 0, 224, 0], [5269, 0, -264, 0], [0, 3780, -291, 291], [0, 6884, 224, -224], [9652, 0, -378, 0], [0, -5569, 200, -200], [0, -7868, 255, -255], [-3524, -3524, 0, 3], [1209, 0, 616, 0], [0, -10114, -488, 488], [3448, 0, 248, 0], [-14571, -14571, 0, -328], [-2256, -2256, 0, 926], [7986, 0, 332, 0], [948, 948, 0, -771], [6380, 0, -288, 0], [-6884, 0, 788, 0], [2556, 0, 264, 0], [0, 4652, -428, 428], [14788, 0, -15, 0], [14921, 14921, 0, -272], [0, 7144, 396, -396], [15204, 15204, 0, -488], [-17074, 0, 344, 0], [0, -15959, 456, -456], [2920, 0, -1122, 0], [6594, 6594, 0, -104], [0, -2645, -416, 416], [19528, 19528, 0, -264], [-3514, 0, 880, 0], [996, 996, 0, -38], [0, 21724, 192, -192], [7612, 0, 345, 0], [0, -7172, -978, 978], [8016, 8016, 0, -276], [-13768, 0, 328, 0], [1308, 1308, 0, 123], [0, -12993, 48, -48], [12862, 12862, 0, 572], [0, -3203, 112, -112], [0, -872, -590, 590], [9964, 0, 224, 0], [0, 9872, 1212, -1212], [-15540, 0, 875, 0], [0, 27548, 64, -64], [20212, 0, -272, 0], [0, -7692, -1545, 1545], [-5486, -5486, 0, 1292], [2804, 0, 1596, 0], [-4456, -4456, 0, -1604], [-17172, -17172, 0, 449], [1970, 0, -40, 0], [-22520, -22520, 0, 168], [-23849, -23849, 0, 360], [-14304, 0, -478, 0], [22592, 22592, 0, 992], [14900, 14900, 0, -749], [-14200, 0, -434, 0], [-2588, 0, -726, 0], [0, -4608, -842, 842], [-19916, -19916, 0, 489], [31424, 0, 320, 0], [34, 0, 184, 0], [0, 3492, 268, -268], [0, 13286, 408, -408], [0, 11004, -468, 468], [-8040, -8040, 0, -1088], [16828, 0, 1696, 0], [-2850, -2850, 0, -1952], [-8908, 0, -1696, 0], [7172, 0, -1190, 0], [34576, 34576, 0, -380], [0, -9116, 2094, -2094], [0, 8230, 2208, -2208], [36916, 0, 3, 0], [0, 42344, -468, 468], [-14853, 0, -888, 0], [24660, 24660, 0, 1019], [0, 24999, -880, 880], [-17064, 0, 1404, 0], [11596, 11596, 0, -641], [13929, 0, 992, 0], [6408, 6408, 0, -826], [0, 5934, 3096, -3096], [14428, 0, 1673, 0], [0, -44552, -252, 252]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_648_i_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_648_4_i_m();".
// 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_648_4_i_m(:prec:=4)
    chi := MakeCharacter_648_i();
    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_648_4_i_m();".
// 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_648_4_i_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_648_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![78961, -2248, 345, 8, 1]>],Snew);
    return Vf;
end function;