
// Make newform 1071.2.a.g in Magma, downloaded from the LMFDB on 15 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1071_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_1071_2_a_g();".
// 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_1071_2_a_g();".
// 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, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, -1, 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_1071_a();"
function MakeCharacter_1071_a()
    N := 1071;
    order := 1;
    char_gens := [596, 766, 190];
    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_1071_a_Hecke(Kf)
    return MakeCharacter_1071_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 := [[-1, 1, 0], [0, 0, 0], [-2, 1, 0], [1, 0, 0], [-1, -2, 0], [-1, 0, -1], [1, 0, 0], [0, -1, -2], [2, -3, 1], [-1, 1, 3], [-2, 4, 1], [1, -5, -4], [-7, 0, -3], [1, 4, 2], [-6, -2, 1], [-1, -1, 4], [-6, -2, -1], [6, -2, 3], [1, 1, 5], [-3, 5, -1], [-2, 6, 6], [-7, -1, -4], [-2, 4, 4], [-10, -2, -4], [-4, -6, 0], [-4, -2, -1], [-1, 2, -1], [0, 3, -7], [-1, 5, -6], [-5, 0, -6], [-8, 9, 3], [-8, 11, 6], [-1, -1, 4], [8, 4, 4], [2, 4, 2], [7, -1, 5], [-8, 7, -2], [-6, -2, 2], [-3, -4, -13], [-4, -5, 4], [8, -2, 0], [6, 0, 10], [6, 0, -8], [-3, 3, 0], [9, 6, 0], [-8, 4, 3], [-5, 11, -2], [-11, -6, -9], [9, -10, -1], [1, -7, -7], [0, 11, 3], [7, -9, 2], [4, 0, -4], [14, -2, 1], [-14, -2, -1], [-2, -10, -4], [1, -12, -1], [-22, -3, 2], [14, -14, -4], [-8, 2, -12], [0, -4, 1], [0, 6, 12], [0, -4, -4], [-12, 8, 6], [14, -6, -3], [10, 0, 0], [-14, 3, 3], [5, -9, 6], [5, 9, 1], [26, -5, -4], [-8, 6, 7], [19, 5, 0], [12, -8, 0], [-15, -9, 3], [10, 0, 4], [-2, -12, 5], [-28, 8, 2], [0, 4, 5], [-11, -8, -12], [-15, -4, 3], [3, -7, 11], [-2, -1, -5], [14, -6, -4], [-18, 5, 2], [0, -4, 5], [-11, -1, -14], [-11, -5, 3], [-2, 9, -15], [-10, -8, -19], [-15, 5, 1], [4, 6, -8], [-15, 4, 3], [16, -16, 0], [2, 6, -6], [19, 1, -2], [4, 7, 16], [-16, 6, 12], [3, 2, 19], [-16, 8, -8], [-4, 0, -6], [1, 7, -14], [1, 1, -6], [0, -14, -20], [-20, -6, -4], [10, 6, -2], [-33, -2, -1], [4, 4, 3], [-10, 10, 7], [10, 8, 4], [-10, 18, -1], [-4, -8, -9], [-11, 2, 12], [17, -9, 3], [4, -6, -17], [-9, 0, -10], [-20, -3, 11], [-18, 2, -6], [6, -2, -10], [4, 1, 13], [15, -23, -6], [-8, 9, -4], [6, -14, 2], [1, -4, 3], [27, 12, -6], [0, -4, 23], [-8, 8, -2], [-6, 16, 8], [16, 3, 2], [8, 4, 26], [24, -18, -10], [-1, -20, 0], [25, 1, -5], [23, 1, 4], [2, -15, -19], [-24, 2, 2], [-15, -4, 15], [14, 2, 7], [2, -8, -15], [10, 20, -10], [-15, 12, 18], [-30, 18, 12], [-24, 25, 7], [17, 1, -6], [19, 4, -10], [-10, -16, -4], [-1, 8, -1], [-2, -12, -7], [2, 8, -12], [8, 4, 0], [29, -15, 2], [-33, 17, 2], [-14, -8, -2], [-2, -9, 17], [17, -2, -13], [12, -12, -18], [-1, 10, -8], [13, -10, -2], [-13, 16, -3], [5, 21, 9], [24, -26, -14], [3, -5, 9], [17, 3, -12], [12, 7, -15], [14, 6, 20], [26, -22, -12], [15, 12, -5], [-4, 14, 18], [12, 0, -10], [32, -22, -4], [8, -10, -6], [-1, -5, -4], [-14, 15, 4], [16, -13, -3], [-11, 10, 16], [19, 4, 7], [4, -25, -6], [-19, 7, -12], [30, -7, -7], [26, 15, -8], [-13, -8, -17], [-9, -10, 8], [-12, 4, -14], [-7, 17, -8], [-14, -2, -4], [14, 18, 8], [30, -18, -10], [-10, -8, 4], [5, 0, 9], [15, -9, 6], [5, -1, 9], [14, 20, 19], [9, 17, 11], [19, -8, -14], [-14, 26, 13], [34, 3, -15], [29, -3, 5], [-6, 20, -10], [-4, 10, -8], [26, 4, 10], [12, 10, -24], [-10, -3, -23], [44, -16, -6], [30, -17, 4], [12, -3, 6], [-40, -4, 1], [34, -4, -1], [10, -2, 28], [-20, 32, 6], [-20, -1, -3], [-13, -3, -13], [-1, -9, 8], [-14, 14, 5], [5, 9, 32], [-20, 14, -8], [10, -31, -16], [16, 0, 14], [-8, -5, -9], [22, -10, -6], [-2, -17, 5], [37, -5, -6], [19, 2, 16], [22, -18, -16], [-28, -8, 8], [-9, -11, -22], [-30, -8, 4], [-35, -5, -19], [-53, 4, -1], [50, -11, 3], [3, 6, -5], [-43, 1, -5], [-14, 7, 21], [10, -18, 1], [-29, 19, 9], [-27, 19, 20], [-24, -16, -8], [-8, 32, 21], [-34, -8, -11], [-48, -1, 5], [-3, -16, 10], [-24, -18, 10], [-4, -1, 1], [4, 16, -8], [1, -2, -11], [35, -32, -22], [-16, 11, 16], [-36, 13, 16], [-40, 14, 8], [-22, 33, 2], [-3, 20, 10], [-28, 20, 2], [-15, 24, 18], [-12, -22, 2], [29, 7, -6], [46, -34, -16], [10, 12, 0], [-17, -25, 18], [-22, 0, -22], [2, 2, 23], [-26, -3, 8], [15, 7, -25], [4, 4, -6], [-14, -4, 10], [-4, 8, -13], [10, 2, -30], [-3, 16, 9], [-7, -10, -21], [3, -7, 24], [2, -2, 19], [28, -2, 30], [19, -3, -6], [12, -18, -5], [0, -21, 21], [26, -15, 16], [17, 21, 37], [6, 32, 0], [-21, 28, 19], [4, -20, 0], [-25, 2, -8], [-21, -18, 20], [2, -6, -8], [-28, 10, -13], [12, 16, 10], [6, -34, 4], [-4, 10, 34], [-34, -20, 11], [-26, 12, 4], [-33, 15, 20], [26, -12, -18], [24, -33, -32], [-52, 22, 12], [-2, 11, 9], [-15, -24, 9], [-10, 6, 22], [-3, -18, -25], [31, 21, 2], [-7, 42, -7], [-6, -14, -10], [44, -22, -16], [-17, 18, -21], [-57, 9, 7], [25, -36, -9], [-24, 26, 22], [-26, 14, 10], [34, -15, 1], [-6, -2, 12], [28, -8, 14], [9, -13, 6], [-38, -10, 9], [32, -8, -26], [-9, -11, 11], [-46, 6, 6], [14, -36, 2], [-19, -10, -24], [18, -21, -13], [14, -36, -6], [-32, -13, -8], [-34, 8, 18], [-8, 17, 5], [8, 12, 1], [-39, 53, 28], [42, 2, 12], [28, 16, -10], [4, -10, 37], [1, 22, -20], [-35, -9, -11], [40, 3, 2], [42, -40, -22], [-22, 22, 19], [-19, 17, 23], [3, 26, 2], [28, 0, -10], [24, 27, 3], [-6, -26, 5], [32, -32, -18], [-37, 33, 0], [-27, 0, 21], [58, -11, 1], [-31, -6, -13], [-8, -36, -3], [32, -19, 8], [-51, -20, 2], [9, -3, -30], [45, 2, -5], [34, -37, -15], [-12, -18, -14], [-38, -23, 17], [-30, -2, -26], [12, 17, 17], [-27, -24, -31], [42, -4, 14], [-22, -13, -4], [9, 13, 0], [63, 11, 6], [-8, 6, 14], [-33, -12, 20], [8, -40, -7], [16, 10, -5], [25, 7, 5], [11, -4, -4], [-6, 14, 2], [-16, 4, 10], [-7, -28, 23], [19, 25, 36], [-4, 20, 24], [-1, -14, -31], [15, -6, -14], [62, -21, -4], [-30, 46, 12], [28, -34, 12], [5, 7, 22], [18, -16, -2], [41, 14, -12], [13, 19, 4], [35, -55, -13], [-28, -6, -18], [-46, 9, 8], [-24, -8, -24], [-36, 11, -2], [8, -30, -7], [42, 7, -20], [9, 26, -35], [35, -41, -22], [41, -1, 12], [-40, -7, 1], [28, 28, 17], [-42, -5, -8], [12, -25, 9], [-10, 0, 32], [7, 7, 14], [-28, 8, -22], [31, -15, -28], [8, 17, 28], [51, -31, -22], [6, 29, -24], [-19, 47, 0], [-11, 17, -32], [46, 22, 14], [38, 10, -1], [13, -33, -32], [-11, -1, 14], [-2, 36, -1], [13, -49, -18], [-19, -8, -12], [-38, -4, -26], [-6, 16, 25], [16, -44, -6], [-11, 15, 17], [-17, 15, -24], [13, -48, 5], [14, -25, -10], [2, 6, -17], [26, 22, 14], [72, 10, 22], [34, 6, -18], [-17, 26, 24], [-29, 15, 23], [-33, 34, -6], [-26, -18, 22], [42, -23, 4], [-14, 28, 29], [-32, 2, 30], [-80, 28, 13], [-8, -23, -31], [36, -12, -9], [-40, 14, 13], [8, -18, -32], [-23, 29, 29], [-47, 20, 16], [-51, 56, 27], [-53, -4, -13]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1071_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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