
// Make newform 1632.2.a.a in Magma, downloaded from the LMFDB on 25 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1632_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_1632_2_a_a();".
// 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_1632_2_a_a();".
// 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_1632_a();"
function MakeCharacter_1632_a()
    N := 1632;
    order := 1;
    char_gens := [511, 613, 545, 1057];
    v := [1, 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_1632_a_Hecke(Kf)
    return MakeCharacter_1632_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], [2], [-3], [3], [-1], [5], [-3], [-2], [10], [-12], [-5], [-9], [8], [4], [-6], [-10], [4], [0], [-12], [2], [0], [-18], [-10], [-4], [-5], [-7], [4], [9], [-15], [-9], [16], [-16], [4], [-4], [-17], [2], [-3], [9], [10], [20], [20], [-26], [-3], [-2], [10], [29], [11], [26], [-17], [-26], [2], [-20], [-10], [4], [3], [-7], [12], [22], [6], [-32], [0], [0], [-6], [6], [1], [-20], [28], [-7], [2], [4], [20], [22], [-18], [-38], [-12], [-2], [3], [9], [-20], [27], [-12], [-1], [16], [20], [18], [-11], [-6], [-24], [28], [17], [18], [-40], [32], [-41], [0], [35], [12], [-20], [22], [14], [-42], [34], [18], [41], [-28], [12], [20], [-42], [-40], [-17], [-10], [26], [1], [-23], [-46], [-40], [-51], [36], [25], [-20], [-45], [39], [-24], [0], [40], [27], [16], [-34], [1], [8], [-42], [13], [-18], [25], [-42], [10], [4], [27], [-16], [-3], [-32], [51], [-18], [-21], [-10], [-54], [20], [18], [32], [6], [-11], [31], [-60], [45], [-11], [-51], [42], [50], [-40], [-44], [-45], [6], [46], [51], [-18], [22], [22], [-46], [16], [-55], [33], [23], [41], [33], [10], [35], [-25], [-9], [11], [60], [8], [12], [-18], [58], [-8], [47], [24], [-32], [-50], [-48], [-43], [-2], [27], [14], [-34], [52], [-18], [-42], [-25], [46], [-35], [49], [14], [-16], [-48], [36], [-9], [52], [-56], [28], [66], [6], [-9], [4], [23], [-10], [3], [-30], [13], [12], [72], [36], [52], [6], [5], [17], [7], [32], [39], [46], [64], [-18], [12], [22], [-34], [-9], [-15], [34], [-23], [-32], [-39], [3], [41], [1], [44], [-65], [51], [46], [1], [70], [-18], [68], [-32], [-30], [4], [-46], [-63], [-18], [14], [48], [52], [60], [3], [13], [28], [-14], [14], [50], [-20], [-29], [-65], [56], [-58], [5], [-30], [-75], [-59], [-66], [20], [4], [72], [14], [0], [54], [-14], [10], [17], [-60], [-31], [-47], [52], [-33], [-44], [25], [-28], [6], [-75], [-44], [17], [-18], [84], [1], [-18], [62], [62], [20], [-10], [6], [-64], [-42], [33], [61], [-24], [43], [-54], [-5], [-68], [0], [22], [-8], [-8], [-43], [-6], [-5], [-90], [84], [20], [-81], [-58], [15], [-16], [-66], [16], [33], [-71], [-47], [8], [-21], [41], [-16], [81], [-23], [-32], [-83], [36], [-3], [-63], [62], [-89], [16], [-36], [80], [7], [-30], [48], [12], [-7], [-70], [10], [3], [-24], [-28], [19], [13], [-33], [-80], [-26], [48], [70], [-45], [70], [56], [52], [15], [8], [-93], [-40], [-11], [37], [6], [-22], [-19], [-44], [-63], [35], [-50], [34], [12], [-76], [27], [72], [13], [40], [4], [48], [-96], [-20], [-36], [-54], [-76], [59], [-30], [-24], [40], [-62], [-92], [-49], [-55], [-8], [42], [-82], [32], [-11], [-78], [-79], [88], [-51], [-28], [86], [14], [59], [8], [-64], [14], [16], [91], [-89], [-73]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1632_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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