
// Make newform 1944.2.a.p in Magma, downloaded from the LMFDB on 02 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1944_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_1944_2_a_p();".
// 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_1944_2_a_p();".
// 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 := [-1, -3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_1944_a();"
function MakeCharacter_1944_a()
    N := 1944;
    order := 1;
    char_gens := [487, 973, 1217];
    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_1944_a_Hecke(Kf)
    return MakeCharacter_1944_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]];
    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, 0, 0], [0, 1, 0], [-1, 1, 0], [1, -2, 1], [-1, -3, 1], [-3, 0, -2], [-3, 2, -3], [2, -2, -1], [-2, 1, -1], [-2, -3, 2], [-5, 1, -4], [-1, -3, 5], [0, 0, 5], [2, 1, 0], [2, -2, 5], [1, 5, -4], [-6, 1, 3], [-8, 1, -3], [5, 7, -5], [-2, 6, -3], [-4, 3, 4], [6, -1, 9], [-6, -7, 2], [-3, 8, 0], [0, 10, -11], [-1, -8, 2], [-1, 12, -1], [-6, 5, -8], [-11, 5, 0], [-1, -3, -5], [4, -5, -6], [-5, 3, 4], [1, -7, 9], [3, 2, 9], [-8, -6, -1], [1, 5, 2], [3, -1, 2], [15, 1, 1], [7, -13, -3], [1, 4, -10], [-1, -6, 5], [1, -9, 13], [-6, 5, -12], [-4, -16, 10], [1, 14, -4], [-3, -7, -2], [11, 3, -2], [9, -6, 3], [6, 2, 4], [-9, 7, -15], [-8, 2, -14], [-2, 6, -11], [-7, -1, 2], [-17, -9, 8], [13, 9, -3], [-5, 2, 7], [-3, -15, 16], [7, -11, 7], [-14, 4, 0], [15, 13, -7], [4, 6, -5], [5, -10, 7], [6, 0, 6], [9, -6, -3], [-1, 0, -10], [1, -4, -14], [8, -4, 10], [11, -1, -2], [0, 0, -12], [-11, 10, 9], [3, -3, 3], [11, -2, -7], [11, 3, -3], [5, -8, 7], [-15, 6, -9], [6, 1, -9], [8, -1, 14], [-12, -10, 2], [16, 1, -1], [-2, -18, 16], [-4, 19, -20], [-21, 7, 5], [25, 1, -2], [-6, 14, -16], [-20, -3, -4], [-8, 11, -13], [1, -14, -9], [31, -2, 4], [5, 5, -7], [5, -9, -7], [-6, -1, 3], [2, -5, 23], [-4, 12, -10], [-4, 21, -13], [-20, -4, -9], [18, 11, -13], [-13, 6, -15], [2, 14, 3], [-3, -7, 2], [6, 1, -13], [19, -1, 8], [2, 5, 15], [-2, -18, 15], [-11, 8, 15], [-18, -13, 0], [7, -6, 0], [-20, -5, -8], [-5, 1, 14], [5, 2, 3], [-10, -8, -8], [-5, 21, 5], [-11, -15, -2], [-17, 17, -13], [-14, 1, -11], [-23, 6, 6], [-3, -12, -15], [7, -11, -6], [39, -7, -1], [21, 23, -10], [-14, 6, 8], [8, -9, 13], [5, -5, -4], [-25, 1, -1], [-1, 1, 7], [25, -8, 2], [-21, 10, 5], [3, -16, 16], [-18, 8, 2], [7, 2, -14], [5, -17, 13], [-13, 10, -4], [-19, -4, 7], [15, 3, -2], [-7, -20, 7], [2, -24, 21], [10, 19, 5], [-12, -18, 23], [8, 22, -25], [4, 31, -21], [-5, 2, -19], [31, 10, -1], [5, -21, -6], [-9, -18, 25], [0, 0, -10], [4, 6, -11], [-7, 11, 5], [-3, -6, 21], [-17, -10, -12], [3, -3, 8], [10, -21, 19], [33, -8, 8], [-11, 20, -14], [-21, -7, 21], [-32, -20, 16], [-2, -23, 1], [-23, 2, 8], [-7, 11, 14], [-12, -11, -2], [14, -2, 15], [38, 17, -11], [3, 22, -26], [-9, -11, 26], [-20, -14, 5], [2, -15, -10], [6, 26, -16], [-34, -8, -4], [-11, 3, 0], [6, 5, -3], [26, -2, -16], [-11, -28, 18], [-21, 0, 15], [7, -32, 12], [-11, 0, 24], [-25, -10, 2], [-20, 2, -11], [-6, 31, -16], [10, -1, -9], [16, 40, -26], [-5, -19, 16], [-19, 24, 3], [-25, -1, 2], [10, -11, -1], [5, -13, 25], [3, 8, 20], [10, -21, -17], [-12, 11, 10], [-9, 9, 21], [-1, -13, 0], [-34, 15, 14], [15, 23, -2], [-4, -7, -2], [-20, -29, 16], [33, -13, 7], [23, -14, -12], [-11, -20, 3], [-12, 3, 2], [-1, -11, 17], [-27, -8, -13], [5, 22, -30], [-24, 14, -6], [4, 25, -27], [-34, -3, -12], [20, -9, -5], [-20, -6, 7], [6, 20, -7], [-14, 9, 2], [22, 13, -7], [-24, -12, 25], [-5, -34, 20], [-5, -20, 5], [22, -11, 5], [21, -17, 27], [18, 0, 19], [45, -11, 8], [-40, 0, -15], [8, 24, -27], [18, 12, 2], [34, -9, -5], [-32, -10, 9], [9, 0, 13], [12, 7, 16], [-31, -5, -3], [39, 10, -13], [-1, -23, 21], [15, 26, -18], [21, 25, -20], [22, 15, 10], [-4, -10, -17], [-36, -21, 17], [25, 13, -22], [52, -12, 0], [20, -21, 27], [25, -9, 33], [17, 11, -23], [-17, -10, -8], [-22, 19, -5], [-3, 4, 3], [31, -6, -1], [43, 13, 1], [29, 4, 3], [-11, -4, 34], [-8, 7, -41], [-24, -9, 14], [-24, 24, -6], [13, 18, -4], [-15, -12, 14], [-5, -22, 0], [9, 13, -18], [26, -11, 37], [9, 18, 3], [30, -3, -9], [40, -15, -1], [31, 21, -12], [-48, 4, -8], [11, 24, -37], [-4, -18, 39], [19, 27, -12], [-17, 1, 17], [-3, 8, 1], [8, -19, -9], [50, -3, -1], [16, 10, -2], [36, 14, -25], [-11, -1, 17], [4, -34, -11], [-44, 2, -8], [22, 10, -27], [49, 29, -17], [-14, -44, 12], [-26, 24, 3], [-28, 23, 17], [58, -7, 23], [15, 20, -17], [23, 12, 1], [17, 1, 25], [18, -14, -8], [6, -30, -11], [-6, -32, 12], [-15, 14, 1], [13, 22, -32], [-22, 29, -3], [14, 33, -11], [-5, -12, -13], [7, -11, 12], [-54, 9, 5], [-13, 25, 25], [-39, -27, 9], [8, 30, -45], [-38, 2, -11], [-19, 19, -13], [31, -7, -24], [-27, 7, 15], [19, 19, -22], [31, -10, 18], [3, 9, 14], [-19, 30, -9], [-23, -13, 12], [-10, 33, 2], [44, 6, -6], [18, -24, -23], [12, 9, 4], [7, 17, 13], [36, -28, 19], [-43, 21, -16], [30, 1, -25], [-23, -15, 4], [15, 18, -11], [-3, -25, 45], [14, 25, -5], [-29, -44, 32], [-5, -34, -3], [9, 7, -13], [25, -16, -13], [47, 31, -28], [33, -4, -27], [-9, 27, 18], [9, 4, -41], [-8, 26, -26], [32, -23, -18], [2, 38, -39], [-38, 4, 16], [-22, -16, -10], [30, -4, 1], [1, 29, -37], [-50, -8, 20], [-32, 21, 13], [5, -4, 49], [5, 15, 4], [-56, 14, -15], [8, 9, -35], [-29, -25, 6], [-22, -12, 14], [38, 8, 4], [-40, -39, 26], [36, -3, 18], [-4, -39, 25], [-4, 51, -22], [38, -26, 34], [-21, -19, 8], [-35, -48, 36], [-10, 13, -3], [-19, -36, 35], [-10, -7, -26], [9, -17, 12], [49, -9, -9], [4, -22, -15], [11, 2, -24], [-56, 29, 15], [-20, 15, -1], [0, 25, -11], [-3, -8, 38], [-36, -15, 14], [15, 2, 50], [-21, 23, -18], [21, 1, 11], [-26, 14, -58], [6, 15, 25], [11, 29, -18], [-23, 11, -54], [12, 20, 18], [5, -17, -15], [-42, -12, -15], [-27, -27, 7], [-12, -9, 8], [-6, -59, 19], [-25, 6, -7], [-76, 2, -9], [-61, 7, -28], [59, 13, -5], [-16, 24, -22], [-35, 17, -23], [-59, -17, 13], [1, -40, 26], [-35, 12, -7], [19, -36, 40], [13, 5, 48], [22, -10, -22], [21, 7, 28], [5, 17, 17], [-6, -1, 32], [-11, 14, 7], [21, 31, -23], [-35, -14, -19], [3, -41, 57], [52, -6, -17], [-30, -42, 15], [23, -24, 57], [61, -7, 8], [-9, -31, 29], [-33, -9, 8], [4, 14, -37], [-20, 26, 6], [-32, -31, 24], [-47, 13, -34], [-24, 35, -56], [21, 38, -37], [45, -14, 7], [-47, -37, 16], [27, -36, 26], [9, -37, 6], [26, -15, 41], [-21, -32, 9], [23, 33, -48], [-58, 10, -4], [5, -19, -6], [-68, -13, 14], [5, 36, -34], [31, -21, 17], [13, 24, -5], [-64, 9, 12], [-46, -27, 31], [-34, -31, 2], [-48, 7, -3], [-5, -43, 18], [-7, 10, -13], [-8, -16, -12], [-31, -18, 6], [1, -33, 25], [14, -21, 39], [-12, 4, 32], [-65, 10, -6], [15, -38, -16], [19, 2, -23], [-12, 16, -42], [14, 1, -5], [-15, 20, -23], [32, 54, -31], [-46, 17, -41], [13, 1, -20]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1944_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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