
// Make newform 42.3.g.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_42_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_42_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_42_3_g_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_42_3_g_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 := [4, 0, 2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2, 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_42_g();"
function MakeCharacter_42_g()
    N := 42;
    order := 6;
    char_gens := [29, 31];
    v := [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_42_g_Hecke();"
function MakeCharacter_42_g_Hecke(Kf)
    N := 42;
    order := 6;
    char_gens := [29, 31];
    char_values := [[1, 0, 0, 0], [1, 0, 1, 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], [2, 0, 1, 0], [2, 2, -2, 4], [-5, -2, -5, -4], [0, 0, 6, 0], [-1, -16, -2, -8], [-16, 2, -8, -2], [-7, -2, 7, -4], [12, 18, 12, 0], [0, 0, 0, 24], [34, 6, 17, -6], [11, 12, 11, 0], [-26, -8, -52, -4], [7, 0, 0, 6], [-22, 2, 22, 4], [0, -18, -60, -18], [-8, -14, -4, 14], [-12, -8, 12, -16], [0, 42, -55, 42], [78, 0, 0, -42], [-22, 40, -11, -40], [-5, -66, -5, 0], [10, 76, 20, 38], [-12, 0, 12, 0], [-12, 8, -24, 4], [12, -48, 6, 48], [7, -38, -7, -76], [-144, 18, -144, 0], [0, -24, 145, -24], [42, 0, 0, -42], [125, 0, 0, 30], [-18, 12, 18, 24], [0, -12, 84, -12], [67, -20, 134, -10], [-132, 36, -132, 0], [0, 84, -170, 84], [216, 0, 108, 0], [-50, -24, -50, 0], [14, -124, 28, -62], [-108, -18, 108, -36], [0, 90, 42, 90], [-59, -88, -118, -44], [-126, 42, -126, 0], [0, -84, 109, -84], [-72, 0, 0, 138], [-72, -28, -36, 28], [-74, 0, 0, -36], [132, 0, 264, 0], [-60, 48, -30, -48], [37, -12, -37, -24], [246, -6, 246, 0], [282, 0, 0, -60], [-192, -104, -96, 104], [20, -148, 40, -74], [6, -6, -6, -12], [0, -132, 96, -132], [-260, 64, -130, -64], [-60, 8, 60, 16], [0, -48, -205, -48], [-96, 0, 0, -162], [38, -66, 19, 66], [-20, -224, -40, -112], [19, 468, 38, 234], [116, -124, 58, 124], [-155, 56, 155, 112], [60, -24, 60, 0], [185, -54, 185, 0], [289, 0, 0, -108], [0, 126, -144, 126], [-36, 232, -72, 116], [-100, 218, -50, -218], [-156, -96, -156, 0], [-422, -22, -211, 22], [325, -252, 325, 0], [-209, 0, 0, -294], [-104, 130, 104, 260], [0, -30, -606, -30], [-109, -108, 109, -216], [420, 102, 420, 0], [-458, 16, -229, -16], [-58, -328, -116, -164], [-85, 0, 0, -84], [0, 318, 54, 318], [-229, -360, -458, -180], [60, 120, -60, 240], [144, 18, 144, 0], [366, 0, 0, 246], [227, 72, 227, 0], [40, 592, 80, 296], [233, 0, 0, 450], [-78, -54, 78, -108], [376, 22, 188, -22], [0, 402, 7, 402], [-84, 0, 0, -228], [329, -30, 329, 0], [-26, 16, -52, 8], [58, 370, -58, 740], [-656, -80, -328, 80], [-235, -94, 235, -188], [-119, 72, -119, 0], [26, 0, 0, -132], [0, -144, -678, -144], [156, 258, 78, -258], [-378, 288, -378, 0], [0, 594, 125, 594], [598, -104, 299, 104], [252, 0, 504, 0], [442, 28, -442, 56], [0, -168, 636, -168], [-47, 96, -94, 48], [55, 26, -55, 52], [0, 108, -562, 108], [294, 0, 0, -660], [374, 62, 187, -62], [-598, 0, 0, -384], [0, -162, 0, -162], [-451, -60, -902, -30], [1260, -36, 630, 36], [-558, -504, -558, 0], [-96, 0, 0, 486], [142, -16, 71, 16], [229, 0, 0, -132], [372, -60, -372, -120], [0, -546, 444, -546], [-19, -426, 19, -852], [-348, 0, 0, -246], [-730, -336, -730, 0], [-38, -428, 38, -856], [-67, -420, -134, -210], [359, 132, -359, 264], [0, 54, 269, 54], [-114, 0, 0, -564], [485, 66, 485, 0], [526, 0, 0, -480], [-104, 22, 104, 44], [479, 376, 958, 188], [-580, 368, -290, -368], [-768, -364, -384, 364], [372, -36, 744, -18], [0, -90, -102, -90], [-288, 840, -576, 420], [210, 66, 210, 0], [0, -444, -502, -444], [-798, 0, 0, -450], [862, 320, 431, -320], [-64, -88, -128, -44], [265, 248, 530, 124], [-1456, 140, -728, -140], [-72, 308, 72, 616], [-690, 240, -690, 0], [-326, 228, -326, 0], [-36, -348, -72, -174], [187, 0, 0, 954], [350, 212, -350, 424], [0, -246, -113, -246], [-480, 0, 0, 498], [737, -138, 737, 0], [-466, 38, 466, 76], [565, -608, 1130, -304], [1312, 70, 656, -70], [-162, 912, -162, 0], [528, 0, 0, 438], [-343, 0, 0, 534], [328, 178, -328, 356], [0, 720, -570, 720], [1432, 166, 716, -166], [0, -246, -281, -246], [266, -504, 133, 504]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_42_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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