
// Make newform 2300.2.a.h in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2300_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_2300_2_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_2300_2_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_2300_a();"
function MakeCharacter_2300_a()
    N := 2300;
    order := 1;
    char_gens := [1151, 277, 1201];
    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_2300_a_Hecke(Kf)
    return MakeCharacter_2300_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 := 2;
    raw_aps := [[0], [3], [0], [4], [2], [5], [-4], [-2], [-1], [-7], [-3], [-2], [-9], [8], [-9], [-2], [0], [-2], [-14], [-3], [3], [-6], [-8], [12], [0], [2], [8], [-2], [0], [20], [17], [-15], [12], [-1], [14], [13], [0], [5], [8], [18], [-25], [18], [2], [-17], [-9], [-10], [4], [16], [0], [-4], [-27], [-3], [-2], [8], [-7], [10], [13], [8], [11], [12], [26], [-16], [-8], [5], [-16], [-2], [11], [-4], [4], [-19], [31], [6], [-24], [34], [-32], [36], [24], [37], [-6], [-11], [24], [-20], [24], [-34], [-13], [-29], [-34], [40], [21], [8], [-22], [16], [-31], [-9], [37], [6], [-3], [-28], [16], [-15], [21], [-28], [-4], [26], [-4], [-33], [19], [-30], [-8], [13], [28], [-20], [30], [-8], [44], [-4], [-10], [-17], [-3], [-48], [-18], [-43], [-42], [9], [44], [-12], [-40], [24], [30], [-28], [-17], [-48], [24], [42], [-51], [-26], [52], [36], [10], [-30], [5], [18], [-45], [48], [-30], [-24], [26], [-3], [13], [-7], [-6], [36], [-12], [39], [38], [-46], [-16], [15], [38], [24], [3], [50], [13], [-14], [-18], [-6], [16], [-22], [-14], [-59], [-38], [37], [56], [8], [-13], [48], [23], [-21], [46], [-16], [-23], [44], [-13], [-18], [-14], [60], [34], [30], [29], [-32], [61], [4], [14], [10], [26], [-36], [50], [40], [-8], [41], [-12], [48], [-17], [-22], [-36], [23], [-10], [23], [-33], [-13], [21], [-65], [8], [-26], [40], [0], [11], [-3], [18], [-15], [39], [-50], [-3], [-36], [39], [15], [-38], [39], [-50], [-52], [-50], [44], [-14], [-46], [44], [-48], [-8], [26], [73], [-37], [-40], [-7], [-37], [-17], [-30], [-3], [7], [10], [34], [6], [-60], [-26], [-6], [64], [-18], [7], [-46], [14], [43], [7], [-44], [-12], [-70], [28], [-30], [-66], [2], [-28], [50], [31], [37], [-62], [-24], [60], [-33], [-33], [-12], [2], [42], [48], [-3], [26], [66], [22], [24], [0], [22], [58], [-64], [-66], [2], [-12], [-18], [30], [79], [18], [4], [21], [-21], [-39], [-24], [78], [52], [-11], [-4], [-79], [-60], [20], [-54], [-34], [-37], [-74], [0], [-59], [-38], [20], [-41], [61], [8], [79], [-32], [-16], [-29], [-16], [-6], [-48], [58], [77], [2], [64], [29], [-15], [-61], [21], [38], [85], [50], [52], [-49], [76], [41], [-40], [38], [46], [-46], [44], [11], [60], [6], [-50], [-15], [-13], [6], [-34], [80], [30], [-92], [-18], [-37], [-4], [-70], [-83], [25], [30], [53], [39], [49], [6], [-22], [19], [-12], [91], [60], [-56], [-3], [19], [-30], [-62], [-12], [9], [90], [66], [-4], [33], [20], [13], [-101], [82], [46], [-32], [-28], [-86], [81], [-52], [-24], [2], [-4], [-36], [20], [70], [-77], [11], [-24], [-44], [-17], [-63], [10], [-11], [-44], [-60], [-31], [-99], [-54], [-34], [-90], [-63], [0], [-44], [-48], [76], [-32], [0], [24], [84], [-85], [-47], [-42], [86], [-3], [65]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2300_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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