
// Make newform 300.3.c.d in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_300_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_300_c_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_300_3_c_d();".
// 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_300_3_c_d();".
// 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 := [16, -8, -8, 2, 3, 1, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [8, 24, -8, -17, 7, 4, 3, -1], [8, 24, 8, -17, -9, 4, 3, -1], [0, 8, 0, -1, -1, 4, 3, -1], [-72, 24, 40, 5, -3, -4, 1, 5], [56, 24, -24, -5, 3, 4, -1, -5], [-40, -8, 8, 5, -3, -4, 1, 5], [136, 24, -48, -19, 13, 20, -7, -11]];
    Rf_basisdens := [1, 16, 16, 8, 16, 16, 16, 8];
    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_300_c();"
function MakeCharacter_300_c()
    N := 300;
    order := 2;
    char_gens := [151, 101, 277];
    v := [1, 2, 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_300_c_Hecke();"
function MakeCharacter_300_c_Hecke(Kf)
    N := 300;
    order := 2;
    char_gens := [151, 101, 277];
    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 := 3;
    raw_aps := [[0, 0, 0, 0, 0, -1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, -2, 0, 0, 1, 0, 3, 0], [1, 1, 1, 1, -1, -2, -1, -1], [-3, -1, -2, 0, -2, 1, 0, -1], [-3, -1, 4, -2, 0, 3, 2, -1], [-3, -1, 1, 1, -2, 2, 8, 3], [-3, 7, -1, -1, -4, 4, 2, 3], [12, 0, -5, -3, -4, -11, -4, 0], [3, 9, -5, -5, 2, 2, 0, -3], [21, 3, -8, 6, 2, -5, -4, 3], [-4, -2, -2, -2, -4, 0, 0, -2], [4, 12, 0, 0, -4, -4, 8, -4], [-3, -5, 7, 7, -6, -4, -4, 3], [-46, -2, -3, -1, -4, 1, 0, -2], [1, 1, 5, 5, -5, -6, 3, -1], [-12, 2, -8, -4, -4, -22, -8, 2], [2, 2, -10, -10, 10, 8, -10, -2], [2, 18, -2, -2, -6, 0, 2, -2], [16, -6, 8, -4, -4, 18, 8, -6], [17, 3, 1, 1, 6, -18, -24, -17], [10, 18, -14, -14, 10, 4, -10, -10], [12, 2, 24, -20, 4, -22, 0, 2], [-70, -4, 20, -4, 4, 24, 12, -4], [-40, -4, 7, 1, 0, 21, 8, -4], [8, -10, 12, 12, -3, -20, -9, -8], [12, -16, 8, 8, 6, -20, 22, -12], [26, -4, 22, 2, 8, 38, 16, -4], [61, 11, -14, 8, 8, -31, -14, 11], [-4, -6, 16, 16, -15, -12, 3, 4], [-7, -35, 5, 5, 9, 2, 29, 7], [63, 1, -20, 2, -8, -19, -10, 1], [9, -25, 5, 5, 12, -14, 82, -9], [-80, 0, -7, -1, -4, -9, -4, 0], [-7, -41, 9, 9, 8, -2, 30, 7], [-13, -7, -16, 26, -2, 57, 12, -7], [10, -46, 10, 10, 18, -20, 62, -10], [5, 47, -17, -17, -4, 12, -54, -5], [44, 12, -1, -15, 4, -67, -20, 12], [-25, 19, 3, 3, -25, 22, 43, 25], [-86, 4, 42, -18, 16, -6, 8, 4], [10, 2, 2, 2, 2, -12, -62, -10], [160, -6, 28, -16, 0, 14, 12, -6], [-154, -6, 5, 7, 0, 37, 12, -6], [-15, -9, 25, 25, -28, -10, -22, 15], [-7, -17, 5, 5, 0, 2, -90, 7], [-26, -32, -2, -2, 5, 28, -37, 26], [-20, 4, -20, -20, 8, 40, 44, 20], [-32, -22, 46, -26, -12, 60, 32, -22], [-105, -11, 18, 4, 0, 59, 22, -11], [-12, -40, -8, -8, 22, 20, -38, 12], [86, 20, -64, 40, 8, -44, -32, 20], [21, -55, -3, -3, 41, -18, 29, -21], [-59, 11, -2, 28, 24, 21, 2, 11], [27, 41, -39, -39, 32, 12, -166, -27], [-132, 8, -33, 41, 12, 25, -4, 8], [-15, 7, -23, -23, 12, 38, 58, 15], [35, -11, 22, -20, -10, 15, 12, -11], [-100, -14, 20, -24, -16, 14, 12, -14], [10, -14, 38, 38, -26, -48, 98, -10], [50, -2, 41, -21, 8, 5, 12, -2], [46, 2, 14, 14, 8, -60, -88, -46], [-36, 0, 16, 16, -34, 20, 106, 36], [-188, 6, 56, -4, 32, 30, 20, 6], [-134, 14, 13, 23, 32, 17, 4, 14], [51, 81, -49, -49, 34, -2, -32, -51], [-80, 2, -12, 0, -4, -18, -8, 2], [-22, -42, -2, -2, 12, 24, 36, 22], [184, 14, -4, -48, -12, -142, -40, 14], [39, -3, -42, -16, -32, -65, -26, -3], [-20, 20, 32, 32, -52, -12, -268, 20], [-10, 0, -14, -14, 9, 24, -73, 10], [-37, -23, 36, -74, -42, -43, 4, -23], [23, 97, -21, -21, -16, -2, 18, -23], [29, 27, 23, 23, -22, -52, -92, -29], [-72, 24, 29, 19, 48, -5, 0, 24], [293, 11, -56, 46, 6, 3, -16, 11], [-50, 24, 18, -26, 20, -106, -28, 24], [-22, 24, -72, 40, 8, -64, -40, 24], [-7, 21, 25, 25, -39, -18, 189, 7], [-32, -6, -90, 62, -20, 52, -8, -6], [32, 88, -4, -4, -24, -28, 56, -32], [-88, 10, -44, -16, -20, -106, -40, 10], [-31, -113, -7, -7, 48, 38, -98, 31], [44, 20, -12, -12, 24, -32, 196, -44], [-206, -52, 62, -38, -40, 142, 64, -52], [-142, -4, 152, -56, 44, 52, 52, -4], [20, 12, 29, -29, 12, -65, -12, 12], [-6, 80, 30, 30, -73, -24, -247, 6], [36, -16, 24, 24, 2, -60, -70, -36], [30, 30, -22, -22, 22, -8, -10, -30], [-60, -58, 0, 0, -1, 60, 149, 60], [3, 147, -89, -89, 17, 86, -175, -3], [-73, 5, -5, -5, -34, 78, 340, 73], [-63, 15, 19, 19, -58, 44, -16, 63], [28, -40, 43, -27, -32, 109, 48, -40], [-102, -20, -14, 22, -16, 90, 24, -20], [-72, 28, 24, 24, -74, 48, 126, 72], [50, -4, 82, 6, 40, 106, 48, -4], [28, 28, 0, 0, 0, -28, -100, -28], [32, -24, 139, -67, 12, 77, 60, -24], [34, 54, -58, -58, 48, 24, -72, -34], [730, -12, -30, -18, -36, -30, -12, -12], [-1, -91, 123, 123, -78, -122, 184, 1], [-578, -40, -96, 72, -52, 168, 28, -40], [-32, 108, 12, 12, -82, 20, 86, 32], [-255, -17, 130, 16, 56, 213, 90, -17], [56, 80, 52, 52, -64, -108, -248, -56], [-46, 24, 68, 92, 104, 180, 56, 24], [-4, 70, 12, 12, -49, -8, -339, 4], [105, 47, 72, -74, 46, -217, -48, 47], [519, -3, 22, -64, -24, -97, -18, -3], [-35, 155, -55, -55, -40, 90, -210, 35], [33, 107, -23, -23, -14, -10, 36, -33], [168, 26, -118, 98, 16, 0, -36, 26], [70, -218, 30, 30, 114, -100, 150, -70], [11, -35, 89, 89, -66, -100, 308, -11], [-132, -28, 95, -7, 16, 165, 72, -28], [35, -61, 95, 95, -47, -130, 137, -35], [408, -2, -20, 48, 12, 82, 16, -2], [-156, 38, -156, 200, 60, 130, -16, 38], [-724, -44, -41, 33, -48, 157, 40, -44], [-8, -148, 20, 20, 50, -12, 170, 8], [-89, -147, -37, -37, 66, 126, 8, 89], [136, -20, 63, 121, 72, 365, 112, -20], [-172, 30, -34, 142, 84, 160, 24, 30], [62, -118, 6, 6, 84, -68, 16, -62], [12, 86, 24, 24, -61, -36, 113, -12], [315, 5, 114, -128, -2, -157, -12, 5], [19, -247, 7, 7, 126, -26, 76, -19], [67, 5, 49, 49, -18, -116, -452, -67], [51, -215, 35, 35, 98, -86, -176, -51], [-49, -7, 250, -144, 46, -17, 60, -7], [162, 44, 22, -126, -8, -362, -96, 44], [-334, 16, 152, -152, 16, -200, -16, 16], [746, 30, -115, 71, 8, -63, -52, 30], [-64, 20, -64, -64, 22, 128, 86, 64], [-632, 32, -13, -59, -4, -227, -68, 32], [792, 58, 186, -126, 88, -240, -28, 58], [53, -165, -47, -47, 156, -6, 274, -53], [-192, 36, 99, -83, 44, -175, -28, 36], [-30, 24, 106, 106, -133, -76, 45, 30], [104, 0, 24, 24, 28, -128, 240, -104], [162, -44, 6, -46, -64, 46, 24, -44], [-60, 36, -56, -56, 8, 116, -328, 60], [-123, 67, -84, -38, 6, -361, -128, 67], [801, 35, -36, -34, 0, -209, -70, 35], [-61, -251, 87, 87, 8, -26, 114, 61], [113, -45, -21, -21, 100, -92, 2, -113], [347, 9, 44, 14, 38, 45, 20, 9], [-8, -122, 108, 32, -52, 538, 192, -122], [-54, 90, -38, -38, -34, 92, -62, 54], [-155, -57, -89, -89, 40, 244, 318, 155], [-26, -238, 106, 106, 0, -80, 264, 26], [-50, -46, 18, 18, -20, 32, -296, 50], [27, -167, 83, 83, 14, -110, 300, -27], [-178, -16, -206, 254, 8, 350, 40, -16], [-546, -56, 12, 4, -48, 188, 64, -56], [-1188, 4, -31, -41, -32, -125, -40, 4], [-18, 42, -38, -38, 8, 56, -576, 18], [-169, -47, -224, 30, -144, -23, -50, -47], [42, 84, -70, -70, 49, 28, 31, -42], [105, 213, -15, -15, -39, -90, 469, -105], [781, 119, -168, 26, 48, -473, -190, 119], [-41, 121, -11, -11, -70, 52, 312, 41], [-25, -7, 95, 95, -104, -70, -238, 25], [471, -75, 112, -182, -110, -27, 40, -75]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_300_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_300_3_c_d();".
// 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_300_3_c_d(:prec:=8)
    chi := MakeCharacter_300_c();
    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_300_3_c_d();".
// 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_300_3_c_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_300_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<7,R![65536, 0, 188416, 0, 12032, 0, 224, 0, 1]>,<13,R![-12464, -5792, -472, 8, 1]>],Snew);
    return Vf;
end function;