
// Make newform 1334.2.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1334_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_1334_2_a_b();".
// 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_1334_2_a_b();".
// 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_1334_a();"
function MakeCharacter_1334_a()
    N := 1334;
    order := 1;
    char_gens := [465, 553];
    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_1334_a_Hecke(Kf)
    return MakeCharacter_1334_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], [1], [-3], [-4], [-3], [5], [-6], [8], [1], [-1], [5], [2], [12], [5], [-3], [9], [0], [-10], [-10], [6], [2], [5], [-12], [12], [-10], [-6], [14], [6], [-7], [6], [8], [-12], [-18], [-22], [15], [14], [-4], [11], [6], [18], [24], [23], [0], [14], [6], [2], [-13], [14], [12], [8], [3], [-6], [-1], [-21], [-3], [-21], [-6], [11], [-22], [21], [-22], [24], [23], [0], [-1], [-18], [35], [8], [-18], [11], [-18], [-15], [-16], [23], [-16], [-24], [12], [5], [33], [8], [0], [32], [0], [-10], [-34], [0], [0], [-22], [36], [-40], [-15], [9], [-28], [33], [8], [33], [27], [-3], [-28], [14], [20], [30], [-15], [-18], [32], [-22], [0], [15], [3], [38], [-1], [29], [36], [-31], [14], [-48], [-16], [18], [-6], [-21], [38], [-19], [36], [12], [2], [3], [-25], [-42], [8], [-22], [5], [48], [-16], [-4], [18], [32], [0], [-52], [48], [-6], [2], [-3], [8], [-27], [-40], [39], [26], [9], [41], [-36], [-37], [-12], [-34], [-15], [44], [-27], [20], [18], [2], [33], [39], [-27], [-31], [-24], [-21], [15], [-4], [2], [47], [36], [12], [-37], [36], [26], [-4], [-30], [-34], [-18], [29], [26], [35], [-15], [14], [-39], [-18], [-21], [-10], [-4], [32], [30], [-31], [-63], [-43], [48], [12], [6], [50], [-34], [63], [0], [-12], [68], [8], [62], [12], [45], [-31], [12], [-45], [5], [-28], [54], [-19], [-27], [-24], [59], [-34], [-12], [18], [-48], [62], [-46], [-66], [29], [60], [-28], [48], [0], [71], [-48], [-28], [-10], [32], [54], [14], [48], [14], [-42], [-18], [9], [-45], [38], [-70], [50], [54], [-12], [-4], [-54], [26], [45], [32], [-27], [-3], [44], [-24], [24], [2], [-1], [18], [-7], [-40], [-51], [-70], [38], [-66], [53], [-54], [78], [20], [-33], [-49], [38], [35], [-76], [-58], [-31], [-12], [41], [20], [-6], [12], [-4], [12], [-58], [-16], [60], [38], [-84], [-28], [-51], [-45], [-72], [33], [63], [68], [27], [35], [-33], [-66], [-52], [-10], [18], [-40], [-9], [-16], [65], [-9], [-49], [-48], [-79], [60], [78], [-18], [-4], [42], [65], [-12], [-60], [-61], [-30], [35], [-13], [78], [-61], [45], [-4], [-40], [-58], [57], [-87], [8], [69], [-82], [24], [71], [72], [-94], [-12], [-58], [26], [8], [54], [90], [-34], [-30], [-33], [56], [44], [-3], [-54], [-28], [-22], [-24], [38], [-70], [-6], [-21], [12], [-72], [-24], [-61], [-15], [39], [-54], [-13], [-4], [36], [-4], [44], [51], [56], [24], [0], [-58], [-1], [84], [-39], [-70], [-63], [-55], [-90], [63], [-55], [72], [86], [-66], [32], [-31], [-55], [-24], [-22], [99], [-75], [53], [87], [-49], [20], [0], [-88], [48], [62], [18], [-76], [-57], [-87], [-70], [29], [-18], [-31], [-18], [-22], [51], [90], [44], [-40], [0], [39], [-10], [-36], [21], [30], [92], [27], [21], [-37], [-105], [30], [12], [38], [15]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1334_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1334_2_a_b();".
// 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_1334_2_a_b(:prec:=1)
    chi := MakeCharacter_1334_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_1334_2_a_b();".
// 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_1334_2_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1334_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1]>,<5,R![3, 1]>],Snew);
    return Vf;
end function;