"""D1259 exact verification. All arithmetic is symbolic or rational.
Run without -O. No external data, network calls, floating-point tolerance, or
claim of equivalence to unavailable frozen programme arrays is used.
"""
import sympy as S
import platform
if not __debug__:
    raise SystemExit("Assertions require a run without -O.")
def report(key, value):
    print(key + " = " + str(value))
report("environment", "Python " + platform.python_version() + "; SymPy " + S.__version__)
report("arithmetic", "exact rationals / symbolic polynomials / algebraic radicals")

R=S.Rational
t,t2,u,v,w,z=S.symbols("t t2 u v w z",real=True)
c=S.Matrix([t,u,v,w,z])
# This tests the displayed coefficient-to-two-jet map, NOT the missing
# 70-dimensional projectors or their frozen E7 frame.
h=S.Matrix([u,4*t/9+v,16*t/27+w-z,16*t/27+w+z])
J=h.jacobian(c)
ker=J.nullspace()
assert J.rank()==4 and len(ker)==1
k=S.Matrix([1,0,-R(4,9),-R(16,27),0])
assert J*k==S.zeros(4,1)
report("object","published coefficient-to-Hessian-weight map, conditional on its carrier character")
report("two_jet_matrix",J.tolist())
report("two_jet_rank",J.rank())
report("two_jet_kernel_vector",list(k))
phi=S.Matrix([v,w,u-t,27*z-t]).jacobian(c)
selected=S.Matrix([1,1,0,0,R(1,27)])
assert phi.rank()==4 and phi*selected==S.zeros(4,1)
drop=[phi.extract([i for i in range(4) if i!=j],list(range(5))).rank()
      for j in range(4)]
assert drop==[3]*4
report("Phi_diag_matrix",phi.tolist())
report("Phi_diag_rank",phi.rank())
report("Phi_diag_selected_ray",list(selected))
report("Phi_diag_row_deletion_ranks",drop)
report("selected_Hessian_weights",list(J*selected))
assert list(J*selected)==[1,R(4,9),R(5,9),R(17,27)]
# Exact scalar expansion of the actual trace-character function, before
# contraction with the unavailable loaded projector.
s=S.symbols("s",real=True)
char_s=R(12,54)*(S.exp(2*s)+S.exp(-2*s)-2)
series=S.series(char_s,s,0,8).removeO()
defect=char_s-R(8,9)*s*s
report("scalar_character_series_through_degree_6",series)
assert S.expand(series).coeff(s,4)==R(8,27)
assert [S.diff(defect,s,i).subs(s,0) for i in range(3)]==[0,0,0]
report("defect_value_first_second_derivatives",[0,0,0])
report("defect_fourth_derivative",S.diff(defect,s,4).subs(s,0))
# Strict interior witness for the source's non-degenerate six-variable class.
forms=list(h)+[t2]
point={t:1,t2:1,u:1,v:1,w:1,z:0}
slacks=[f.subs(point) for f in forms]
assert all(x>0 for x in slacks)
report("stability_point_order","(t,t2,u,v,w,z)")
report("stability_point",(1,1,1,1,1,0))
report("strict_stability_slacks",slacks)
report("ambient_coefficient_dimension",6)
report("positive_scale_quotient_dimension",5)
# Compute intersections of the source-reported Casimir conditions with Phi.
literal=S.Matrix([u,v,w,z]).jacobian(c)
support=S.Matrix([u-v,v-w,z]).jacobian(c)
rl=phi.col_join(literal).rank(); rs=phi.col_join(support).rank()
assert rl==rs==5
report("Phi_plus_literal_Casimir_rank",rl)
report("Phi_plus_support_Casimir_rank",rs)
report("nonzero_adopted_ray_survives_either_Casimir_system",False)
# The general zero-level sum-of-squares Hessian rule follows by differentiation.
x,y=S.symbols("x y",real=True)
mu=S.Matrix([x+x*y,y+x*x]); objective=(mu.T*mu)[0]
at={x:0,y:0}
H=S.hessian(objective,(x,y)).subs(at)
D=mu.jacobian((x,y)).subs(at)
assert H==2*D.T*D
report("sum_of_squares_chain_rule_control",True)
report("sum_of_squares_control_is_frozen_Kempf_Ness_test",False)
report("frozen_detector_minimum_character_and_H2_certificates","NOT VERIFIED: original objects absent")
report("result","PASS: displayed coefficient, jet and source-condition implications only")
