
// Make newform 1700.2.a.a in Magma, downloaded from the LMFDB on 04 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1700_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_1700_2_a_a();".
// 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_1700_2_a_a();".
// 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_1700_a();"
function MakeCharacter_1700_a()
    N := 1700;
    order := 1;
    char_gens := [851, 477, 1601];
    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_1700_a_Hecke(Kf)
    return MakeCharacter_1700_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], [0], [1], [1], [-4], [0], [-6], [-1], [-2], [0], [-2], [6], [-3], [-6], [-10], [4], [-3], [-2], [-1], [-12], [-6], [-8], [3], [-14], [9], [-10], [0], [-2], [3], [3], [5], [-3], [2], [-5], [1], [-12], [-12], [0], [-16], [0], [10], [-18], [8], [5], [22], [21], [-7], [18], [24], [-22], [24], [-3], [12], [6], [20], [-8], [21], [4], [6], [-8], [15], [28], [-12], [-10], [-20], [-9], [-1], [21], [0], [-17], [25], [11], [0], [30], [-8], [-12], [23], [-12], [-1], [33], [34], [17], [-36], [-18], [1], [-18], [22], [6], [36], [-32], [-12], [-13], [-24], [9], [-6], [28], [-46], [-35], [33], [0], [-6], [-37], [22], [24], [-21], [30], [20], [13], [-29], [-18], [-4], [14], [18], [-23], [-6], [30], [-48], [-13], [10], [12], [-51], [-19], [-6], [-34], [3], [-8], [13], [-34], [21], [-40], [34], [33], [-43], [39], [19], [-3], [6], [-31], [6], [37], [12], [-43], [-39], [-56], [54], [-22], [36], [-2], [-12], [28], [9], [28], [-21], [26], [-6], [-5], [24], [36], [-33], [-50], [0], [39], [-21], [-37], [28], [-22], [-36], [-48], [-46], [-15], [19], [32], [6], [-28], [48], [46], [29], [4], [-63], [-14], [-33], [-24], [-21], [-32], [-14], [-22], [-24], [-32], [12], [-34], [9], [-9], [-66], [14], [64], [-42], [48], [30], [8], [58], [-1], [0], [51], [56], [24], [48], [-10], [-50], [42], [-41], [48], [21], [44], [-50], [-33], [-57], [57], [17], [56], [45], [61], [-42], [2], [-12], [0], [-2], [-51], [1], [29], [-4], [39], [64], [24], [32], [-24], [57], [-24], [-21], [-22], [52], [-10], [54], [12], [-32], [72], [-22], [24], [-5], [-30], [-30], [26], [3], [36], [38], [28], [-30], [25], [40], [-60], [32], [-32], [-48], [-28], [-54], [30], [40], [66], [20], [-20], [-26], [-4], [-2], [-56], [-12], [-37], [5], [-18], [30], [41], [-51], [35], [-20], [-72], [-26], [30], [26], [39], [30], [36], [18], [33], [-14], [24], [50], [30], [-39], [-44], [-77], [-18], [59], [-75], [-40], [64], [42], [-22], [-36], [43], [51], [6], [-60], [16], [-36], [5], [-72], [0], [-2], [-21], [-28], [-38], [-66], [-20], [-66], [29], [-19], [-5], [-9], [-54], [-70], [-48], [5], [-54], [-4], [21], [-13], [36], [-40], [52], [-59], [6], [36], [14], [6], [-84], [62], [-74], [-63], [36], [56], [70], [-75], [-47], [-19], [-18], [-18], [-27], [-36], [-18], [-32], [66], [30], [-57], [-38], [-50], [-42], [-56], [2], [-48], [-40], [-9], [-81], [32], [58], [57], [69], [-14], [21], [37], [0], [63], [61], [-54], [41], [-48], [44], [67], [-11], [-24], [-70], [-36], [-6], [76], [-42], [-44], [20], [-90], [68], [15], [-58], [54], [-20], [-36], [6], [56], [7], [-66], [-56], [9], [4], [90], [90], [-52], [49], [12], [-39], [7], [-60], [-42], [-45], [-2], [75], [30], [-56], [-30], [15], [6], [38], [-48]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1700_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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