
// Make newform 1155.2.a.n in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1155_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_1155_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_1155_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_1155_a();"
function MakeCharacter_1155_a()
    N := 1155;
    order := 1;
    char_gens := [386, 232, 661, 211];
    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_1155_a_Hecke(Kf)
    return MakeCharacter_1155_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 := [[2], [-1], [-1], [-1], [-1], [-2], [-3], [-5], [3], [-3], [0], [-6], [-4], [7], [-4], [9], [-11], [-1], [-2], [-8], [4], [10], [11], [-7], [1], [-6], [-3], [-4], [-4], [13], [19], [10], [-6], [16], [-14], [18], [-3], [-14], [8], [-6], [0], [-14], [-12], [-18], [4], [-4], [10], [11], [-21], [-16], [18], [-21], [-2], [-4], [-6], [-24], [-15], [29], [22], [-6], [-14], [9], [-26], [4], [19], [-22], [-11], [31], [-36], [7], [-30], [-9], [13], [-25], [5], [6], [-34], [10], [-28], [-2], [23], [1], [-24], [2], [27], [12], [-8], [-37], [-30], [22], [20], [-30], [-2], [-3], [23], [17], [19], [-25], [20], [-4], [1], [-42], [28], [9], [10], [42], [-42], [42], [6], [-19], [-14], [-10], [18], [10], [-35], [10], [9], [12], [3], [41], [16], [29], [-15], [-4], [-50], [-1], [-35], [-15], [-37], [46], [-2], [26], [9], [-22], [22], [-45], [14], [24], [38], [-6], [44], [-53], [2], [48], [-38], [-15], [-44], [-2], [-28], [-21], [-19], [-15], [8], [-33], [58], [-24], [10], [50], [8], [38], [27], [-34], [-5], [29], [45], [54], [47], [-46], [10], [-26], [-37], [28], [49], [1], [-38], [26], [58], [-16], [54], [34], [-20], [-24], [-18], [3], [19], [21], [-7], [11], [32], [24], [26], [18], [-16], [-45], [66], [4], [52], [-54], [-3], [24], [37], [31], [43], [-14], [57], [-36], [12], [-62], [-23], [-32], [23], [63], [-16], [-62], [24], [10], [-23], [34], [-28], [-21], [48], [-8], [-24], [-34], [-27], [-42], [-16], [68], [-24], [23], [-58], [-28], [62], [17], [61], [-41], [6], [-16], [-66], [-60], [-76], [-65], [-8], [31], [45], [-18], [-16], [63], [-18], [48], [34], [56], [-52], [52], [-65], [-25], [53], [-52], [-32], [-2], [75], [0], [5], [-32], [-30], [-10], [24], [63], [-9], [14], [-26], [38], [-75], [0], [46], [16], [-81], [-32], [-36], [56], [3], [-12], [-4], [-73], [-78], [-66], [-20], [48], [54], [36], [-4], [-46], [-38], [-12], [50], [-72], [-72], [10], [16], [-52], [14], [10], [-6], [7], [25], [-16], [-17], [-2], [-36], [-33], [-24], [48], [11], [20], [-12], [-27], [8], [17], [26], [-35], [-80], [72], [-18], [-54], [36], [-76], [-20], [-69], [-16], [-46], [-20], [22], [-29], [36], [6], [16], [47], [50], [6], [40], [-92], [-17], [-28], [32], [11], [-25], [-70], [56], [0], [60], [-95], [92], [-54], [22], [80], [21], [-48], [6], [-50], [-75], [-15], [6], [72], [35], [31], [34], [25], [-51], [-40], [-36], [-16], [-44], [21], [-43], [-18], [60], [9], [16], [-22], [-34], [37], [55], [32], [68], [65], [-36], [23], [-9], [32], [68], [37], [6], [56], [-13], [18], [-99], [-96], [54], [49], [-19], [-31], [9], [-67], [82], [-74], [-57], [-36], [30], [-15], [-1], [-60], [-72], [36], [-18], [-90], [91], [33], [25], [22], [-76], [-60], [30], [-57], [-56], [-89], [-70], [-81], [7], [102], [14]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1155_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1155_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_1155_2_a_n(:prec:=1)
    chi := MakeCharacter_1155_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_1155_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_1155_2_a_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1155_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-2, 1]>,<13,R![2, 1]>,<17,R![3, 1]>],Snew);
    return Vf;
end function;