
// Make newform 39.4.b.b in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_39_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_39_b_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_39_4_b_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_39_4_b_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 := [48, 0, 23, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 19, 0, 1], [12, 0, 1, 0]];
    Rf_basisdens := [1, 1, 2, 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_39_b();"
function MakeCharacter_39_b()
    N := 39;
    order := 2;
    char_gens := [14, 28];
    v := [2, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_39_b_Hecke();"
function MakeCharacter_39_b_Hecke(Kf)
    N := 39;
    order := 2;
    char_gens := [14, 28];
    char_values := [[1, 0, 0, 0], [-1, 0, 0, 0]];
    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 := 4;
    raw_aps := [[0, 1, 0, 0], [3, 0, 0, 0], [0, 2, -1, 0], [0, -2, 2, 0], [0, -8, -3, 0], [-1, 8, 1, -4], [30, 0, 0, -12], [0, -10, -8, 0], [-72, 0, 0, 0], [-102, 0, 0, 12], [0, -38, 20, 0], [0, -68, -4, 0], [0, 66, 13, 0], [-328, 0, 0, 12], [0, 36, 1, 0], [222, 0, 0, 48], [0, -52, -1, 0], [58, 0, 0, 28], [0, -54, -54, 0], [0, 168, -41, 0], [0, 156, 42, 0], [1072, 0, 0, 16], [0, -188, 63, 0], [0, 138, 25, 0], [0, -68, 14, 0], [-1074, 0, 0, 48], [-316, 0, 0, 36], [-564, 0, 0, -120], [0, -152, -82, 0], [534, 0, 0, 60], [-632, 0, 0, -56], [-516, 0, 0, -96], [0, -58, 47, 0], [56, 0, 0, -84], [0, 222, 229, 0], [0, 522, 0, 0], [-646, 0, 0, 60], [0, 350, -188, 0], [0, -92, -267, 0], [-306, 0, 0, 360], [-1812, 0, 0, -96], [194, 0, 0, -420], [-696, 0, 0, -24], [0, -984, -60, 0], [0, -406, 227, 0], [-1520, 0, 0, 184], [2212, 0, 0, 376], [0, 1306, 116, 0], [0, -504, -359, 0], [0, 248, 346, 0], [1794, 0, 0, 312], [0, -496, -73, 0], [0, -820, 46, 0], [-516, 0, 0, -168], [-1062, 0, 0, -216], [-2400, 0, 0, 24], [-4062, 0, 0, 84], [0, 438, -438, 0], [-1042, 0, 0, -192], [0, -842, 447, 0], [172, 0, 0, 40], [0, 1138, -681, 0], [0, -2142, -450, 0], [8640, 0, 0, -72], [1522, 0, 0, -680], [0, 1050, 559, 0], [0, 2258, -134, 0], [-1214, 0, 0, -176], [900, 0, 0, 432], [0, -1036, 460, 0], [0, -750, -167, 0], [0, 28, -351, 0], [1628, 0, 0, -348], [1486, 0, 0, -32], [0, 814, 464, 0], [0, -1884, 73, 0], [4410, 0, 0, 324], [0, -740, 56, 0], [0, 754, -15, 0], [0, 3196, -46, 0], [-2748, 0, 0, -744], [0, 136, -46, 0], [0, 724, 105, 0], [-8234, 0, 0, -308], [-7108, 0, 0, 924], [6732, 0, 0, 504], [0, 586, -1179, 0], [0, 1460, -362, 0], [0, -446, 555, 0], [0, -1426, 166, 0], [4836, 0, 0, 312], [0, -40, -1021, 0], [0, -1622, 200, 0], [-8292, 0, 0, 1128], [0, 1746, -810, 0], [2184, 0, 0, -672], [0, 4778, 407, 0], [11934, 0, 0, 252], [-17992, 0, 0, 108], [0, 2320, -214, 0], [728, 0, 0, -1932], [0, 2778, 199, 0], [7620, 0, 0, 552], [-8430, 0, 0, 888], [6940, 0, 0, -1256], [0, 936, -1116, 0], [0, -4228, 383, 0], [0, -2054, 549, 0], [3552, 0, 0, 264], [7858, 0, 0, 184], [7292, 0, 0, 468], [0, 2484, -936, 0], [0, -5646, -1499, 0], [0, 90, 2286, 0], [0, 2386, -460, 0], [13494, 0, 0, 492], [0, 810, -198, 0], [-2688, 0, 0, -1416], [22218, 0, 0, -564], [-11076, 0, 0, -1920], [0, -12, -816, 0], [7022, 0, 0, 1284], [8934, 0, 0, -1608], [0, 3704, -643, 0], [0, -6286, 454, 0], [-20958, 0, 0, -1308], [0, 6272, 1342, 0], [20256, 0, 0, -384], [-2264, 0, 0, 1552], [0, -2224, 1882, 0], [0, -7294, 1462, 0], [0, 504, 1423, 0], [-11572, 0, 0, 3348], [2794, 0, 0, -2444], [0, -5634, 1027, 0], [0, 2944, 1520, 0], [0, 2422, 2025, 0], [0, 6882, -2706, 0], [16482, 0, 0, -1596], [870, 0, 0, 2964], [0, -5290, -1748, 0], [0, -8586, -2063, 0], [9380, 0, 0, 1716], [0, 3840, 1081, 0], [14626, 0, 0, -1580], [0, 7608, 1951, 0], [0, 804, 3048, 0], [-966, 0, 0, -1680], [-34244, 0, 0, 88], [0, -3844, 1829, 0], [0, -9476, 8, 0], [18954, 0, 0, -2520], [-14372, 0, 0, -704], [-27528, 0, 0, 1536], [15580, 0, 0, -1976], [3984, 0, 0, -816], [29992, 0, 0, -2384], [0, 8426, -715, 0], [22366, 0, 0, 2668], [0, -6730, -1291, 0], [0, 2012, 2219, 0], [5658, 0, 0, -1680], [0, -942, 492, 0], [1212, 0, 0, 5736], [0, -5594, 5019, 0], [0, 4544, -2341, 0], [-18964, 0, 0, -2844], [11006, 0, 0, 1440]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_39_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_39_4_b_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_39_4_b_b(:prec:=4)
    chi := MakeCharacter_39_b();
    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_39_4_b_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_39_4_b_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_39_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![48, 0, 23, 0, 1]>],Snew);
    return Vf;
end function;