
// Make newform 7.12.a.b in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_7_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_7_12_a_b();".
// 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_7_12_a_b();".
// 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
        poly := [-4704, -818, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-1, 2, 0], [-546, -5, 1]];
    Rf_basisdens := [1, 1, 6];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_7_a();"
function MakeCharacter_7_a()
    N := 7;
    order := 1;
    char_gens := [3];
    v := [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_7_a_Hecke(Kf)
    return MakeCharacter_7_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 := 12;
    raw_aps := [[26, 0, 1], [-47, -11, 10], [1735, 177, 2], [-16807, 0, 0], [-345566, -5082, 7436], [-629447, 2331, -9450], [5259228, 41898, -61548], [4108411, -48081, 32958], [2071536, -553728, -508416], [-41787380, 1978326, -487060], [54294270, -1586946, 2285244], [190636332, 4496814, 290844], [291582704, -10082058, -8852564], [153695726, -5533374, 6422724], [906078186, -16537302, 10946196], [45042798, 35890512, 5811360], [1959822699, 18634647, -38340978], [727406287, 161957553, -86546430], [-8984462204, 167269200, -93370464], [5461196988, -137467932, -42991032], [1915034550, -21412500, -270287784], [-18218095492, 83144796, 157585464], [41304138687, 178290147, 172922022], [-30235729070, 773412936, 167810960], [-24166196124, -661659054, -599814684], [72111615431, 309419505, -1171911038], [-8180230990, -1827865590, 390054996], [-132226410080, 392992404, -937315672], [84234027888, 3042880218, -1456433676], [-54290403032, 1659208110, 227057180], [119702290332, 1656075540, -6780770136], [-196097119037, -9662016201, 3418037198], [420244944318, -6786006780, 7540868232], [-127424165211, 3307695153, 4682982402], [-535916099910, -15611140692, 336198360], [-164089866868, 3722902764, -22681339560], [-653043488879, 23471273955, -1527381594], [90192229342, -19135578966, 9901843092], [800967009094, 21643637358, -3522232420], [1309545468965, -21644699865, -9581807954], [-529354364200, -50160396300, 12898056040], [248918449633, -25726178037, 11947840758], [2265577677172, -4563686988, 41867032040], [387373249868, 75351936114, -32994095580], [-633125970230, -88389474180, -33719275912], [56802953454, 3507677190, -23763662388], [3741543601084, -52195662792, -54519517584], [8797088070316, -84198570372, -6187547016], [10333997460411, 138362649183, -40981019874], [922360719293, 48105098295, 197840240334], [9177363388002, -138747603960, 66662809872], [4528408983408, 271009528104, -165470333616], [357633697388, -76420047414, 26108306388], [5568588410245, 237625359825, -81811240126], [-13107434964982, 91810979304, -461932640048], [-1689719291012, 448962948, 141791843720], [-21389523337131, -374571366537, 292866695502], [621668195224, -538891312104, 399040574256], [8391411729214, -3179456784, -122848872480], [-2244507584422, -679919864400, 434741063008], [-14871527178149, 854336331831, -78442203186], [23622826351187, 593500201245, -436739160998], [-64976890009997, -142715563305, 2886032142], [22746808515216, -503803843848, -667508548368], [13670479315520, 336550849014, 112487277996], [51173736639282, 90055294812, 849247756344], [32450841584222, -2163116622030, -290936451420], [24348606301588, -479314157910, 1545934625172], [-1820857513738, 2438813680506, -1444975684556], [-15094374880571, -1990402079337, 1245705478542], [68988496168594, -218765475072, 1576103418368], [-36643799261428, -1197586115124, 207296181016], [-19677009884124, 4049165150628, 213189059016], [-57756087309286, -1859698435476, 1143545369688], [107795072900202, 3041280049710, -2155585987044], [52461056728842, 2515212177018, 2214874745652], [-155856210214044, 680323577094, 1069208450508], [-135395753285451, -1616292847209, -1919216303730], [126559824445836, 3411530741010, -3187912400028], [91481987409668, -170603322390, -1119244100076], [-343982976686139, 2219904313641, 262446071538], [-42737041012426, 3887390182872, -4558479894864], [-195851473242072, 2353190284368, -3892080304992], [-40061263003832, -4067949709194, 426999707820], [87970120346184, 3530627019264, -5742037352448], [415776940292292, 3019572159576, -2461417462608], [15593321300874, 2775518274312, -337277150448], [-102693214224092, -13700785492590, 6337472049252], [-475069665152047, -5250317321613, 2991069045958], [105424968508624, 3701185615680, -4372157204352], [171880764986391, -7597900234965, -5314762641354], [-7997658058066, 1624896075918, -11693082739748], [425250543386896, -4055073090096, 1948650127776], [-261215738173780, 4790417355696, 6871153627744], [337030084236340, -1987409920608, 4049520022080], [-76105480398780, 9795736524924, 2741257207416], [-222530379850171, -9760507658025, -2966481651698], [428967828938280, -8077599772386, -82542424836], [-168820010542457, 7303361439747, -7201270929690], [-1089122417640070, 13067841444084, 5505858066024], [653660490692890, -12582664943226, 21960091225548], [-351260849368454, -1510364039436, 7159572168296], [1255057619129281, 1199157275109, 18468498648554], [-1175611114264148, 13095356714874, -18135160245964], [-297909645706894, 7545943257846, 5177547561132], [-368310222084278, 5080675970520, 10450109936304], [389793443916457, -9809243171595, -1103483334646], [-553518567634026, -1804092027036, -21229426385592], [-534760948364976, 15725620867992, 11169796959792], [667957563242802, -16743416798136, 16589523177360], [1747058812536176, 17045022658512, 15281254528416], [-749520387566108, 10244995569270, -50518494922068], [616846183603260, 39303449112522, 9905062172628], [1008683408377003, -202913542185, -23139064797426], [-1023527945486044, -69881081459028, 39379162945752], [2004337687551972, 34431176607450, -30903901529100], [-795096734470927, -26840221436091, -57864178764630], [1526599536887294, 18513077667318, 29080426860460], [1304786084712864, -25609399031622, 26743543396788], [1070169329402322, 52996801760238, 339653917596], [253738368121051, 17608252636341, -26566671069558], [-2246217792332498, -29808349747092, -7485769885608], [2594439549217977, 51483644956851, -43813603929786], [1874471925377460, 26819772840168, -7341213275184], [2688973748840315, -26121334673985, 10426738789854], [1807024514315400, -26403818753550, -17009332883676], [-2518106542151916, 51004761775254, -74826005164308], [2671206348294966, -14051184707082, -9890360552532], [-1548013826381974, 60542419401954, 95139051775236], [-4422302168092425, 40750491643353, 51536585809746], [-1155819375635586, 99423220100106, -7243471356588], [-787760503313292, -34840202220684, -97963977346200], [3826418565419896, -44071359357984, 160883769995712], [710962929278492, 52522809176670, 66972219011964], [2276142717726156, 106765255479234, -142159043252028], [-2238102311088112, -145981283947842, -8499114686916], [-1591280288747581, -10342449554259, 20766482729338], [5411073473489119, 68086834855539, 29541787045830], [7388238556494733, -32854653703809, -49850795382178], [1095332521874748, 227538802083978, 40524179984724], [-1385956851900481, -202215590179029, -63586603611594], [-2473868432317018, -70745015501160, -743721924560], [1127077885944384, 25572031082136, -122818321090512], [-1343639886154704, 229625277731220, -38270898419544], [-5619763230461161, -99938252480007, 92031936407442], [7784476485594970, -6273392269326, 307853573874980], [-2308632925604467, 219160785838743, -299579431028850], [901688877347764, -20952909073926, 161784226981940], [-11165969915817403, 203254480329105, -152516008237374], [-3982993141424964, 157169170827900, -222851263681416], [20011948292092804, -127652326799634, 134512631407260], [11064180295354326, 89775950576340, -41082787030488], [3362903208880448, -263127177012, -234315408113640], [-10001399831328718, -162508142043798, 76725363025684], [-2804426173389208, 29555837132556, 194401966730904], [-13220393176918884, -431952179170092, 266171996275752], [-5268903589312288, -13089648932808, 365632588753776], [9652196343721028, 191865879401730, 212616704312644], [-13319201731548090, -141747116631876, -172118641233672], [-9753599524326649, 350981701018329, -72587650425518], [1190129867247578, -235367041451178, -175297226337428], [4461536460179234, -359143346134440, 168750575305648], [-30625839746401688, -114402404882808, -25472741350896], [5091471602320851, 632421838422159, -573354043778178], [28509346911794934, -237349211500380, -285321069651000], [9376750690788714, 360615346797618, 446131951439268], [10036992880395848, -184456496290248, -186582991523472], [4046804692327483, 415795769649669, -714560126357718]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_7_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_7_12_a_b();".
// 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_7_12_a_b(:prec:=3)
    chi := MakeCharacter_7_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 12));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_7_12_a_b();".
// 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_7_12_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_7_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([<2,R![225104, -2854, -77, 1]>],Snew);
    return Vf;
end function;