
// Make newform 38.6.a.b in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_38_a();"
// 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_6_a_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_6_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_38_a();"
function MakeCharacter_38_a()
    N := 38;
    order := 1;
    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;

function MakeCharacter_38_a_Hecke(Kf)
    return MakeCharacter_38_a();
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 := 6;
    raw_aps := [[4], [-14], [-45], [-121], [-381], [-100], [933], [361], [-552], [2394], [-4024], [9182], [-2250], [-23377], [-26595], [-16008], [-126], [21335], [-51760], [8574], [11153], [-1660], [95964], [118848], [-153760], [153198], [-44506], [-122634], [54956], [101586], [-309778], [-2295], [89181], [189971], [-435915], [-316570], [-445042], [349580], [277962], [526566], [-150750], [-186118], [7683], [-993736], [430290], [511211], [1274390], [678146], [-54300], [1157813], [282939], [1420239], [-1029982], [-968379], [-1937208], [-1315311], [-2086716], [613856], [163013], [-194490], [-362197], [1951644], [-1825360], [688989], [-1624594], [1406646], [2425064], [-2057824], [-1959147], [2124905], [-4289790], [1532391], [-2199208], [1188548], [-1322242], [-216432], [2087703], [-3314095], [2007720], [465992], [-108132], [-1497388], [3770976], [3871622], [-155758], [-7496883], [-1536408], [1783787], [396273], [85121], [-6238827], [-5829084], [-10124998], [-8533116], [9887021], [729804], [-6266280], [-6243516], [8154326], [-6935713], [-6743596], [9477309], [-3426702], [10068216], [9876716], [-5531389], [-335067], [8548590], [-2268264], [-1019398], [7553888], [8381117], [-12710991], [-4298620], [771779], [-6052344], [3909563], [18596187], [-5718471], [-9629910], [-11889568], [-4781146], [20449122], [5600556], [-11582455], [-773058], [21938090], [934359], [8427173], [17509202], [-436741], [-5449356], [9639800], [15905927], [-5989551], [3490055], [-16900854], [-14934916], [-951504], [17709039], [3942416], [-36699255], [6516791], [-26882232], [-12048424], [18066522], [-24648286], [31503738], [-13835161], [23581782], [-18252982], [26347437], [12520991], [-3135342], [-26006692], [-1316262], [34581188], [10503318], [19766513], [23209206], [-4286148], [52011780], [-16466968], [21403932], [7370868], [37845660], [-40054750], [-35237047]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_38_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_38_6_a_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_6_a_b(:prec:=1)
    chi := MakeCharacter_38_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_6_a_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_6_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_38_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![14, 1]>],Snew);
    return Vf;
end function;