
// Make newform 300.4.d.c in Magma, downloaded from the LMFDB on 13 March 2026.
// To make the character of type GrpDrchElt, type "MakeCharacter_300_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_300_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_300_4_d_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_300_4_d_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 := [1, 0, 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_300_d();"
function MakeCharacter_300_d()
    N := 300;
    order := 2;
    char_gens := [151, 101, 277];
    v := [2, 2, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_300_d_Hecke();"
function MakeCharacter_300_d_Hecke( : Kf := false)
    N := 300;
    order := 2;
    char_gens := [151, 101, 277];
    char_values := [[1, 0], [1, 0], [-1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := ISA(Type(Kf), Fld), 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]];
    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, 0], [0, -3], [0, 0], [0, 13], [6, 0], [0, 5], [0, 78], [-65, 0], [0, 138], [-66, 0], [299, 0], [0, 214], [360, 0], [0, 203], [0, -78], [0, 636], [-786, 0], [467, 0], [0, 217], [-360, 0], [0, -286], [-272, 0], [0, 498], [0, 0], [0, 511], [-1812, 0], [0, -1708], [0, -1236], [1543, 0], [0, 1884], [0, -2072], [-2508, 0], [0, -1566], [196, 0], [1278, 0], [1385, 0], [0, 481], [0, -2815], [0, -1956], [0, 2382], [-4386, 0], [-2275, 0], [714, 0], [0, 1547], [0, -438], [-437, 0], [1625, 0], [0, 875], [0, 4680], [-1469, 0], [0, 3960], [-2652, 0], [5753, 0], [3588, 0], [0, 564], [0, 1248], [7266, 0], [3224, 0], [0, 151], [7566, 0], [0, 1469], [0, -306], [0, -4661], [2514, 0], [0, 6707], [0, -4632], [-988, 0], [0, 9079], [0, 5994], [286, 0], [0, -11466], [624, 0], [0, -8477], [0, -11257], [1213, 0], [0, -11076], [8016, 0], [0, -6851], [12636, 0], [-10829, 0], [6252, 0], [-15730, 0], [-10062, 0], [0, 707], [12493, 0], [0, -5928], [2028, 0], [0, -17030], [-1044, 0], [0, 9704], [0, 4194], [12174, 0], [0, 1651], [-6456, 0], [559, 0], [0, -14208], [8082, 0], [20502, 0], [0, 2069], [1553, 0], [0, -12584], [0, -20754], [0, 14370], [-15774, 0], [-3055, 0], [0, 25021], [0, 18828], [0, 19968], [-9684, 0], [23243, 0], [0, 22984], [0, 13754], [0, -9468], [28099, 0], [14807, 0], [20844, 0], [0, 3692], [0, 20004], [0, -17862], [-18552, 0], [-12382, 0], [0, -1690], [0, -5682], [0, 31512], [20648, 0], [-11874, 0], [10699, 0], [5598, 0], [0, -22859], [0, -31642], [12220, 0], [0, 17892], [-16648, 0], [0, -31733], [-10068, 0], [-24323, 0], [0, -2184], [0, 37531], [0, -28644], [17316, 0], [425, 0], [-25194, 0], [0, -33961], [0, 1956], [47302, 0], [-30726, 0], [0, -34489], [0, -8484], [520, 0], [0, -6864], [0, 38857], [-10080, 0], [0, -34549], [0, 18798], [0, -50276], [-20502, 0], [-37817, 0], [11778, 0], [0, 18409], [-9330, 0], [0, -49146], [0, -5928], [0, 9520], [12558, 0], [0, 46206], [0, -32676], [-8137, 0], [0, -18578]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_300_d_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_300_4_d_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_300_4_d_c(:prec:=2)
    chi := MakeCharacter_300_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_4_d_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_300_4_d_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_300_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![169, 0, 1]>],Snew);
    return Vf;
end function;