
// Make newform 1656.2.a.j in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1656_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_1656_2_a_j();".
// 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_1656_2_a_j();".
// 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
        poly := [-4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_1656_a();"
function MakeCharacter_1656_a()
    N := 1656;
    order := 1;
    char_gens := [415, 829, 1289, 649];
    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_1656_a_Hecke(Kf)
    return MakeCharacter_1656_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, 0], [0, 0], [-2, 0], [0, 0], [-2, 2], [3, -1], [0, -2], [2, -2], [1, 0], [-1, -1], [-5, 1], [-2, 4], [-3, 5], [-8, 0], [-7, 3], [-2, 0], [0, -4], [6, -4], [-2, 2], [-11, -1], [-7, -3], [-2, 2], [-4, -4], [4, -6], [-4, 6], [-2, -4], [-4, -4], [-6, -2], [8, 2], [8, -2], [-7, 3], [-11, 3], [-12, 2], [-11, 3], [10, 4], [-13, 1], [16, 2], [1, 7], [16, 0], [10, 0], [11, -3], [14, -4], [-10, -6], [-3, 1], [-1, 7], [6, 2], [-4, 0], [-8, -8], [-4, 4], [20, -2], [-7, 9], [-3, -9], [-14, 8], [-4, 4], [13, -3], [14, 2], [-21, 3], [24, 0], [5, 5], [0, -2], [-2, 2], [0, 6], [-8, -4], [-3, -1], [0, -6], [14, -4], [-7, 15], [4, -10], [-4, 8], [-27, -3], [-13, -1], [-2, 10], [-4, -4], [10, -8], [8, -8], [-20, 4], [8, 6], [19, -1], [18, 4], [-7, 13], [0, -8], [-24, 2], [16, -8], [6, -12], [-3, -9], [17, 7], [2, -4], [4, 6], [3, -5], [0, -8], [6, -6], [-4, 4], [17, 3], [-5, 5], [-9, 9], [2, 6], [-5, -13], [-4, 10], [12, 4], [25, -7], [17, -17], [4, 2], [-12, -4], [2, -4], [-24, -8], [5, -7], [9, -9], [26, -12], [-8, 0], [-31, 5], [4, 12], [0, 18], [-10, 16], [16, -8], [12, 12], [4, -6], [-14, 6], [9, -13], [-11, 9], [4, 4], [6, 4], [-17, -9], [14, -16], [-21, -3], [-4, -16], [-8, 14], [-28, 6], [8, 8], [-30, 6], [40, -6], [5, 3], [-28, 4], [16, -16], [-14, -8], [23, 3], [-6, 0], [-12, -6], [-20, 4], [-6, 4], [-26, 0], [23, -7], [6, -12], [51, 1], [8, 8], [-14, 4], [-44, 4], [-10, 16], [-15, 1], [31, 9], [-9, -11], [6, -16], [-8, -2], [12, -16], [-15, 3], [46, 2], [34, -2], [44, 4], [-27, 5], [22, 4], [-36, 2], [1, -1], [-38, 4], [21, -9], [-22, 6], [26, -20], [6, 18], [-16, -16], [14, 0], [10, 16], [5, 1], [30, -6], [-11, 13], [4, -12], [-16, 2], [-35, 15], [-20, 2], [-11, -5], [-19, -7], [-10, -6], [-12, 14], [17, -13], [28, -4], [37, -11], [6, 16], [18, -18], [16, -2], [30, -16], [-58, 2], [-31, 5], [16, -24], [-9, 15], [-20, -16], [-10, -14], [-2, 20], [10, 22], [28, 2], [6, 12], [8, -6], [-40, 14], [-9, -19], [20, 2], [-48, 8], [17, 9], [-2, -12], [-60, 4], [15, -1], [-10, -6], [21, -13], [-27, -3], [-7, -9], [-1, 23], [9, 13], [0, 24], [18, -2], [16, -16], [56, 2], [-13, 9], [-9, -21], [-14, 30], [41, 5], [-25, 19], [22, 10], [-9, -5], [16, -32], [-3, 19], [-49, -5], [-10, 16], [-57, -3], [-42, 10], [28, -8], [2, 20], [-16, -16], [42, -2], [22, -16], [28, -20], [36, 4], [24, -22], [38, 0], [29, 3], [11, 17], [-40, 24], [-29, -11], [-13, 17], [-1, -29], [10, 20], [-11, 7], [-25, -3], [-46, 14], [30, -30], [-10, 2], [24, 2], [-18, 0], [-26, -6], [52, -2], [-18, 4], [51, 5], [6, 20], [50, -10], [-5, 11], [-11, 1], [-68, 4], [44, -4], [-54, 4], [32, -6], [42, 4], [10, 14], [-14, -4], [-60, -6], [-30, 14], [7, 15], [-3, 21], [10, -2], [48, -14], [-16, -16], [-19, 17], [7, -11], [-36, -16], [10, -12], [-10, 4], [-8, -24], [3, 25], [2, -18], [-30, -10], [38, -12], [-40, 4], [8, 24], [-6, 16], [-62, 4], [40, -8], [-46, 12], [-42, -16], [-16, -8], [14, 8], [-26, 10], [17, 1], [30, -24], [48, -8], [-11, -15], [7, -7], [-11, -21], [20, 22], [-42, -8], [-64, -8], [-17, 1], [12, 20], [11, -5], [20, -16], [-12, 6], [70, -6], [-46, 4], [21, -17], [-46, 12], [20, 26], [-31, -1], [22, -30], [-16, -14], [-53, -3], [-59, 7], [0, -6], [-35, 29], [-24, 24], [-12, 6], [-43, -7], [-40, -8], [-10, -24], [0, -14], [26, 14], [-39, -1], [2, 6], [-36, -14], [5, 29], [-23, 21], [45, -1], [-9, 25], [-14, 14], [-25, -7], [-74, 4], [-16, 6], [-29, 11], [-28, 12], [-57, 3], [0, 22], [-38, 16], [-22, 14], [-42, 24], [-36, -16], [35, -9], [36, 8], [-50, -14], [50, -20], [27, -27], [-63, 5], [-46, 8], [-2, -30], [36, -18], [54, 16], [-64, 0], [-14, 22], [47, -13], [44, 4], [34, 24], [15, -37], [-9, -3], [-30, 14], [25, 7], [-11, -15], [-15, -3], [10, 14], [74, 8], [-25, 17], [-12, 8], [21, -25], [-16, -2], [-32, -8], [43, -25], [-1, -15], [46, 2], [-42, -4], [52, -6], [59, 3], [18, -4], [10, 4], [20, -20], [-19, -3], [48, -8], [19, -15], [61, -1], [6, -8], [-6, -2], [4, -28], [64, -22], [-6, -8], [-19, 19], [-28, -8], [36, -20], [-6, 24], [-4, -12], [-32, -10], [-16, 8], [42, -32], [-45, -17], [31, 3], [-52, 28], [24, -2], [-79, 5], [-17, 5], [-26, -20], [47, 3], [-64, 16], [-28, 8], [11, 27], [-35, 25], [18, 22], [-30, 14], [18, -24], [-49, -1], [-40, 32], [-4, -4], [48, -18], [44, 4], [44, -6], [-56, 2], [32, 0], [12, 0], [-39, -3], [-7, 21], [74, -18], [-70, -4], [-49, 17], [1, 3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1656_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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