
// Make newform 3344.2.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3344_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_3344_2_a_a();".
// 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_3344_2_a_a();".
// 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_3344_a();"
function MakeCharacter_3344_a()
    N := 3344;
    order := 1;
    char_gens := [2927, 837, 2433, 705];
    v := [1, 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_3344_a_Hecke(Kf)
    return MakeCharacter_3344_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], [-3], [-2], [-1], [-1], [-7], [-3], [-1], [-3], [1], [-2], [-6], [-2], [-4], [0], [3], [-7], [-12], [-15], [-6], [-9], [8], [-16], [-16], [8], [-2], [12], [-11], [11], [20], [16], [14], [-17], [-10], [14], [12], [18], [-14], [-18], [-2], [4], [-2], [-3], [4], [-28], [-7], [23], [-2], [-25], [20], [6], [-13], [-20], [30], [18], [-12], [18], [3], [-8], [-16], [22], [15], [-16], [-11], [-27], [1], [-17], [4], [-6], [10], [-31], [-27], [-16], [-13], [1], [-26], [-14], [-30], [-2], [-14], [6], [3], [22], [0], [22], [14], [-32], [23], [-40], [32], [12], [-28], [20], [6], [20], [23], [-18], [-4], [-11], [8], [-8], [-30], [8], [4], [-2], [-17], [-20], [26], [4], [0], [14], [28], [18], [20], [-8], [0], [34], [-3], [12], [-27], [25], [8], [27], [16], [-10], [-24], [-50], [25], [-23], [-6], [-52], [-8], [34], [-2], [-15], [-37], [-19], [-41], [-33], [-17], [25], [-8], [-53], [-13], [53], [-32], [30], [-6], [14], [38], [27], [-38], [-20], [20], [-1], [30], [-39], [-51], [-41], [-15], [8], [4], [-56], [-28], [12], [-50], [44], [-2], [48], [16], [-31], [-5], [-24], [1], [-50], [-2], [0], [36], [-34], [56], [55], [-15], [22], [-24], [-11], [24], [-17], [-36], [30], [-57], [16], [-24], [-12], [9], [-24], [0], [15], [-48], [-23], [-3], [-6], [-8], [-13], [-26], [42], [54], [-43], [-45], [35], [-11], [11], [-60], [-56], [67], [10], [64], [-53], [-20], [-42], [2], [6], [4], [-6], [37], [-63], [-10], [36], [-40], [18], [48], [14], [-17], [16], [-12], [68], [-7], [38], [54], [-40], [50], [-36], [0], [39], [71], [40], [8], [0], [-12], [4], [-9], [-40], [-15], [33], [-58], [-74], [-6], [44], [52], [-58], [-78], [-14], [60], [32], [-19], [42], [-11], [-35], [63], [13], [-4], [59], [7], [49], [9], [8], [-21], [-60], [53], [38], [-20], [-18], [25], [-17], [-23], [76], [72], [-67], [74], [-1], [-60], [64], [-30], [-44], [-52], [-14], [-14], [-64], [-48], [25], [84], [34], [-45], [3], [13], [40], [-6], [57], [-39], [0], [-32], [28], [-40], [-48], [-28], [48], [50], [-60], [-18], [-61], [-34], [-91], [67], [37], [-34], [-35], [16], [-7], [15], [4], [18], [-40], [57], [7], [2], [-30], [6], [-15], [12], [23], [-15], [-19], [27], [37], [80], [15], [-20], [-56], [-37], [-28], [24], [44], [34], [-66], [-18], [-70], [44], [-3], [32], [53], [-22], [-58], [-6], [-66], [-45], [22], [-6], [-2], [2], [70], [-4], [35], [-33], [-91], [80], [50], [-41], [-40], [-46], [-85], [-40], [-50], [-58], [13], [14], [-17], [42], [-73], [-12], [42], [-38], [78], [29], [-6], [-60], [46], [42], [-52], [-24], [-4], [-30], [98], [-86], [-48], [-54], [58], [-17], [-18], [48], [51], [56], [94], [-48], [-71], [-16], [63], [28], [54], [58], [91], [-62], [6], [71], [-101], [-15], [86], [-23], [-39], [-99], [-18], [-33]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3344_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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