
// Make newform 3344.2.a.c 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_c();".
// 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_c();".
// 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], [-1], [-3], [-2], [1], [4], [-2], [1], [7], [4], [3], [-5], [4], [2], [0], [-14], [5], [-8], [-7], [9], [8], [-6], [-2], [-13], [-11], [-14], [16], [6], [2], [13], [-8], [-6], [-15], [-14], [10], [-6], [5], [-20], [12], [6], [-23], [-17], [17], [-12], [18], [0], [0], [-9], [-22], [11], [20], [10], [-8], [5], [14], [26], [-22], [24], [22], [-2], [-16], [-8], [-16], [-4], [-9], [-27], [-9], [-14], [-28], [14], [3], [-8], [-1], [-22], [-13], [-13], [-19], [-26], [-6], [-10], [-12], [6], [-30], [25], [-12], [1], [-9], [-16], [36], [-43], [-39], [-12], [11], [36], [-12], [22], [39], [25], [20], [-8], [28], [-14], [0], [-20], [-32], [-23], [4], [20], [24], [2], [22], [4], [-6], [35], [-17], [-21], [33], [9], [-29], [-30], [-43], [-34], [18], [32], [5], [-50], [35], [-1], [-13], [0], [-18], [-24], [21], [-38], [-40], [0], [26], [32], [53], [12], [22], [-54], [-25], [12], [9], [23], [-2], [12], [-19], [0], [-40], [21], [36], [42], [-20], [36], [-10], [-38], [24], [-34], [57], [-38], [-4], [15], [33], [51], [-32], [6], [30], [19], [-50], [-58], [-28], [-44], [-23], [-31], [-34], [-33], [-32], [64], [32], [-42], [5], [38], [21], [18], [16], [40], [-54], [-6], [-11], [-6], [13], [6], [-48], [23], [-18], [-13], [-62], [-14], [-24], [-26], [34], [48], [-21], [-67], [-55], [68], [60], [-40], [-4], [7], [-9], [-12], [-6], [-13], [52], [0], [32], [3], [-56], [8], [69], [69], [-56], [58], [-10], [-32], [-72], [12], [-67], [-24], [-2], [36], [-19], [34], [-15], [-24], [43], [-63], [-9], [-16], [-4], [-7], [48], [12], [-52], [28], [-50], [-50], [24], [-42], [41], [-58], [-18], [-48], [-34], [-10], [9], [38], [28], [16], [10], [-66], [-58], [24], [-37], [21], [-18], [66], [49], [47], [50], [-60], [-24], [11], [75], [-46], [-24], [-28], [-76], [61], [64], [-2], [32], [21], [74], [22], [45], [78], [-31], [-36], [28], [-30], [10], [-44], [29], [3], [-18], [-22], [-14], [0], [-40], [36], [-73], [83], [-17], [-35], [-36], [60], [-84], [-2], [-90], [-23], [-32], [74], [63], [-90], [-78], [40], [12], [-47], [78], [49], [-46], [47], [43], [71], [-48], [-76], [-46], [-42], [62], [60], [-60], [33], [1], [-4], [39], [10], [45], [23], [16], [-1], [35], [-90], [-73], [-13], [12], [51], [28], [63], [30], [36], [-10], [-38], [-43], [-2], [66], [45], [-2], [42], [-17], [-62], [31], [57], [-24], [26], [-92], [37], [-48], [-94], [-48], [82], [9], [12], [-30], [-86], [38], [54], [46], [39], [62], [-10], [-24], [19], [-32], [45], [72], [-77], [1], [-9], [45], [43], [-5], [-36], [-74], [14], [68], [98], [-74], [33], [104], [30], [96], [14], [-34], [-36], [36], [99], [-46], [-22], [20], [30], [-58], [-43], [-12], [-77], [-2], [74], [-1], [56], [56], [82], [-6], [-101], [103], [-30], [-17], [104]];
    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_c();".
// 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_c(: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_c();".
// 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_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3344_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![3, 1]>,<7,R![2, 1]>],Snew);
    return Vf;
end function;