
// Make newform 3952.2.a.f in Magma, downloaded from the LMFDB on 10 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_3952_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_3952_2_a_f();".
// 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_3952_2_a_f();".
// 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_3952_a();"
function MakeCharacter_3952_a()
    N := 3952;
    order := 1;
    char_gens := [495, 2965, 2433, 3745];
    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_3952_a_Hecke( : Kf := Kf)
    return MakeCharacter_3952_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], [0], [2], [-2], [6], [-1], [1], [1], [3], [-6], [9], [-1], [3], [3], [6], [6], [-5], [7], [-3], [-8], [-6], [4], [-6], [10], [1], [15], [16], [18], [-1], [12], [16], [-7], [-14], [-9], [-24], [15], [3], [24], [13], [6], [2], [-12], [-15], [2], [18], [-3], [16], [-20], [-24], [16], [-19], [24], [11], [-12], [-6], [-12], [-16], [-4], [19], [-15], [-4], [15], [-11], [-16], [13], [14], [25], [12], [16], [16], [12], [2], [-1], [-28], [20], [7], [3], [-22], [-6], [-19], [5], [10], [11], [26], [12], [36], [-13], [-18], [36], [-4], [-19], [-18], [0], [-13], [-32], [24], [29], [-34], [20], [-10], [-8], [36], [-14], [-36], [-32], [-20], [-24], [-34], [-26], [-10], [22], [-26], [30], [-24], [-34], [-46], [-34], [20], [23], [34], [-35], [-30], [42], [-24], [-40], [-15], [-20], [-51], [23], [-38], [-4], [1], [-20], [-37], [26], [-4], [-1], [1], [-2], [29], [-23], [-12], [-40], [-23], [-6], [-9], [-26], [-28], [33], [41], [25], [15], [-11], [-24], [28], [-34], [31], [24], [-47], [58], [-26], [-56], [-10], [40], [21], [16], [62], [26], [-50], [-14], [-51], [-13], [24], [22], [46], [15], [18], [-20], [30], [-34], [-12], [-30], [-36], [63], [42], [6], [16], [-1], [-14], [-46], [0], [34], [0], [-39], [-5], [-64], [20], [-14], [-24], [-11], [18], [-10], [-33], [4], [14], [-2], [32], [18], [-36], [-28], [-43], [-53], [17], [12], [32], [-10], [-19], [-30], [3], [54], [-22], [36], [-43], [-4], [14], [6], [-48], [12], [-52], [60], [67], [36], [-36], [-30], [-29], [-10], [4], [-64], [15], [24], [61], [45], [-32], [-29], [71], [33], [56], [-31], [43], [-61], [48], [62], [12], [26], [-18], [0], [-37], [43], [-66], [80], [74], [42], [76], [38], [-56], [48], [43], [-10], [-47], [33], [-20], [28], [42], [-59], [68], [-68], [-54], [47], [-33], [-63], [-16], [-14], [32], [-85], [-66], [7], [-61], [54], [58], [42], [49], [-25], [-74], [-11], [10], [42], [-30], [27], [48], [-44], [-13], [-33], [-65], [78], [52], [22], [-72], [-50], [70], [-77], [-27], [-78], [38], [42], [2], [64], [-62], [-37], [2], [-11], [64], [2], [-24], [-81], [38], [20], [-57], [-19], [56], [-6], [-2], [30], [40], [56], [2], [88], [-34], [-30], [70], [-36], [71], [-33], [69], [2], [11], [-60], [53], [-36], [24], [30], [-20], [29], [18], [2], [78], [-12], [-8], [40], [-37], [-37], [12], [69], [-18], [11], [79], [76], [-24], [21], [38], [-53], [-42], [-6], [72], [32], [61], [21], [-27], [-34], [-69], [-52], [-63], [14], [38], [84], [5], [33], [-100], [-37], [56], [-100], [47], [57], [74], [4], [5], [2], [88], [-4], [22], [-32], [22], [-21], [-56], [-53], [-6], [-49], [0], [-37], [44], [10], [-16], [10], [72], [101], [-73], [45], [-33], [100], [48], [36], [-52], [69], [-50], [-76], [-54], [39], [50], [18], [56], [-1]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3952_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3952_2_a_f();".
// 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_3952_2_a_f(:prec:=1)
    chi := MakeCharacter_3952_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_3952_2_a_f();".
// 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_3952_2_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3952_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<5,R![-2, 1]>,<7,R![2, 1]>],Snew);
    return Vf;
end function;