
// Make newform 3344.2.a.r in Magma, downloaded from the LMFDB on 20 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_r();".
// 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_r();".
// 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 := [-1, -4, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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], [0, -1, 0], [0, 1, 0], [-1, -1, 0], [1, 0, 0], [-1, 1, -2], [0, 2, 0], [1, 0, 0], [-2, -1, 1], [4, -2, 1], [-1, -2, 3], [4, 1, 1], [-3, 3, -4], [-4, 0, -3], [2, 2, 2], [-2, 0, -2], [-4, -1, 3], [-2, 2, -2], [-3, 6, -1], [-6, -1, 0], [2, -2, 0], [-2, 4, -2], [-8, 4, -3], [-4, -3, -5], [-4, -1, 3], [2, -8, 2], [2, 0, 5], [-10, 0, 0], [-6, 0, 4], [4, -1, 7], [6, -2, 8], [-5, -9, 6], [9, -2, 3], [-8, 4, -1], [4, 10, -2], [-2, -4, -4], [-8, -3, -2], [-4, -4, 2], [-2, 6, -2], [-4, 0, -3], [3, 0, 3], [-4, 1, -9], [-6, 5, -7], [-6, 0, 7], [-6, -4, -4], [4, -8, 2], [-8, 0, 2], [4, 5, -3], [8, -10, 4], [-15, 0, -1], [0, -8, 0], [-6, -6, -7], [-10, -4, 5], [-16, -1, -5], [8, -10, 8], [10, -6, -3], [-14, 4, -2], [1, -1, 6], [-2, 8, 4], [-10, 6, -5], [-9, 1, 0], [13, -9, -2], [8, -8, -2], [12, 4, -6], [18, -7, 8], [-4, -5, 5], [-4, 3, -8], [-7, 5, -14], [-12, -4, -8], [6, 4, -4], [2, -5, 3], [-17, 9, 2], [10, -1, 7], [-12, 0, -11], [11, -2, 1], [19, -6, 7], [16, -7, 4], [-5, -9, 8], [2, 12, 2], [-6, -2, -1], [6, 2, 10], [-18, 4, -6], [4, 2, 6], [12, -1, -7], [16, 8, -8], [20, 7, 3], [-2, -13, 13], [-12, 4, 2], [0, -12, 8], [4, 19, -13], [6, -11, -5], [-9, 9, -14], [18, 5, -2], [-17, 5, -8], [-6, -10, -6], [-6, -18, 5], [4, 5, 1], [26, 1, 9], [-12, 12, 10], [30, -2, -4], [-8, 8, -12], [8, -2, 6], [-14, 6, -2], [13, -1, -8], [3, 5, -4], [-13, 4, 3], [-24, -4, 8], [10, -2, -6], [-2, -8, 7], [15, -9, 18], [18, 4, 6], [22, -10, -2], [21, -11, -6], [14, 7, -5], [-6, -13, 5], [16, 17, -5], [0, 11, 3], [16, 7, 13], [9, 8, -9], [14, 8, -10], [0, 3, 15], [11, 3, 2], [4, 20, -13], [0, 8, -8], [-10, -19, 1], [8, 18, -6], [-15, -8, -9], [-4, 5, -5], [16, -7, 17], [-2, -6, 2], [-28, 4, -9], [-2, -6, 2], [0, 11, 7], [-12, 12, -1], [24, 12, -2], [6, 6, -10], [10, 0, -8], [-32, 4, -2], [24, 7, 1], [0, -20, 20], [-22, 8, -8], [18, 0, -2], [-28, 1, -7], [-28, -4, 0], [-6, 9, 1], [18, -7, -2], [28, -10, 10], [7, 13, 8], [-24, -1, 11], [-5, -11, -10], [-12, 2, 11], [5, -2, 11], [0, -16, 2], [-16, -10, -10], [-12, -16, 8], [4, 0, -12], [10, 10, 5], [19, -17, 6], [-26, -6, 2], [-14, 0, -16], [-28, -5, -7], [6, -8, -8], [-4, 4, 0], [28, 7, -10], [-16, 15, -17], [1, -12, 11], [5, -13, -2], [14, 24, -14], [-9, -25, 2], [13, -20, 19], [-20, 6, -2], [8, 14, 12], [-14, -8, -21], [0, -8, -6], [28, -5, -1], [6, -1, -12], [-35, -7, 2], [38, -5, 7], [-6, -10, -14], [-32, 0, -10], [-40, 0, -4], [8, 18, 10], [38, 1, 5], [18, -2, 1], [16, 23, -13], [-2, 4, 20], [-34, -6, 2], [-8, 2, 5], [6, 2, -7], [-22, -18, 11], [-22, -15, 11], [-3, 25, 2], [-12, -15, 16], [-24, -2, 8], [-5, 17, -24], [50, -1, 1], [-24, -14, 14], [19, -10, 19], [6, 0, -8], [-16, 6, 6], [-5, 21, -10], [4, 6, 14], [-20, -6, -2], [-38, 12, -9], [0, 5, 15], [-22, 9, -27], [26, -19, -7], [-18, 6, -12], [-8, 8, 0], [-28, -2, -23], [2, -10, 12], [44, -9, 20], [24, 17, -5], [-15, -9, -12], [24, 10, 2], [16, 9, 11], [-4, 20, 12], [-38, -2, -14], [33, -1, 6], [-9, 10, 7], [11, -19, -2], [-2, 22, -18], [-44, 3, -7], [-2, 21, -7], [12, 36, -20], [6, 20, 16], [20, 10, 16], [-2, 0, 19], [-32, -12, -2], [16, -2, 23], [-4, 21, -4], [10, -2, 4], [4, -14, 0], [-37, 17, -12], [18, -11, -9], [-23, 21, -16], [1, 14, 7], [-2, 6, -2], [-18, -1, -13], [-23, 6, -3], [32, 15, 4], [-17, 5, -24], [4, -20, 18], [-12, 19, 3], [10, -30, -10], [11, -15, -18], [24, 0, -10], [32, -6, 19], [-2, 24, -6], [-20, -6, 14], [2, 2, -18], [-44, -6, -4], [-6, 9, -21], [2, -8, 6], [-32, 10, -14], [-4, -18, 23], [-56, 8, -7], [-26, 8, -6], [-32, 11, 9], [6, 20, 4], [-8, -32, 22], [-22, 10, -16], [30, 8, 12], [4, 8, -9], [-34, 26, 5], [-20, 20, -32], [-32, -3, 17], [6, 3, 0], [-8, -30, 20], [16, -6, 28], [10, 1, 5], [44, -9, 38], [-19, 29, 14], [2, 24, -9], [-2, 24, 1], [-32, -3, -9], [-18, -5, -11], [-24, -28, 5], [2, 32, -27], [0, -30, -3], [38, -14, -6], [-2, -3, 5], [-30, 8, -7], [24, -12, 11], [-16, 10, -17], [-10, -7, 3], [-17, -13, 14], [-9, -29, 4], [-26, 21, 1], [2, -2, 11], [-21, -16, -7], [4, -24, -4], [39, 1, 20], [16, 2, 12], [12, -36, -9], [-6, 6, -14], [9, 18, -7], [26, -13, -9], [-6, 32, -24], [-8, 16, -5], [-10, -24, -2], [-17, -11, 22], [-28, 16, -20], [36, 30, -9], [-52, 9, 5], [-32, -19, 1], [3, -14, -7], [-20, 31, -9], [-12, -8, 16], [4, 4, 0], [3, 5, -10], [-40, 12, -5], [33, -3, 30], [0, 25, 0], [3, -27, 18], [14, -4, 16], [26, -1, 23], [-22, 4, 10], [34, 6, -13], [-10, -2, -2], [40, -12, 18], [46, -23, -3], [-18, -4, 0], [12, -13, -5], [-14, 0, -26], [-44, -15, 7], [-5, 22, -7], [0, -37, 2], [-4, 4, 2], [48, -16, -10], [-16, -38, 22], [-4, 38, -14], [-10, 22, -9], [8, -16, 0], [-24, 14, 11], [-8, 23, 1], [54, 1, -5], [-11, -5, 8], [22, 11, 3], [-18, 6, -19], [-28, -41, 19], [50, -3, 10], [-25, -7, -2], [2, 9, 30], [24, 17, -13], [18, -28, 6], [-25, -26, -9], [1, -24, -3], [-18, 38, -4], [30, -17, -17], [6, -34, 2], [-12, -15, -5], [15, 11, 24], [-6, 38, -10], [33, -19, -8], [18, 34, -17], [-18, 9, 23], [-36, 0, -17], [-14, 0, 16], [-5, -8, 1], [38, 16, -8], [6, 20, -18], [-14, 9, 18], [34, 4, -2], [-6, 35, -19], [32, 15, 11], [44, -8, 36], [-36, 10, -8], [-22, -16, -1], [-18, 37, -19], [-7, 11, 0], [20, -26, 2], [4, 14, 21], [-20, 38, -14], [6, 1, 29], [37, -29, -16], [7, -33, 12], [8, 42, 2], [-50, -24, 10], [10, 12, -24], [48, -14, -10], [37, -4, 9], [-47, -19, -4], [-14, 12, 14], [-28, 20, 8], [52, 1, 1], [-8, 4, 18], [-60, 9, -14], [-53, -11, -8], [10, -15, 4], [10, 1, -19], [-22, -9, 13], [20, -5, -5], [14, -5, -15], [-15, -12, -1], [38, -20, -1], [48, -6, 2], [30, -32, 4], [-47, -17, -8], [16, 10, 6], [-13, 7, -16], [-2, 15, -28], [-34, 22, -18], [-20, -10, -2], [-4, -14, -15], [-20, -10, 26], [-17, 11, -4], [-58, -26, 10], [-12, -8, -16], [14, -9, 11], [-42, -4, -8], [12, 42, -16], [-24, 20, -36], [16, -36, 23], [38, 28, -8], [-34, -27, 13], [-16, -28, -10], [67, 10, 3], [-29, -21, 14], [-20, -6, 4], [2, 27, -5], [-26, 14, -34], [18, 6, 12], [18, 16, -16], [52, 10, -2], [16, -27, 37], [3, -14, -11], [4, 14, 4], [12, 37, -21], [6, 24, -21]];
    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_r();".
// 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_r(:prec:=3)
    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_r();".
// 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_r( : 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, -4, 0, 1]>,<5,R![-1, -4, 0, 1]>,<7,R![-2, -1, 3, 1]>],Snew);
    return Vf;
end function;