
// Make newform 1638.2.a.b in Magma, downloaded from the LMFDB on 19 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_b();".
// 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_b();".
// 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], [-3], [1], [-3], [1], [3], [5], [3], [3], [-10], [-7], [-6], [5], [0], [-6], [-6], [-7], [-4], [6], [-1], [-10], [-6], [6], [2], [-6], [-13], [-18], [-1], [0], [8], [-15], [9], [14], [12], [-13], [5], [2], [-21], [-18], [-12], [-22], [15], [-22], [-6], [5], [23], [8], [-18], [-22], [24], [12], [-22], [9], [-6], [24], [18], [2], [-28], [30], [-16], [-30], [-4], [0], [14], [-18], [14], [-25], [12], [-16], [30], [30], [32], [14], [2], [-9], [-6], [8], [-18], [5], [3], [26], [18], [14], [35], [6], [21], [20], [-33], [-25], [-3], [-15], [32], [36], [-4], [-12], [-33], [-3], [-34], [-1], [20], [6], [-39], [-42], [44], [2], [6], [12], [9], [26], [-1], [-49], [-33], [17], [-13], [-42], [-25], [12], [-15], [0], [-4], [-13], [-42], [-21], [32], [30], [26], [0], [-25], [-28], [-34], [0], [44], [8], [0], [53], [39], [17], [-48], [18], [-7], [-36], [-34], [21], [-55], [-48], [8], [54], [14], [-12], [2], [3], [17], [-42], [44], [-27], [-34], [18], [2], [42], [-27], [-36], [41], [48], [-57], [21], [-34], [-58], [20], [-42], [57], [50], [48], [50], [-31], [54], [38], [-18], [-16], [23], [-7], [0], [26], [-18], [12], [-18], [38], [-7], [-4], [-21], [20], [-27], [20], [-15], [-30], [-9], [-4], [-46], [-12], [-12], [-30], [-37], [-22], [-52], [-6], [-42], [32], [-30], [-45], [2], [-43], [12], [-16], [-6], [-9], [-55], [68], [51], [36], [30], [-4], [38], [21], [56], [63], [-52], [27], [-48], [35], [15], [74], [38], [41], [48], [-10], [-33], [41], [72], [-12], [42], [-24], [-22], [-1], [38], [12], [-6], [-16], [60], [68], [-39], [-43], [30], [12], [-46], [6], [-33], [56], [44], [-12], [47], [-70], [36], [-4], [-7], [36], [14], [-6], [-54], [-4], [-18], [50], [-4], [35], [14], [14], [-46], [63], [-19], [-40], [84], [24], [-67], [0], [44], [-7], [-24], [-61], [-36], [-10], [-51], [45], [-9], [-3], [-6], [-76], [-42], [-64], [18], [63], [-1], [50], [12], [-52], [-30], [-13], [50], [-78], [35], [24], [-58], [84], [78], [12], [53], [54], [-4], [-12], [48], [-7], [-66], [-46], [68], [54], [71], [-27], [38], [-10], [89], [3], [-87], [17], [-27], [8], [0], [-64], [30], [5], [60], [50], [-49], [-10], [-78], [-51], [32], [66], [60], [-13], [-10], [-72], [60], [8], [26], [42], [80], [14], [15], [-75], [-84], [18], [-30], [59], [-33], [-45], [-24], [20], [-10], [-93], [-43], [-55], [12], [2], [-78], [-57], [53], [38], [-18], [-3], [-31], [-69], [8], [66], [63], [17], [-30], [-76], [15], [89], [53], [-70], [-42], [8], [-54], [-60], [-10], [60], [-10], [-10], [15], [41], [60], [14], [-66], [23], [-60], [18], [-49], [-82], [-27], [-31], [54], [62], [-45], [-54], [-13], [77], [-36], [90], [17], [-96], [78], [33], [62], [-48], [84], [-61], [-6], [-60], [-42], [-43], [0]];
    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_b();".
// 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_b(: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_b();".
// 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_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1638_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 1]>,<11,R![3, 1]>,<17,R![-3, 1]>,<19,R![-5, 1]>],Snew);
    return Vf;
end function;