
// Make newform 169.4.a.j in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_169_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_169_4_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_169_4_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_169_a();"
function MakeCharacter_169_a()
    N := 169;
    order := 1;
    char_gens := [2];
    v := [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_169_a_Hecke(Kf)
    return MakeCharacter_169_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 := 4;
    raw_aps := [[3, -1], [4, -3], [5, 5], [-8, 1], [-16, 15], [0, 0], [27, 16], [68, 5], [56, 33], [47, -60], [20, 100], [117, -44], [-279, 20], [276, -97], [40, -140], [355, -165], [432, -55], [151, 200], [192, 91], [-116, 105], [-335, 85], [100, 40], [-80, -100], [-398, -125], [442, -469], [471, -320], [-1400, 320], [-968, 601], [490, -260], [271, 388], [1372, 481], [-20, 840], [-647, -16], [-52, -245], [1873, 40], [-420, -540], [671, -417], [1884, -1103], [728, -1721], [1994, -223], [28, 445], [-865, -105], [1824, 335], [1069, 352], [-418, 1731], [-832, -1325], [-1056, 945], [1144, -33], [-772, -1051], [-2030, 1700], [-4438, 276], [-5276, -300], [-1691, 2680], [5736, -45], [-1377, -116], [-2244, -1587], [-2598, 45], [-4616, 695], [-5897, 804], [7265, 725], [2904, -303], [-211, -3148], [-1820, 2120], [6200, -1120], [-5430, 1980], [2985, -2355], [-484, -2815], [1395, 1295], [-4368, -349], [-2138, 815], [41, -3552], [-4104, -1200], [-3888, 3081], [3019, -5088], [-5548, 5355], [3076, 1653], [755, 3135], [-3098, -3189], [-859, -280], [-4143, -1020], [11768, -395], [-9487, 625], [7796, 2455], [-4509, 2488], [8652, -975], [-9160, -2100], [-3958, 4295], [3873, -7016], [561, -5860], [-13040, -1080], [-1700, -5220], [6092, -2225], [-6652, -1111], [1116, -1175], [-3280, 6500], [2924, 1027], [-13827, 4540], [163, 6575], [-1056, -5243], [-4675, 7785], [-160, 6020], [18137, -3004], [16144, -3513], [-4898, 3645], [2740, 2880], [7261, -4047], [-6852, -5001], [-6603, 81], [4780, 1600], [11211, 3080], [8012, 3351], [-20291, 1492], [-17483, 2876], [-11580, -2840], [4556, -9175], [-11189, -4840], [16976, 3973], [12008, -2011], [1514, -3743], [19612, -5585], [-10479, -6000], [7531, -8612], [23790, -4580], [-14744, -5967], [20824, -8305], [-5930, -8220], [4693, 1720], [-10328, 1575], [3060, 5860], [10965, -17855], [-17932, -2225], [-11516, -8193], [-7316, 3645], [-1902, 5649], [-5166, 5415], [13578, 3255], [-12546, 13907], [14112, 941], [7418, -7471], [-1913, -8660], [-12268, -1700], [30846, 2795], [2576, -7197], [22964, -13728], [13063, -7220], [18348, -4055], [1165, -5755], [-10565, -8065], [-340, -4480], [-27708, 11516], [-35407, -3176], [19831, -8140], [4000, -15140], [43768, 2349], [-16292, 13149], [29180, -3420], [2028, -13765], [24813, 160], [-17745, -3605], [-12150, 1760], [-3532, 2499], [-9954, 8693], [-7880, 10040], [15564, 10135], [-8603, 15716], [18140, 8500], [6764, -3695], [-5897, -1496]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_169_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_169_4_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_169_4_a_j(:prec:=2)
    chi := MakeCharacter_169_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_169_4_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_169_4_a_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_169_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![2, -5, 1]>],Snew);
    return Vf;
end function;