
// Make newform 38.4.c.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_38_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_38_c_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_38_4_c_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_38_4_c_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 := [1, -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_38_c();"
function MakeCharacter_38_c()
    N := 38;
    order := 3;
    char_gens := [21];
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_38_c_Hecke();"
function MakeCharacter_38_c_Hecke(Kf)
    N := 38;
    order := 3;
    char_gens := [21];
    char_values := [[0, -1]];
    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 := [[-2, 2], [5, -5], [12, -12], [8, 0], [9, 0], [0, -26], [-114, 114], [-38, -57], [0, 78], [0, 204], [98, 0], [-334, 0], [-177, 177], [316, -316], [0, 492], [0, -678], [579, -579], [0, 352], [0, -755], [-6, 6], [145, -145], [316, -316], [-567, 0], [0, 114], [943, -943], [0, 1500], [-658, 0], [-1440, 0], [-1280, 1280], [-369, 0], [0, -902], [453, -453], [0, -2001], [0, 2935], [-6, 6], [2774, 0], [2728, -2728], [-1657, 0], [0, -2784], [-2382, 2382], [-3645, 0], [0, 1906], [1332, 0], [-1322, 1322], [3726, 0], [0, 478], [820, -820], [-926, 926], [4977, 0], [-4312, 0], [1821, -1821], [324, 0], [0, -341], [0, -4863], [0, 3453], [120, -120], [7068, -7068], [-1388, 1388], [-5164, 0], [0, 645], [-6743, 6743], [-252, 0], [7117, -7117], [4410, 0], [0, -6725], [0, 4866], [2375, 0], [10333, -10333], [-5109, 5109], [-2044, 0], [-1701, 0], [-1770, 1770], [0, 10558], [-124, 0], [44, 0], [-9312, 9312], [0, 11292], [334, -334], [-5613, 5613], [0, 3355], [5796, 0], [-650, 650], [0, -4206], [0, 13078], [2602, -2602], [0, -7923], [2529, 0], [7037, 0], [11730, -11730], [11762, 0], [-3465, 0], [0, 4020], [18146, 0], [-18528, 18528], [16003, -16003], [0, -13998], [0, 11328], [-1575, 0], [0, 21268], [0, -7748], [0, -13796], [0, -15672], [-12663, 0], [-14670, 0], [4643, 0], [-18277, 0], [12612, -12612], [0, 23109], [0, 78], [21707, 0], [728, 0], [-9218, 9218], [0, 27861], [23156, 0], [0, -25136], [-10401, 10401], [11737, -11737], [-23058, 0], [-22860, 0], [0, -2820], [0, 11860], [-31330, 0], [-4410, 0], [-180, 0], [-27412, 0], [-25836, 25836], [0, -14930], [-4146, 4146], [18856, -18856], [17318, 0], [25831, -25831], [-510, 510], [0, 694], [-4598, 4598], [32697, 0], [0, 4510], [0, 3012], [12131, 0], [8442, 0], [-8307, 0], [0, 16], [0, 22254], [0, 39670], [0, -15483], [27188, 0], [-1752, 1752], [16462, -16462], [-22101, 22101], [0, -43559], [15570, 0], [10144, -10144], [40887, 0], [0, -12599], [0, -49152], [8989, -8989], [26406, 0], [43514, 0], [-28149, 28149], [0, -35747], [0, -12990], [-11724, 11724], [-25017, 25017], [25894, -25894], [-36591, 36591], [-24687, 0], [25968, -25968], [-18584, 18584], [0, 48232]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_38_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_38_4_c_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_38_4_c_b(:prec:=2)
    chi := MakeCharacter_38_c();
    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_38_4_c_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_38_4_c_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_38_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![25, -5, 1]>],Snew);
    return Vf;
end function;