
// Make newform 1690.2.a.h in Magma, downloaded from the LMFDB on 03 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_1690_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_1690_2_a_h();".
// 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_1690_2_a_h();".
// 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_1690_a();"
function MakeCharacter_1690_a()
    N := 1690;
    order := 1;
    char_gens := [677, 171];
    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_1690_a_Hecke( : Kf := Kf)
    return MakeCharacter_1690_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 := [[1], [0], [1], [-3], [-3], [0], [-4], [7], [-4], [-8], [-10], [3], [-2], [6], [-1], [-9], [-4], [-14], [4], [6], [4], [10], [0], [1], [16], [0], [-1], [-18], [-2], [-8], [13], [13], [12], [-5], [-6], [-2], [-3], [8], [19], [-21], [4], [-14], [-6], [-24], [13], [-18], [13], [19], [-4], [-22], [-18], [2], [-25], [23], [8], [11], [24], [8], [19], [2], [-14], [9], [-14], [-24], [6], [31], [-20], [30], [-16], [-16], [0], [10], [0], [-6], [27], [-28], [12], [-21], [-39], [17], [12], [16], [-24], [16], [-6], [-18], [-15], [10], [-40], [-40], [20], [-28], [19], [-43], [-4], [23], [-6], [-31], [22], [10], [2], [-31], [24], [-11], [-37], [-10], [-2], [20], [26], [5], [21], [11], [-22], [-3], [-36], [-9], [16], [-7], [-11], [-36], [-14], [-8], [-50], [-2], [29], [-40], [8], [28], [35], [-1], [39], [-8], [-4], [-29], [21], [-2], [31], [-4], [2], [-10], [21], [-18], [-37], [30], [-28], [-26], [-14], [18], [-1], [-32], [-34], [5], [-26], [-13], [-10], [-12], [50], [-14], [14], [-24], [38], [-22], [-33], [-49], [-24], [-3], [-10], [-11], [-33], [6], [-44], [58], [-6], [26], [-10], [9], [-8], [-16], [-16], [-6], [52], [63], [-61], [48], [-45], [10], [5], [32], [-11], [10], [-42], [48], [-39], [-6], [26], [-14], [26], [1], [-28], [48], [-30], [2], [-34], [53], [-9], [14], [-8], [36], [41], [-11], [-32], [42], [69], [-14], [10], [-57], [40], [45], [-53], [65], [-4], [14], [59], [-20], [-58], [-12], [-6], [-30], [56], [-25], [-31], [49], [-8], [-51], [-56], [-17], [14], [10], [13], [-58], [-66], [-20], [-11], [10], [-54], [-64], [16], [21], [5], [69], [-22], [15], [-37], [54], [39], [15], [26], [42], [-21], [-42], [-65], [48], [4], [-2], [66], [56], [34], [63], [76], [54], [6], [68], [28], [-76], [-40], [8], [-6], [26], [-6], [-28], [-49], [60], [84], [68], [34], [10], [26], [-47], [38], [51], [60], [-60], [24], [-19], [-22], [18], [-22], [5], [36], [-44], [28], [38], [-52], [2], [-20], [34], [-72], [-14], [24], [-11], [21], [-10], [45], [4], [-57], [-11], [-40], [-30], [30], [-78], [4], [52], [72], [-64], [26], [22], [52], [-14], [59], [6], [80], [-51], [-44], [-60], [60], [0], [20], [92], [37], [41], [5], [-36], [-42], [20], [15], [-5], [60], [-62], [54], [21], [-43], [-52], [34], [-53], [52], [-42], [-84], [31], [-60], [9], [13], [15], [-24], [3], [-70], [-60], [58], [-37], [58], [12], [35], [-32], [72], [-84], [50], [39], [-50], [66], [21], [44], [-38], [-36], [51], [-72], [-47], [-63], [-58], [-38], [94], [-12], [-1], [-83], [-9], [-20], [8], [-86], [14], [70], [-68], [74], [-14], [-4], [24], [20], [38], [70], [18], [-99], [46], [1], [62], [-53], [-8], [1], [-50], [6], [-6], [-87], [82], [0], [-38], [-43], [-15], [21], [-44], [45], [-60], [23], [-25], [94]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1690_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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