
// Make newform 3234.2.a.h in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3234_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_3234_2_a_h();".
// 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_3234_2_a_h();".
// 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_3234_a();"
function MakeCharacter_3234_a()
    N := 3234;
    order := 1;
    char_gens := [1079, 199, 2059];
    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_3234_a_Hecke(Kf)
    return MakeCharacter_3234_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], [0], [-1], [2], [3], [2], [3], [-6], [-4], [2], [-3], [2], [-9], [6], [-12], [5], [5], [12], [-16], [17], [9], [-6], [17], [12], [14], [3], [11], [-6], [11], [-12], [18], [14], [6], [-1], [20], [-1], [-12], [12], [12], [-10], [24], [2], [12], [2], [-22], [20], [-21], [-10], [27], [6], [-10], [-24], [6], [-12], [9], [-16], [-10], [3], [14], [-24], [-22], [21], [2], [-33], [11], [-22], [-33], [11], [-12], [30], [-10], [-1], [29], [24], [21], [2], [30], [14], [-24], [20], [-6], [35], [-19], [18], [30], [38], [6], [-16], [0], [24], [8], [-3], [-28], [-6], [6], [18], [26], [23], [-46], [-36], [0], [-6], [38], [17], [36], [6], [27], [-28], [-1], [-31], [30], [11], [-16], [0], [-28], [39], [-21], [-33], [32], [-34], [-30], [12], [-19], [-30], [-28], [-21], [14], [-49], [38], [-30], [2], [14], [15], [-46], [-27], [-22], [-27], [-9], [26], [-24], [14], [33], [-34], [33], [17], [-21], [35], [39], [-37], [6], [-13], [12], [47], [-27], [-7], [18], [-34], [-18], [18], [27], [-43], [-42], [-36], [-45], [-22], [26], [50], [-6], [-27], [-58], [18], [-34], [20], [18], [26], [6], [47], [14], [26], [-15], [2], [-27], [-24], [42], [53], [17], [-16], [42], [14], [-39], [5], [30], [-12], [6], [2], [-64], [30], [-48], [18], [56], [-58], [-16], [54], [42], [20], [9], [-15], [-16], [2], [-21], [68], [0], [6], [-70], [56], [-15], [45], [-54], [14], [47], [66], [-64], [3], [-1], [36], [-9], [-7], [12], [-28], [-28], [-16], [21], [-1], [24], [-22], [-36], [54], [63], [-36], [-52], [-4], [26], [30], [6], [-10], [42], [-4], [0], [14], [-42], [33], [-19], [6], [-12], [-40], [-46], [18], [-40], [41], [12], [-13], [47], [-12], [44], [-57], [36], [-46], [-72], [-28], [-49], [-10], [17], [-52], [-58], [6], [17], [8], [21], [-42], [-58], [48], [-61], [44], [69], [-22], [36], [-10], [-42], [45], [-78], [-33], [-33], [-49], [54], [-10], [21], [51], [-10], [-40], [-66], [-64], [-30], [-13], [83], [48], [-4], [84], [-61], [-66], [42], [-15], [-49], [72], [2], [66], [36], [35], [-6], [62], [-73], [-36], [-64], [42], [38], [71], [-1], [60], [-48], [-91], [-30], [-40], [9], [-28], [-42], [32], [54], [-22], [20], [32], [6], [36], [50], [-30], [-60], [50], [-52], [48], [69], [-22], [47], [21], [35], [86], [42], [9], [-63], [-42], [51], [26], [-57], [24], [63], [68], [-91], [78], [8], [2], [-6], [29], [-78], [6], [-16], [-16], [-60], [-42], [92], [87], [80], [9], [-60], [35], [42], [-40], [-45], [50], [-94], [-28], [33], [59], [-81], [30], [53], [-99], [-40], [32], [-66], [68], [-66], [86], [-12], [-40], [-60], [18], [77], [62], [45], [-37], [18], [-10], [0], [12], [20], [-40], [3], [-90], [-46], [-12], [-6], [-21], [29], [48], [36], [-46], [-39], [0], [-90], [65], [-12]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3234_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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