Difference between revisions of "CoCoA:HowTo:Round Coefficients"
KHiddemann (talk | contribs) m (→About) |
m (Bot: Replacing category HowTo with HowTo Old) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 5: | Line 5: | ||
it may be quite handy to get a rounded value instead of these loooong fractions. Their is no code inside CoCoA to do this automatically, but you can do it quite easy, using CoCoA's [[CoCoA:FloatApprox|FloatApprox]] or [[CoCoA:FloatStr|FloatStr]] functions. | it may be quite handy to get a rounded value instead of these loooong fractions. Their is no code inside CoCoA to do this automatically, but you can do it quite easy, using CoCoA's [[CoCoA:FloatApprox|FloatApprox]] or [[CoCoA:FloatStr|FloatStr]] functions. | ||
− | == | + | == First version: Converting to string == |
+ | |||
P := x^2 + 11/10 y + 1/3 z; | P := x^2 + 11/10 y + 1/3 z; | ||
− | |||
S := Sum(Flatten([ ['( ',FloatStr(LC(M)),'*',Sprint(LT(M)),') +'] | M In Monomials(P)])); | S := Sum(Flatten([ ['( ',FloatStr(LC(M)),'*',Sprint(LT(M)),') +'] | M In Monomials(P)])); | ||
− | |||
S; | S; | ||
code by [[User:Dheldt|dheldt]], 23 Jun 2005 (CEST) | code by [[User:Dheldt|dheldt]], 23 Jun 2005 (CEST) | ||
− | == | + | == Second version: Only rounding coefficients == |
P := x^2 + 11/10 y + 1/3 z; | P := x^2 + 11/10 y + 1/3 z; | ||
− | |||
S := Sum([ FloatApprox(LC(M),1/10^3)LT(M) | M In Monomials(P)]); | S := Sum([ FloatApprox(LC(M),1/10^3)LT(M) | M In Monomials(P)]); | ||
− | |||
S; | S; | ||
code by [[User:Dheldt|dheldt]], 23 Jun 2005 (CEST) | code by [[User:Dheldt|dheldt]], 23 Jun 2005 (CEST) | ||
+ | |||
+ | == Third version: Using DecimalStr() == | ||
+ | <code> | ||
+ | Define PolyStr(Poly,MultSymb) | ||
+ | T := Sum(Flatten( | ||
+ | [[SignStr(LC(M))," " ,DecimalStr(Abs(LC(M))),MultSymb ,Sprint(LT(M)), " "] | | ||
+ | M In Monomials(Poly)])); | ||
+ | If (T[1] IsIn "+")=True Then | ||
+ | T := Sum([T[I] | I In 2..Len(T)]); | ||
+ | EndIf; | ||
+ | Return T; | ||
+ | EndDefine; | ||
+ | </code> | ||
+ | |||
+ | Code by Matthias Machnik, 26 April 2008 | ||
==Examples== | ==Examples== | ||
− | + | Output First Version: | |
+ | |||
+ | ( 1.000000000*10^0*x^2) +( 1.100000000*10^0*y) +( 3.333333333*10^(-1)*z) + | ||
+ | |||
+ | Output Second Version: | ||
+ | |||
+ | x^2 + 11/10y + 3333/10000z | ||
+ | |||
+ | Calling Third Version with: PolyStr(P,Ascii(183)); | ||
+ | |||
+ | 1·x^2 + 1.1·y + 0.333·z | ||
− | [[Category:CoCoA4]] [[Category:HowTo | + | [[Category:CoCoA4]] |
+ | [[Category:HowTo Old]] |
Latest revision as of 09:43, 29 October 2020
About
CoCoA computes polynomials either Q or in Z/(p). In case you have polynomials in Q and you do some computations with them, after some time their coefficients probably look quite ugly (ToDo: Add Example). To get a feeling for the coefficients, it may be quite handy to get a rounded value instead of these loooong fractions. Their is no code inside CoCoA to do this automatically, but you can do it quite easy, using CoCoA's FloatApprox or FloatStr functions.
First version: Converting to string
P := x^2 + 11/10 y + 1/3 z; S := Sum(Flatten([ ['( ',FloatStr(LC(M)),'*',Sprint(LT(M)),') +'] | M In Monomials(P)])); S;
code by dheldt, 23 Jun 2005 (CEST)
Second version: Only rounding coefficients
P := x^2 + 11/10 y + 1/3 z; S := Sum([ FloatApprox(LC(M),1/10^3)LT(M) | M In Monomials(P)]); S;
code by dheldt, 23 Jun 2005 (CEST)
Third version: Using DecimalStr()
Define PolyStr(Poly,MultSymb)
T := Sum(Flatten(
[[SignStr(LC(M))," " ,DecimalStr(Abs(LC(M))),MultSymb ,Sprint(LT(M)), " "] |
M In Monomials(Poly)]));
If (T[1] IsIn "+")=True Then
T := Sum([T[I] | I In 2..Len(T)]);
EndIf;
Return T;
EndDefine;
Code by Matthias Machnik, 26 April 2008
Examples
Output First Version:
( 1.000000000*10^0*x^2) +( 1.100000000*10^0*y) +( 3.333333333*10^(-1)*z) +
Output Second Version:
x^2 + 11/10y + 3333/10000z
Calling Third Version with: PolyStr(P,Ascii(183));
1·x^2 + 1.1·y + 0.333·z