
// Make newform 2704.2.a.f in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2704_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_2704_2_a_f();".
// 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_2704_2_a_f();".
// 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_2704_a();"
function MakeCharacter_2704_a()
    N := 2704;
    order := 1;
    char_gens := [2367, 677, 1185];
    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_2704_a_Hecke(Kf)
    return MakeCharacter_2704_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], [3], [-1], [6], [0], [-3], [2], [0], [6], [-4], [7], [0], [1], [3], [0], [-6], [8], [14], [-3], [-2], [-8], [12], [6], [10], [-12], [4], [-12], [7], [-6], [-20], [21], [0], [13], [6], [17], [14], [-16], [0], [0], [-3], [20], [18], [4], [-3], [-2], [13], [-19], [0], [13], [-27], [15], [10], [-24], [9], [12], [24], [11], [-28], [6], [4], [-21], [2], [30], [-1], [6], [8], [23], [-3], [19], [-24], [0], [-26], [-4], [20], [21], [-6], [34], [-36], [-32], [-9], [-17], [-33], [-25], [-26], [-21], [-6], [10], [-9], [-40], [-36], [-21], [-16], [9], [-40], [30], [18], [-9], [-20], [-11], [-17], [-3], [-39], [15], [-5], [-38], [24], [-18], [-6], [-19], [-14], [-38], [24], [-28], [29], [-18], [14], [6], [0], [36], [22], [-19], [48], [24], [8], [0], [-26], [-6], [10], [-23], [20], [-9], [40], [-16], [6], [-32], [39], [-40], [-42], [-33], [20], [3], [-14], [18], [38], [0], [37], [-42], [4], [-45], [13], [21], [-29], [0], [37], [-30], [16], [-36], [-34], [21], [6], [15], [-31], [3], [54], [39], [-2], [-46], [-38], [6], [30], [-23], [24], [-8], [-14], [-33], [44], [54], [-20], [8], [8], [-12], [-22], [18], [-36], [54], [38], [-10], [4], [24], [29], [36], [-65], [30], [3], [-54], [40], [44], [-48], [54], [-21], [-8], [13], [17], [-24], [-6], [-7], [-12], [-42], [31], [38], [6], [64], [-18], [57], [28], [4], [-27], [12], [21], [-4], [41], [-60], [-7], [-9], [-46], [15], [12], [-62], [30], [-58], [52], [32], [51], [-53], [-33], [22], [33], [-48], [-30], [-66], [25], [-32], [61], [42], [-12], [-43], [-36], [-22], [-66], [-41], [6], [-3], [-1], [48], [-30], [-16], [-46], [-36], [34], [-44], [21], [49], [2], [24], [-35], [39], [24], [-4], [12], [68], [2], [-26], [-32], [14], [-16], [-6], [-62], [-20], [63], [-36], [-67], [42], [-26], [32], [-18], [65], [57], [71], [42], [-12], [-15], [0], [-42], [26], [60], [46], [-12], [15], [-82], [23], [-9], [-38], [-60], [-41], [-20], [-39], [-76], [-36], [-52], [-36], [33], [-33], [76], [33], [-43], [-54], [-48], [64], [-18], [28], [-32], [60], [-43], [-48], [74], [38], [8], [84], [48], [-62], [-48], [-80], [36], [-16], [72], [34], [-36], [-68], [70], [-53], [-45], [-69], [-26], [81], [45], [38], [44], [33], [42], [-22], [-38], [-45], [64], [86], [-54], [-51], [-66], [51], [-15], [-26], [78], [-30], [-48], [52], [17], [81], [-16], [65], [63], [7], [15], [30], [-32], [2], [42], [-42], [88], [-30], [-10], [-30], [-30], [-52], [6], [68], [3], [-37], [68], [38], [-48], [10], [-87], [18], [-35], [63], [-31], [29], [21], [76], [63], [-41], [-6], [77], [84], [57], [70], [37], [0], [-58], [24], [17], [-66], [-69], [73], [-31], [0], [-39], [-80], [-66], [-30], [-48], [-74], [96], [-105], [16], [78], [-3], [72], [56], [-90]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2704_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2704_2_a_f();".
// 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_2704_2_a_f(:prec:=1)
    chi := MakeCharacter_2704_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_2704_2_a_f();".
// 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_2704_2_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2704_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![-3, 1]>,<7,R![1, 1]>],Snew);
    return Vf;
end function;