
// Make newform 3997.1.cz.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3997_cz();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_3997_cz_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_3997_1_cz_a();".
// 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 ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        Kf := CyclotomicField(570);
    end if;
    return [ #coeff eq Kf!0 select 0 else &+[ elt[1]*Kf.1^elt[2] : elt in coeff]  : coeff in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_3997_cz();"
function MakeCharacter_3997_cz()
    N := 3997;
    order := 570;
    char_gens := [2285, 1716];
    v := [285, 352];
    // 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_3997_cz_Hecke();"
function MakeCharacter_3997_cz_Hecke(Kf)
    N := 3997;
    order := 570;
    char_gens := [2285, 1716];
    char_values := [[[-1, 0]], [[1, 176]]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 1;
    raw_aps := [[[-1, 201], [1, 244]], [], [], [[-1, 93]], [[-1, 3], [1, 140]], [], [], [], [[1, 106], [-1, 155]], [[1, 40], [-1, 105]], [], [[1, 98], [1, 194]], [], [[-1, 29], [-1, 75]], [], [[1, 20], [1, 48]], [], [], [[-1, 19], [1, 144]], [[1, 100], [1, 166]], [], [[1, 156], [1, 170]], [], [], [], [], [], [[-1, 229], [-1, 271]], [[1, 118], [1, 262]], [[1, 218], [-1, 231]], [[-1, 91], [-1, 127]], [], [[-1, 1], [-1, 225]], [], [[1, 142], [-1, 167]], [[-1, 123], [-1, 161]], [], [[-1, 45], [1, 54]], [], [], [[1, 128], [1, 154]], [], [[-1, 69], [1, 200]], [[1, 130], [1, 174]], [[-1, 97], [-1, 219]], [], [[1, 38], [-1, 111]], [], [], [], [[1, 148], [1, 226]], [[1, 208], [1, 258]], [], [], [], [[-1, 5], [1, 74]], [], [], [[-1, 171], [1, 252]], [[1, 28], [1, 164]], [], [], [], [], [], [[1, 136], [1, 242]], [[1, 50], [-1, 83]], [[-1, 151], [-1, 183]], [[-1, 163], [-1, 253]], [], [], [[1, 152], [1, 236]], [], [[1, 116], [1, 254]], [[-1, 15], [1, 266]], [], [[1, 178], [-1, 275]], [], [[-1, 95], [1, 250]], [], [], [[1, 182], [-1, 213]], [[-1, 137], [1, 256]], [], [], [[1, 30], [-1, 235]], [[-1, 119], [-1, 277]], [[-1, 37], [1, 76]], [], [[-1, 129], [-1, 149]], [], [], [[1, 44], [1, 68]], [[-1, 55], [-1, 187]], [[1, 72], [1, 190]], [], [], [], [], [[1, 84], [-1, 139]], [[-1, 157], [1, 214]], [[1, 82], [1, 264]], [], [[-1, 79], [-1, 81]], [[-1, 35]], [], [], [], [[-1, 87], [1, 134]], [], [], [[1, 60], [-1, 177]], [[-1, 181], [1, 240]], [], [[-1, 47], [-1, 51]], [[1, 24], [-1, 209]], [], [], [[-1, 53], [1, 222]], [[1, 110], [1, 228]], [], [[1, 132], [-1, 217]], [], [[1, 268], [-1, 273]], [], [[1, 76], [1, 78]], [[-1, 119], [-1, 193]], [], [], [], [[1, 66], [1, 206]], [[-1, 185], [-1, 239]], [[-1, 17], [-1, 257]], [[1, 102], [1, 114]], [], [], [], [], [], [[-1, 89], [1, 270]], [], [[1, 132], [-1, 259]], [[-1, 61], [-1, 227]], [[1, 36], [1, 104]], [], [], [], [], [], [[1, 238], [-1, 279]], [[1, 90], [1, 150]], [], [[1, 30], [-1, 163]], [], [[1, 16], [-1, 131]], [[1, 68], [1, 152]], [[-1, 115], [-1, 241]], [], [], [], [[-1, 189], [1, 266]], [[1, 52], [1, 92]], [[1, 18], [-1, 227]], [], [[1, 4], [1, 202]], [], [[-1, 87], [-1, 197]], [], [[1, 252], [-1, 281]], [], [[1, 8], [-1, 283]], [], [[1, 58], [-1, 125]], [[1, 10], [-1, 85]], [], [], [[1, 86], [-1, 207]], [[1, 202], [-1, 217]], [], [], [[-1, 71], [1, 244]], [], [[-1, 117], [-1, 255]], [], [[1, 44], [-1, 239]], [], [[-1, 121], [-1, 223]], [], [[1, 168], [1, 224]], [], [], [[1, 12], [-1, 65]], [[-1, 41], [1, 104]], [], [[-1, 111], [1, 128]], [], [[1, 56], [1, 244]], [[-1, 65], [-1, 201]], [], [], [[-1, 89], [1, 232]], [], [], [], [], [], [], [[1, 38], [-1, 113]], [[-1, 71], [1, 192]], [], [[1, 124], [-1, 213]], [], [[1, 192], [-1, 255]], [], [], [], [[1, 208], [1, 256]], [], [[1, 34], [1, 92]], [[-1, 225], [1, 280]], [[1, 218], [1, 248]], [], [[1, 12], [1, 120]], [[-1, 7], [-1, 185]], [], [[1, 4], [1, 12]], [], [[1, 108], [1, 278]], [], [[-1, 153], [1, 232]], [[-1, 1], [1, 28]], [], [[-1, 11], [1, 150]], [[-1, 99], [-1, 241]], [], [], [], [[-1, 151], [-1, 199]], [[-1, 165], [1, 210]], [], [[1, 36], [-1, 73]], [], [], [[1, 64], [1, 86]], [], [], [], [], [[1, 144], [-1, 179]], [[-1, 17], [-1, 137]], [[1, 32], [1, 148]], [], [[1, 90], [1, 138]], [], [], [[-1, 21], [1, 114]], [[-1, 39], [-1, 257]], [], [], [], [[1, 220], [1, 284]], [[1, 64], [1, 72]], [], [], [], [], [[-1, 17], [1, 18]], [], [[1, 102], [1, 230]], [[1, 24], [-1, 183]], [], [[1, 8], [-1, 165]], [], [[1, 42], [1, 94]], [], [], [[-1, 209], [-1, 243]], [[1, 26], [-1, 71]], [[-1, 105], [-1, 233]], [], [], [[1, 46], [-1, 129]], [], [], [], [[1, 56], [-1, 145]], [[1, 20], [-1, 97]], [[-1, 207], [1, 228]], [], [], [[1, 80], [-1, 215]], [], [[1, 96], [1, 158]], [], [[-1, 23], [1, 42]], [], [], [], [], [], [], [[1, 52], [-1, 249]], [[-1, 147], [-1, 263]]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3997_cz_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3997_1_cz_a();".
// 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_3997_1_cz_a(:prec:=144)
    chi := MakeCharacter_3997_cz();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 1));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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;

