
// Make newform 1232.2.a.i in Magma, downloaded from the LMFDB on 04 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1232_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_1232_2_a_i();".
// 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_1232_2_a_i();".
// 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_1232_a();"
function MakeCharacter_1232_a()
    N := 1232;
    order := 1;
    char_gens := [463, 309, 353, 673];
    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_1232_a_Hecke(Kf)
    return MakeCharacter_1232_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], [-1], [0], [-2], [2], [7], [-10], [-7], [-9], [-2], [4], [-8], [2], [15], [-14], [-3], [-3], [10], [-10], [0], [-11], [7], [12], [-12], [18], [16], [-3], [10], [-2], [5], [-6], [-2], [-10], [-9], [-4], [-18], [-4], [1], [5], [17], [-6], [18], [16], [10], [-15], [4], [-7], [2], [16], [4], [-1], [-30], [6], [-18], [-20], [28], [-24], [16], [18], [-16], [24], [-19], [-3], [7], [2], [30], [26], [21], [12], [-35], [-4], [3], [-15], [-11], [-14], [18], [-18], [40], [-26], [12], [-29], [6], [-23], [31], [40], [-18], [-23], [-9], [-16], [-25], [-18], [20], [-4], [-15], [-45], [-20], [4], [-16], [10], [-4], [-6], [-20], [-21], [-28], [18], [-16], [-32], [-34], [16], [18], [21], [7], [-17], [-1], [-21], [-13], [-50], [-13], [-8], [42], [36], [-29], [16], [-5], [-27], [37], [-44], [38], [16], [13], [-50], [36], [-44], [-38], [-10], [-9], [-18], [18], [-30], [19], [24], [-37], [-3], [-26], [-36], [35], [28], [-22], [31], [20], [6], [-8], [32], [-4], [34], [-48], [42], [-19], [-44], [18], [-55], [-15], [-3], [48], [28], [48], [-11], [20], [-30], [30], [-44], [39], [-13], [22], [-45], [26], [10], [-52], [-8], [49], [28], [21], [54], [-52], [-20], [24], [30], [-49], [-42], [-11], [18], [14], [33], [16], [-61], [22], [14], [-22], [-12], [38], [34], [-63], [15], [55], [6], [20], [-28], [-60], [45], [-13], [12], [64], [45], [0], [-70], [-36], [-5], [60], [-58], [29], [-39], [-52], [26], [6], [-32], [-52], [-36], [-23], [-2], [56], [-68], [71], [66], [-25], [6], [-25], [3], [-41], [46], [52], [-71], [-30], [14], [56], [-20], [-6], [-26], [-16], [56], [-43], [54], [52], [-6], [38], [-26], [39], [-36], [-44], [48], [-30], [16], [-18], [52], [-5], [-65], [6], [0], [-41], [71], [14], [12], [-54], [-11], [43], [58], [50], [0], [-36], [-47], [-66], [18], [34], [-27], [42], [-26], [27], [-24], [-51], [68], [-4], [-28], [62], [-8], [27], [29], [-6], [-8], [32], [-36], [-78], [-60], [15], [23], [-81], [19], [80], [-52], [-62], [26], [-86], [-19], [-50], [-50], [-7], [14], [-26], [78], [-68], [5], [18], [29], [-22], [-7], [71], [29], [-54], [-48], [72], [54], [-22], [-70], [82], [7], [49], [70], [77], [44], [-51], [-57], [84], [-17], [-85], [24], [85], [35], [-40], [-31], [40], [55], [-30], [-18], [-36], [36], [3], [-74], [-72], [-51], [-10], [-18], [51], [48], [81], [53], [-24], [36], [76], [-35], [-20], [-36], [64], [80], [21], [92], [78], [-58], [-64], [-32], [10], [47], [-58], [-78], [-68], [9], [40], [-61], [34], [-67], [89], [37], [45], [-3], [59], [86], [-88], [82], [-28], [0], [66], [-43], [24], [42], [56], [22], [-46], [40], [-84], [-3], [72], [-24], [-20], [-88], [-90], [-21], [-64], [-31], [-54], [-34], [39], [8], [36], [-18], [54], [-3], [-13], [-48], [-11], [38]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1232_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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