
// Make newform 169.4.a.g in Magma, downloaded from the LMFDB on 23 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_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_169_4_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 := [-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 := [[0, -1], [4, -3], [2, -1], [10, -11], [-34, -12], [0, 0], [18, -17], [26, 32], [104, -12], [-70, 96], [26, 34], [-102, -5], [126, -22], [72, 143], [-278, 121], [-74, 30], [246, -124], [-434, -190], [-150, 232], [-50, 231], [-98, -260], [-524, 40], [-1070, 182], [166, 388], [718, -508], [558, 142], [-152, -304], [1216, -140], [-782, 43], [1246, -236], [328, -344], [604, 567], [2326, -202], [-676, 847], [2134, 244], [2106, -441], [1802, -924], [-2610, 430], [1718, -902], [-1642, -1114], [-80, -29], [-358, 558], [2916, -874], [-794, -206], [2306, -1395], [680, 670], [1284, -537], [3214, -1737], [-694, -1018], [-470, 101], [1334, -621], [1006, 993], [-1466, 2536], [-3516, 1092], [894, 151], [3996, 480], [834, 2190], [874, 1193], [2830, -3402], [-2290, -484], [-5316, 1056], [1682, 1487], [2830, 1748], [584, 2858], [1434, 3345], [-378, 2580], [4322, 122], [-1686, 1841], [7332, -1753], [-6518, -409], [6566, -1650], [2670, -54], [4380, 858], [-2246, 2010], [2834, -378], [-3866, -2721], [1730, -4392], [8326, 696], [-3442, 530], [-4746, 5646], [-3988, 6781], [-6262, 6139], [-8482, 1417], [-13098, 1909], [2616, -2190], [10028, -111], [3830, -1276], [-8082, -2216], [-7734, -1669], [-9194, -1734], [11404, -1008], [9146, 2137], [7058, 1070], [5376, -6329], [-2134, -178], [-5812, -4394], [726, 112], [-11030, -1303], [-4488, -4580], [-2830, -5763], [7172, 1301], [-1534, 797], [1744, -2025], [3262, 8085], [1648, -3555], [4966, -4152], [2994, -1566], [21510, -4416], [-1772, 5578], [5634, 1559], [9572, -1134], [-194, 9088], [-14858, 4610], [-11226, 1534], [20666, 1235], [23590, -3976], [12290, -6848], [-7288, 11282], [-1918, -7682], [1360, -7028], [-2298, -9636], [-4238, 6007], [7254, -1538], [-19418, -1986], [6778, 5846], [7954, 10266], [10558, -1964], [8656, -4854], [22248, -1238], [-4926, -1199], [-5986, -8978], [17278, 729], [-21152, -1704], [-6426, -4218], [-15762, 7068], [5886, 474], [-12606, 131], [-18738, 5978], [14234, -10156], [-7958, 10489], [-16702, 1522], [-8574, -4201], [-5584, 1350], [5606, 540], [46, -12764], [20550, -6446], [-6590, -595], [17242, -15436], [-8920, 16580], [10542, 1397], [25946, -9371], [6190, -2725], [-2552, -13307], [-30020, -4164], [-22952, 4875], [-33844, -690], [13260, -4756], [-49458, -2450], [-12246, 3532], [24546, -13027], [-20698, -2964], [-2514, -3859], [33094, -2509], [30852, 7249], [-19058, 7136], [21818, -10859], [-13828, 8902], [-19586, 18784]];
    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_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_169_4_a_g(: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_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_169_4_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_169_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-4, 1, 1]>],Snew);
    return Vf;
end function;