
// Make newform 648.4.i.o in Magma, downloaded from the LMFDB on 16 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_o();".
// 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_o();".
// 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 := [1, 1, 2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [1, -2, 2, -1], [-3, 18, -6, 3], [12, 0, 0, 6]];
    Rf_basisdens := [1, 2, 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_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], [0, -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], [0, -2, -1, 0], [3, -3, 2, 2], [-26, 26, -3, -3], [0, -13, 2, 0], [94, 0, 0, 1], [-37, 0, 0, 8], [0, -74, -5, 0], [-144, 144, 8, 8], [0, 124, -8, 0], [171, 0, 0, -10], [0, 0, 32, 0], [128, -128, -4, -4], [-66, 66, 1, 1], [476, 0, 0, -14], [0, 502, -21, 0], [17, -17, -34, -34], [0, 433, 16, 0], [388, 0, 0, 30], [937, 0, 0, 12], [-91, 91, -26, -26], [668, -668, -46, -46], [438, 0, 0, 21], [19, -19, 88, 88], [720, -720, -72, -72], [0, 95, 50, 0], [366, 0, 0, -103], [130, 0, 0, 100], [0, 1434, -35, 0], [-1004, 0, 0, 92], [0, 440, 92, 0], [294, -294, 29, 29], [0, 171, -100, 0], [0, -724, 118, 0], [-2711, 2711, 14, 14], [0, 2114, -60, 0], [453, 0, 0, 48], [0, -354, -81, 0], [-1152, 1152, 32, 32], [876, 0, 0, 10], [-1815, 0, 0, -182], [-1806, 1806, -121, -121], [0, -1343, -28, 0], [-294, 0, 0, -13], [-983, 0, 0, -126], [0, -241, 196, 0], [-2188, 2188, 256, 256], [1452, -1452, -150, -150], [0, 3614, 184, 0], [-2824, 0, 0, 164], [0, -4468, 134, 0], [-57, 57, 52, 52], [-3080, 0, 0, -348], [0, 860, 302, 0], [-812, 812, 342, 342], [-3650, 0, 0, -31], [-5857, 0, 0, 26], [3638, -3638, 348, 348], [3392, -3392, -32, -32], [0, 4456, -4, 0], [0, -3894, -155, 0], [2088, 0, 0, 172], [0, 210, -87, 0], [-4611, 4611, -204, -204], [156, -156, -206, -206], [6595, -6595, -4, -4], [0, -4101, 16, 0], [0, 156, 302, 0], [933, -933, 294, 294], [380, -380, -158, -158], [4254, 0, 0, -303], [5399, -5399, 70, 70], [0, -3731, 366, 0], [2135, 0, 0, -468], [0, 1296, -216, 0], [-2834, 2834, -599, -599], [-2290, 0, 0, -492], [0, -10668, 330, 0], [0, -1833, -16, 0], [0, -7986, 151, 0], [-3773, 3773, -258, -258], [11242, 0, 0, -437], [490, 0, 0, -156], [36, -36, -504, -504], [-5812, 5812, 346, 346], [7106, 0, 0, -753], [-2318, 2318, 1004, 1004], [-1118, 1118, 303, 303], [0, -365, 462, 0], [1794, 0, 0, -337], [-12124, 12124, -178, -178], [-371, 0, 0, -1030], [0, 7122, -7, 0], [0, -7344, 424, 0], [5706, 0, 0, 283], [0, -6330, -189, 0], [12034, 0, 0, 207], [6631, 0, 0, -520], [-2979, 0, 0, -186], [1867, -1867, -212, -212], [-20242, 0, 0, 105], [0, 3472, -1304, 0], [-9182, 9182, -321, -321], [0, 14109, 884, 0], [-12541, 0, 0, 788], [682, -682, -357, -357], [7556, 0, 0, -1122], [0, 15408, -872, 0], [1838, -1838, -644, -644], [0, -13853, 234, 0], [-2953, 0, 0, -806], [0, 12942, -329, 0], [2549, -2549, -408, -408], [1087, 0, 0, 378], [1468, -1468, -990, -990], [0, 3552, -996, 0], [-17168, 0, 0, -856], [0, 4452, -78, 0], [-132, 132, 1410, 1410], [0, -1559, -1406, 0], [2549, -2549, 832, 832], [20518, -20518, 749, 749], [6968, 0, 0, -188], [-1808, 1808, 1044, 1044], [18702, 0, 0, 217], [-11309, 11309, 1210, 1210], [-9892, 0, 0, 722], [-18372, 18372, 192, 192], [0, -15650, 1228, 0], [25880, 0, 0, 228], [0, -12026, 1795, 0], [0, 15477, -578, 0], [10661, 0, 0, 902], [0, 4898, -2655, 0], [0, 33841, 480, 0], [-15708, 0, 0, -1490], [0, 11069, 112, 0], [0, -11384, -1244, 0], [4828, 0, 0, 338], [-6512, 0, 0, 2916], [-6762, 6762, -907, -907], [0, -20951, 1470, 0], [-38270, 0, 0, 559], [-1583, 0, 0, 898], [-33852, 33852, 830, 830], [-7931, 7931, 1350, 1350], [-8796, 8796, 366, 366], [0, 31527, 1028, 0], [23858, 0, 0, -313], [0, -4755, 62, 0], [-5246, 0, 0, -209], [21125, 0, 0, -1748], [0, 1124, -1358, 0], [16721, -16721, -1224, -1224], [-5864, 5864, 2004, 2004], [-6140, 0, 0, -912], [2420, -2420, 2022, 2022], [-3981, 0, 0, 3036], [0, -7926, 1349, 0], [-13446, 13446, -1997, -1997], [-8082, 0, 0, -3303], [0, 1789, -538, 0], [-3378, 0, 0, 169], [0, 39324, 878, 0], [20370, -20370, -873, -873], [-40871, 0, 0, -982], [36938, -36938, -324, -324]];
    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_o();".
// 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_o(: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_o();".
// 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_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_648_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![30976, -704, 192, 4, 1]>],Snew);
    return Vf;
end function;