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