
// Make newform 1350.2.a.q in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1350_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_1350_2_a_q();".
// 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_1350_2_a_q();".
// 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_1350_a();"
function MakeCharacter_1350_a()
    N := 1350;
    order := 1;
    char_gens := [1001, 1027];
    v := [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_1350_a_Hecke(Kf)
    return MakeCharacter_1350_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 := [[1], [0], [0], [-1], [0], [2], [6], [-1], [6], [-6], [5], [-7], [12], [11], [12], [0], [-6], [-7], [-4], [6], [-7], [-1], [-6], [-6], [5], [-12], [5], [6], [-13], [12], [-16], [18], [12], [-13], [18], [-7], [17], [-4], [-18], [-6], [0], [-22], [-24], [-1], [-12], [8], [20], [17], [-12], [-13], [-6], [24], [2], [-30], [-24], [-18], [6], [11], [-1], [-12], [5], [-6], [-19], [12], [-34], [-6], [35], [26], [-6], [-19], [-24], [-6], [-28], [29], [32], [6], [0], [-7], [-24], [26], [18], [-1], [18], [-13], [5], [30], [30], [26], [6], [-31], [6], [-18], [8], [-24], [-7], [-6], [-24], [12], [23], [-25], [35], [42], [-24], [30], [23], [23], [6], [6], [-48], [11], [5], [-43], [36], [11], [-4], [30], [-4], [0], [24], [-30], [-31], [-37], [24], [6], [-28], [-6], [-49], [30], [5], [14], [20], [-24], [23], [23], [-12], [38], [-18], [-37], [36], [-42], [29], [36], [-16], [12], [-25], [54], [26], [-36], [-13], [54], [-31], [18], [-19], [36], [17], [6], [-37], [-54], [-25], [-36], [18], [24], [-13], [24], [6], [24], [23], [-10], [41], [-54], [42], [-7], [-48], [-19], [-16], [42], [17], [-6], [8], [-43], [-16], [-36], [47], [-18], [-6], [36], [-34], [56], [29], [-18], [-19], [-30], [-43], [0], [-60], [-30], [-49], [-1], [12], [-36], [-12], [-37], [-13], [17], [24], [-66], [-55], [6], [6], [8], [50], [-54], [-28], [-48], [54], [-1], [-19], [-30], [48], [24], [-61], [-61], [-42], [20], [-12], [-22], [-36], [-18], [-16], [-72], [59], [-16], [35], [36], [35], [12], [5], [60], [36], [-54], [0], [-52], [-55], [-34], [54], [-54], [17], [48], [-13], [-42], [-22], [6], [-12], [-10], [-36], [60], [-13], [44], [18], [38], [-49], [36], [59], [65], [-12], [-28], [-18], [18], [-4], [54], [5], [11], [-46], [-4], [5], [23], [54], [-55], [83], [-30], [-24], [-76], [42], [26], [-76], [30], [26], [54], [53], [-18], [-42], [54], [12], [66], [41], [-72], [-13], [-30], [-78], [17], [23], [60], [5], [78], [23], [5], [60], [-37], [66], [29], [-36], [-18], [6], [-79], [-48], [77], [18], [6], [-55], [6], [53], [-34], [-48], [65], [-36], [29], [-19], [41], [6], [42], [26], [12], [56], [-66], [-19], [12], [5], [60], [-19], [56], [-31], [18], [60], [-91], [90], [42], [35], [29], [-36], [48], [-85], [29], [60], [-13], [-22], [78], [12], [12], [-30], [48], [-67], [-60], [36], [0], [47], [-37], [54], [-79], [-10], [18], [65], [72], [-66], [-67], [35], [-30], [-18], [-10], [36], [-7], [18], [-30], [-25], [-48], [5], [18], [-67], [23], [11], [30], [53], [-60], [30], [92], [-60], [-19], [-40], [60], [35], [-54], [-58], [-48], [23], [18], [-6], [-16], [59], [-48], [-76], [6], [71], [-90], [24], [-64], [41], [66], [102], [-31], [-54], [36], [-24], [-31], [-12], [-30], [-34], [-42], [48], [66], [35], [36]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1350_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1350_2_a_q();".
// 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_1350_2_a_q(:prec:=1)
    chi := MakeCharacter_1350_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_1350_2_a_q();".
// 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_1350_2_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1350_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![1, 1]>,<11,R![0, 1]>,<13,R![-2, 1]>,<17,R![-6, 1]>],Snew);
    return Vf;
end function;