
// Make newform 1400.2.a.d in Magma, downloaded from the LMFDB on 30 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_1400_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_1400_2_a_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_1400_2_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_1400_a();"
function MakeCharacter_1400_a()
    N := 1400;
    order := 1;
    char_gens := [351, 701, 1177, 801];
    v := [1, 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_1400_a_Hecke( : Kf := Kf)
    return MakeCharacter_1400_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]];
    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], [-1], [0], [1], [-1], [1], [3], [-4], [-2], [-1], [-6], [-2], [-10], [0], [-9], [14], [6], [-4], [-10], [-16], [-10], [-11], [-4], [12], [19], [0], [-1], [-16], [-15], [-14], [-18], [6], [8], [-14], [-6], [13], [14], [-10], [15], [15], [20], [-2], [3], [-4], [6], [10], [1], [15], [7], [18], [0], [17], [2], [10], [-18], [-24], [-6], [-4], [18], [15], [21], [31], [25], [0], [-7], [-12], [28], [-34], [22], [8], [-9], [4], [-17], [-20], [-28], [-24], [9], [15], [13], [-28], [-30], [1], [31], [-6], [-30], [0], [27], [-18], [12], [16], [-27], [2], [38], [5], [23], [29], [10], [-8], [28], [-41], [-4], [36], [-24], [-6], [-44], [-31], [20], [-19], [7], [0], [1], [4], [-2], [-38], [-13], [-18], [15], [32], [24], [-21], [-28], [20], [-3], [4], [-48], [-41], [-17], [-50], [36], [9], [15], [-42], [17], [-8], [22], [-8], [13], [31], [27], [-51], [-42], [45], [10], [42], [-50], [54], [-14], [10], [-8], [-6], [-52], [16], [-16], [-12], [-14], [-12], [-27], [14], [-1], [-28], [-30], [-30], [32], [-18], [-40], [1], [40], [27], [17], [54], [20], [-36], [-21], [-14], [56], [16], [51], [-31], [-28], [-24], [4], [-36], [36], [-6], [24], [34], [-10], [-29], [25], [0], [14], [-48], [13], [54], [-36], [-7], [43], [-14], [23], [19], [6], [52], [2], [-8], [-32], [47], [10], [44], [50], [-8], [58], [30], [34], [-17], [-4], [64], [-22], [14], [-8], [26], [-10], [-32], [-51], [-60], [-65], [-22], [3], [11], [47], [36], [-68], [0], [-72], [5], [-25], [27], [50], [12], [29], [30], [-4], [20], [-13], [38], [21], [-20], [-56], [-60], [33], [4], [-12], [-48], [18], [4], [46], [-9], [-10], [8], [-47], [57], [-56], [-22], [22], [29], [-78], [30], [-31], [-28], [78], [-48], [-38], [-46], [-17], [-32], [-42], [67], [68], [30], [-63], [68], [-61], [-23], [-56], [10], [55], [-69], [-40], [36], [70], [-50], [1], [-83], [74], [-54], [-48], [-54], [-10], [-63], [-52], [-77], [47], [38], [47], [-12], [41], [42], [24], [76], [-24], [-74], [-48], [-33], [58], [-28], [4], [-12], [-12], [-25], [39], [-91], [-20], [-26], [-70], [54], [-8], [-34], [-11], [84], [34], [-34], [-74], [-26], [-26], [49], [36], [-5], [61], [-34], [12], [3], [-34], [-34], [-62], [-77], [18], [48], [-2], [-12], [54], [19], [10], [-40], [-27], [51], [29], [-38], [64], [-70], [8], [14], [-44], [-18], [-58], [15], [-28], [52], [-45], [44], [-19], [75], [64], [-18], [-61], [-8], [4], [-76], [3], [1], [-26], [37], [72], [50], [26], [-70], [74], [-33], [3], [-79], [-52], [45], [93], [27], [-95], [-76], [-13], [-40], [4], [6], [63], [10], [-60], [-46], [-54], [-77], [48], [20], [-50], [83], [-92], [-20], [71], [36], [90], [92], [50], [40], [3], [56], [-22], [-1], [5], [-38], [36], [36], [-99], [-33], [-2], [-22], [38], [-64]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1400_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1400_2_a_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_1400_2_a_d(:prec:=1)
    chi := MakeCharacter_1400_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_1400_2_a_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_1400_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1400_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<11,R![1, 1]>,<13,R![-1, 1]>],Snew);
    return Vf;
end function;