
// Make newform 624.4.bv.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_624_bv();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_624_bv_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_624_4_bv_a();".
// 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_624_4_bv_a();".
// 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, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_624_bv();"
function MakeCharacter_624_bv()
    N := 624;
    order := 6;
    char_gens := [79, 469, 209, 145];
    v := [6, 6, 6, 5];
    // 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_624_bv_Hecke();"
function MakeCharacter_624_bv_Hecke(Kf)
    N := 624;
    order := 6;
    char_gens := [79, 469, 209, 145];
    char_values := [[1, 0], [1, 0], [1, 0], [0, 1]];
    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], [-3, 3], [3, -6], [-12, 6], [-30, -30], [-13, -39], [0, 117], [28, -14], [18, -18], [99, -99], [-112, 224], [65, 65], [-21, -21], [0, -82], [42, -84], [-261, 0], [912, -456], [0, 719], [406, 406], [540, -270], [395, -790], [440, 0], [-690, 1380], [876, 876], [-1336, 668], [-1575, 1575], [-794, 0], [450, -450], [344, -688], [0, 1701], [-1664, 1664], [1476, 0], [1170, -585], [0, 1124], [3774, -1887], [946, -1892], [1259, 0], [-3408, 1704], [1812, 1812], [0, -4266], [3006, -3006], [-1873, 0], [0, -2736], [-1503, -1503], [2148, 2148], [0, 1198], [2392, -2392], [1176, 1176], [-2484, 1242], [2004, -4008], [-1854, 0], [2574, -5148], [-482, 241], [0, 4104], [-1989, 1989], [738, -738], [0, 2106], [-396, -396], [0, -3665], [-993, 1986], [-1826, 1826], [-582, 291], [-1126, 2252], [3798, 0], [1378, 0], [4101, -8202], [-11632, 5816], [9001, 0], [0, 3294], [-6088, -6088], [-1431, -1431], [3126, -6252], [-9946, 9946], [0, 7301], [1976, 1976], [6672, -3336], [9153, 0], [2336, -1168], [4797, 4797], [-12030, 6015], [4176, -4176], [8355, -16710], [3798, 3798], [0, 6605], [-8542, 8542], [14328, 0], [3480, -1740], [-1667, -1667], [4158, -2079], [-1546, 3092], [-13878, 0], [-636, -636], [19772, -9886], [450, -450], [-7632, 15264], [0, 5346], [-3387, -3387], [-9657, 0], [21626, -21626], [-3101, 6202], [-16946, 0], [2229, 2229], [0, 21672], [1386, -1386], [-1162, 0], [-4645, 9290], [15948, 15948], [-159, 318], [-22356, 0], [18083, -18083], [0, 5480], [10241, 10241], [11394, -5697], [-2376, 4752], [14624, -7312], [0, 3789], [-19528, 9764], [-27792, 27792], [594, -594], [0, 17748], [9117, 9117], [-20933, 20933], [3402, 0], [-28848, 14424], [-8006, -8006], [-21906, 0], [-15078, 7539], [0, -14220], [5282, 0], [6593, -13186], [11856, 11856], [12024, 12024], [-4834, 4834], [-9046, 9046], [-13896, 6948], [21676, 21676], [-18144, 9072], [4336, -2168], [0, 7830], [6165, -6165], [17228, -34456], [-17184, -17184], [0, -8920], [10416, -20832], [21023, -21023], [25488, -12744], [-15363, 30726], [-12771, 0], [-17134, 0], [4578, -9156], [11390, -5695], [-29169, 29169], [-928, 0], [-14400, 14400], [19684, -19684], [-24480, 0], [0, 38608], [11790, -5895], [28495, 0], [6192, -12384], [-19608, -19608], [0, -23814], [-29762, 59524], [0, -12312], [12435, 12435], [18828, -37656], [-22330, 22330], [0, 24931]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_624_bv_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_624_4_bv_a();".
// 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_624_4_bv_a(:prec:=2)
    chi := MakeCharacter_624_bv();
    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_624_4_bv_a();".
// 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_624_4_bv_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_624_bv();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![27, 0, 1]>],Snew);
    return Vf;
end function;