
// Make newform 363.3.b.l in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_363_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_363_b_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_363_3_b_l();".
// 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_363_3_b_l();".
// 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 := [1331, 0, 1061, 0, 282, 0, 29, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [541, 0, 260, 0, 30, 0, 1, 0], [6897, -1121, 3575, -377, 517, -47, 22, -2], [10527, -9199, 3432, -5304, 330, -706, 11, -27], [0, 812, 0, 611, 0, 94, 0, 4], [-17061, 13683, -10868, 6812, -1738, 894, -77, 35], [-6897, -2745, -3575, -1599, -517, -235, -22, -10]];
    Rf_basisdens := [1, 1, 52, 286, 1144, 143, 1144, 286];
    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_363_b();"
function MakeCharacter_363_b()
    N := 363;
    order := 2;
    char_gens := [122, 244];
    v := [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_363_b_Hecke();"
function MakeCharacter_363_b_Hecke(Kf)
    N := 363;
    order := 2;
    char_gens := [122, 244];
    char_values := [[-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 := 3;
    raw_aps := [[0, 1, 0, 0, 0, 0, 0, 0], [0, 0, -1, 1, 0, -1, 1, 0], [0, -1, 0, 1, 0, -1, 0, 1], [-5, 0, -2, 1, 0, -1, 0, -1], [0, 0, 0, 0, 0, 0, 0, 0], [-5, 0, -2, -3, 0, 3, 0, 3], [0, 5, 0, 2, -5, -4, 5, -3], [-5, 0, 9, 3, 2, -1, 2, -1], [0, -2, 0, 1, -6, -10, 6, -5], [0, 2, 0, 2, -2, -2, 2, 0], [-6, 0, -15, 0, 5, 5, 5, 5], [21, 0, 16, 9, 0, -9, 0, -9], [0, -10, 0, 6, 13, 14, -13, 19], [15, 0, 6, -2, 5, 7, 5, 7], [0, -23, 0, -4, 18, 15, -18, 14], [0, -7, 0, -6, 6, 15, -6, 0], [0, -11, 0, -7, -11, -12, 11, -18], [-15, 0, -17, 13, 11, -2, 11, -2], [17, 0, -21, -15, -12, 3, -12, 3], [0, -3, 0, 3, 2, 11, -2, 5], [56, 0, 18, -1, -3, -2, -3, -2], [1, 0, -48, 17, 8, -9, 8, -9], [0, -12, 0, 6, 7, 10, -7, 13], [0, -16, 0, -6, 5, 8, -5, -1], [-41, 0, 5, 10, 8, -2, 8, -2], [0, -36, 0, 21, 8, 2, -8, 29], [44, 0, 32, -10, 12, 22, 12, 22], [0, -34, 0, -22, 17, 4, -17, -5], [76, 0, -40, -4, 2, 6, 2, 6], [0, -18, 0, -5, -21, -59, 21, -26], [90, 0, -8, 22, 8, -14, 8, -14], [0, 21, 0, -36, -9, 6, 9, -45], [0, 64, 0, -10, -29, -28, 29, -39], [-74, 0, -45, 24, 29, 5, 29, 5], [0, -24, 0, -63, 64, 82, -64, 1], [-80, 0, -10, 50, 34, -16, 34, -16], [-40, 0, 26, -4, -12, -8, -12, -8], [114, 0, -43, -16, -20, -4, -20, -4], [0, 71, 0, 1, -30, -13, 30, -29], [0, -11, 0, 25, -40, -13, 40, -15], [0, 4, 0, 27, -7, 7, 7, 20], [-65, 0, -118, 7, -16, -23, -16, -23], [0, 44, 0, 63, -56, -34, 56, 7], [-144, 0, -7, -10, 15, 25, 15, 25], [0, 7, 0, 32, 8, -9, -8, 40], [-241, 0, -78, 27, 18, -9, 18, -9], [-168, 0, -43, 11, -12, -23, -12, -23], [83, 0, -22, 29, 6, -23, 6, -23], [0, 82, 0, -11, -41, -97, 41, -52], [-90, 0, -108, 72, 14, -58, 14, -58], [0, 17, 0, 70, -87, -151, 87, -17], [0, -51, 0, -40, 18, 29, -18, -22], [-44, 0, 176, -52, -37, 15, -37, 15], [0, 61, 0, 20, -36, 25, 36, -16], [0, -89, 0, 28, 53, 71, -53, 81], [0, -17, 0, -51, 100, 157, -100, 49], [0, -40, 0, 12, -20, -20, 20, -8], [-83, 0, -119, -83, -27, 56, -27, 56], [-228, 0, -111, -24, 7, 31, 7, 31], [0, 4, 0, 65, -135, -152, 135, -70], [86, 0, 118, 24, -38, -62, -38, -62], [0, -112, 0, 23, 28, 22, -28, 51], [-62, 0, -71, 97, 16, -81, 16, -81], [0, -64, 0, -15, 72, 140, -72, 57], [-99, 0, 70, -17, 39, 56, 39, 56], [0, -122, 0, -79, 102, 34, -102, 23], [151, 0, 218, 42, -47, -89, -47, -89], [-170, 0, 284, -10, -11, -1, -11, -1], [0, 14, 0, -92, -1, 19, 1, -93], [85, 0, 78, -93, -10, 83, -10, 83], [0, 26, 0, -32, 25, -57, -25, -7], [0, 4, 0, -36, 38, 118, -38, 2], [191, 0, 137, -103, -45, 58, -45, 58], [-119, 0, 80, -115, -82, 33, -82, 33], [-63, 0, -16, 98, 49, -49, 49, -49], [0, 82, 0, 11, -126, -134, 126, -115], [0, 45, 0, 32, -132, -139, 132, -100], [-61, 0, -100, 65, 26, -39, 26, -39], [0, -17, 0, -35, 95, 271, -95, 60], [-118, 0, 98, -24, -70, -46, -70, -46], [0, -103, 0, 168, -37, 35, 37, 131], [119, 0, -5, -167, -31, 136, -31, 136], [0, -49, 0, -40, -10, 69, 10, -50], [86, 0, -4, -46, -9, 37, -9, 37], [91, 0, 142, 79, 88, 9, 88, 9], [0, 6, 0, -155, 69, 28, -69, -86], [0, -106, 0, 146, 1, -73, -1, 147], [-254, 0, 169, 45, -18, -63, -18, -63], [0, -115, 0, -81, 32, -157, -32, -49], [-164, 0, -95, 26, 105, 79, 105, 79], [0, -160, 0, 91, 36, -4, -36, 127], [0, -121, 0, 130, -28, 37, 28, 102], [-63, 0, 52, 99, 126, 27, 126, 27], [0, -53, 0, -31, 9, 9, -9, -22], [239, 0, 35, 50, 12, -38, 12, -38], [0, 76, 0, 44, 28, 180, -28, 72], [0, 7, 0, 180, -148, -209, 148, 32], [0, 90, 0, -136, 27, 53, -27, -109], [-102, 0, -219, -24, 82, 106, 82, 106], [-189, 0, -25, -37, -117, -80, -117, -80], [290, 0, 325, 0, 58, 58, 58, 58], [0, -223, 0, 31, 74, 39, -74, 105], [0, 106, 0, -70, 43, 8, -43, -27], [0, 140, 0, 214, -243, -435, 243, -29], [-323, 0, -193, 41, -79, -120, -79, -120], [32, 0, 536, -24, -21, 3, -21, 3], [0, -28, 0, -49, 95, 13, -95, 46], [0, 55, 0, 121, -231, -220, 231, -110], [0, -132, 0, -162, 188, 92, -188, 26], [-299, 0, 30, -66, -139, -73, -139, -73], [-542, 0, -329, -66, 87, 153, 87, 153], [-150, 0, -214, 132, 36, -96, 36, -96], [0, 126, 0, -195, -51, -129, 51, -246], [85, 0, 14, 153, 137, -16, 137, -16], [188, 0, 327, -54, -89, -35, -89, -35], [0, 163, 0, 88, -89, -133, 89, -1], [-606, 0, -319, -7, 40, 47, 40, 47], [0, 347, 0, 90, -180, -261, 180, -90], [0, -197, 0, -114, 240, 169, -240, 126], [0, -208, 0, -52, 273, 247, -273, 221], [168, 0, -207, 8, -5, -13, -5, -13], [141, 0, -190, -169, -117, 52, -117, 52], [0, -122, 0, -82, 84, 76, -84, 2], [0, 46, 0, 95, 92, 54, -92, 187], [402, 0, -370, -112, -65, 47, -65, 47], [0, -9, 0, -145, 0, -25, 0, -145], [-64, 0, 117, -14, -83, -69, -83, -69], [0, 138, 0, -37, 132, 392, -132, 95], [-100, 0, -367, -130, -91, 39, -91, 39], [-494, 0, 194, 230, 118, -112, 118, -112], [675, 0, -280, 71, 45, -26, 45, -26], [0, 248, 0, -17, 26, 298, -26, 9], [-477, 0, 179, -71, -107, -36, -107, -36], [-851, 0, -232, 25, 26, 1, 26, 1], [0, -254, 0, -105, 81, -21, -81, -24], [163, 0, -201, -297, -135, 162, -135, 162], [0, 72, 0, -59, 210, 358, -210, 151], [288, 0, 168, 198, 151, -47, 151, -47], [0, 118, 0, 96, -24, 132, 24, 72], [0, -227, 0, 43, 19, 24, -19, 62], [-102, 0, 133, 215, 122, -93, 122, -93], [0, -186, 0, -23, 66, -62, -66, 43], [175, 0, 164, 103, 32, -71, 32, -71], [0, -102, 0, 120, 107, 299, -107, 227], [-424, 0, -747, 168, 37, -131, 37, -131], [0, 18, 0, 409, -186, -44, 186, 223], [619, 0, 54, -103, 68, 171, 68, 171], [0, 92, 0, 34, 25, 240, -25, 59], [-753, 0, -456, 83, -35, -118, -35, -118], [0, 322, 0, 44, -200, -58, 200, -156], [-569, 0, -826, -73, 64, 137, 64, 137], [0, 109, 0, 215, -91, -27, 91, 124], [92, 0, 26, -37, -45, -8, -45, -8], [0, 198, 0, 7, 156, 538, -156, 163], [387, 0, -133, 47, 22, -25, 22, -25], [0, -308, 0, -133, 216, -38, -216, 83], [177, 0, 304, 169, 146, -23, 146, -23], [0, -424, 0, -89, 279, 359, -279, 190], [-373, 0, 106, 149, 217, 68, 217, 68], [0, 413, 0, -94, -194, -109, 194, -288], [0, 127, 0, -175, -49, 96, 49, -224], [0, -236, 0, -283, 309, 84, -309, 26], [-599, 0, -1135, -43, 59, 102, 59, 102], [0, -10, 0, -254, 188, 366, -188, -66], [0, 8, 0, -347, 188, -20, -188, -159], [0, -244, 0, 34, 72, 66, -72, 106], [-79, 0, 847, -71, -41, 30, -41, 30], [67, 0, -191, 109, 195, 86, 195, 86]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_363_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_363_3_b_l();".
// 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_363_3_b_l(:prec:=8)
    chi := MakeCharacter_363_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_363_3_b_l();".
// 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_363_3_b_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_363_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![1331, 0, 1061, 0, 282, 0, 29, 0, 1]>,<5,R![2816, 0, 6576, 0, 1687, 0, 79, 0, 1]>,<7,R![-396, -138, 35, 14, 1]>],Snew);
    return Vf;
end function;