
// Make newform 1521.2.a.s in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1521_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_1521_2_a_s();".
// 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_1521_2_a_s();".
// 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, -2, -1, 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_1521_a();"
function MakeCharacter_1521_a()
    N := 1521;
    order := 1;
    char_gens := [677, 847];
    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_1521_a_Hecke(Kf)
    return MakeCharacter_1521_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 := [[1, -1, -1], [0, 0, 0], [1, 2, -1], [-1, 0, -1], [3, -1, 3], [0, 0, 0], [2, -3, 2], [2, 1, 0], [-3, 4, -5], [2, -1, 3], [8, -3, 5], [-7, -1, 0], [5, -1, 3], [-7, 4, -2], [2, 1, 0], [5, -1, -3], [0, -4, 2], [-2, -1, 6], [0, -4, 7], [-4, 7, -5], [-7, 6, -9], [0, 0, -3], [3, 1, -2], [2, -6, -1], [-3, 12, -2], [-1, 4, 3], [-6, 0, -4], [1, 7, -3], [-5, -5, -5], [-9, 1, -2], [3, 6, 0], [2, -9, 9], [5, 10, 0], [-9, 9, -5], [14, 0, -3], [7, 8, -1], [11, -10, 14], [-6, 6, 5], [7, 2, -2], [-8, 11, 5], [-3, 12, 1], [-8, -6, 7], [12, -12, 13], [3, -16, 12], [3, 9, -8], [8, -6, 1], [8, 5, 5], [7, -5, -7], [3, 0, -8], [6, -3, 13], [-8, 1, -2], [3, -13, -2], [2, -14, 1], [-6, 1, -18], [0, -8, -7], [-6, 6, 11], [-16, 9, -8], [21, 1, 0], [-9, -9, 1], [1, 2, -7], [0, 1, -14], [5, -2, 17], [6, 1, -1], [18, -4, 1], [2, -13, -10], [12, -14, 7], [9, -5, 9], [4, 1, 1], [7, -1, -2], [-14, 8, 0], [-24, 10, -21], [-8, 12, 7], [2, -10, 11], [9, -15, -1], [-5, 11, -6], [-6, 16, -3], [19, 0, 4], [4, 3, -4], [-6, 1, -13], [27, -2, -1], [-7, 4, -12], [-10, 5, -6], [20, -16, 9], [-20, 15, -14], [-4, -11, -7], [-10, 3, -4], [-18, -3, -4], [-19, 14, -9], [13, -12, 8], [20, -12, 4], [15, -26, 20], [-3, 5, -2], [17, 6, -12], [-9, 5, -23], [10, 2, 16], [-17, 12, -21], [-10, 2, -2], [-3, 2, -16], [5, 2, -17], [-1, -19, 5], [-1, 27, -16], [24, -13, 5], [23, -9, 10], [11, -7, -5], [7, -6, 27], [-13, 5, -4], [10, -2, 14], [12, -18, 26], [-14, -16, 11], [-5, 22, 2], [6, -5, -9], [-13, 7, -24], [-10, 0, -12], [-28, 11, -22], [17, -13, -14], [-19, 18, 9], [0, -21, -8], [9, -11, 13], [16, -22, 23], [39, -9, 17], [-6, 28, -5], [-5, -3, 3], [-3, -1, 0], [15, 0, -3], [-3, 5, 15], [1, -17, 3], [-12, -1, 11], [4, -20, 4], [-1, -27, 2], [-24, 18, 1], [-19, 7, -24], [-20, -1, -16], [-19, -8, 9], [17, -5, 1], [-24, 14, -7], [4, 6, -16], [-25, 14, -7], [15, -13, 13], [-20, 2, -4], [-7, 4, -2], [2, 7, -26], [-13, 35, -29], [-1, -18, 21], [2, 31, -3], [29, -20, 1], [13, -6, -6], [-9, 16, -5], [-10, 34, -22], [15, -6, -25], [0, -15, 0], [-37, 2, -6], [34, -5, -2], [14, -17, 10], [8, 15, -7], [-13, -27, 3], [-18, -3, 19], [-7, -21, 11], [19, -16, 14], [0, 10, -19], [3, 29, -11], [-39, 3, -11], [32, -16, 25], [-19, 3, -28], [-1, -10, -18], [23, -25, 16], [24, -18, 27], [9, 1, 1], [-16, 24, -23], [12, 2, 6], [11, 1, 36], [26, -27, 38], [-10, 1, -33], [1, 1, 5], [-23, 7, 6], [-25, 13, -22], [21, 4, -10], [-25, 10, -11], [-2, -27, -1], [13, -19, 28], [25, -24, 17], [17, -10, 24], [-38, 4, -6], [-3, 21, -3], [-15, 22, -1], [16, 1, 20], [-16, 24, -31], [-22, 33, -34], [38, -8, -5], [4, -19, -26], [-21, 9, -30], [-34, -11, -5], [19, -16, -22], [-18, 23, 16], [-13, -3, 4], [10, -42, 26], [14, 11, 19], [21, -14, 25], [-38, 10, -14], [-1, -14, -26], [-43, 22, -33], [25, 17, 2], [-23, 19, 12], [-21, -4, -15], [37, -34, 43], [19, 7, -24], [4, 4, -9], [-15, -6, -7], [-33, 3, 7], [-25, -9, -3], [7, -4, 10], [-13, 6, 10], [11, -48, 7], [15, 19, -21], [-25, 31, -38], [-21, 26, -17], [2, 2, 26], [26, -27, 34], [8, 30, -6], [30, -40, 29], [24, -2, 30], [-6, 10, 18], [10, 21, -23], [-18, 10, 9], [-39, 21, -17], [32, -20, 7], [-28, 30, -39], [-5, 13, -38], [2, 14, 11], [-12, 10, 15], [-61, 10, -18], [17, 24, -11], [-39, 22, -1], [4, -21, -10], [5, -1, -28], [-17, 0, 13], [53, -29, 37], [-1, -5, 20], [14, 6, -10], [48, -25, 5], [42, -35, 27], [-3, 17, 31], [-19, 29, -14], [35, -28, 34], [-5, -11, 13], [-37, 15, -13], [-40, 8, -25], [10, -21, 35], [0, -23, 37], [18, -5, -14], [-21, -20, -8], [-2, -3, 30], [19, -50, 10], [38, 9, 8], [25, -11, 48], [-6, 28, 14], [-5, -12, 12], [-15, 19, 4], [-2, -5, 2], [-5, -10, 0], [1, -31, -16], [7, 49, -24], [21, -10, 46], [16, 5, 37], [-18, 46, -9], [-32, 3, -16], [13, 19, -19], [20, 1, 29], [-61, 9, -20], [-28, 17, -4], [-49, -7, 1], [-61, 17, -23], [-37, -6, 4], [23, -28, 20], [9, -9, 11], [-21, 13, -51], [22, -18, 32], [-6, -26, 11], [-27, 9, 29], [-8, 50, -25], [-35, -3, 16], [6, 35, -15], [46, 3, 17], [3, -1, 0], [27, -31, 7], [-4, -48, 30], [-2, 14, -28], [-13, -5, -6], [27, 3, -10], [27, -52, 26], [28, -34, 41], [-55, 7, 5], [-8, -3, 15], [-10, 7, 21], [-36, 20, 6], [1, -17, 6], [16, -23, 22], [-27, 10, -43], [-37, -3, -17], [-4, 15, 10], [52, -7, -21], [35, -2, 15], [0, -5, -11], [-17, 35, -27], [2, 34, 7], [3, -15, -15], [-9, -3, -41], [37, -22, 4], [36, 15, -1], [-26, 41, -41], [20, -30, 26], [15, -24, 36], [-30, 13, 19], [50, -37, 38], [4, 27, -20], [21, -15, 31], [-18, 26, -22], [55, -10, 11], [3, 8, 18], [-10, -13, 19], [-9, -19, 21], [57, -23, 23], [4, 37, -27], [-10, 7, 14], [2, -21, -4], [-2, 5, 15], [36, -6, -4], [-32, -36, 14], [-46, 1, -6], [-26, -33, 11], [-2, -5, -26], [-32, 24, 26], [22, -20, -14], [-10, -21, 18], [-33, 34, -19], [-35, -1, 7], [-22, -48, 12], [32, -15, -8], [-15, 5, -33], [4, -19, 6], [44, -28, 16], [-41, -4, 5], [35, -47, 33], [-79, 9, 9], [-20, -21, -6], [34, 22, 11], [-1, -11, -39], [18, 20, 5], [-16, 58, -2], [4, -17, -35], [-35, 21, -32], [28, 20, -7], [-11, -20, -3], [31, 25, -17], [-41, 13, -27], [-4, -38, -16], [-58, 6, -29], [69, -12, 11], [-28, -34, 23], [-36, -16, 22], [10, 21, -5], [-3, 4, 54], [25, -3, 43], [-3, 29, -33], [25, 6, 39], [8, -26, 9], [14, 21, 2], [37, -26, -2], [19, -44, 38], [41, -16, -4], [-58, 47, -64], [33, 3, -19], [20, 25, -9], [6, 10, -14], [-28, 15, -41], [-39, 6, 23], [-40, 28, -32], [17, -21, 27], [32, -20, 21], [-21, -6, -10], [-4, 11, 7], [41, -16, -20], [-10, -9, -32], [20, -14, 13], [7, 21, -29], [46, 2, 29], [63, -37, 69], [-14, -26, 0], [-30, -18, 17], [-36, 36, -9], [27, 4, -5], [-31, 53, -15], [12, -7, -24], [58, -13, 13], [46, -28, 70], [38, -2, 36], [38, -16, 40], [-18, 37, -22], [-30, 15, 27], [-9, -15, 12], [42, -32, 16], [55, -33, 42], [-14, -17, -9], [-24, -15, 15], [31, -30, 57], [-23, -26, 28], [53, -30, 5], [-5, 69, -18], [39, -41, 43], [38, 24, 12], [36, -9, 49], [61, -47, 70], [-11, 11, -48], [6, -40, 26], [-53, 3, -43], [-8, 15, 9], [19, 23, -33], [54, 1, -25], [-7, 56, -40], [45, 0, 12], [28, 10, 35], [-36, -1, -5], [56, -14, 59], [41, -28, 1], [8, -21, 5], [14, 44, -33], [35, 6, 9], [45, -57, 47], [-3, -40, -19], [34, -14, 33], [-16, -27, -22]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1521_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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