
// Make newform 38.6.a.c in Magma, downloaded from the LMFDB on 25 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_c();".
// 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_c();".
// 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 := [-360, -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_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, 0], [2, -1], [-21, -3], [55, 4], [331, -1], [804, 5], [37, -10], [361, 0], [-1568, -49], [-1398, 315], [-432, -316], [5158, 172], [8014, 602], [5511, 281], [-6635, 1115], [9992, 601], [-39254, -73], [22223, -825], [2352, 3101], [-30610, -1268], [9601, -2984], [33628, -134], [-6580, 2446], [66232, -4276], [87968, 2622], [-68098, -2780], [12534, -4336], [-16978, 1633], [95084, 2307], [191770, 2228], [168694, -9932], [-64479, -4521], [-43251, 12090], [-318229, -1263], [110141, 11587], [-115130, 19600], [138606, -16036], [-328708, -3984], [106338, -21870], [188886, -31632], [-68918, 22118], [171306, -30184], [136579, 22844], [-116560, -3402], [-37982, -2842], [278315, 22836], [84670, 55], [-287382, 20456], [439100, -38675], [487309, 49837], [873555, 26319], [-141681, 26016], [-342662, 1558], [-279267, -35463], [1471512, -9324], [153753, -33633], [867932, 28846], [113048, -42225], [-955523, 9691], [-1013738, 52346], [586243, -39809], [1177028, -9797], [-1344936, -5764], [254605, 110402], [-3107466, -4945], [-28866, -29475], [-2994312, -40033], [196736, -119364], [818509, 81341], [2938609, 40045], [49962, -107385], [721095, -85758], [2206328, -23724], [2328332, 116607], [1143062, -59037], [-1613224, 153490], [1929551, -27125], [2963529, 19019], [-1860816, -177888], [964272, -203944], [1021164, 74616], [-1250204, -102959], [-4935136, 71230], [3548574, -49228], [876586, -223478], [-1202619, -103815], [-4659192, 9342], [-3032389, 184614], [-3590967, 27603], [8174937, -43933], [-6403123, 132085], [1069860, -98664], [-2494198, 37200], [-2834892, 87672], [1877957, -402417], [-3810716, -95365], [4014872, 226246], [4190524, 83576], [-1262114, 311725], [3751511, -32421], [6291380, -188664], [4364917, -125989], [3910410, -196422], [-393576, -94200], [5088236, 163992], [-3261677, -603200], [-1008707, -157633], [-7236290, 343460], [-9471136, 52216], [-8642358, 166514], [-9430096, -102534], [-2679595, 91629], [1937769, 511017], [-1676924, 617458], [4007963, -265851], [5160808, -507730], [222451, -776681], [-3234501, -53532], [9023649, -347529], [3032098, -306127], [-7246280, 708751], [7226486, -535026], [148626, -113871], [1464156, -209448], [-2676527, -582323], [5712574, -117346], [-6739798, 759204], [-7653017, -975940], [2771333, 503034], [-6728654, -230930], [1380115, 471637], [9700572, -797544], [1101064, 173848], [-3772081, 594723], [22548161, -225062], [-611337, -198178], [-23551086, -90843], [-4203644, 458869], [11276096, 215701], [-11283345, 864018], [12275272, -125549], [10814433, 359775], [-11305673, -84458], [-11986248, -705399], [20646560, 105327], [5570106, -759690], [-18002478, -703432], [21661050, 435018], [1966207, -914153], [-29949874, 342232], [6511058, 1150641], [-4197435, -994143], [9617895, -591937], [12110322, 1168812], [13258444, 10351], [-30562422, 838326], [3791980, 566569], [7188702, 393579], [-11309983, 1080720], [13768894, -817909], [18055516, -104812], [25496556, -1445982], [10217736, -888112], [19227228, 476544], [-2729412, -1136832], [10259148, -663234], [42854890, 467962], [-14868895, -194841]];
    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_c();".
// 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_c(:prec:=2)
    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_c();".
// 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_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_38_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![-358, -3, 1]>],Snew);
    return Vf;
end function;