
// Make newform 1568.3.g.h in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1568_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1568_g_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_1568_3_g_h();".
// 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_1568_3_g_h();".
// 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 := [16, 0, 6, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 2, 0, 1], [0, 10, 0, 1], [6, 0, 2, 0]];
    Rf_basisdens := [1, 4, 4, 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_1568_g();"
function MakeCharacter_1568_g()
    N := 1568;
    order := 2;
    char_gens := [1471, 197, 1473];
    v := [1, 1, 2];
    // 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_1568_g_Hecke();"
function MakeCharacter_1568_g_Hecke(Kf)
    N := 1568;
    order := 2;
    char_gens := [1471, 197, 1473];
    char_values := [[-1, 0, 0, 0], [-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 := 3;
    raw_aps := [[0, 0, 0, 0], [2, -1, 0, 0], [0, 0, 1, -1], [0, 0, 0, 0], [-4, -6, 0, 0], [0, 0, -1, 1], [-18, 4, 0, 0], [2, 19, 0, 0], [0, 0, -8, -1], [0, 0, -6, 0], [0, 0, -4, -6], [0, 0, -10, -4], [10, -12, 0, 0], [20, 2, 0, 0], [0, 0, -4, -4], [0, 0, 12, 10], [46, -11, 0, 0], [0, 0, 3, 5], [56, 16, 0, 0], [0, 0, -16, -8], [-58, 8, 0, 0], [0, 0, -16, 4], [22, 13, 0, 0], [-78, -24, 0, 0], [34, 92, 0, 0], [0, 0, -1, -3], [0, 0, 20, -6], [24, 28, 0, 0], [0, 0, 18, -12], [-76, -44, 0, 0], [0, 0, -32, -1], [-34, -95, 0, 0], [46, -8, 0, 0], [-38, 103, 0, 0], [0, 0, -8, 42], [0, 0, -16, 33], [0, 0, 27, 21], [68, -122, 0, 0], [0, 0, -68, 8], [0, 0, 19, 21], [-176, -84, 0, 0], [0, 0, -49, -27], [0, 0, 8, -24], [-52, -148, 0, 0], [0, 0, -36, 2], [0, 0, -28, 54], [0, 116, 0, 0], [0, 0, 0, 2], [-54, -113, 0, 0], [0, 0, 21, -29], [-306, 80, 0, 0], [0, 0, -24, 45], [194, -188, 0, 0], [-66, -135, 0, 0], [54, -264, 0, 0], [0, 0, -16, 60], [0, 0, -25, -23], [0, 0, -104, 2], [0, 0, 36, 6], [-286, 96, 0, 0], [-262, 59, 0, 0], [0, 0, 39, 69], [270, 33, 0, 0], [0, 0, -8, 8], [-246, 116, 0, 0], [0, 0, 32, 2], [-268, -38, 0, 0], [0, -116, 0, 0], [220, 78, 0, 0], [0, 0, 97, 19], [250, 232, 0, 0], [0, 0, 0, -69], [0, 0, 40, -70], [0, 0, -104, 26], [-204, 58, 0, 0], [0, 0, -28, 40], [0, 0, 2, -16], [0, 0, -163, -9], [200, -84, 0, 0], [-298, -108, 0, 0], [666, -29, 0, 0], [0, 0, 92, -42], [0, 0, 40, 83], [230, 260, 0, 0], [0, 0, -64, -38], [-56, -384, 0, 0], [-98, -256, 0, 0], [-124, 276, 0, 0], [0, 0, 129, -53], [0, 0, 80, 80], [-558, -149, 0, 0], [0, 0, -12, 14], [0, 0, -56, -93], [224, 116, 0, 0], [56, -20, 0, 0], [0, 0, 152, -30], [0, 0, 21, -105], [-522, -220, 0, 0], [694, 129, 0, 0], [0, 0, 100, -6], [524, 58, 0, 0], [0, 0, 44, 38], [310, -351, 0, 0], [-224, -316, 0, 0], [52, 442, 0, 0], [-102, -584, 0, 0], [-150, -121, 0, 0], [174, 32, 0, 0], [0, 0, 64, 16], [258, -328, 0, 0], [0, 0, -200, 8], [0, 0, -102, 68], [-264, 308, 0, 0], [-222, -225, 0, 0], [0, 0, 72, 0], [336, -212, 0, 0], [774, -359, 0, 0], [0, 0, 180, -78], [0, 0, -14, 216], [524, 114, 0, 0], [0, 0, -117, 105], [50, 40, 0, 0], [0, 0, 151, -83], [616, 44, 0, 0], [-706, 241, 0, 0], [0, 0, -42, 148], [0, 0, 62, -156], [0, 0, 220, -40], [0, 0, -60, -136], [0, 0, -105, 33], [276, 190, 0, 0], [0, 0, -112, 59], [0, 0, -72, 75], [0, 0, -126, -44], [194, -572, 0, 0], [234, -292, 0, 0], [0, 0, 9, -113], [690, -165, 0, 0], [0, 0, -57, 1], [844, 356, 0, 0], [-790, 83, 0, 0], [0, 0, -240, 50], [0, 0, 120, -112], [-912, 448, 0, 0], [0, 0, 43, -171], [0, 0, 28, -38], [0, 0, -99, -101], [-506, -244, 0, 0], [-474, -499, 0, 0], [0, 0, -216, -100], [0, 0, -282, -72], [594, 288, 0, 0], [-1080, 140, 0, 0], [0, 0, 132, 92], [104, -392, 0, 0], [0, 0, -64, -7], [0, 0, -392, 24], [502, 380, 0, 0], [-746, -96, 0, 0], [0, 0, -375, -85], [908, -654, 0, 0], [938, 416, 0, 0], [0, 0, 104, -193], [30, -443, 0, 0], [158, -8, 0, 0], [0, 0, -116, -50], [0, 0, -168, 200], [0, 0, 225, -169], [36, -100, 0, 0], [0, 0, 209, 183], [112, -796, 0, 0], [0, 0, -251, 135], [0, 0, 96, -92], [-1102, 48, 0, 0], [0, 0, 52, -144], [-78, 8, 0, 0], [-632, -308, 0, 0], [0, 0, -22, -60], [0, 0, -192, -72], [0, 0, -227, 183], [0, 0, -152, -69], [1422, -435, 0, 0], [0, 0, 70, 260], [958, 312, 0, 0], [0, 0, 96, -35], [0, 0, -155, 163], [0, 0, -116, -218], [-206, 171, 0, 0], [142, -1096, 0, 0], [0, 0, -404, 62], [-1710, -96, 0, 0], [224, -640, 0, 0], [-32, -392, 0, 0], [0, 0, 399, 1], [-288, -224, 0, 0], [1146, -552, 0, 0], [672, -100, 0, 0], [0, 0, -86, -216], [538, 960, 0, 0], [0, 0, 200, -230], [0, 0, 104, 162], [0, 0, -148, 146], [0, 0, -219, -125], [-718, 816, 0, 0], [-1690, 537, 0, 0], [0, 0, 549, 31], [0, 0, -100, 374], [12, 214, 0, 0], [92, -660, 0, 0], [1150, 265, 0, 0], [-112, -708, 0, 0], [0, 0, 5, -285], [0, 0, 576, 1], [842, 411, 0, 0], [0, 0, -80, -154], [1146, 324, 0, 0], [0, 0, -192, -252], [-1334, 620, 0, 0], [0, 0, 144, 180], [0, 0, 194, -80], [0, 0, 234, -116], [0, 0, -208, 54], [-238, 720, 0, 0], [0, 0, -56, -271], [-510, -141, 0, 0], [0, 0, 326, 192], [-582, -1200, 0, 0], [0, 0, 344, -135], [0, 0, -128, -118], [-940, -514, 0, 0], [0, 0, -250, -108], [114, -1289, 0, 0], [0, 0, -96, 80], [-286, 1120, 0, 0], [-902, -1317, 0, 0], [0, 0, 408, 86], [-58, 492, 0, 0], [0, 0, -4, 46], [-1176, 312, 0, 0], [0, 0, 40, 196], [612, 498, 0, 0], [-1434, 241, 0, 0], [0, 0, -304, -138], [0, 0, -368, 106], [-214, -1944, 0, 0], [0, 0, 28, -58], [0, 0, 248, -300], [998, 605, 0, 0], [-340, -454, 0, 0], [0, 0, 280, -92], [0, 0, 128, -2], [650, 1600, 0, 0], [0, 0, -88, 444], [-1278, 240, 0, 0], [0, 0, -261, -123], [584, 728, 0, 0], [0, 0, -634, 56], [170, 895, 0, 0], [0, 0, 45, -53], [-610, 1236, 0, 0], [0, 0, 24, -323], [468, -1230, 0, 0], [0, 0, 405, 67], [0, 0, 135, 169], [306, -1196, 0, 0], [2718, 397, 0, 0], [0, 0, 200, 422], [-46, 16, 0, 0], [432, 1400, 0, 0], [0, 0, 10, 8], [0, 0, -245, -291], [-2916, -34, 0, 0], [-758, -496, 0, 0], [0, 0, -64, 155], [430, 296, 0, 0], [0, 0, -232, -432], [-884, 690, 0, 0], [0, 0, -536, 138], [-1860, -452, 0, 0], [-1602, 1137, 0, 0], [0, 0, -168, -166], [0, 0, -568, 212], [0, 0, -100, 528], [0, 0, 535, 197], [-1174, 1083, 0, 0], [0, 0, 224, 145], [-574, 544, 0, 0], [0, 0, 364, -342], [0, 0, 332, 112], [370, -440, 0, 0], [0, 0, -392, 138], [814, -183, 0, 0], [-24, 84, 0, 0], [-430, -401, 0, 0], [0, 0, 360, -442], [0, 0, -203, -417], [0, 0, -616, -86], [0, 0, 21, 139], [-474, 397, 0, 0], [890, 587, 0, 0], [782, -1524, 0, 0], [0, 0, 38, 208], [0, 0, 192, -88]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1568_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1568_3_g_h();".
// 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_1568_3_g_h(:prec:=4)
    chi := MakeCharacter_1568_g();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_1568_3_g_h();".
// 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_1568_3_g_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1568_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![2, -4, 1]>],Snew);
    return Vf;
end function;