Hey,
   You need to make sure the order of the variables agrees:

sage: A = QQ['t']
sage: B = A['x','y']
sage: C = QQ['t', 'x','y']
sage: C.coerce(B.0)
x

The reason the order is important is say you have QQ['x,y'] and QQ['y,x'], 
should the coercion be index to index or name to name? (Although I'd 
advocate for multivariate polynomial rings to sort their generators upon 
creation so that QQ['x,y'] is QQ['y,x'] in the sense of python.)

Best,
Travis

On Thursday, October 1, 2015 at 1:05:20 PM UTC-5, len...@ackermans.info 
wrote:
>
> Thanks for your answer! I was able to create the required homomorphism 
> with your suggested method. Out of curiosity: how would you explicitly 
> define what is mapped to the variable of the inner polynomial ring? (When 
> for example, they have different names.) The homomorphism in your example 
> clearly sends x in B to x in C, so why doesn't hom() tell this? Is there a 
> way to find out what a homomorphism actually does, apart from by giving it 
> some input?
>
> My original problem was actually coercing from a multivariate polynomial 
> ring over a polynomial ring to a single multivariate polynomial ring. In 
> this case there is no coercion:
> sage: A = QQ['t']
> sage: B = A['x','y']
> sage: C = QQ['x','y', 't']
> sage: C.coerce(B(1))
> (...)
> TypeError: no canonical coercion from Multivariate Polynomial Ring in x, y 
> over Univariate Polynomial Ring in t over Rational Field to Multivariate 
> Polynomial Ring in x, y, t over Rational Field
>
>
> On Thursday, October 1, 2015 at 6:47:36 PM UTC+2, Nils Bruin wrote:
>>
>> On Thursday, October 1, 2015 at 8:15:39 AM UTC-7, len...@ackermans.info 
>> wrote:
>>>
>>> In my code I often encounter elements of polynomial rings of polynomial 
>>> rings. Since some functions only work on multivariate polynomial rings I 
>>> need to create a homomorphism, but I'm unable to do this. What is the 
>>> recommended way?
>>>
>>
>> This is actually one of the things where sage's coercion framework is 
>> pretty advanced. You do have to make sure that the names of the variables 
>> agree, tough. Sage uses those names to figure out how to coerce. To define 
>> the homomorphism:
>>
>> sage: A=QQ['x']
>> sage: B=A['y']
>> sage: C=QQ['x','y']
>> sage: B.hom([C.1])
>> Ring morphism:
>>   From: Univariate Polynomial Ring in y over Univariate Polynomial Ring 
>> in x over Rational Field
>>   To:   Multivariate Polynomial Ring in x, y over Rational Field
>>   Defn: y |--> y
>>
>> The homomorphism is described by "send the generator of B (over A) to the 
>> second generator of C, and figure out yourself how to coerce A into C". It 
>> can do the latter because both A and C are generated over a common subring 
>> (QQ) and the generator of A has a name that is equal to the name of a 
>> generator of C: an obvious target.
>>
>> Indeed, these rules allow sage to even coerce B into C: you can just mix 
>> them in expressions:
>>
>> sage: A.0+B.0+C.0
>> 2*x + y
>>
>> It chooses the multivariate ring as the common parent:
>>
>> sage: parent(A.0+B.0+C.0) is C
>> True
>>
>> You can also obtain the coercion map sage uses itself and use it as the 
>> homomorphism you asked for at the top:
>>
>> sage: C.coerce_map_from(B)
>> Conversion map:
>>   From: Univariate Polynomial Ring in y over Univariate Polynomial Ring 
>> in x over Rational Field
>>   To:   Multivariate Polynomial Ring in x, y over Rational Field
>>
>>  
>>
>>> Further questions:
>>> Why is there no standard coercion between nested polynomial rings and 
>>> multivariate polynomial rings?
>>>
>>
>> There is such a coercion 
>>
>> Should we change functions on polynomials like factor() to work in the 
>>> case of nested polynomial rings, by using a coercion to multivariate 
>>> polynomial rings?
>>>
>>
>> In individual cases: possibly. In general: no. There could be merit in 
>> "nested" arithmetic.  
>>  
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to