
// Make newform 1296.2.a.j in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1296_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_1296_2_a_j();".
// 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_1296_2_a_j();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_1296_a();"
function MakeCharacter_1296_a()
    N := 1296;
    order := 1;
    char_gens := [1135, 325, 1217];
    v := [1, 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;

function MakeCharacter_1296_a_Hecke(Kf)
    return MakeCharacter_1296_a();
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 := 2;
    raw_aps := [[0], [0], [3], [-2], [6], [5], [-3], [-2], [-6], [3], [4], [5], [-6], [10], [0], [-6], [12], [5], [-2], [-6], [-1], [10], [0], [-3], [-10], [6], [16], [-12], [-7], [9], [-2], [6], [-15], [-8], [15], [4], [5], [4], [-18], [-9], [-12], [14], [-6], [-25], [15], [-20], [-14], [10], [-18], [5], [-15], [0], [11], [-30], [-27], [-24], [3], [10], [-10], [-15], [4], [15], [-20], [24], [-25], [15], [-26], [-10], [18], [2], [30], [18], [-32], [14], [4], [-30], [-30], [-7], [-15], [35], [0], [29], [-24], [11], [-8], [0], [18], [35], [-6], [4], [-12], [-18], [-20], [-6], [22], [0], [-6], [-30], [10], [41], [40], [15], [0], [-3], [-44], [-37], [30], [-39], [12], [-1], [10], [-10], [-15], [40], [4], [21], [40], [-18], [-30], [-6], [29], [-1], [42], [0], [10], [39], [-19], [-18], [10], [14], [52], [36], [10], [-10], [9], [23], [-45], [-38], [-45], [-3], [16], [-21], [4], [30], [38], [18], [14], [45], [-8], [-6], [-55], [-6], [16], [-30], [-8], [54], [-38], [-27], [23], [15], [-18], [45], [-38], [54], [-30], [-24], [-26], [53], [-37], [-6], [-12], [-31], [24], [11], [-50], [42], [10], [-9], [-20], [17], [-20], [-42], [41], [18], [60], [-57], [17], [34], [-22], [30], [11], [0], [22], [51], [30], [6], [-49], [-10], [33], [30], [27], [-26], [-22], [23], [6], [-30], [-2], [60], [-30], [10], [-58], [51], [-50], [30], [72], [14], [58], [9], [-48], [-21], [41], [58], [57], [-20], [-72], [17], [-54], [-66], [-20], [-48], [-19], [-38], [-50], [57], [-20], [-12], [-13], [54], [0], [6], [66], [10], [-20], [-10], [-30], [42], [70], [-30], [-68], [0], [5], [9], [-60], [23], [-45], [30], [-19], [-62], [27], [38], [-80], [0], [-43], [50], [30], [28], [15], [9], [40], [-9], [-19], [-50], [-10], [52], [23], [-80], [-48], [53], [-25], [12], [60], [40], [-12], [5], [52], [-36], [-25], [-30], [22], [45], [-54], [-30], [-39], [36], [-19], [-57], [64], [51], [18], [28], [59], [-18], [-2], [6], [64], [38], [42], [38], [12], [65], [-66], [18], [81], [-50], [48], [38], [-12], [-24], [35], [18], [-68], [-58], [-69], [16], [-30], [59], [-2], [46], [90], [-6], [14], [15], [88], [54], [34], [60], [53], [-51], [14], [-20], [-55], [-78], [63], [-20], [-21], [-30], [-79], [-80], [84], [75], [-26], [-25], [-57], [46], [-43], [-51], [-72], [-6], [42], [-30], [-55], [21], [18], [12], [-20], [-25], [-30], [-20], [74], [-66], [-50], [24], [-57], [4], [-43], [18], [36], [-25], [78], [23], [3], [-39], [40], [33], [58], [66], [28], [17], [34], [72], [35], [15], [30], [70], [36], [-25], [58], [45], [-50], [-69], [77], [-30], [58], [-27], [27], [70], [17], [-15], [70], [12], [-1], [3], [6], [4], [35], [87], [-12], [-8], [57], [-84], [30], [-70], [-60], [-72], [-25], [15], [60], [-99], [-56], [-60]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1296_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1296_2_a_j();".
// 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_1296_2_a_j(:prec:=1)
    chi := MakeCharacter_1296_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_1296_2_a_j();".
// 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_1296_2_a_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1296_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-3, 1]>,<7,R![2, 1]>,<11,R![-6, 1]>],Snew);
    return Vf;
end function;