ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 07 Mar 2016 14:12:02 -0600Display symbolic expressions without expansionhttp://ask.sagemath.org/question/32729/display-symbolic-expressions-without-expansion/I'm trying to write symbolic expressions with many intermediate variables. When I use the "show" function to display the final symbolic expression, it always shows the expression with the intermediate variables fully expanded. How can I show the final expression in terms of intermediate variables? Here is a simplified example:
var('t')
cc=matrix(SR, 3, 3, t)
I1=cc.trace()
Q=exp(I1)
show(Q)
With the above code, the output would be
\begin{equation}
Q=e^{3t}
\end{equation}
How can I generate an output as
\begin{equation}
Q=e^{I1}
\end{equation}
LiangMon, 07 Mar 2016 14:12:02 -0600http://ask.sagemath.org/question/32729/How can extract different terms from a symbolic expression?http://ask.sagemath.org/question/10256/how-can-extract-different-terms-from-a-symbolic-expression/I am trying to dissect an equation into its terms for statements such as "the first term in the denominator...". Optimally, I would like a list of terms, e.g.:
var('a b c x')
ex1 = a*x^2 + b/x - c
ex1.terms()
> [a*x^2, b/x, c]
This used to be possible ages ago by just typing
ex1[0]
> a*x^2
ex1[1]
> b/x
etc.
Similarly, I would like to be able to extract the terms inside a log or exponent, nominator, denominator... Is there a structured way to do this?
**UPDATE:** Following on from the first answer given below, I tried:
var('a b c x')
ex1 = a*x^2 + b/x - c
print ex1.operands()
print ex1.operator()
> [a*x^2, -c, b/x]
> <built-in function add>
Other examples:
var('a b c x')
ex1 = a*x^2*b*c
print ex1.operands()
print ex1.operator()
> [a, b, c, x^2]
> <built-in function mul>
var('a b c x')
ex1 = a+x^2+b+c
print ex1.operands()
print ex1.operator()
> [x^2, a, b, c]
> <built-in function add>
I could now iterate through each list and link the terms with the top operator given by ex1.operator() to recover the original equation. Does anyone know how to do this?
**How can I use the outputs of .operands() and .operator() to recover the original equation?**
Thanks again!
stanWed, 19 Jun 2013 01:23:35 -0500http://ask.sagemath.org/question/10256/Equations solvinghttp://ask.sagemath.org/question/25479/equations-solving/ Hi everybody, i've got this two equations:
eqq0: V_f*cos(delta(t))=v(t)
eqq1: V_f*sin(delta(t))=l*diff(psi(t))
I need to isolate V_f from the first equation, obtaining V_f=v(t)/cos(delta(t)) and substitute it in the second equation. Then i have to isolate diff(psi(t))=[v(t)*tan(delta(t))]/l.
I did :
a=solve (eqq0,V_f)
V_ff=a.right()
but it says *"AttributeError: 'Sequence_generic' object has no attribute 'right'"*
How can i solve this two equations?
Thank you.
SilviaSun, 11 Jan 2015 13:16:36 -0600http://ask.sagemath.org/question/25479/numerator_denominator()http://ask.sagemath.org/question/10615/numerator_denominator/I have a very big rational function and i want to obtain the numerator and denominator.
with .numerator_denominator() it takes forever
with .numerator_denominator(False) not every term is expanded.
with .expand() and .combine() it takes forever and the result is not a single fraction.
I need a single fraction N(x)/D(x) where N and D are polynomials in x, it is not important how big !alessandroWed, 16 Oct 2013 11:58:48 -0500http://ask.sagemath.org/question/10615/Difference Between Constants and Variableshttp://ask.sagemath.org/question/10583/difference-between-constants-and-variables/Hello all. I'm working with variational calculus in sage and working with
complicate expression `F` of a lot of variables declared with `var`.
I used `var` to declare both variables and constants. My problem is that I have
to know the exact number of variables which some generic expression depends. By
this reason the member function `variables` of an expression does not work, i.e.,
it returs both variables and constants because them was declared with `var` function.
I'm wondering if there is some type of variable in sage that can act as a variable to construct an expression and don't be returned by the `variables` member function.Rafael RojasTue, 08 Oct 2013 05:33:02 -0500http://ask.sagemath.org/question/10583/save_session runs foreverhttp://ask.sagemath.org/question/10566/save_session-runs-forever/Hello,
when I try to save a session (save_session command) with some expressions that are very long (composed by sums with hundred of terms) then the save session command runs for hours without producing any result nor writing anything to the file that should be created for storing the session on disk.
What can I do ?alessandroThu, 26 Sep 2013 02:22:48 -0500http://ask.sagemath.org/question/10566/Compare symbolic expressionshttp://ask.sagemath.org/question/10185/compare-symbolic-expressions/Following is the problem I am trying to solve:
I have multiple complex symbolic expressions f1(x1,x2,...,xn),f2(x1,x2,...,xn),...,fn(x1,x2,...,xn) which are functions of symbolic variables x1,x2,...,xn. I also have some constraints for the symbolic variables, e.g. 0 is smaller than x1, 2 is smaller than x2, x2 is integer, etc. How can I find out for two selected symbolic expressions which one is larger for given constraints?
I've tried something like:
<code> assume(0<x1,2<x2)
assume(x2,'integer')
bool(f1>f2) </code>
It seems to work for more simple symbolic expressions. For more complex symbolic expressions (where a solution definitely exists) SAGE seems not to be able to compare the expressions. Is there a way to overcome this?
KurtMMon, 03 Jun 2013 04:51:40 -0500http://ask.sagemath.org/question/10185/Is there a way to update an expression with new variable values?http://ask.sagemath.org/question/10087/is-there-a-way-to-update-an-expression-with-new-variable-values/Is there a way to update an expression if the symbolic variables that it contains have been overwritten? For example,
sage: var('beta')
beta
sage: eq = x == beta
sage: beta = 1
sage: eq
x == beta
Is there a way for it to change to `x == 1`? I wrote the following function to do this, but I wanted to know if something similar existed already.
def update(expr):
return expr.subs(dict(zip(expr.variables(), map(lambda v:eval(str(v)), expr.variables()))))Eviatar BachFri, 03 May 2013 13:02:11 -0500http://ask.sagemath.org/question/10087/Evaluate expression with unknownshttp://ask.sagemath.org/question/9816/evaluate-expression-with-unknowns/I'm trying to evaluate an integral that comes out with a crazy long result. I'm not going to paste it here because it really is quite long, which is essentially the problem. The result actually only has a few instances of unknowns in it, 90% of it's length comes from un-evaluated constants (like 2^(1/7), log(11.5), stuff like that). So it sort of looks like:
f(x) = (x* 2* pi* log(5)* 6^1.5) / (3^4*pi^2+x)
except it spans 10 lines.
If I could get sage to just express all of that stuff as a solid number, then the resulting expression wouldn't be so prohibitively long (I think it would actually evaluate out to something similar to the example I gave, number*x/(number+x) ). But numerical_approx() won't take anything with unknowns in it, so I can't just plug that expression into n().
How does one evaluate the knowns in an expression that contains unknowns?
Thank you ahead of time for your help!adamhgMon, 18 Feb 2013 07:59:38 -0600http://ask.sagemath.org/question/9816/Does a subtraction symbolic expression actually exist?http://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/Can someone please give me an example of a symbolic expression in sage of the subtraction variety? (by subtraction variety I mean using the subtraction operator)
Precisely: how can I create an object `o` of type `sage.symbolic.expression.Expression` such that `o.operator()` is `operator.sub`? It seems that subtraction expressions are always converted to additions.
For example:
sage: x = var('x')
sage: (x-1).operator()
<built-in function add>
rmp251Tue, 18 Dec 2012 15:28:15 -0600http://ask.sagemath.org/question/9398/Piece-wise functions and plottinghttp://ask.sagemath.org/question/9377/piece-wise-functions-and-plotting/Hi,
I have a piece-wise defined function that I want to plot (and potentially do other symbolic stuff with) and I was wondering how to do this. The problem is that I am defining my function as a Python function:
<pre>def F(x,y):
if( x <= y ):
return x*y
return x+y
</pre>
So I am gluing together two pieces, and I would like to be able to do
<pre>(x,y) = var('x,y')
contour_plot( F(x,y), (x,0,1), (y,0,1) )</pre>
But the problem is that this only plots the second part. This occurs because x <= y evaluates as false (they are variables) and F(x,y) is always evaluated as x+y. In Mathematica there is the <b>Which</b> function that works on symbolic expression to make piece-wise definitions. Is there an equivalent in Sage? Is there another way to do this? If I had a function (say, <i>delta</i>) that just evaluated as 1 if the symbolic expression was true, and 0 if not, I could craft the function as:
<pre>F(x,y) = delta( x<=y ) * (x*y) + delta( x>y ) * (x+y)</pre>
But as it is, I think there is no way to do this. Is there?
Thanks a lot for your help,
EdgarEdgarTue, 02 Oct 2012 04:20:42 -0500http://ask.sagemath.org/question/9377/How to get an integer from a complex expression?http://ask.sagemath.org/question/8720/how-to-get-an-integer-from-a-complex-expression/Hi, `(22/3*sqrt(2)*sqrt(3) + 485/27)^(1/3) + 1/9/(22/3*sqrt(2)*sqrt(3) + 485/27)^(1/3) + 20/3` equals 10. But how can I get 10 as an integer, as the complex expression has more complex solutions?RolandbFri, 17 Feb 2012 10:33:35 -0600http://ask.sagemath.org/question/8720/How do I evaluate symbolic expressions numerically in notebook()http://ask.sagemath.org/question/8589/how-do-i-evaluate-symbolic-expressions-numerically-in-notebook/I have solved an equation using this statement
s1 = solve(eq,ss)
The result is
ss = **rhs**
By setting all other variables in the right-hand side of the equation, I can retrieve a value using, among other things,
print **rhs**
or
N(**rhs**),
but when I try
N(ss)
I get the error message "cannot evaluate symbolic expression numerically". How can I evaluate the left-hand side as if it were the right-hand side?
khentiamentiuTue, 10 Jan 2012 12:52:18 -0600http://ask.sagemath.org/question/8589/