ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Aug 2023 22:30:34 +0200Basic fraction manipulation functions? (neophyte question)https://ask.sagemath.org/question/72615/basic-fraction-manipulation-functions-neophyte-question/ Somewhere I thought I saw a sage function that splits fractions, e.g.
(a + b)/c to a/c + b/c
I haven't been able to find anything like this in the online documentation. If this exists, does anyone know the function name?
In general, I'm having a hard time finding documentation on basic expression manipulation. E.g. I've used expr.collect(x) but I can't find documentation on it. Similarly for expr.op[0], x.substitute(a == b) and simplify(expr) I'd appreciate any suggestions re finding documentation on these sort of operations.
thanks
SmrzWed, 16 Aug 2023 22:30:34 +0200https://ask.sagemath.org/question/72615/why there is no exact result with this fractions sum ?https://ask.sagemath.org/question/63152/why-there-is-no-exact-result-with-this-fractions-sum/Hi, here is my code, in order to use it with sagetex (I'm new with sage). I want to make a sum with two fractions.
L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0], 4)
print(L)
print(type(L[0]))
L[0]/L[1]+L[2]/L[3]
and the result is :
[-11, 12, 7, -7]
<class 'int'>
-1.9166666666666665
so, no exact value.
I've made an other test :
L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0],4)
print(L)
print(type(L[0]))
print(L[0]/L[1]+L[2]/L[3])
QQ(L[0]/L[1]+L[2]/L[3])
and the output is :
[-6, -8, -4, 5]
<class 'int'>
-0.050000000000000044
-52215647853572/1044312957071439
instead of -1/20. I would like to understand how to achieve the exact and simplified fraction.
Thank you
-----------------------------------------------
Edit :
I tried with this 1 min ago :
L = random.sample([n for n in range(-15, 15) if n != -1 and n != 1 and n != 0],4)
print(L)
print(type(L[0]))
print(L[0]/L[1]+L[2]/L[3])
print(Integer(L[0])/Integer(L[1])+Integer(L[2])/Integer(L[3]))
QQ(L[0]/L[1]+L[2]/L[3])
and the output is :
[5, -8, -6, -13]
<class 'int'>
-0.16346153846153844
-17/104
-39792720200266/243437817695745
so ... i have to do this with "Integer" ?ryssWed, 06 Jul 2022 16:30:31 +0200https://ask.sagemath.org/question/63152/Square root of a fractionhttps://ask.sagemath.org/question/60172/square-root-of-a-fraction/I have the following variable and function:
var('r')
f = function('f')(r)
with an equation given by:
eq = sqrt(-1/(f^2*sin(r)^2))-1/sqrt(-f^2*sin(r)^2)
which, in LaTeX expression, looks like:
\begin{equation}
\sqrt{-\frac{1}{f\left(r\right)^{2} \sin\left(r\right)^{2}}} - \frac{1}{\sqrt{-f\left(r\right)^{2} \sin\left(r\right)^{2}}}
\end{equation}
In principle, this should be equal to zero. However,
bool(eq==0)
gives me `False`. I have tried with `.simplify_full()` and following the suggestion in [here](https://ask.sagemath.org/question/60054/some-straightforward-square-root-fractions-are-not-simplified/), I have used `._sympy_().simplify()`:
eq = eq._sympy_().simplify()
bool(eq==0)
but I still get `False`. Am I missing something?kekoThu, 09 Dec 2021 10:32:05 +0100https://ask.sagemath.org/question/60172/Some straightforward square root fractions are not simplifiedhttps://ask.sagemath.org/question/60054/some-straightforward-square-root-fractions-are-not-simplified/I'm a Sage beginner who's trying to apply it to a set of equations I'm working on. In that process, I came across an issue where SageMath 9.4 is not simplifying some very straightforward square root fractions in my expressions. Let me show you a minimal example:
x = var('x')
sqrt(1-x)/(1-x)
$$-\frac{\sqrt{-x + 1}}{x - 1}$$
What I was expecting to get, of course, is $1 / \sqrt{1 - x}$. Calling `simplify()` or `full_simplify()` on the expression doesn't make a difference; I still get the same thing out. I experimented with other square root fractions as well to see if this issue recurs:
sqrt(x)/x
$$1/\sqrt{x}$$
sqrt(1+x)/(1+x)
$$1/\sqrt{1+x}$$
sqrt(x-1)/(x-1)
$$1/\sqrt{x-1}$$
So in other words, Sage automatically simplifies all of the other expressions I tried in exactly the way that I would expect. To try out things a bit further, I tried to see if it helps to apply Sympy:
( sqrt(1-x)/(1-x) )._sympy_().simplify()
$$1/\sqrt{1-x}$$
That works. (The `simplify()` argument is essential here; otherwise, the fraction is not automatically simplified.)
So, is there some subtle finesse here that I'm not understanding, or did I stumble across a bug in Sage's simplification algorithms?Erlend M. ViggenFri, 03 Dec 2021 15:46:29 +0100https://ask.sagemath.org/question/60054/Conjugate multiplication of square roothttps://ask.sagemath.org/question/48086/conjugate-multiplication-of-square-root/ Is there a simple way to simplify a formula using conjugate multiplication of the square roots? For example, when I perform
var('a,b,d')
exp = 1/(a+b*sqrt(d))
exp.full_simplify()
I would like to get
(b*sqrt(d) - a)/(b^2*d - a^2)
but what I actually get is just the form that I started with. Even if I specify the assumptions
assume(d,'real')
assume(d>0)
the conjugate multiplication does not happen automatically. I would like to be able to tell Sage, that I want the conjugate multiplication. In some cases that are relevant to me, the conjugate multiplication would simplify my expressions significantly.TommiFri, 27 Sep 2019 21:34:51 +0200https://ask.sagemath.org/question/48086/Pretty print factorizations as fractionshttps://ask.sagemath.org/question/46864/pretty-print-factorizations-as-fractions/Hi all,
If I have an object whose factorization makes sense when expressed as a fraction, how do I get Sage to pretty print its factorization as a fraction instead of a product of factors? For example,
sage: R.<x> = PolynomialRing(QQ)
sage: f = (x - 1)^2 / (x + 1)
sage: f
(x^2 - 2*x + 1)/(x + 1)
sage: f.factor()
(x + 1)^-1 * (x - 1)^2
but ideally I would like some way to pretty print `f` as `(x - 1)^2/(x + 1)`.
Thanks,
Henryliu.henry.hlSat, 08 Jun 2019 16:31:28 +0200https://ask.sagemath.org/question/46864/Numerical approximation of coefficients in fractionshttps://ask.sagemath.org/question/43158/numerical-approximation-of-coefficients-in-fractions/I am aware that for expressions in the type
$$eq = c_0 + c_1x + c_2x^2...$$
the coefficients of x can be expressed as decimals by doing
eq.polinomial(RR)
however, I noticed that if it is in the form
$$eq = \dfrac{c_0 + c_1x}{c_2 + c_3x}$$
or in any form where it is impossible to express as $c_0 + c_1x^n$ where n is some power of x, the eq.polinomial(RR) only returns an error giving TypeError: fraction must have unit denominator.
How can I approximate $eq = \dfrac{c_0 + c_1x}{c_2 + c_3x}$ where $c_0, c_1, c_2, c_3$ becomes some decimals?
I am aware that $\dfrac{c_0 + c_1x}{c_2 + c_3x}$ is not a polynomial however I do not know what it is.IsamuThu, 26 Jul 2018 03:49:12 +0200https://ask.sagemath.org/question/43158/Can this fraction be simplified ?https://ask.sagemath.org/question/42157/can-this-fraction-be-simplified/During some calculations, I came across a fraction of this kind :
$$\frac{\sqrt{2}+2}{\sqrt{2}+1}$$
Which should be equal to $\sqrt{2}$.
I am surprised to see that Sage can't simplify this fraction with simplify_full :
( (sqrt(2)+2)/(sqrt(2)+1) ).simplify_full()
returns the same. Just to be sure:
bool( (sqrt(2)+2)/(sqrt(2)+1) == sqrt(2) )
returns true
Am I missing a simplification option ? How can I get Sage to simplify this fraction ?
To clarify, the original expression I encountered was this one :
$$\frac{3(x^4+4\sqrt{3}(x^2+6)\sqrt{x^2+3}+24x^2+72)}{\sqrt{3}(x^5+24x^3+72x)+12(x^3+6x)\sqrt{x^2+3}}$$
which is equal to $\frac{\sqrt{3}}{x}$. Sage can show the equality, but cannot simplify the expression (but maybe it's normal, this is not as trivial as the first example...). Substituting $x=1$ in this formula give something very similar to the expression above.
It can be obtained with:
f = 3*(x^4+4*sqrt(3)*(x^2+6)*sqrt(x^2+3)+24*x^2+72)/(sqrt(3)*(x^5+24*x^3+72*x)+12*(x^3+6*x)*sqrt(x^2+3))Florentin JaffredoWed, 25 Apr 2018 12:08:26 +0200https://ask.sagemath.org/question/42157/Run LCM fractionshttps://ask.sagemath.org/question/31915/run-lcm-fractions/ I am doing some matrix multiplications, and as result I get
![image description](http://i.imgur.com/DdUz2Ec.jpg)
Now see position 1,1 of the matrix: I want SageMath to return me a result that has only one fraction (using LCM), not two fractions. Which command should I use?CaterpillarWed, 30 Dec 2015 16:12:07 +0100https://ask.sagemath.org/question/31915/Why is_prime(6/3) results as False?https://ask.sagemath.org/question/26051/why-is_prime63-results-as-false/
sage: (6/3).is_integer()
True
sage: (6/3).is_prime()
FalselogomathFri, 06 Mar 2015 19:46:49 +0100https://ask.sagemath.org/question/26051/how to run fraction elenment in Multivariate Polynomial Ring in over Finite Field ringhttps://ask.sagemath.org/question/10825/how-to-run-fraction-elenment-in-multivariate-polynomial-ring-in-over-finite-field-ring/how to run fraction elenment in Multivariate Polynomial Ring in over Finite Field ring
k.<a> = GF(27);k;k.list();(k.prime_subfield()).list()
k(a+1);k(3/4);k(3/56);k(3);k(5/8);k(5*a);k(a*8);k(5*a)/k(a*8);k(5*a)/k(a*8)==k(5/8)==k(50*a)/k(a*80);k(302*a)/k(a*301)==k(20/121)
frac=k.fraction_field();frac;k.is_integrally_closed();k.integral_closure().list()
F=Frac(k['x,y']);F;F(3*x/4);F(7*x/2-3);F(y)/F(y^2+3)
Fraction Field of Multivariate Polynomial Ring in x, y over Finite Field
in a of size 3^3
0
-x
Traceback (click to the left of this block for traceback)
...
NameError: name 'y' is not defined
k.divides(28*a, 3/4);k(28*a)/k(3/4)
True
Traceback (click to the left of this block for traceback)
...
ZeroDivisionError: division by zero in finite field.
cjshThu, 12 Dec 2013 03:34:24 +0100https://ask.sagemath.org/question/10825/Decimal to fraction?https://ask.sagemath.org/question/10540/decimal-to-fraction/I did .1/8, and the output was 0.0125000000. Is there a way to get it to display as 1/80.bxdinSun, 15 Sep 2013 12:29:57 +0200https://ask.sagemath.org/question/10540/Display decimal as a fraction?https://ask.sagemath.org/question/10301/display-decimal-as-a-fraction/The number 1.5 represents 3/2 in decimal form.
In sage I can get 3/2 to display as a decimal: 3/2.n()
I haven't had any luck converting 1.5 to a fraction.bxdinSun, 30 Jun 2013 20:32:27 +0200https://ask.sagemath.org/question/10301/Get a matrix to display answers as decimals/floats, not fraction?https://ask.sagemath.org/question/10294/get-a-matrix-to-display-answers-as-decimalsfloats-not-fraction/I searched and couldn't find a viable solution:
I successfully executed the following:
<pre>
ma = matrix([[25, 5, 1], [49, 7, 1], [81, 9, 1]])
mb = matrix([[1121], [626], [967]])
show(ma)
show(mb)
ms = ma^-1 * mb
show(ms)
</pre>
Unfotunately though, the statement, show(ms), displays the answer/values as fractions, meaning I have to manually enter each of the value 3 separate times, as part of a float function, i.e., float(fraction/number).
How can I force show(ms), to display the values as decimals?
Also I find the matrix more readable/writable if I can enter each row's values by their own line, instead of the same line, as seen on the the, ma= & mb= statements. I remember I could this with ease in MatLab. I'd appreciate knowing how to that as well.
This post also has an unanswered question if anyone is up for the challenge, :P, j/k:
http://ask.sagemath.org/question/2732/display-y-intercept
bxdinSat, 29 Jun 2013 08:07:37 +0200https://ask.sagemath.org/question/10294/latex(-(x-1)/(x+1)) still brokenhttps://ask.sagemath.org/question/7712/latex-x-1x1-still-broken/I can't understand, why this is still broken
because since a few months this problem
is reported.
sage: latex(-(x-1)/(x+1)) ---> $\frac{-x-1}{x+1}$
sage version 4.5amaleaWed, 29 Sep 2010 05:16:58 +0200https://ask.sagemath.org/question/7712/eliminating fractions and roots from equationshttps://ask.sagemath.org/question/9400/eliminating-fractions-and-roots-from-equations/I'm trying to solve the following equations for $a$ and $c$, where all numbers are real, $0\leq a < 1$ and $0 < c$:
[sqrt(abs((b - 1)*(a + 1)/sqrt(c^2 + abs(b - 1)^2) + a)^2 + abs((a +
1)*c/sqrt(c^2 + abs(b - 1)^2))^2) == a + 1, sqrt(abs(-(b + 1)*(a -
1)/sqrt(c^2 + abs(b + 1)^2) + a)^2 + abs(-(a - 1)*c/sqrt(c^2 + abs(b +
1)^2))^2) == -a + 1]
$$\sqrt{\left|\frac{(b-1)\cdot(a+1)}{\sqrt{c^2+|b-1|^2}+a}\right|^2+\left|\frac{(a+1)\cdot c}{\sqrt{c^2+|b-1|^2}}\right|^2}=a+1$$
$$\sqrt{\left|\frac{-(b+1)\cdot(a-1)}{\sqrt{c^2+|b+1|^2}+a}\right|^2 + \left|\frac{-(a-1)\cdot c}{\sqrt{c^2+|b+1|^2}}\right|^2}=-a+1$$
Now solve itself seems to take forever on this without coming up with a result. On the other hand, I as a human have a pretty good idea how I'd solve such beasts: square both sides to get rid of the outer square roots, then multiply both sides with the common denominator, then move the single remaining square root to one side and all the rest to the other side and square again.
I know that these steps *might* introduce additional solutions, which are valid solutions of the modified system but not of the original one. Nevertheless, I'd like to be able to get at them, probably with some indication how good they are.
I wrote a bit of code to massage my equations for me:
def massage(e):
e = e.simplify().simplify_radical().full_simplify()
e = e.power(2).simplify()
e = e.multiply_both_sides(e.lhs().denominator()).simplify()
e = e.subtract_from_both_sides(e.rhs()).simplify()
e = e.subtract_from_both_sides([
term for term in e.lhs().operands() if 'sqrt' in str(term)][0])
e = e.simplify().power(2).simplify()
e = e.subtract_from_both_sides(e.rhs()).expand()
e = e.simplify().simplify_radical().full_simplify()
return(e)
But this sequence is highly specific to the equations at hand. And the part about how to identify which operand contains the nested square root is plain ugly. **So what better methods are there to perform this kind of equation simplification?** Preferrably in a much more automated way.
*Just for your information:* If I modify my equations using the code above, I am able to get 9 solutions. I'm not sure whether I actually trust them, as I would have expected something else, but there might be something wrong with either my expectation or the way I obtained the euqations.MvGMon, 08 Oct 2012 12:43:35 +0200https://ask.sagemath.org/question/9400/divide numerator and denominator by certain valuehttps://ask.sagemath.org/question/8785/divide-numerator-and-denominator-by-certain-value/Hi,
is it somehow possible to divide the numerator and denominator of a fraction by the same value? For example, (a*b + c)/(b*d + a + c) gets (a + c/b)/(d + a/b + c/b) if I divide it by b. matthjesMon, 12 Mar 2012 05:55:24 +0100https://ask.sagemath.org/question/8785/partial fraction decomposition function for multivariate rational expressionshttps://ask.sagemath.org/question/8429/partial-fraction-decomposition-function-for-multivariate-rational-expressions/Hi all:
I'd like to extend Sage's partial fraction decomposition function in the QuotientField class to a function that works on quotients of *multivariate* polynomials. To this end, i've found it convenient to store a rational expression $F = P/(Q_1^{e_1} \cdots Q_m^{e_m})$ as a Python list of the form [P,[Q_1,e_1],...,[Q_m,e_m]], where $Q_1,\ldots,Q_m$ are the irreducible factors of $F$'s denominator. Let's call these special kinds of lists 'widgets'. I have several auxiliary functions that manipulate widgets.
Code design questions for you. Should i make a new class for widgets, and if so, where in the Sage tree of modules should i put this class? If not, where do i put the auxiliary functions that manipulate widgets?
Thanks for your attention.
Alex araichevWed, 02 Nov 2011 19:11:23 +0100https://ask.sagemath.org/question/8429/How to Rationalize the Denominator of a Fraction ?https://ask.sagemath.org/question/8362/how-to-rationalize-the-denominator-of-a-fraction/Hi, experts.<br/>
<br/>
Is there any way to rationalize the denomintor of a fraction ?<br/>
<br/>
For example, I tried<br/>
a = 1 / (2 * sqrt(2) + 3)
b = a.simplify_full(); b;
c = a.simplify_factorial(); c;
d = a.simplify_radical(); d;
e = a.simplify_rational(); e;
expecting any of them to return "`3 - 2*sqrt(2)`" or "`-2*sqrt(2) + 3`". <br/>
However, all of the above commands return `1/(2*sqrt(2) + 3)`,<br/>
whose denominator is not rational.<br/>
<br/>
I know<br/>
(1) Sage uses Maxima.<br/>
(2) Standalone version of Maxima can rationalize the denominator by typing "`ratsimp(a), algebraic: true;`".<br/>
(3) Sage accepts "`maxima.ratsimp(a)`", but I don't know how to pass the Maxima option "`algebraic: true;`" to Sage.<br/>
Is there any way to rationalize the denominator with Sage ?<br/>
<br/>
Thanks in advance.<br/>
-Tatsuya
supertatSun, 09 Oct 2011 03:25:35 +0200https://ask.sagemath.org/question/8362/how to get output in a mixed fraction?https://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/regular rational sage style is like
sage:22/10
i want make it look like (thru pprint it will just look like a mixed on papper)
sage:2+2/10
is this possible?umrenSat, 21 Aug 2010 14:27:07 +0200https://ask.sagemath.org/question/7583/