
// Make newform 3380.2.a.q in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3380_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_3380_2_a_q();".
// 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_3380_2_a_q();".
// 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, -3, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-1, 2, 0, 0], [0, -3, -1, 1], [-2, -1, 1, 0]];
    Rf_basisdens := [1, 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_3380_a();"
function MakeCharacter_3380_a()
    N := 3380;
    order := 1;
    char_gens := [1691, 677, 1861];
    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_3380_a_Hecke(Kf)
    return MakeCharacter_3380_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, 1, 0], [1, 0, 0, 0], [2, 0, 1, -1], [0, 0, 0, 1], [0, 0, 0, 0], [2, -1, 1, -2], [-1, 1, -2, 1], [-2, -1, -1, 0], [0, -1, 0, 2], [2, 0, 4, -2], [6, 0, 3, -1], [6, 0, 0, -1], [3, 2, 1, -2], [0, 0, 0, 2], [1, 3, -4, 4], [4, -2, 2, 1], [0, -1, -2, -2], [4, 0, -1, 5], [3, -3, 0, -3], [5, -1, -2, 0], [-1, 1, 2, 4], [-8, -2, 2, -2], [4, -2, 2, -3], [0, -2, -3, -1], [-4, 1, -2, 0], [-7, 1, -4, -2], [-1, -2, 1, 6], [1, 3, -4, 2], [-6, 1, -3, -2], [10, -1, 1, -4], [-1, -3, -2, 2], [-5, 1, -7, 1], [-2, -1, 4, -4], [8, 2, -2, 5], [3, 1, 0, 0], [1, -1, -2, 6], [7, -1, -1, 3], [-5, 1, -7, -1], [1, -2, 5, -4], [-7, 4, -2, 6], [-7, 1, -10, 6], [-6, -1, 6, -4], [12, 2, -3, -1], [6, 0, -3, 9], [1, 2, -2, -8], [0, -1, -2, 2], [-1, 3, -5, 5], [-1, 5, 1, -3], [4, -4, -4, 0], [9, -1, -6, 2], [-4, 8, -8, 2], [3, -5, 0, -7], [7, 4, -4, 2], [19, 0, -1, -2], [-5, -2, -7, 8], [-9, -4, 6, -4], [7, 1, -4, -1], [7, 0, 3, 0], [2, 2, -8, 4], [-7, 6, -3, 2], [-1, -1, 1, 1], [-3, -1, 0, -6], [7, 5, 2, -6], [-4, -2, 2, 2], [-4, -4, -2, 0], [-13, -1, 4, -3], [-5, 5, 4, 4], [4, -3, -7, -2], [-9, -3, -6, 11], [5, -1, 1, -11], [-4, -4, 4, 2], [13, -4, 1, -2], [-14, -3, 3, 0], [3, 1, 6, -15], [-14, -2, -13, 9], [15, -1, 6, 8], [9, 5, 3, -11], [-3, -3, 0, 3], [-4, 0, -2, 5], [-13, 4, -2, 6], [14, 2, -8, 4], [-26, 4, -4, 3], [7, -4, 1, 4], [-13, -2, 2, -6], [-9, 3, -6, 0], [-8, -2, 2, 3], [7, 1, -1, 13], [2, -4, 4, -3], [-7, 5, -2, 2], [-20, 0, -10, 4], [2, 2, 10, -7], [0, 2, 3, -11], [7, -10, 8, 0], [12, -2, 0, -2], [-1, 0, 1, 8], [-2, -2, 14, -13], [-20, 6, -16, 4], [-7, 0, 9, -16], [23, 3, 4, -6], [23, 3, -6, 12], [-5, 7, -1, 3], [-22, 1, 1, 6], [-22, 7, 4, 6], [8, 0, 0, 2], [29, -5, 10, 4], [-14, -2, -1, 17], [1, -5, 2, 0], [7, 5, 2, -12], [-14, -3, 6, -18], [-1, 0, -3, 18], [8, 2, 7, -7], [-11, 7, -13, 3], [-12, 8, 0, 2], [-5, 3, -16, 11], [3, -2, -12, 4], [-31, -1, -5, 9], [13, -2, -7, 8], [12, 3, 9, -12], [1, 0, 2, -14], [18, 0, 12, -3], [-17, 0, -15, 6], [17, 3, 10, -10], [-13, -1, -5, -1], [-5, -5, 8, -15], [-24, 2, 0, 8], [10, 2, 2, 15], [3, -8, 18, -14], [9, 1, 2, -16], [26, 2, -2, -4], [-8, -2, -10, 5], [-25, 5, 1, -3], [2, 7, 2, 4], [-5, 6, -9, -8], [-15, 3, 12, -7], [11, 7, -2, -7], [17, -1, 13, 3], [-20, 0, -1, -9], [32, -3, 13, 2], [-13, -4, -2, -8], [6, 2, -12, -2], [-3, 3, 0, 9], [6, 5, -5, -8], [-13, 5, 4, 2], [-11, 8, -14, -4], [-10, -4, -14, 5], [1, -7, -4, 12], [-10, 0, -2, 14], [-17, -3, -6, 18], [1, -5, -16, 10], [-1, 7, 1, -5], [-19, 4, -8, 12], [0, 4, 8, 2], [12, 3, 9, 6], [4, -7, -11, -4], [-14, 2, -4, -6], [-24, -7, -8, 14], [5, -7, 10, -5], [5, -5, 14, 2], [1, 13, -4, 2], [-25, -1, 7, -1], [-8, 13, -13, 14], [18, 0, -6, 10], [-13, 2, 4, -2], [-8, -8, -1, 17], [-3, 9, -6, -14], [10, 5, -14, 10], [16, -13, 13, -4], [20, -6, 4, -4], [37, -1, 2, 6], [-33, -3, 0, -8], [23, 1, 16, -15], [-14, -3, -10, 16], [-3, 11, -3, 7], [0, -4, -8, -8], [9, 2, 6, -4], [-3, 11, -12, 3], [15, 9, 0, -10], [-8, -3, 21, -18], [-22, 13, -16, 6], [2, 6, -20, 18], [-2, -14, 8, -4], [-2, 0, 6, 10], [-13, -1, 4, -4], [-19, -7, -11, 3], [10, -1, 8, -12], [-14, 0, -6, 6], [12, 14, -6, -2], [2, 10, -2, 5], [23, -1, 4, 3], [3, -2, 11, -8], [-5, 13, -1, 5], [33, -5, 14, 4], [-16, -10, 4, 9], [31, -2, 11, -10], [20, 5, -5, -8], [35, -1, 4, 10], [7, -4, 1, 8], [-17, -5, 8, -20], [-17, -5, -4, -10], [9, 9, 6, 1], [17, 0, 0, 12], [13, -5, -1, 5], [-5, -1, -2, 14], [-7, -1, -2, 7], [-15, -4, -15, 26], [-9, -1, 0, -8], [22, 3, 5, -20], [19, 13, -10, 1], [-5, -4, 4, -18], [12, 1, -13, 10], [5, -7, 10, -20], [-14, -3, 9, 4], [-16, -4, 7, -9], [29, -1, 10, 13], [14, 2, -8, -12], [14, -2, 8, 14], [-10, 5, -14, -8], [12, 6, -3, 5], [17, -13, 22, -8], [11, 0, 0, -6], [24, -4, 0, 6], [-7, -7, 16, -2], [28, -2, -7, -3], [-9, -4, 9, -4], [20, -2, 8, -16], [18, -11, 9, 4], [-17, -2, -22, 20], [9, 4, 11, -26], [9, -3, 12, 8], [6, -1, 7, -8], [-20, -1, -8, 2], [9, -5, 0, -11], [-7, -7, 10, -20], [-1, 3, 12, 18], [-19, -1, 4, 6], [8, -10, -14, -5], [4, 4, 11, -9], [0, 3, 0, 6], [-12, -1, -6, 20], [18, 0, -21, 9], [9, -8, 8, -16], [20, -11, 17, -4], [21, -11, 6, 1], [-2, -2, -19, 27], [-16, 0, -8, 8], [21, -13, 15, -1], [15, 9, 12, -1], [-37, 3, -14, 9], [-6, -5, 15, -8], [-14, -8, -1, -5], [-23, -5, -4, 25], [25, -17, 20, -6], [7, 2, 22, -14], [43, -3, 14, 10], [19, 1, 2, 11], [29, 10, 8, -10], [22, 4, 5, 5], [9, -5, 24, -20], [28, 4, 23, -13], [-29, -13, 4, -2], [-1, 6, -17, 8], [-4, -4, 4, 8], [29, -12, 9, -12], [2, 14, 1, 9], [10, 15, 0, -6], [27, 9, -6, -5], [-40, 8, -20, 4], [14, 2, 13, -29], [-5, -4, 11, -18], [2, 0, 0, -18], [-11, 7, -4, -2], [9, 3, 15, 15], [-3, 2, -2, 4], [11, 10, -1, 2], [9, -1, 9, 9], [19, 1, 5, 5], [14, 10, 16, 0], [39, 7, 0, 5], [23, 4, -8, -6], [-3, 4, -3, 16], [35, -7, -8, -5], [-14, -2, -10, -4], [38, -2, 4, -9], [19, -3, 32, -6], [-38, 6, -16, -8], [7, -3, -30, 18], [-9, -3, 6, -20], [7, -5, 26, -5], [10, -9, -4, -2], [-34, 5, -2, 16], [-15, 6, 9, -6], [-4, -10, 7, -31], [1, 1, -16, 7], [-26, 5, -17, 16], [34, 4, 20, -16], [17, 3, -18, 26], [19, -8, -7, 14], [-4, -13, 4, -2], [-10, 2, -11, 3], [5, -2, -1, -2], [-4, -4, -26, 8], [7, 6, -6, -12], [-33, 7, -12, 10], [-11, 8, 4, -10], [25, 11, -13, 1], [26, -10, 10, 4], [-2, -4, 4, -2], [-39, -3, -18, 19], [-5, 5, -2, -22], [36, 1, -9, -2], [-29, -5, 2, 1], [-26, 0, 8, -14], [9, 8, 1, -8], [14, 8, -11, 19], [38, -11, 20, -4], [7, 7, 2, -27], [-1, -7, -8, -24], [43, 5, -13, 13], [32, -11, 28, -6], [-22, -2, -4, 34], [34, -4, 20, -12], [-15, -6, -24, 24], [0, 2, -27, 27], [-20, -8, 2, 12], [20, -5, -22, -2], [-2, -10, 8, -14], [-9, -5, 33, -23], [-22, 7, -23, 24], [33, 4, -3, 16], [11, -3, 28, -21], [-7, -1, 10, 10], [34, 9, -12, 12], [-30, 0, 15, -1], [-29, 15, -16, 9], [15, -9, 6, -26], [17, -5, -26, 5], [36, 12, 15, -25], [-29, -7, -4, 15], [31, -9, 12, 6], [-1, 1, -14, -20], [-14, -11, 11, -22], [0, 0, 0, -16], [48, -7, 10, -14], [-40, 2, 7, 11], [-14, -22, 20, -12], [-54, -4, 4, 4], [13, -1, -1, 3], [4, 4, -4, -19], [11, 4, 7, -24], [-25, -9, -8, 24], [-22, 2, 1, -1], [-31, -13, -20, 21], [35, 6, 21, -22], [24, 1, -16, 26], [17, 3, -20, -4], [-12, 6, -6, 1], [-10, -10, 16, -21], [8, 6, 10, -10], [25, -11, 14, 10], [14, 0, -11, 19], [-25, 14, -20, 4], [-22, 9, 7, -4], [19, -11, -10, 1], [-22, 19, -13, -2], [-16, 3, -15, -12], [8, -4, 25, 1], [-15, -3, 3, -25], [-6, -4, 4, -26], [-12, 1, -6, -26], [6, 5, 4, -8], [12, 6, -6, -22], [-17, -13, -10, 6], [-2, 9, 6, 0], [-19, 12, -3, 20], [-12, 12, -24, 26], [25, -14, 8, 8], [-13, 21, -11, 7], [11, -4, 22, 16], [-23, -6, -3, 30], [-26, -2, 8, -8], [-2, 4, -19, 1], [23, -1, 22, 14], [-7, 11, -2, -20], [-43, 5, -2, -5], [38, 8, 1, -13], [24, -16, 12, -13], [7, 5, -14, 2], [26, -22, 16, -14], [-34, 3, 1, 14], [2, -20, 22, -15], [19, 19, -1, -7], [-5, -11, 8, 20], [3, 2, 1, -14], [18, 0, 12, 1], [-33, 14, -11, 10], [-40, -8, -14, 23], [-1, 11, 22, -20], [-23, -9, -30, 6], [-2, 4, -28, 21], [15, 3, 18, -25], [16, -3, -25, 28], [15, 17, -15, 7], [19, 13, 26, -12], [-11, 13, 2, -19], [-19, -8, -4, 10], [-35, -3, -7, -23], [-45, -3, 12, -13], [-47, 3, -16, -2], [-41, 7, 2, -5], [5, -5, -4, 0], [7, 6, 23, -32], [62, 7, 13, -12], [-31, -2, -16, 30], [-37, -8, 5, -24], [-27, -3, 18, -12], [67, -5, 2, 9], [-11, 9, 18, 12], [36, -6, 27, 7], [6, 5, 3, 8], [-14, -5, -16, -4], [-5, 5, 2, 16], [-54, 6, -21, -15], [8, 0, 4, -4], [-21, 3, 15, 13], [-16, 2, -11, 3], [33, 9, -6, 12], [0, 12, -12, 40], [-69, -7, 12, -9], [55, -10, 14, -6]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3380_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3380_2_a_q();".
// 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_3380_2_a_q(:prec:=4)
    chi := MakeCharacter_3380_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_3380_2_a_q();".
// 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_3380_2_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3380_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 10, -6, -2, 1]>,<7,R![-3, 6, 6, -6, 1]>,<19,R![33, 0, -30, 0, 1]>],Snew);
    return Vf;
end function;