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