
// Make newform 300.3.c.g 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_g();".
// 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_g();".
// 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, -12, 55, -84, 50, 0, -7, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-5669, 20620, -20680, 291, 3897, -16, -599, -73], [3582, -21000, 27350, -14163, -381, 1968, 32, -291], [-1399, 17810, -30040, 17961, 157, -2506, -24, 347], [-1399, 16780, -30040, 17961, 157, -2506, -24, 347], [2372, -19070, 34050, -23728, 914, 3308, -143, -486], [-5953, 44700, -72710, 45672, -606, -6302, 132, 924], [-5363, 45870, -85630, 57082, -1446, -8062, 162, 1134]];
    Rf_basisdens := [1, 515, 515, 515, 515, 515, 515, 515];
    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, -1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1, -1], [-1, -1, -2, -2, 1, -1, -1, 0], [2, -1, 0, 4, 1, 1, 0, -1], [-2, 2, -5, 1, -6, -2, -2, -1], [-1, -1, 2, 2, -7, -4, 1, -2], [1, 1, 4, 4, -5, 5, -1, -4], [-6, 2, -7, -1, -2, -2, 0, -5], [-4, -4, -4, -4, -4, -7, 3, 3], [-24, 2, -10, 2, -2, -2, 0, -8], [-4, 2, 5, -9, -6, -2, -2, 9], [3, 3, 2, 2, 5, -8, 3, 4], [2, 2, 8, 8, -10, -12, -4, -10], [-38, 0, 1, 7, -8, 0, -4, 5], [4, 4, 0, 0, 12, 2, -4, 0], [24, -7, 14, -2, 23, 7, 8, -1], [1, 1, 14, 14, -25, 14, 1, -12], [5, 5, 0, 0, 15, -1, -3, 2], [20, 10, -18, -14, -18, -10, -4, -4], [2, 2, 12, 12, -18, -6, 0, -10], [-4, -4, 4, 4, -20, -18, -12, -20], [16, 0, -4, -4, 8, 0, 4, -8], [35, -8, 4, 12, 24, 8, 8, -12], [18, -4, 7, 25, -12, 4, -8, 11], [0, 0, -20, -20, 40, 8, -14, 6], [7, 7, -2, -2, 25, 33, -5, 4], [-58, -7, 14, -2, 23, 7, 8, -1], [16, -4, 8, 16, -4, 4, -4, 8], [-10, -10, 0, 0, -30, -18, 18, 8], [7, 7, 2, 2, 17, 33, -13, -8], [86, 8, 6, -30, -16, -8, -4, 18], [-8, -8, -8, -8, -8, 60, -2, -2], [44, -2, 6, 10, -6, 2, -4, 8], [-12, -12, 16, 16, -68, -45, -1, -29], [-16, -17, 46, 6, 33, 17, 8, 21], [17, 17, -2, -2, 55, -20, 1, 20], [-1, -1, -24, -24, 45, -47, -5, 18], [-34, -2, 13, -29, 26, 2, 12, -1], [-6, -6, -8, -8, -2, 4, -10, -8], [-28, 3, -28, 16, -3, -3, 0, -25], [1, 1, 0, 0, 3, -151, 7, 8], [-49, -10, 34, 14, 2, 10, -4, 28], [-32, 10, 14, -62, -2, -10, 4, 20], [-10, -10, -16, -16, 2, -23, -3, 3], [-17, -17, 2, 2, -55, 64, 5, -14], [8, 8, -8, -8, 40, 47, -13, 3], [-19, -19, -26, -26, -5, 47, 29, 36], [-10, 5, -2, -2, -21, -5, -8, 11], [204, -18, 15, 13, 62, 18, 22, -25], [5, 5, -28, -28, 71, -53, -11, 22], [-129, -2, 0, 24, -14, 2, -8, 6], [6, 6, -24, -24, 66, 150, -26, 4], [92, -28, 34, 46, 60, 28, 16, -10], [9, 9, -24, -24, 75, 147, 1, 34], [-90, -6, 33, -25, 22, 6, 8, 19], [-4, -4, 20, 20, -52, -136, 2, -22], [-4, 5, -16, -36, 27, -5, 16, -27], [210, -14, -15, 83, 2, 14, -6, -23], [-11, -11, 6, 6, -45, -92, -13, -30], [-120, -2, -24, 8, 26, 2, 12, -38], [25, 25, 30, 30, 15, 180, -17, -22], [8, 8, -16, -16, 56, -66, 18, 42], [-7, 4, -6, -2, -12, -4, -4, 2], [16, -4, -22, 46, -4, 4, -4, -22], [10, 10, 28, 28, -26, 218, 6, -12], [-303, 18, -26, 26, -90, -18, -36, 28], [20, 20, 60, 60, -60, 90, 12, -28], [6, -8, -52, 84, 8, 8, 0, -60], [10, 42, -73, -99, -38, -42, 2, -33], [1, 1, 72, 72, -141, 75, 13, -58], [44, 44, 36, 36, 60, -67, 15, 23], [-88, 3, 0, 4, -19, -3, -8, 11], [15, 15, 34, 34, -23, 126, -1, -20], [-2, -2, -28, -28, 50, 30, -6, 20], [314, 4, -31, 31, -20, -4, -8, -19], [108, -1, -44, 64, -15, 1, -8, -37], [204, -30, 91, -39, 98, 30, 34, 27], [-155, 12, -2, -6, -52, -12, -20, 30], [23, 23, -6, -6, 81, -15, -9, 20], [-76, 6, 14, 42, -86, -6, -40, 60], [-17, -17, 8, 8, -67, -165, -43, -68], [-329, -22, -6, 54, 62, 22, 20, -48], [-2, -2, -64, -64, 122, 23, -37, 25], [42, 42, 28, 28, 70, 54, -14, 0], [-268, -20, -18, 42, 76, 20, 28, -66], [-12, 10, -72, 0, 22, -10, 16, -78], [-38, -8, 53, 19, -32, 8, -20, 65], [36, 36, 84, 84, -60, 168, -2, -50], [21, 21, -6, -6, 75, 237, -59, -32], [-2, -2, 4, 4, -14, -312, 0, -6], [-12, -12, -48, -48, 60, -327, -23, 13], [-28, -28, -28, -28, -28, 172, -36, -36], [47, 47, 66, 66, 9, -16, -89, -108], [-19, -19, 44, 44, -145, 103, -27, -90], [252, 50, -54, -74, -122, -50, -36, 32], [282, 26, -79, -21, -30, -26, -2, -51], [-21, -21, -54, -54, 45, 26, -43, -10], [16, 35, -36, -56, -83, -35, -24, 23], [0, 0, -40, -40, 80, 96, 86, 126], [-96, -22, 76, -4, 38, 22, 8, 46], [55, 55, 30, 30, 105, -177, -33, -8], [-314, -42, 69, 63, 78, 42, 18, 9], [33, 33, -50, -50, 199, 114, -23, 60], [-171, 36, -8, -136, -36, -36, 0, 28], [-45, -45, -70, -70, 5, -131, -45, -20], [192, -58, 137, 75, 78, 58, 10, 69], [14, 14, -48, -48, 138, 14, -50, 12], [-21, 52, -30, -74, -156, -52, -52, 74], [42, 42, 28, 28, 70, 138, -28, -14], [124, -54, 128, -88, 230, 54, 88, -14], [-68, -34, 133, -97, 134, 34, 50, 49], [-17, -17, -102, -102, 153, 208, 1, 86], [50, 50, 68, 68, 14, -93, -47, -65], [96, -8, 22, 18, 0, 8, -4, 18], [-24, -24, 104, 104, -280, -116, 98, -30], [40, 40, -40, -40, 200, -108, 32, 112], [-432, 58, 4, -132, -162, -58, -52, 114], [-17, -17, -46, -46, 41, -19, 79, 108], [232, 50, -104, -144, -2, -50, 24, -78], [-420, 30, -42, -166, 58, -30, 44, -56], [138, -58, 133, 75, 82, 58, 12, 63], [-68, -68, -12, -12, -180, 84, 88, 32], [46, 46, 60, 60, 18, 26, -42, -56], [-278, -90, 129, 79, 242, 90, 76, -37], [274, -83, 134, 70, 211, 83, 64, -13], [-69, -69, 40, 40, -287, -153, 1, -108], [-12, -12, -168, -168, 300, -141, -65, 91], [622, 24, -4, -28, -88, -24, -32, 52], [-28, -28, -32, -32, -20, -264, 40, 44], [44, 44, -44, -44, 220, 192, -116, -28], [22, 22, -32, -32, 130, 46, 110, 164], [-164, -55, 126, -50, 199, 55, 72, -1], [452, -40, 120, 40, 40, 40, 0, 80], [347, -44, 154, 62, 4, 44, -20, 130], [-690, 28, -57, -71, -12, -28, 8, -37], [13, 13, 62, 62, -85, 212, -83, -132], [-136, -8, 6, -54, 88, 8, 40, -42], [-740, -42, 77, 31, 102, 42, 30, 5], [29, 29, 30, 30, 27, 130, 75, 74], [-364, 30, -128, -48, 26, -30, 28, -126], [-46, -46, -124, -124, 110, 89, -93, -15], [3, 3, -38, -38, 85, -371, -45, -4], [-232, 66, -116, -68, -146, -66, -40, -10], [35, 35, -124, -124, 353, 355, 9, 168], [246, 33, -126, 154, -193, -33, -80, -13], [264, -72, 54, 170, 136, 72, 32, -50], [-42, -42, -92, -92, 58, 114, -46, 4], [30, 30, 100, 100, -110, 66, 90, 20], [-330, 61, -54, -62, -189, -61, -64, 71], [-144, 36, -134, 94, -140, -36, -52, -46], [77, 77, 38, 38, 155, -150, -33, 6], [-70, -70, -20, -20, -170, -80, 36, -14], [44, 44, 16, 16, 100, -16, 52, 80], [-18, -18, -124, -124, 194, 64, -104, 2], [28, 28, 76, 76, -68, 483, 53, 5], [-406, 62, -217, 109, -202, -62, -70, -85], [105, 48, -186, 82, -136, -48, -44, -94], [216, 58, -104, -40, -146, -58, -44, -2], [-31, -31, -94, -94, 95, 5, -99, -36], [600, -64, 4, 12, 304, 64, 120, -180], [-14, -14, 124, 124, -290, -406, 12, -126], [-78, -78, -96, -96, -42, -272, 90, 108], [622, 12, -66, -94, 100, -12, 56, -110], [20, 20, 100, 100, -140, -72, 92, 12], [-134, -134, -124, -124, -154, -97, 13, 3], [-262, -16, -108, 92, 96, 16, 40, -164]];
    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_g();".
// 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_g(: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_g();".
// 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_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_300_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<7,R![961, 0, 9532, 0, 6470, 0, 188, 0, 1]>,<13,R![-1559, 3916, -418, -4, 1]>],Snew);
    return Vf;
end function;