
// Make newform 3234.2.a.f in Magma, downloaded from the LMFDB on 07 May 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_f();".
// 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_f();".
// 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], [0], [0], [1], [4], [-3], [1], [-3], [-9], [-2], [-7], [6], [11], [3], [0], [-9], [10], [-4], [3], [4], [-16], [0], [0], [1], [-15], [4], [-18], [-10], [12], [-1], [-18], [12], [19], [9], [-19], [13], [2], [-12], [6], [-15], [10], [0], [-10], [-21], [-20], [-4], [-8], [-18], [-26], [9], [-24], [28], [-15], [-12], [-18], [-6], [16], [32], [-15], [-20], [-9], [4], [21], [1], [-12], [-10], [-10], [12], [-8], [24], [-18], [4], [-22], [14], [-15], [12], [13], [-6], [-14], [-9], [17], [-12], [-29], [37], [3], [0], [2], [-21], [8], [33], [-6], [-4], [-36], [-10], [-30], [36], [30], [4], [20], [-19], [45], [36], [-21], [29], [22], [-12], [-9], [48], [-38], [-32], [-16], [18], [-14], [-16], [18], [46], [-48], [24], [-18], [-35], [44], [33], [33], [-50], [-27], [35], [-9], [22], [-14], [-4], [6], [2], [-7], [-30], [-50], [-54], [-23], [-42], [-6], [-20], [42], [14], [42], [19], [36], [-20], [57], [40], [24], [32], [-12], [2], [0], [-40], [9], [-7], [-6], [-8], [21], [3], [30], [-37], [0], [-12], [39], [8], [28], [-28], [-6], [-42], [22], [-30], [8], [-32], [-30], [47], [18], [16], [28], [-52], [-18], [29], [33], [-3], [60], [56], [-56], [-4], [-18], [61], [42], [20], [30], [18], [-30], [44], [-37], [42], [-54], [-42], [-5], [-50], [-44], [63], [36], [16], [-6], [30], [58], [2], [48], [44], [12], [0], [-59], [-40], [30], [-12], [66], [-10], [-65], [30], [-70], [36], [-64], [6], [-3], [-17], [-48], [5], [61], [-31], [-57], [58], [72], [-11], [-6], [60], [-48], [33], [64], [-14], [11], [-45], [-36], [16], [-39], [-49], [-36], [-40], [27], [33], [46], [-54], [-18], [62], [37], [42], [-20], [-16], [42], [-32], [-2], [-66], [-38], [-6], [54], [23], [18], [-59], [-52], [34], [-61], [-50], [34], [-33], [-10], [-34], [-6], [42], [8], [-30], [-14], [-23], [39], [62], [15], [64], [75], [-66], [33], [42], [18], [-58], [30], [-20], [18], [66], [25], [-50], [57], [-25], [9], [-82], [-46], [-36], [-41], [-39], [-10], [78], [-54], [45], [-4], [-36], [82], [45], [-42], [13], [-81], [-65], [-67], [3], [-16], [45], [46], [80], [28], [-42], [-6], [44], [48], [-5], [0], [44], [-36], [-61], [-33], [79], [-29], [26], [-24], [-81], [-76], [48], [-12], [13], [26], [42], [-18], [-41], [29], [30], [1], [14], [-39], [0], [-96], [-21], [0], [62], [-45], [-24], [-60], [64], [-55], [51], [20], [-88], [45], [88], [42], [66], [28], [5], [-27], [72], [-56], [78], [-8], [84], [-36], [-16], [-33], [-41], [72], [50], [-41], [95], [-21], [-37], [-30], [-69], [-26], [-3], [68], [79], [12], [38], [-57], [-14], [-36], [32], [102], [99], [-32], [17], [81], [-2], [-33], [10], [-15], [27], [71], [8], [90], [42], [-14], [-18], [-18], [-42], [64], [15], [-90], [61], [84], [72], [21], [16], [6]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3234_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![0, 1]>,<13,R![-4, 1]>,<17,R![3, 1]>],Snew);
    return Vf;
end function;