#NEWLINE# @ public #NEWLINE# def primitive_element( extension , x = None , **args ) : #NEWLINE# #INDENT# 'Construct a common number field for all extensions. ' #NEWLINE# if ( not extension ) : #NEWLINE# #INDENT# raise ValueError ( "can't compute primitive element for empty extension" ) #NEWLINE# #UNINDENT# if ( x is not None ) : #NEWLINE# #INDENT# ( x , cls ) = ( sympify ( x ) , Poly ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( x , cls ) = ( Dummy ( 'x' ) , PurePoly ) #NEWLINE# #UNINDENT# if ( not args . get ( 'ex' , False ) ) : #NEWLINE# #INDENT# extension = [ AlgebraicNumber ( ext , gen = x ) for ext in extension ] #NEWLINE# ( g , coeffs ) = ( extension [ 0 ] . minpoly . replace ( x ) , [ 1 ] ) #NEWLINE# for ext in extension [ 1 : ] : #NEWLINE# #INDENT# ( s , _ , g ) = sqf_norm ( g , x , extension = ext ) #NEWLINE# coeffs = ( [ ( s * c ) for c in coeffs ] + [ 1 ] ) #NEWLINE# #UNINDENT# if ( not args . get ( 'polys' , False ) ) : #NEWLINE# #INDENT# return ( g . as_expr ( ) , coeffs ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( cls ( g ) , coeffs ) #NEWLINE# #UNINDENT# #UNINDENT# generator = numbered_symbols ( 'y' , cls = Dummy ) #NEWLINE# ( F , Y ) = ( [ ] , [ ] ) #NEWLINE# for ext in extension : #NEWLINE# #INDENT# y = next ( generator ) #NEWLINE# if ext . is_Poly : #NEWLINE# #INDENT# if ext . is_univariate : #NEWLINE# #INDENT# f = ext . as_expr ( y ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( 'expected minimal polynomial, got %s' % ext ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# f = minpoly ( ext , y ) #NEWLINE# #UNINDENT# F . append ( f ) #NEWLINE# Y . append ( y ) #NEWLINE# #UNINDENT# coeffs_generator = args . get ( 'coeffs' , _coeffs_generator ) #NEWLINE# for coeffs in coeffs_generator ( len ( Y ) ) : #NEWLINE# #INDENT# f = ( x - sum ( [ ( c * y ) for ( c , y ) in zip ( coeffs , Y ) ] ) ) #NEWLINE# G = groebner ( ( F + [ f ] ) , ( Y + [ x ] ) , order = 'lex' , field = True ) #NEWLINE# ( H , g ) = ( G [ : ( - 1 ) ] , cls ( G [ ( - 1 ) ] , x , domain = 'QQ' ) ) #NEWLINE# for ( i , ( h , y ) ) in enumerate ( zip ( H , Y ) ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# H [ i ] = Poly ( ( y - h ) , x , domain = 'QQ' ) . all_coeffs ( ) #NEWLINE# #UNINDENT# except CoercionFailed : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise RuntimeError ( 'run out of coefficient configurations' ) #NEWLINE# #UNINDENT# ( _ , g ) = g . clear_denoms ( ) #NEWLINE# if ( not args . get ( 'polys' , False ) ) : #NEWLINE# #INDENT# return ( g . as_expr ( ) , coeffs , H ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( g , coeffs , H )