
// Make newform 540.3.b.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_540_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_540_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_540_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_540_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 := [4, 0, 15, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [14, 19, 2, 1], [0, 11, 0, 1], [14, -19, 2, -1]];
    Rf_basisdens := [1, 4, 2, 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_540_b();"
function MakeCharacter_540_b()
    N := 540;
    order := 2;
    char_gens := [271, 461, 217];
    v := [2, 1, 1];
    // 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_540_b_Hecke();"
function MakeCharacter_540_b_Hecke(Kf)
    N := 540;
    order := 2;
    char_gens := [271, 461, 217];
    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, 0, 0, 0], [0, 0, 0, 0], [-1, 0, 0, -1], [0, 0, -1, 0], [0, -2, -3, 2], [0, 3, 3, -3], [-1, 1, 0, 1], [3, 3, 0, 3], [7, -1, 0, -1], [0, 3, 9, -3], [7, 6, 0, 6], [0, 6, 2, -6], [0, 1, 3, -1], [0, 9, 1, -9], [-14, -4, 0, -4], [51, 6, 0, 6], [0, -11, -9, 11], [11, 3, 0, 3], [0, 6, -8, -6], [0, 15, 3, -15], [0, -12, -17, 12], [3, 9, 0, 9], [67, -10, 0, -10], [0, 11, -9, -11], [0, 3, 28, -3], [0, -13, 6, 13], [0, 0, -4, 0], [6, 21, 0, 21], [-61, -9, 0, -9], [138, -6, 0, -6], [0, -9, -16, 9], [0, -15, -6, 15], [-129, -15, 0, -15], [98, -6, 0, -6], [0, 4, -3, -4], [-2, -3, 0, -3], [0, -33, -23, 33], [0, 30, 56, -30], [-57, -27, 0, -27], [203, -14, 0, -14], [0, 31, 6, -31], [-13, -21, 0, -21], [0, -18, -24, 18], [0, -24, 19, 24], [-125, 20, 0, 20], [-138, 3, 0, 3], [-69, 3, 0, 3], [0, -30, -62, 30], [-25, 1, 0, 1], [235, -15, 0, -15], [338, -8, 0, -8], [0, 8, 60, -8], [19, -27, 0, -27], [0, 54, 30, -54], [-201, 33, 0, 33], [202, 2, 0, 2], [0, 6, 54, -6], [151, 12, 0, 12], [0, 3, -43, -3], [0, 57, 81, -57], [0, -3, -49, 3], [327, 33, 0, 33], [0, 30, 0, -30], [0, -57, -75, 57], [0, 3, 34, -3], [-229, -20, 0, -20], [58, 36, 0, 36], [0, -45, -55, 45], [78, -9, 0, -9], [-109, 51, 0, 51], [314, 10, 0, 10], [0, -18, -48, 18], [0, 72, 35, -72], [0, 66, 0, -66], [-317, 15, 0, 15], [23, -59, 0, -59], [0, 44, 3, -44], [0, 81, 77, -81], [0, 74, 18, -74], [631, -6, 0, -6], [0, -13, -63, 13], [331, 39, 0, 39], [0, 19, 123, -19], [0, -24, -41, 24], [-177, -36, 0, -36], [63, 57, 0, 57], [0, -93, -15, 93], [0, -69, -124, 69], [0, -27, -96, 27], [0, 27, 66, -27], [339, 12, 0, 12], [0, 113, 105, -113], [0, -69, -75, 69], [0, 33, 174, -33], [603, -3, 0, -3], [-369, -57, 0, -57], [0, 9, -120, -9], [0, -1, 51, 1], [0, -81, 23, 81], [274, 42, 0, 42], [0, -81, -25, 81], [-474, -87, 0, -87], [-514, 19, 0, 19], [0, 77, 111, -77], [-317, -75, 0, -75], [0, -3, 71, 3], [603, -42, 0, -42], [-153, -135, 0, -135], [0, -171, -51, 171], [263, -78, 0, -78], [0, -117, -35, 117], [0, 60, -26, -60], [-49, 55, 0, 55], [-326, -84, 0, -84], [143, -108, 0, -108], [0, -98, -78, 98], [0, -111, -93, 111], [519, -45, 0, -45], [-533, 14, 0, 14], [0, -8, -117, 8], [-286, 66, 0, 66], [0, -114, -33, 114], [-310, 16, 0, 16], [-425, 89, 0, 89], [-221, -93, 0, -93], [0, -23, 66, 23], [-318, 90, 0, 90], [0, 74, 234, -74], [0, -81, 30, 81], [0, -36, -52, 36], [-637, -3, 0, -3], [-702, -48, 0, -48], [-265, 30, 0, 30], [0, -120, -310, 120], [0, -57, -75, 57], [879, -30, 0, -30], [-849, -15, 0, -15], [0, -93, -247, 93], [59, 88, 0, 88], [0, -147, -207, 147], [514, -66, 0, -66], [0, 116, 132, -116], [0, 120, 177, -120], [695, 61, 0, 61], [-150, 156, 0, 156], [0, -110, -216, 110], [0, 198, 92, -198], [-681, -147, 0, -147], [739, -45, 0, -45], [-1065, 27, 0, 27], [0, 243, 115, -243], [0, 51, 135, -51], [0, 21, -45, -21], [711, -27, 0, -27], [0, 54, 42, -54], [0, -15, -15, 15], [-738, 105, 0, 105], [0, -87, -237, 87], [0, -21, 22, 21], [0, 121, 270, -121], [483, -126, 0, -126], [170, 202, 0, 202], [0, -87, -98, 87], [0, 93, 36, -93], [-1086, -72, 0, -72], [-361, 97, 0, 97], [343, 180, 0, 180], [0, -33, -261, 33]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_540_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_540_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_540_3_b_a(:prec:=4)
    chi := MakeCharacter_540_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_540_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_540_3_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_540_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<7,R![400, 0, 59, 0, 1]>,<17,R![-50, 3, 1]>],Snew);
    return Vf;
end function;