
// Make newform 1638.2.a.n in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1638_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_1638_2_a_n();".
// 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_1638_2_a_n();".
// 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_1638_a();"
function MakeCharacter_1638_a()
    N := 1638;
    order := 1;
    char_gens := [911, 703, 379];
    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_1638_a_Hecke(Kf)
    return MakeCharacter_1638_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 := [[1], [0], [-1], [-1], [-5], [1], [-1], [-1], [-5], [-1], [-6], [7], [-2], [1], [0], [-6], [-6], [-7], [-8], [-10], [9], [2], [-2], [6], [14], [6], [7], [6], [-7], [-16], [4], [9], [-13], [-2], [-24], [-7], [13], [-6], [-3], [-2], [0], [10], [15], [14], [-6], [25], [-5], [-12], [-10], [22], [4], [-12], [-10], [25], [18], [8], [30], [-30], [-8], [2], [-4], [-26], [12], [-12], [-30], [10], [-18], [-9], [20], [-4], [-10], [30], [-24], [34], [-2], [9], [10], [-4], [10], [-13], [19], [22], [-26], [30], [-41], [6], [-33], [12], [29], [21], [29], [-33], [-40], [-16], [-8], [28], [21], [-15], [-42], [25], [-28], [14], [-23], [22], [-28], [-42], [-42], [40], [-15], [2], [-37], [-7], [-43], [-37], [25], [-14], [5], [-24], [-3], [0], [-20], [-21], [2], [5], [-8], [6], [6], [-44], [3], [12], [46], [-28], [28], [-32], [-28], [-29], [-11], [-37], [-12], [6], [-37], [-4], [-2], [-13], [33], [0], [16], [-18], [-14], [-24], [-10], [-1], [29], [10], [-12], [45], [-10], [-6], [6], [46], [-61], [-56], [-21], [48], [-51], [43], [-6], [-26], [40], [-50], [-41], [42], [-24], [-18], [45], [6], [-6], [42], [-16], [23], [-37], [32], [14], [22], [48], [-18], [-6], [-5], [8], [61], [-32], [19], [36], [-21], [10], [-21], [-28], [54], [52], [60], [-42], [-25], [54], [-44], [46], [-14], [-20], [18], [9], [-46], [-11], [-16], [-32], [-46], [1], [-33], [16], [39], [-36], [50], [52], [2], [15], [8], [-33], [-4], [-9], [-16], [-1], [17], [-14], [10], [11], [56], [58], [9], [-25], [-52], [-20], [-38], [-8], [-2], [59], [74], [-20], [-6], [28], [24], [-16], [49], [-37], [54], [76], [14], [74], [-31], [36], [76], [4], [9], [-2], [48], [-44], [-47], [-32], [34], [18], [54], [60], [46], [-46], [56], [-35], [-30], [50], [34], [-39], [59], [0], [44], [-4], [-65], [-36], [52], [-53], [-64], [11], [12], [10], [57], [17], [39], [39], [-70], [68], [-14], [48], [-78], [-1], [-35], [58], [-60], [48], [30], [-1], [-42], [10], [59], [-24], [-14], [-24], [-78], [68], [-23], [-42], [-24], [16], [44], [-73], [90], [74], [-40], [-90], [61], [-49], [-38], [26], [-53], [51], [45], [-81], [-15], [-8], [-4], [40], [-86], [-29], [-28], [38], [-45], [10], [-62], [79], [44], [-18], [84], [-45], [-30], [56], [-76], [-64], [-86], [90], [56], [6], [-85], [-45], [12], [2], [54], [-59], [51], [-53], [72], [8], [6], [25], [23], [-95], [-12], [46], [-54], [19], [25], [-18], [50], [-27], [31], [-57], [-52], [-66], [45], [43], [30], [76], [1], [-53], [77], [-74], [-34], [-8], [-50], [4], [-18], [96], [-74], [62], [-85], [85], [12], [-14], [-90], [-67], [24], [30], [-45], [-78], [55], [67], [-26], [-26], [95], [42], [7], [37], [12], [-46], [53], [-24], [-86], [-75], [-70], [36], [-36], [-19], [-18], [36], [-6], [-17], [-68]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1638_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1638_2_a_n();".
// 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_1638_2_a_n(:prec:=1)
    chi := MakeCharacter_1638_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_1638_2_a_n();".
// 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_1638_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1638_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![1, 1]>,<11,R![5, 1]>,<17,R![1, 1]>,<19,R![1, 1]>],Snew);
    return Vf;
end function;