
// Make newform 1474.2.a.b in Magma, downloaded from the LMFDB on 05 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1474_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_1474_2_a_b();".
// 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_1474_2_a_b();".
// 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, -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_1474_a();"
function MakeCharacter_1474_a()
    N := 1474;
    order := 1;
    char_gens := [805, 1409];
    v := [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_1474_a_Hecke(Kf)
    return MakeCharacter_1474_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, 0], [0, 1], [-2, 2], [0, 2], [1, 0], [4, -3], [4, -2], [-3, 3], [2, 0], [-6, -2], [-5, 1], [6, -3], [7, -5], [8, -2], [12, -2], [2, 2], [2, -2], [7, -11], [1, 0], [8, -6], [2, -4], [-8, 0], [-4, 0], [-13, 1], [0, 8], [-8, 11], [-2, 10], [11, 5], [-9, 5], [8, -12], [-16, 3], [-6, 7], [2, -10], [2, -4], [0, 10], [0, 5], [9, -15], [-4, -2], [5, 5], [14, -4], [7, -9], [-14, 5], [-11, 15], [-10, 18], [7, 1], [12, -8], [-14, 7], [-2, -4], [10, -17], [-2, -8], [-2, 15], [0, -6], [16, -4], [-4, -13], [4, 3], [-12, 3], [14, -13], [-10, -12], [-10, 6], [18, -11], [26, -7], [18, -8], [0, 0], [6, 5], [-14, -6], [21, -11], [-8, 19], [13, -15], [6, 14], [12, 2], [28, -12], [-25, 11], [-16, 8], [2, 16], [-20, 1], [-19, -1], [-6, -11], [9, 1], [-4, -14], [2, -8], [18, -4], [7, -9], [8, -15], [32, -4], [20, -16], [-9, -5], [20, 3], [-4, 22], [10, -8], [-8, 8], [36, -2], [-24, 16], [9, 7], [-14, 11], [-29, 3], [-8, 18], [-13, 7], [6, 8], [22, -1], [-20, 3], [-2, 22], [-26, 14], [4, -14], [20, 0], [32, -8], [-12, 32], [16, 11], [-6, 29], [-37, -3], [6, 6], [-29, 19], [24, -12], [36, -3], [24, -16], [-30, -5], [-6, 30], [-26, 8], [0, 8], [-2, 10], [-19, 35], [26, -6], [-2, 15], [-33, 21], [-5, -1], [-14, -16], [25, 7], [-6, 3], [-14, 24], [-2, 23], [-14, 0], [18, -12], [-20, -7], [-26, 2], [16, -18], [12, -16], [-26, 1], [-38, 16], [34, -26], [-13, 35], [22, -1], [12, 2], [22, 2], [24, -42], [18, -23], [-11, -7], [-2, 18], [-2, -28], [18, 23], [6, 10], [-20, 34], [12, 2], [6, -12], [-43, 9], [-36, 0], [18, -14], [32, 0], [-28, 28], [36, -2], [-3, -15], [32, -9], [0, 6], [-2, 30], [4, -12], [-18, 22], [-12, 33], [26, -19], [-18, 27], [24, -26], [-32, 2], [-8, 8], [7, -19], [-27, 45], [11, -17], [11, 11], [-38, 33], [-24, -12], [-28, 28], [2, 12], [22, -42], [-10, 6], [-14, 6], [49, -17], [-13, -1], [12, -20], [-34, 15], [-30, -1], [-15, 11], [27, -9], [-14, 0], [-22, 24], [-28, -7], [2, -1], [20, 24], [48, -30], [-12, -30], [23, -7], [4, -14], [4, 0], [-37, 3], [11, -13], [12, 2], [-12, -8], [38, -30], [-6, 39], [-19, 9], [18, -15], [-30, -8], [52, 0], [20, -40], [-28, 11], [-30, 24], [26, -22], [38, 3], [44, 8], [6, 4], [-28, 2], [24, -15], [-48, 28], [30, -45], [-61, 15], [6, 36], [-43, 17], [-8, -14], [2, -46], [-20, 32], [34, -58], [48, 11], [-14, 17], [-29, -13], [46, -6], [-22, 6], [-34, 38], [-13, -33], [-17, -13], [24, 2], [-6, 38], [-32, -21], [28, -24], [-10, -24], [-16, 30], [-12, -12], [-48, 32], [-26, -31], [-6, 4], [30, 29], [8, 14], [-50, 36], [3, -1], [-34, 24], [-32, -14], [38, 16], [10, -42], [-43, 3], [-8, 25], [1, -1], [38, -50], [-6, 22], [14, -9], [-34, 0], [-2, 8], [-59, 21], [-27, 3], [-34, 8], [-16, 59], [15, -11], [28, -66], [-58, 2], [-14, -28], [-8, 4], [-13, -39], [-32, -2], [-5, -21], [6, 30], [23, -25], [34, -4], [-6, -31], [20, 13], [50, -38], [-26, 6], [-20, 40], [-4, -27], [50, -22], [-12, 14], [-27, 35], [16, 12], [-58, 4], [24, -5], [34, -34], [-62, 7], [-2, 33], [-41, -1], [-31, 53], [64, -16], [29, -33], [-4, -49], [-56, 12], [-7, 3], [4, -30], [5, 7], [36, 4], [-30, 0], [8, 30], [3, 7], [-16, 54], [-12, 0], [-22, -18], [72, -28], [26, -5], [-4, -6], [39, -71], [50, -30], [-39, 13], [28, -64], [-22, 26], [24, 32], [42, -49], [14, 32], [-28, 40], [55, -27], [3, -21], [-21, 13], [69, 3], [-4, -8], [26, -15], [-24, 0], [7, -3], [8, 48], [14, 14], [-24, -36], [-63, 11], [-12, -8], [-30, 39], [24, 4], [26, 32], [58, -7], [28, 10], [-36, 10], [-4, -30], [8, 24], [-24, -16], [11, -37], [8, -27], [32, 25], [26, -6], [46, -77], [-44, 24], [30, 6], [37, -63], [13, 21], [-58, 10], [47, 1], [-12, -2], [-22, 29], [16, 42], [51, 21], [-41, 23], [10, -24], [-6, 60], [6, -57], [64, -48], [54, -42], [-12, 58], [-20, 12], [-8, -32], [34, -2], [30, -60], [-30, 0], [29, -55], [2, -41], [17, -23], [-28, 70], [-25, 7], [-68, 11], [-20, 16], [-28, -30], [18, -58], [-20, 70], [-8, 18], [-63, -11], [26, 10], [-10, 42], [-36, 24], [4, -54], [13, -7], [-22, 16], [14, -3], [23, 5], [10, 51], [10, -8], [40, 24], [13, 23], [12, -28], [-32, 56], [55, -5], [6, 33], [42, 30], [12, -16], [61, -45], [35, -49], [71, -39], [-18, -12], [-60, -17], [-56, 46], [-8, -46], [32, -9], [-4, -38], [-2, 40], [-48, -30], [36, 32], [-61, 63], [-50, 50], [-16, 48], [-62, 6], [48, 8], [-58, 47], [-77, -5], [32, 8], [-94, 14], [20, -23], [-73, -1], [-22, 18], [8, 18], [-13, 1], [-30, 0], [-28, 36], [-46, 36], [-20, -24], [14, 2], [8, -40], [76, -28]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1474_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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