
// Make newform 24.3.b.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_24_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_24_b_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_24_3_b_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_24_3_b_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 := [6, -6, 3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [2, 4, 3, 1], [6, 0, 1, -1], [-6, 8, -1, 1]];
    Rf_basisdens := [1, 4, 4, 4];
    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_24_b();"
function MakeCharacter_24_b()
    N := 24;
    order := 2;
    char_gens := [7, 13, 17];
    v := [1, 1, 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_24_b_Hecke();"
function MakeCharacter_24_b_Hecke(Kf)
    N := 24;
    order := 2;
    char_gens := [7, 13, 17];
    char_values := [[-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, 1, -1, 0], [0, 0, 1, 0], [0, 0, 0, 2], [2, -4, 2, -2], [-8, 0, 0, 0], [-4, 8, -4, 0], [-2, 0, -8, 0], [8, 0, -4, 0], [-4, 8, -4, 4], [8, -16, 8, -6], [6, -12, 6, 10], [-4, 8, -4, -4], [10, 0, 24, 0], [8, 0, -12, 0], [-4, 8, -4, -12], [-8, 16, -8, -2], [-32, 0, 12, 0], [4, -8, 4, 12], [-64, 0, 12, 0], [20, -40, 20, -4], [50, 0, 32, 0], [-18, 36, -18, 2], [40, 0, -16, 0], [-50, 0, 48, 0], [14, 0, -48, 0], [24, -48, 24, -2], [-26, 52, -26, 26], [-80, 0, -68, 0], [28, -56, 28, -56], [-86, 0, -16, 0], [-6, 12, -6, -42], [160, 0, 20, 0], [58, 0, -24, 0], [32, 0, -84, 0], [0, 0, 0, -22], [34, -68, 34, 30], [-28, 56, -28, 84], [-40, 0, 36, 0], [24, -48, 24, -88], [64, -128, 64, 14], [256, 0, -36, 0], [-60, 120, -60, 24], [-88, 176, -88, -24], [-118, 0, 96, 0], [-56, 112, -56, 22], [22, -44, 22, 10], [-64, 0, -116, 0], [10, -20, 10, 6], [88, 0, 120, 0], [36, -72, 36, -80], [-266, 0, -80, 0], [0, 0, 0, 80], [2, 0, 144, 0], [-104, 0, 64, 0], [82, 0, 96, 0], [-80, 160, -80, 128], [-56, 112, -56, 10], [-6, 12, -6, 54], [52, -104, 52, 24], [166, 0, -160, 0], [128, 0, 244, 0], [-40, 80, -40, 46], [-448, 0, 44, 0], [40, -80, 40, -120], [86, 0, -96, 0], [-8, 16, -8, 26], [-112, 0, -164, 0], [398, 0, -96, 0], [88, 0, 216, 0], [84, -168, 84, 44], [346, 0, -48, 0], [52, -104, 52, 28], [-58, 116, -58, 10], [44, -88, 44, -108], [-40, 0, 44, 0], [8, -16, 8, 136], [96, -192, 96, -158], [148, -296, 148, 28], [-338, 0, -24, 0], [2, 0, 144, 0], [88, 0, 104, 0], [-76, 152, -76, -56], [-124, 248, -124, 44], [14, 0, -160, 0], [-82, 164, -82, 82], [-104, 0, -72, 0], [238, 0, -200, 0], [146, 0, 240, 0], [64, -128, 64, -58], [34, -68, 34, -178], [-200, 0, -200, 0], [60, -120, 60, -92], [26, -52, 26, 134], [-80, 0, 36, 0], [320, 0, 132, 0], [36, -72, 36, -84], [-8, 16, -8, 138], [178, 0, -8, 0], [-40, 0, -84, 0], [-212, 424, -212, 40], [296, 0, 132, 0], [80, -160, 80, -178], [-536, 0, 216, 0], [250, 0, 216, 0], [176, 0, -444, 0], [-394, 0, -352, 0], [880, 0, -12, 0], [-758, 0, -144, 0], [-44, 88, -44, -180], [-406, 0, 0, 0], [-294, 588, -294, 22], [76, -152, 76, -140], [-410, 0, 288, 0], [-736, 0, 180, 0], [26, -52, 26, 134], [550, 0, 312, 0], [-184, 0, -156, 0], [-148, 296, -148, 20], [112, -224, 112, -162], [-272, 0, -316, 0], [-84, 168, -84, 28], [-34, 0, -128, 0], [-272, 544, -272, -94], [616, 0, 304, 0], [728, 0, 340, 0], [152, -304, 152, 194], [20, -40, 20, 256], [100, -200, 100, -68], [-230, 460, -230, -90], [236, -472, 236, 48], [-304, 0, 212, 0], [240, -480, 240, -112], [214, -428, 214, 26], [244, -488, 244, -272], [274, 0, -568, 0], [26, 0, -128, 0], [40, -80, 40, 118], [-232, 0, -116, 0], [-176, 352, -176, 6], [-86, 0, -440, 0], [584, 0, 380, 0], [40, -80, 40, 254], [162, -324, 162, -98], [112, 0, -804, 0], [-100, 200, -100, -280], [268, -536, 268, -124], [-36, 72, -36, 188], [-566, 0, 248, 0], [-712, 0, 36, 0], [-312, 624, -312, -24], [100, -200, 100, -244], [-14, 0, 384, 0], [56, 0, 644, 0], [-144, 288, -144, 320], [-616, 0, 84, 0], [176, -352, 176, 96], [178, -356, 178, -146], [766, 0, 296, 0], [302, 0, 192, 0], [256, -512, 256, -90], [256, 0, 300, 0], [946, 0, -200, 0], [-202, 404, -202, 426], [-344, 0, 576, 0], [-962, 0, -24, 0], [-144, 288, -144, -320], [326, -652, 326, -310], [-52, 104, -52, -236]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_24_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_24_3_b_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_24_3_b_a(:prec:=4)
    chi := MakeCharacter_24_b();
    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_24_3_b_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_24_3_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_24_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;