
// Make newform 3520.2.a.w in Magma, downloaded from the LMFDB on 26 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_3520_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_3520_2_a_w();".
// 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_3520_2_a_w();".
// 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_3520_a();"
function MakeCharacter_3520_a()
    N := 3520;
    order := 1;
    char_gens := [2751, 1541, 2817, 321];
    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_3520_a_Hecke( : Kf := Kf)
    return MakeCharacter_3520_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], [-1], [1], [1], [2], [-5], [-7], [6], [1], [5], [-11], [2], [4], [-6], [1], [-10], [-5], [-8], [-9], [-6], [10], [-6], [17], [16], [2], [-12], [-12], [-10], [-12], [8], [-19], [8], [4], [3], [6], [-3], [5], [-23], [2], [-24], [14], [-20], [-17], [8], [15], [-27], [10], [14], [14], [3], [18], [26], [-2], [-14], [-21], [-24], [-20], [24], [-2], [18], [10], [6], [-11], [22], [-9], [-12], [-3], [10], [22], [-2], [-8], [32], [-22], [-22], [-34], [-6], [-22], [-5], [-12], [-4], [32], [-12], [0], [20], [36], [26], [-15], [15], [10], [9], [12], [-4], [-7], [-28], [24], [12], [-18], [32], [45], [-20], [14], [14], [-36], [31], [14], [19], [18], [-11], [30], [43], [-2], [24], [12], [-1], [-25], [-11], [-2], [-49], [-5], [-22], [-33], [32], [47], [-10], [-5], [-42], [-31], [-26], [24], [-32], [9], [41], [10], [-2], [-10], [29], [-4], [-2], [-42], [19], [-18], [12], [-24], [16], [24], [34], [51], [-2], [-6], [58], [-14], [-41], [8], [-23], [19], [16], [19], [10], [53], [-13], [29], [-1], [-32], [32], [6], [48], [-32], [2], [-19], [57], [-42], [-18], [39], [-8], [-61], [-52], [16], [43], [63], [18], [-19], [-29], [-41], [22], [-30], [-12], [-28], [14], [-24], [8], [8], [-34], [-44], [-40], [-42], [20], [30], [22], [35], [-39], [-10], [11], [-8], [8], [-3], [-56], [-16], [50], [-8], [33], [0], [48], [-45], [-32], [19], [51], [26], [-64], [35], [-58], [-46], [45], [-20], [72], [59], [-54], [-69], [-64], [60], [30], [-61], [-50], [-38], [36], [-45], [-53], [-6], [-56], [65], [-29], [-4], [32], [-18], [-66], [-54], [0], [-30], [37], [11], [22], [-60], [-18], [-29], [76], [-27], [-24], [-18], [-6], [37], [-24], [12], [15], [-62], [0], [28], [-8], [9], [-64], [-54], [30], [-17], [40], [-82], [43], [-46], [-48], [26], [-70], [-33], [-31], [-44], [-56], [83], [56], [21], [2], [42], [-37], [-18], [-4], [-60], [-17], [11], [26], [-78], [-56], [-21], [4], [62], [49], [-60], [-24], [-36], [-50], [-64], [-4], [-40], [9], [-56], [-84], [-90], [88], [45], [17], [-58], [38], [84], [-78], [60], [3], [-38], [10], [14], [5], [-21], [-38], [-44], [-83], [44], [-35], [62], [-44], [-10], [-23], [-71], [-2], [-21], [77], [53], [54], [54], [69], [-49], [-39], [63], [28], [-53], [-24], [7], [85], [-2], [60], [-61], [-53], [-75], [-71], [-69], [-6], [48], [10], [68], [68], [-29], [-43], [72], [-46], [53], [36], [-20], [56], [0], [75], [62], [-22], [0], [2], [59], [28], [-47], [-28], [82], [20], [-67], [-56], [80], [16], [-5], [-40], [-54], [-35], [47], [12], [72], [24], [54], [-6], [7], [34], [51], [69], [-14], [28], [0], [-90], [38], [-25], [-82], [47], [70], [14], [-8], [-36], [28], [50], [64], [-40], [92], [-30], [-35], [-96], [64], [-24], [33], [-10], [-57], [11], [20], [52], [90]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3520_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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