
// Make newform 320.6.d.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_320_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_320_d_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_320_6_d_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_320_6_d_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 := [34180205, -1528906, -2235516, 29654, 49869, 506, -384, -4, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [3699160158415, -987814371481, -163085600918, 38223976863, 2862879190, -394224929, -13653287, 1351312], [-3980607119045, 1550708292741, 163085600918, -38223976863, -2862879190, 394224929, 13653287, -1351312], [233594355624595, 6684424237957, -8470823112146, 680074862701, 129626329330, -11237259467, -595364489, 46680604], [1741708282831, -227603504321, -76894837308, 8082523469, 1112583304, -86408283, -4782019, 324498], [-148559382301075, 7063580525697, 5939121203576, -143197714893, -61548799060, 1922738643, 204376019, -8770602], [269387125903700, -47531360053324, -16457519954998, 1960333630208, 269861003750, -22832683396, -1189115032, 87017312], [305925739736305, -140104059489243, -8601384396614, 5044385010495, 138978838510, -43714650957, -502754801, 109345770]];
    Rf_basisdens := [1, 281446960630, 281446960630, 844340881890, 5117217466, 76758261990, 422170440945, 422170440945];
    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_320_d();"
function MakeCharacter_320_d()
    N := 320;
    order := 2;
    char_gens := [191, 261, 257];
    v := [2, 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_320_d_Hecke();"
function MakeCharacter_320_d_Hecke(Kf)
    N := 320;
    order := 2;
    char_gens := [191, 261, 257];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 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 := 6;
    raw_aps := [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, -1, 0, 0, 0], [0, -25, 0, 0, 0, 0, 0, 0], [40, 0, 3, 0, 0, 0, 1, 0], [0, -92, 0, 0, 0, 2, 0, -3], [0, -220, 0, 0, -3, 5, 0, -1], [-300, 0, -3, -5, 0, 0, -5, 0], [0, 204, 0, 0, -30, -14, 0, 1], [-220, 0, 117, -10, 0, 0, -11, 0], [0, 1480, 0, 0, -210, 10, 0, -20], [-3880, 0, 150, 40, 0, 0, 0, 0], [0, 1230, 0, 0, -312, 40, 0, -4], [2698, 0, 165, -33, 0, 0, -47, 0], [0, 7020, 0, 0, -135, -70, 0, 50], [-5960, 0, 321, -80, 0, 0, -43, 0], [0, 10020, 0, 0, -141, -45, 0, 153], [0, 13524, 0, 0, -570, 30, 0, -135], [0, 17350, 0, 0, 1290, 50, 0, 30], [0, -18300, 0, 0, -723, -10, 0, 110], [-30840, 0, -1290, -180, 0, 0, -180, 0], [5800, 0, 141, -205, 0, 0, -5, 0], [-40720, 0, -1020, 60, 0, 0, 410, 0], [0, -14300, 0, 0, -69, 110, 0, -60], [-9774, 0, -1470, 254, 0, 0, 206, 0], [3120, 0, -3015, -145, 0, 0, 475, 0], [0, 61460, 0, 0, -2610, 170, 0, 380], [-49100, 0, 837, -570, 0, 0, -371, 0], [0, 39000, 0, 0, -6237, 140, 0, -140], [0, 96450, 0, 0, 1590, -250, 0, 130], [32050, 0, -2466, 330, 0, 0, -770, 0], [-97340, 0, 7605, -530, 0, 0, -265, 0], [0, 43228, 0, 0, 11940, -110, 0, -305], [106950, 0, -9558, -970, 0, 0, -70, 0], [0, -92604, 0, 0, -2910, 394, 0, 859], [0, -39050, 0, 0, -11580, 700, 0, 940], [93320, 0, 11430, 240, 0, 0, 710, 0], [0, -136770, 0, 0, 5070, 1510, 0, 590], [0, -40600, 0, 0, 4479, 660, 0, 2640], [274740, 0, 5583, -1650, 0, 0, 111, 0], [0, 4770, 0, 0, -96, -720, 0, -1332], [0, 59316, 0, 0, -9750, 1354, 0, -1561], [0, -195920, 0, 0, -4680, -1340, 0, 1690], [-81560, 0, -5070, 1780, 0, 0, 410, 0], [173200, 0, 3939, -355, 0, 0, 1045, 0], [0, -108700, 0, 0, -2835, 125, 0, -3445], [354560, 0, -22860, 3920, 0, 0, -170, 0], [0, 19892, 0, 0, 12780, -1562, 0, -3617], [206020, 0, 5115, -1210, 0, 0, -1395, 0], [0, -262960, 0, 0, -5295, 4160, 0, 950], [0, -324820, 0, 0, -3480, -3140, 0, -1410], [632840, 0, -23505, 4985, 0, 0, 1525, 0], [227560, 0, 12540, -3080, 0, 0, 1130, 0], [-361198, 0, 18615, 4293, 0, 0, 3627, 0], [0, -632772, 0, 0, 15480, 2222, 0, 2497], [-487810, 0, 120, 3560, 0, 0, 4900, 0], [567360, 0, -7179, 7260, 0, 0, -1293, 0], [0, -332490, 0, 0, -71670, 2370, 0, -4290], [207280, 0, 39690, -5340, 0, 0, -2120, 0], [0, -629870, 0, 0, -23016, 1200, 0, -4072], [-840822, 0, -18765, -4335, 0, 0, 4155, 0], [0, 1049600, 0, 0, 441, 5880, 0, -6630], [0, 264750, 0, 0, -29268, -9180, 0, 7344], [0, -247700, 0, 0, 3753, -7830, 0, -8370], [85360, 0, 63090, 8020, 0, 0, -7270, 0], [1019870, 0, -55140, -1220, 0, 0, -7300, 0], [0, -47680, 0, 0, -65091, 9005, 0, -397], [0, -1235468, 0, 0, -93120, 2198, 0, -4477], [171050, 0, 92628, -12620, 0, 0, -11020, 0], [0, 435500, 0, 0, 48723, -11190, 0, -260], [0, 215140, 0, 0, -55800, -2620, 0, 5250], [1176990, 0, 34170, 11910, 0, 0, -2250, 0], [1478120, 0, -28380, -24760, 0, 0, -1460, 0], [-1692460, 0, -39675, -9370, 0, 0, -4925, 0], [0, -426110, 0, 0, 99336, 14640, 0, -7288], [0, -199636, 0, 0, 65070, -9074, 0, 12811], [-3422580, 0, 7299, -2550, 0, 0, 2433, 0], [0, 1784490, 0, 0, 83730, 13530, 0, -4290], [0, 323260, 0, 0, 27573, -9075, 0, 15191], [-1686242, 0, -68220, -19588, 0, 0, -8292, 0], [-744374, 0, 193815, 10949, 0, 0, 91, 0], [0, 25276, 0, 0, 39690, -17586, 0, -4861], [0, -66130, 0, 0, 70980, 31740, 0, -22340], [-4134720, 0, -27870, -17940, 0, 0, -15540, 0], [3525900, 0, 108051, 28925, 0, 0, 23425, 0], [1350280, 0, 104580, -7440, 0, 0, -30140, 0], [0, 1995800, 0, 0, 54009, 28420, 0, -3870], [-1645306, 0, -111585, -5259, 0, 0, 119, 0], [-3183950, 0, 5418, 14750, 0, 0, -11450, 0], [0, -441500, 0, 0, 32370, 30550, 0, 12340], [-632840, 0, 33375, 7520, 0, 0, 31575, 0], [0, -4299960, 0, 0, 80805, -22140, 0, 27750], [-3274360, 0, 156180, -3820, 0, 0, 7660, 0], [2774480, 0, -82761, 17420, 0, 0, -4787, 0], [0, -2963468, 0, 0, -181260, -28610, 0, -12905], [0, 1199996, 0, 0, 84630, 6430, 0, 16615], [903380, 0, 335853, 6830, 0, 0, 8701, 0], [0, 661740, 0, 0, 48060, 33480, 0, -26730], [-4731582, 0, -128460, 28972, 0, 0, 28108, 0], [0, 2645000, 0, 0, 45069, -260, 0, 34810], [0, -2168140, 0, 0, 33330, -59930, 0, 23460], [0, -3910600, 0, 0, 6633, 24780, 0, -3930], [0, 2208450, 0, 0, -35454, -33390, 0, -36018], [0, 7005240, 0, 0, -51945, 16860, 0, -10200], [-206646, 0, 323715, 43481, 0, 0, 8759, 0], [0, -2923132, 0, 0, 128460, -58590, 0, 49905], [3297950, 0, 55092, -47420, 0, 0, 22880, 0], [0, 1591500, 0, 0, -259917, 8530, 0, -26680], [5909350, 0, 232674, -43450, 0, 0, -31050, 0], [1476480, 0, 5040, 43560, 0, 0, 52830, 0], [-4233582, 0, 179715, -62303, 0, 0, -24517, 0], [5889240, 0, -348663, 24760, 0, 0, -21371, 0], [0, -6230380, 0, 0, 199833, -28655, 0, 30111], [-1239460, 0, -247155, -55165, 0, 0, -16625, 0], [0, -3001004, 0, 0, -69870, -36370, 0, 8165], [11806640, 0, -2850, -14620, 0, 0, 13450, 0], [-5585918, 0, 394335, 68365, 0, 0, -9445, 0], [0, 5429100, 0, 0, -170625, -13550, 0, 19000], [3555800, 0, -338733, -55720, 0, 0, 889, 0], [0, -2142840, 0, 0, 282597, -37635, 0, 48099], [0, -3527764, 0, 0, 57930, 17974, 0, 20439], [0, -12508070, 0, 0, 194220, 16860, 0, -68260], [283420, 0, 169635, 37605, 0, 0, 9825, 0], [0, 6901800, 0, 0, 242967, -27705, 0, -30711], [0, 6246500, 0, 0, -88971, -3690, 0, 84790], [0, -7352012, 0, 0, 554040, -4258, 0, -16333], [0, -811550, 0, 0, 214650, -9350, 0, 123550], [0, -6751200, 0, 0, 436470, 24650, 0, 3040], [-3645480, 0, -229980, 41040, 0, 0, -31410, 0], [3791600, 0, 268803, 62820, 0, 0, 29801, 0], [0, -6841730, 0, 0, -259002, 51670, 0, -56834], [0, 2093924, 0, 0, 219930, 115330, 0, -84185], [-5459720, 0, 56733, -102920, 0, 0, -29089, 0], [13113480, 0, -9570, 24460, 0, 0, 85810, 0], [0, -13117490, 0, 0, 460134, -66690, 0, 92378], [2961462, 0, 109200, -27872, 0, 0, -15248, 0], [2548886, 0, -537630, 174, 0, 0, -63714, 0], [0, 9842720, 0, 0, 386055, 116015, 0, -56215], [0, -4672700, 0, 0, -426675, 51350, 0, 2150], [0, 9545600, 0, 0, -875583, 103145, 0, -52561], [4342954, 0, 843180, 89620, 0, 0, 90740, 0], [0, -3190228, 0, 0, -70440, 49510, 0, -60095], [0, 4721670, 0, 0, 897090, 17490, 0, -21570], [-1319780, 0, 434301, -20870, 0, 0, 24317, 0], [0, -1256300, 0, 0, 298947, 83030, 0, 97020], [0, -6020930, 0, 0, -254730, 48990, 0, 77690], [-21161360, 0, 79320, -44420, 0, 0, -28360, 0], [0, -5589420, 0, 0, 127161, -182055, 0, 69687], [911450, 0, 84348, -139260, 0, 0, -99160, 0], [0, -11188764, 0, 0, 240870, -26506, 0, -71971], [-5701200, 0, -829329, 35940, 0, 0, 64707, 0], [0, -7175110, 0, 0, 201504, 58120, 0, -85932], [10249618, 0, 158565, -65465, 0, 0, 181445, 0], [0, -9132000, 0, 0, -348861, 63280, 0, -52580], [-24689880, 0, -400137, -36840, 0, 0, 34221, 0], [0, 9456920, 0, 0, 638715, -34820, 0, 127600], [3408600, 0, -349410, -110400, 0, 0, -18120, 0], [-1217920, 0, -1210560, -125240, 0, 0, -101370, 0], [-71954, 0, 465195, -10295, 0, 0, 67835, 0], [-160600, 0, 116427, 213685, 0, 0, 142385, 0], [0, 4918560, 0, 0, -47400, 49020, 0, 7350], [0, -4540860, 0, 0, 237405, 63510, 0, -145950], [-10721550, 0, -630114, 86330, 0, 0, 112730, 0], [5596900, 0, -182361, -16090, 0, 0, 60663, 0], [0, -8037588, 0, 0, -792480, 161938, 0, 94643], [-277800, 0, 471357, 66275, 0, 0, -152425, 0], [-11632080, 0, -18327, -151740, 0, 0, 29691, 0], [16421920, 0, -241770, -29560, 0, 0, -123690, 0], [0, -3794280, 0, 0, -19323, 36885, 0, 61959]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_320_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_320_6_d_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_320_6_d_b(:prec:=8)
    chi := MakeCharacter_320_d();
    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_320_6_d_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_320_6_d_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_320_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![8262810000, 0, 149500800, 0, 796456, 0, 1568, 0, 1]>,<7,R![400612500, 4400400, -41520, -160, 1]>],Snew);
    return Vf;
end function;