
// Make newform 315.4.a.h in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_315_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_315_4_a_h();".
// 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_315_4_a_h();".
// 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 := [-4, -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_315_a();"
function MakeCharacter_315_a()
    N := 315;
    order := 1;
    char_gens := [281, 127, 136];
    v := [1, 1, 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_315_a_Hecke(Kf)
    return MakeCharacter_315_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 := 4;
    raw_aps := [[0, -1], [0, 0], [-5, 0], [-7, 0], [4, -4], [0, -22], [42, -26], [22, -44], [34, 14], [152, 30], [-114, 18], [18, -30], [114, -52], [-60, 166], [272, -30], [378, 52], [284, 0], [-354, 90], [396, -98], [488, -162], [-104, 290], [136, 328], [-284, 300], [202, -476], [572, 482], [726, -472], [204, -212], [-374, -30], [310, -840], [430, 564], [1048, -424], [-1572, 8], [846, -800], [-142, 796], [-1288, 778], [-904, -832], [2104, 238], [1876, -914], [-1948, -862], [-718, 1936], [512, 948], [-1930, -22], [1084, -1054], [130, -852], [4694, -88], [42, 586], [-2712, 1560], [-68, 1516], [3040, -300], [1234, -1406], [1866, -1360], [4420, -970], [-3846, 1688], [-1460, 2432], [-2294, 1162], [5022, -778], [5050, 444], [-2902, -1818], [5002, -514], [8256, -112], [3668, -2400], [-1954, -1560], [528, -1428], [-864, 3172], [512, -1102], [1730, 1160], [-3552, 1920], [2282, -1180], [-1750, -334], [3830, 326], [3974, -394], [-6076, 4038], [-28, -3612], [1790, 3510], [1084, -2732], [-8896, -1022], [7700, -898], [-6164, 3430], [-6580, -1772], [-5042, -604], [-13388, 592], [3494, -2040], [-3112, 5374], [3016, -146], [1650, 2814], [-7590, 378], [776, 4804], [-2106, 5572], [-6770, 7048], [3520, 5084], [-5984, -1672], [-5888, -812], [-1888, 4624], [-5392, -5180], [-10916, 5852], [-6648, 934], [-3886, 5488], [4078, -936], [14636, -556], [8534, 436], [-14740, -3562], [14674, -6632], [-14764, 3024], [5620, -124], [6896, 4244], [2840, -3486], [-5908, -8056], [13286, 1202], [1516, -7430], [16942, -3220], [-19972, 732], [538, 1602], [-5222, 7768], [-9686, -3888], [20104, 2392], [12224, 7284], [-3184, -1184], [-4340, -6586], [15930, 6640], [-13408, 8960], [7470, 2266], [1958, -6064], [14654, -1028], [210, 7098], [19334, -1460], [1064, -12554], [-15118, 4928], [7120, 10676], [-15112, 9536], [16592, -174], [-33040, -1448], [-18670, 6302], [13748, 812], [-14898, -2322], [18794, -6392], [-2170, 9688], [7138, 7996], [-7836, 8824], [20914, -11112], [3852, 5492], [15690, 3844], [14852, -11142], [-2080, -5084], [-23918, -4830], [-24338, -5882], [-7048, -10168], [8524, 8166], [-26090, 3934], [18154, -12320], [-4422, 2530], [17730, -13354], [-9450, 20596], [32724, -6842], [4896, 15638], [12868, -20010], [-48092, 730], [-40916, 3892], [19794, -8472], [-15820, 4874], [-23494, 12932], [-1370, 21462], [-32194, 1468], [-15960, 19456], [25332, -5904], [12834, 8840], [-32012, -8754], [12508, -23356], [29516, -1266], [-17122, 2152], [44606, -4716], [-4252, 2164], [3714, -7130], [41304, -190], [-246, -5484], [-36850, 12314], [21254, -15084], [-8204, 2408], [43468, 6798], [28784, -2208], [54810, -710], [19656, 5700], [-8388, 26360], [13334, 21814], [45114, -5674], [28350, -16834], [-12546, 12516], [52930, 834], [-24220, 17116], [-28314, -15048], [22704, 5940], [55388, -10758], [-16054, -6238], [5444, 14780], [18382, 9680], [36742, -12866], [15822, -8406], [-23718, 20028], [-13314, 25282], [-19974, -870], [34972, 6902], [53116, 8798], [-33030, 26314], [-60756, -2926], [17410, 7772], [19460, 16712], [28114, 492], [10982, 25794], [53434, -11614], [-38292, -4656], [11530, -14736], [75470, -3544], [48866, -132], [19272, -19256], [-12900, 16748], [-27072, -23960], [-45250, 28496], [71984, -1400], [-13698, -26104], [10142, -3226], [-12862, -1256], [-52382, -6752], [-6658, -29250], [14532, -13348], [24424, -10716], [-32560, 26324], [74198, -11596], [-51310, 14634], [35888, 6542], [-65836, 8916], [-11384, -27076], [-10834, 18706], [-19154, -27220], [14296, 656], [-16332, -18472], [-45096, 2028], [-62772, 12338], [70678, -25564], [-55858, 17584], [31928, -8604], [-32120, -28096], [2782, 4870], [27322, -8976], [-57160, 31880], [-61946, 16852], [-41394, -886], [-73856, 3204], [-15544, -22712], [-48964, 16440], [-39516, 47380], [32250, -10902], [-17846, 23390], [-39426, -11624], [-19882, 4646], [58022, 3648], [-18214, 25036], [-23236, -29472], [-18902, 37228], [33492, 2664], [-45198, -5028], [-81048, 25366], [-42152, -7372], [42442, -47598], [-50214, 29958], [-744, -23434], [-15114, -11714], [107070, -6492], [27820, 30702], [-111650, -4352], [35332, 3722], [17770, 28564], [-29814, 3710], [15348, 27778], [-42012, -26690], [-60612, -8988], [58740, -52042], [49856, 16160], [104858, -21630], [27954, -55312], [16998, -39552], [-18380, -28112], [-22148, 34462], [82960, -15232], [25652, -1110], [-14054, -26438], [-124452, -4316], [-2212, 26334], [36742, 30868], [41706, -3940], [12878, -48966], [-113670, 27944], [58380, -26146], [93420, 13080], [-44294, 5512], [15356, -39616], [-1074, -43646], [34206, -60660], [21126, 1058], [-1054, -45596], [26260, 43816], [-116844, -56], [41332, -40390], [-153238, 7104], [-100596, 11172], [79302, -30914], [38040, 20980], [-39582, -31512], [29238, 23678], [19370, 16230], [76928, -9394], [-8058, 2846], [-31556, -40266], [79696, -42442], [-80228, 47708], [-90836, -30898], [92026, -9902], [-52670, 71804], [-48236, 27272], [-1504, -47974], [-77956, 33546], [-109676, 54200], [64866, 38880], [-29474, 27368], [-81626, -32460], [-70128, 72056], [-30286, -31460], [16722, 67984], [-94280, -17388], [-13268, -1140], [1758, 222], [-5914, -36816], [-40738, 11416], [-5494, 16592], [72850, 33066], [39652, 14340], [114684, -36984], [-40204, 70444], [30462, 10932], [-139958, 6174], [16578, 4356], [-202680, 6624], [-194106, 3198], [-80878, -37504], [-30646, 80664], [25816, -296], [25066, -65716], [39872, 11844], [9090, 28714], [-27620, -29222], [101576, -40440], [88334, -18000], [86662, -39744], [23846, 7004], [42988, 4226], [-151520, -5600], [62746, -75664], [-117874, 5286], [-35656, -55092], [24180, -46896], [23590, -98612], [11770, -3150], [-123330, 28134], [-23270, 85552], [165266, -25814], [49192, 31776], [-43412, 41552], [-24562, 3572], [26298, 69636], [-31592, -23068], [47030, 10876], [-42152, 38200], [38258, 45232], [194794, 12746], [128400, -32594], [-70278, 47398], [-5286, -17194], [-119724, 27808], [-41544, 40554], [-65136, -2594], [91238, -6024], [69744, -3506], [128158, -26452], [15558, 15116], [72656, -69808], [-57550, 6812], [-9182, -45292], [113496, 49410], [24124, 49028], [113312, -26398], [27788, 45150], [172768, -1426], [142014, 21364], [74918, 65420], [62216, -31136], [-172016, 45092], [228440, -21282], [33450, 24848], [-99642, -44802], [59622, 17572], [-26448, 36032], [-12864, -47854], [100866, 70370], [-218378, 41388], [-114568, 55916], [36334, -74702], [140882, -22084], [9230, 18758], [150842, -17450], [-119360, 35324], [40432, -64020], [93324, -115440], [-40236, 121682], [145790, -66332], [-9510, -9438], [-18968, -35548], [26386, -92892], [131938, -44168], [123500, 10254], [-105320, 61880], [-112430, 1818], [-99848, 93478], [-10436, 111646], [140436, -10402], [20274, -29298], [137300, 56888], [-72376, -67398], [49078, -75560], [67470, -53218], [-237252, -33000], [113494, -92880], [-151392, -9680]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_315_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_315_4_a_h();".
// 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_315_4_a_h(:prec:=2)
    chi := MakeCharacter_315_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_315_4_a_h();".
// 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_315_4_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_315_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-4, 1, 1]>],Snew);
    return Vf;
end function;