# Conversion from a ring to another for matrix

Suppose I have inherited of a matrix with real numbers that is X.yyyy style numbers. Is there a simple way to transform this matrix in a matrix composed of rational numbers and vis-versa ? And what if the matrix is composed of mixed numbers ? In that last case, could I impose a ring ?

Say M1=matrix(2, 2,(1.2,2.3564,3.0000,5.4321)), M2=matrix(2, 2,(1/2,2/6, 3/8,. 5.4321)), M3=matrix(2, 2,(1.2,3/4,1,2.756))

edit retag close merge delete

Sort by » oldest newest most voted

The method change_ring is made for that:

sage: m = random_matrix(RDF, 3, 3)
sage: m
[  -0.5774223516523238    0.4648049987667806  -0.17171365230946778]
[-0.056437596814425284   -0.8482416820866097   -0.4433923816239411]
[  -0.8602958788377144   -0.6032286081857376    0.2704814323965121]
sage: m.change_ring(QQ)
[-122806609/212680733   66491472/143052403  -40134866/233731363]
[ -53603174/949777755  -91233993/107556602   -33210813/74901632]
[-538125967/625512664 -107008889/177393591   51117399/188986721]


Note that it does not change m:

sage: m
[  -0.5774223516523238    0.4648049987667806  -0.17171365230946778]
[-0.056437596814425284   -0.8482416820866097   -0.4433923816239411]
[  -0.8602958788377144   -0.6032286081857376    0.2704814323965121]

more

Nice I haven't found this command.Thanks.

( 2020-05-27 11:13:07 +0200 )edit

Note that this isn't a command but the use of a method (in this case belonging to the class of m). The list of methods that can be applied to m can be obtained by dir(m) ; perusing this list is often ... inspirational, shall we say...

( 2020-05-27 12:09:01 +0200 )edit