
// Make newform 1694.2.a.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1694_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_1694_2_a_g();".
// 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_1694_2_a_g();".
// 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_1694_a();"
function MakeCharacter_1694_a()
    N := 1694;
    order := 1;
    char_gens := [969, 365];
    v := [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_1694_a_Hecke(Kf)
    return MakeCharacter_1694_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 := [[1], [0], [-4], [1], [0], [-2], [4], [6], [4], [2], [-2], [10], [-4], [8], [2], [6], [-12], [14], [-12], [-8], [-4], [0], [6], [-6], [-14], [-6], [18], [16], [14], [14], [-8], [-6], [6], [-14], [-2], [24], [-8], [4], [-4], [14], [4], [20], [-4], [-2], [-6], [-14], [8], [-2], [2], [20], [-30], [-16], [-12], [12], [-6], [0], [12], [20], [30], [10], [-6], [-18], [10], [14], [-2], [6], [-20], [18], [-8], [10], [-6], [16], [22], [10], [4], [-10], [-30], [24], [-18], [-16], [-32], [-2], [-16], [2], [-28], [-36], [-18], [-2], [-30], [-32], [0], [16], [-28], [36], [44], [36], [-28], [10], [34], [-14], [12], [-14], [34], [-6], [-28], [14], [-12], [12], [24], [-8], [-8], [-46], [18], [8], [-12], [-6], [4], [6], [10], [8], [20], [-22], [-26], [36], [36], [30], [18], [-26], [-10], [22], [12], [24], [28], [26], [48], [-16], [-48], [22], [-16], [30], [-38], [-18], [-4], [-20], [-20], [30], [-2], [-32], [28], [-44], [-34], [30], [-44], [-16], [-52], [-36], [-24], [-30], [-12], [14], [-4], [22], [-32], [56], [2], [18], [16], [42], [14], [20], [-24], [-40], [32], [26], [2], [-50], [28], [26], [12], [26], [48], [-14], [10], [8], [-32], [-36], [2], [0], [-14], [44], [-26], [-64], [20], [16], [-28], [-6], [34], [50], [-12], [12], [38], [-28], [16], [28], [-48], [-36], [-46], [-60], [30], [-40], [10], [16], [8], [68], [12], [-2], [-32], [-72], [48], [-46], [-6], [-4], [6], [-8], [18], [-10], [-26], [0], [-28], [40], [-22], [-50], [-16], [30], [-24], [-12], [-30], [-18], [-12], [-46], [-12], [62], [-6], [-34], [-72], [60], [-38], [52], [-36], [0], [-30], [28], [16], [10], [-74], [-48], [26], [-46], [-4], [-20], [-40], [-28], [30], [34], [-42], [-36], [-18], [-50], [20], [-62], [64], [4], [30], [-80], [-28], [62], [36], [34], [38], [54], [-24], [-40], [48], [-22], [-82], [-36], [-74], [-58], [66], [24], [-18], [-40], [66], [46], [38], [34], [-30], [4], [-14], [66], [-80], [-34], [-56], [-36], [-28], [-34], [28], [4], [-24], [-66], [48], [2], [10], [-12], [0], [-60], [28], [0], [-18], [-6], [70], [-22], [14], [-20], [-66], [-18], [-76], [-48], [72], [6], [18], [-46], [4], [-62], [20], [72], [58], [-24], [66], [0], [-10], [66], [70], [-20], [18], [-36], [-52], [-20], [0], [24], [-30], [38], [-50], [-80], [-90], [0], [34], [-70], [38], [0], [-74], [-40], [16], [84], [32], [-78], [-58], [56], [70], [84], [-64], [32], [10], [40], [-42], [0], [16], [40], [76], [-60], [32], [10], [16], [-50], [74], [94], [44], [12], [8], [98], [58], [-80], [-20], [20], [96], [-30], [-32], [-82], [20], [22], [78], [18], [-24], [-18], [-18], [-28], [-26], [-90], [-16], [64], [84], [42], [36], [20], [-94], [48], [-8], [-82], [-26], [88], [-38], [6], [40], [-82], [86], [96], [84], [66], [56], [80]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1694_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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