
// Make newform 1344.4.b.a in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1344_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1344_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_1344_4_b_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.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_1344_4_b_a();".
// 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 := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [1, 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_1344_b();"
function MakeCharacter_1344_b()
    N := 1344;
    order := 2;
    char_gens := [127, 1093, 449, 577];
    v := [1, 2, 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_1344_b_Hecke();"
function MakeCharacter_1344_b_Hecke(Kf)
    N := 1344;
    order := 2;
    char_gens := [127, 1093, 449, 577];
    char_values := [[-1, 0], [1, 0], [1, 0], [-1, 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, 0], [-3, 0], [0, -8], [-14, -7], [0, -2], [0, -8], [0, 44], [52, 0], [0, 66], [246, 0], [-116, 0], [314, 0], [0, 156], [0, 218], [192, 0], [150, 0], [-204, 0], [0, -336], [0, -294], [0, -470], [0, 72], [0, 794], [-252, 0], [0, -124], [0, -832], [0, 128], [508, 0], [0, 166], [-974, 0], [-246, 0], [0, 1066], [-2772, 0], [2418, 0], [1676, 0], [-594, 0], [0, -558], [0, -1472], [0, 1210], [2760, 0], [0, -192], [0, 350], [0, 2192], [0, -2822], [274, 0], [1998, 0], [-4948, 0], [0, 34], [-2572, 0], [6348, 0], [0, -1040], [4434, 0], [0, 2738], [0, 2888], [2772, 0], [0, 2004], [0, -3190], [0, -2208], [6388, 0], [3602, 0], [258, 0], [4228, 0], [0, -4584], [-5108, 0], [-2928, 0], [0, -1096], [3438, 0], [0, -662], [-574, 0], [0, -6202], [0, 872], [0, 1468], [0, 6466], [-11956, 0], [-1646, 0], [0, 6874], [9768, 0], [-11850, 0], [0, -4120], [11178, 0], [0, -2824], [7524, 0], [-15406, 0], [0, 3122], [0, 2168], [-436, 0], [0, -7090], [-3774, 0], [-2710, 0], [0, 7696], [0, -614], [9708, 0], [-12000, 0], [0, -4798], [0, -7074], [0, -414], [-1272, 0], [0, 7672], [0, -1892], [-8332, 0], [12890, 0], [0, -4278], [-6954, 0], [16788, 0], [17418, 0], [0, 3698], [0, 13608], [252, 0], [0, 12364], [0, -9238], [0, 13472], [16436, 0], [-11822, 0], [-25494, 0], [11660, 0], [0, -494], [-8214, 0], [-7868, 0], [17232, 0], [-7338, 0], [0, -15090], [0, 13464], [-24158, 0], [0, 1808], [0, -1938], [36188, 0], [33006, 0], [-1478, 0], [22800, 0], [-31532, 0], [0, -8696], [0, 22490], [0, -13726], [0, -5110], [6394, 0], [0, -5660], [0, 5696], [0, -7032], [-2020, 0], [0, -19848], [27162, 0], [-18124, 0], [-44010, 0], [0, -334], [0, -6490], [0, 21808], [19392, 0], [0, 16752], [0, 1908], [31516, 0], [0, -7350], [40714, 0], [0, -15068], [0, -5054], [9384, 0], [0, -16582], [0, -20718], [0, 13602], [0, -14156], [0, -13880], [0, -136], [0, -23810], [28962, 0], [0, -7422], [47556, 0], [4434, 0], [45816, 0], [0, 5994], [0, -3360], [51394, 0], [0, 31328], [0, -9626], [0, 720], [0, 10314], [32794, 0], [33164, 0], [0, -1284], [0, -20942], [-30402, 0], [-7364, 0], [0, -856], [0, 12490], [-58740, 0], [-10078, 0], [0, 27780], [0, 15842], [0, 20816], [66082, 0], [16724, 0], [15130, 0], [43752, 0], [0, -28360], [0, -33258], [0, 26642], [0, -19320], [0, 24438], [0, -26260], [-974, 0], [34778, 0], [0, 43372], [18912, 0], [10830, 0], [-67804, 0], [0, -45304], [0, 27504], [13380, 0], [0, -42160], [60788, 0], [0, -10274], [24426, 0], [-21332, 0], [-50398, 0], [0, -37512], [0, 39378], [87756, 0], [22584, 0], [0, -24048], [0, -34598], [0, -5500], [0, -4518], [51918, 0], [-4070, 0], [35212, 0], [74946, 0], [0, -56630], [-30012, 0], [-47878, 0], [0, -18580], [0, 38594], [48508, 0], [0, 23430], [51202, 0], [-21500, 0], [0, 42506], [52050, 0], [16972, 0], [-93480, 0], [0, -30944], [-103050, 0], [0, 3358], [-64776, 0], [0, 27974], [-39172, 0], [51476, 0], [-5278, 0], [0, -24340], [-39264, 0], [71836, 0], [-54780, 0], [0, 10466], [0, 62570], [-28814, 0], [0, 65012], [0, -7798], [0, -21552], [0, 36056], [0, 18086], [-63358, 0], [77956, 0], [0, 26224], [0, 70384], [0, 34762], [0, 22790], [0, 46584], [0, -4280], [0, 44636], [101252, 0], [15822, 0], [0, 40708], [0, 50930], [-3378, 0], [0, 63584], [0, 49106], [0, -17440], [0, -14214], [0, 36232], [114700, 0], [0, -54610], [-70846, 0], [-62630, 0], [-143148, 0], [-120264, 0], [0, 31298], [-86736, 0], [0, -39408], [15188, 0], [0, 41410], [-63406, 0], [42918, 0], [-58892, 0], [0, -51596], [-117282, 0], [17484, 0], [-25494, 0], [-41292, 0], [-10598, 0], [0, 5440], [89828, 0], [0, -21120], [-3324, 0], [20780, 0], [0, 77176], [9246, 0], [0, -28102]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1344_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1344_4_b_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_1344_4_b_a(:prec:=2)
    chi := MakeCharacter_1344_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(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_1344_4_b_a();".
// 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_1344_4_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1344_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![192, 0, 1]>,<19,R![-52, 1]>],Snew);
    return Vf;
end function;