
// Make newform 3380.2.a.d in Magma, downloaded from the LMFDB on 23 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_d();".
// 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_d();".
// 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_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], [-1], [-1], [5], [-5], [0], [-1], [-3], [3], [-1], [0], [7], [-5], [5], [12], [2], [-11], [-13], [3], [13], [-2], [-4], [12], [7], [11], [-13], [0], [-9], [18], [-1], [7], [4], [3], [13], [11], [-24], [10], [5], [-13], [-17], [11], [10], [-15], [23], [27], [21], [-5], [19], [17], [10], [6], [-8], [11], [-15], [15], [11], [-9], [7], [-13], [30], [23], [7], [-12], [0], [6], [-18], [1], [14], [-27], [35], [-5], [-24], [3], [19], [-21], [-3], [-10], [-13], [27], [19], [-17], [10], [-21], [7], [-29], [-20], [-21], [11], [-33], [-8], [-8], [11], [17], [15], [-4], [-11], [3], [22], [-21], [-2], [-20], [-13], [9], [39], [4], [-42], [3], [-2], [-36], [-5], [-31], [-25], [27], [-4], [-27], [27], [5], [-9], [39], [17], [3], [11], [42], [49], [-5], [26], [23], [33], [-8], [-34], [15], [-9], [-25], [-9], [19], [19], [-37], [-11], [23], [-37], [-8], [-25], [-31], [12], [-25], [-43], [34], [-26], [-36], [24], [31], [-45], [20], [31], [-17], [-8], [49], [-45], [-18], [14], [31], [3], [16], [-7], [-45], [-16], [3], [7], [-26], [42], [20], [-37], [25], [31], [8], [43], [-17], [-30], [47], [-37], [52], [4], [-30], [38], [19], [39], [2], [-52], [7], [-67], [-41], [-19], [16], [-41], [-59], [-25], [-30], [39], [62], [40], [7], [-39], [27], [14], [15], [-21], [4], [45], [-13], [5], [59], [-42], [39], [-7], [25], [-58], [-24], [-57], [63], [34], [-17], [-24], [18], [-47], [27], [18], [-13], [-55], [13], [16], [31], [-41], [47], [-22], [36], [-8], [23], [51], [-11], [59], [-61], [-25], [-23], [47], [67], [-24], [13], [15], [5], [-69], [7], [39], [24], [15], [42], [21], [75], [-21], [-46], [-21], [-8], [-13], [2], [-61], [-53], [-71], [27], [6], [9], [-37], [-70], [28], [-9], [-35], [75], [-69], [9], [58], [47], [1], [-9], [67], [-12], [-77], [-28], [-72], [46], [-30], [-31], [3], [3], [-3], [-45], [5], [27], [18], [-48], [51], [-5], [47], [39], [-2], [23], [-4], [21], [35], [80], [-22], [-41], [26], [53], [27], [-10], [55], [61], [15], [49], [-56], [31], [19], [36], [38], [27], [-9], [-22], [-25], [-44], [-11], [-45], [-65], [-25], [82], [-17], [57], [55], [12], [-29], [-33], [-77], [80], [18], [-45], [6], [7], [-81], [44], [-14], [29], [83], [27], [-40], [7], [27], [81], [55], [-2], [5], [-51], [46], [-64], [-57], [55], [59], [51], [-77], [-53], [87], [-67], [-34], [45], [41], [32], [-62], [-49], [83], [-80], [49], [15], [39], [79], [-62], [-33], [-16], [-58], [45], [-45], [-11], [26], [-48], [57], [-1], [51], [52], [-77], [-27], [-29], [-5], [-6], [68], [-25], [59], [-41], [55], [-50], [51], [-43], [47], [91], [-28], [-77], [-10], [7], [33], [-91], [95], [-82], [21], [16], [7], [29], [-65], [98], [-97], [-52], [-13], [-65], [-92], [-42], [-79], [1]];
    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_d();".
// 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_d(:prec:=1)
    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_d();".
// 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_d( : 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, 1]>,<7,R![-5, 1]>,<19,R![3, 1]>],Snew);
    return Vf;
end function;