
// Make newform 3344.2.a.j in Magma, downloaded from the LMFDB on 23 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_j();".
// 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_j();".
// 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
        poly := [-3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0], [0, -1], [0, -1], [-1, 1], [1, 0], [-3, -1], [0, 2], [1, 0], [2, -2], [-8, 1], [1, 3], [8, 0], [-5, -1], [0, 1], [2, 4], [2, 2], [4, 0], [-2, 0], [-1, -5], [6, 3], [-14, 2], [-2, 6], [-4, -3], [0, 6], [-4, 8], [2, -2], [-2, -5], [-2, 4], [-2, -4], [-4, 0], [-10, -2], [-1, -5], [-15, -3], [4, -9], [-12, -4], [6, -8], [16, 1], [-8, -2], [2, 0], [0, 7], [-7, 1], [16, -2], [6, 0], [10, -7], [-6, 0], [4, -10], [-8, 2], [4, 4], [12, 2], [-3, -5], [-16, 0], [-10, 7], [6, -9], [-20, 0], [-16, -2], [-2, -5], [14, -10], [-15, -1], [-14, -4], [6, -13], [11, 3], [-9, 5], [-20, 10], [12, 2], [18, -13], [12, -6], [-4, 3], [-1, -5], [20, 4], [-14, 4], [18, -4], [15, -7], [6, 8], [8, -1], [-31, 1], [-19, 3], [16, 3], [7, 9], [-6, 6], [-14, 15], [-14, -4], [-6, -10], [8, 4], [-8, 6], [-16, 0], [12, -4], [-6, 6], [0, 10], [-4, 4], [24, -8], [6, 10], [-17, 9], [-18, 11], [19, 7], [-22, 0], [30, -9], [20, -4], [10, 0], [4, -6], [-6, -14], [0, 8], [-12, 8], [6, 4], [27, -1], [-17, 3], [19, -17], [24, -16], [-10, -8], [18, -23], [-31, -3], [6, -6], [2, 0], [-3, -15], [-30, -4], [42, 2], [-12, -6], [-4, -4], [0, 2], [-19, 19], [26, -14], [20, -12], [-19, 25], [0, -11], [-8, 0], [-34, 4], [-28, 4], [-11, -13], [8, 14], [8, -12], [22, 0], [-16, 15], [-18, 0], [16, 0], [4, 7], [0, 2], [22, -20], [18, -20], [12, 10], [16, 14], [-36, 12], [14, 16], [-10, -10], [-32, -8], [28, 0], [-2, -4], [6, -9], [-32, 8], [9, -11], [-20, 12], [37, -13], [-16, -13], [5, 5], [16, -6], [-20, -12], [-4, 0], [12, 20], [-34, 3], [-13, -5], [22, 4], [22, 16], [28, 6], [2, -16], [-4, 4], [-20, 9], [-20, 12], [-1, 17], [-37, -7], [-22, 2], [-15, -19], [-15, -21], [-24, -8], [-16, -6], [38, -13], [-8, 18], [-36, 0], [22, 1], [-47, -3], [-2, 8], [38, -12], [32, 6], [8, -16], [36, 8], [-18, 16], [-22, 13], [4, 12], [6, 16], [-22, 24], [8, -7], [6, -7], [6, 5], [-2, -18], [25, 13], [4, -23], [-28, -14], [39, -21], [46, 2], [0, -8], [47, -7], [-18, -16], [40, -16], [1, 11], [40, 0], [-52, 8], [-46, -3], [12, 10], [-30, 16], [-10, 8], [-18, -6], [0, -12], [-4, 17], [-2, -18], [12, 5], [-28, -2], [43, -5], [-12, -16], [0, 6], [-20, 32], [14, 20], [-33, -3], [19, -15], [-31, -5], [-2, -8], [12, -6], [-2, -8], [8, -8], [-42, 12], [36, -18], [-6, 29], [24, -2], [-36, 5], [-36, -9], [-26, -14], [16, -34], [5, -35], [-14, 6], [-55, -5], [-47, -7], [-6, -8], [18, -8], [-1, 25], [-24, 27], [-45, 7], [-8, 30], [-20, -4], [-50, 20], [-13, 13], [-12, 2], [-24, -13], [-42, 10], [-20, -4], [2, 24], [0, 26], [-18, 18], [34, 10], [-40, 4], [-24, 13], [0, 13], [-18, -10], [-12, 18], [-38, 4], [12, -14], [-14, -22], [-14, 0], [32, 13], [-18, -21], [-20, 12], [8, -32], [-26, 1], [52, -6], [-36, 30], [-42, 12], [-20, 25], [29, -3], [-2, -17], [-26, 15], [8, -14], [18, 2], [-12, 19], [26, 3], [-36, 15], [2, -32], [-46, 16], [-18, -7], [20, 13], [28, -39], [-42, 10], [-49, 7], [-43, 11], [-34, 24], [-46, 3], [-33, 13], [-36, 32], [25, -19], [12, -34], [0, 25], [46, -12], [7, 9], [-30, 32], [-46, 12], [20, -17], [-34, 2], [-35, -5], [20, -12], [-12, -5], [28, -16], [52, 12], [49, -19], [-20, 16], [16, 4], [40, -4], [11, 1], [0, 35], [-9, -9], [24, -23], [3, -3], [10, 20], [-38, 16], [30, -22], [-22, 45], [10, -36], [-8, 10], [-58, 16], [-38, 28], [28, 12], [30, 10], [4, 30], [-39, 17], [40, 1], [0, -2], [36, -18], [-56, 4], [-24, -20], [-46, -7], [40, 0], [60, -15], [-24, 18], [-38, 18], [3, 27], [14, 20], [-14, 15], [36, -32], [-6, -23], [13, 31], [38, -33], [-68, 2], [-18, 14], [-85, 5], [43, -1], [-54, 6], [22, -28], [58, 0], [-40, -14], [11, 13], [6, 0], [-37, -7], [18, 3], [6, -26], [24, -45], [6, 8], [21, -33], [10, 4], [-30, -6], [54, 3], [66, -10], [2, -22], [-12, 40], [-32, 12], [-32, 38], [2, 9], [30, 0], [-53, 11], [28, -28], [48, 13], [36, 20], [38, -16], [39, -21], [-9, 13], [44, 20], [22, -14], [14, 4], [60, -12], [-19, 37], [-31, -9], [-2, 14], [68, 0], [12, -12], [0, -14], [4, -39], [63, 7], [30, -15], [26, -4], [14, -14], [72, -12], [-10, 2], [-41, 29], [38, 5], [44, -4], [6, -28], [11, 15], [-16, -16], [-7, 11], [-34, -7], [26, 16], [24, -12], [48, -35], [-12, 28], [11, 9], [34, -4], [-12, 16], [26, 12], [-30, -20], [-48, 6], [24, -40], [-20, 15], [-18, -16], [-18, -36], [-48, -18], [-19, -25], [-13, -9], [12, 30], [-14, 36], [98, 0], [14, -14], [-22, -8], [16, -12], [8, 4], [-47, 17], [4, 6], [-64, 14], [-22, -5]];
    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_j();".
// 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_j(:prec:=2)
    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_j();".
// 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_j( : 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, 1]>,<5,R![-3, 1, 1]>,<7,R![-3, 1, 1]>],Snew);
    return Vf;
end function;