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.Wed, 18 Dec 2019 15:33:25 -0600can you programmatically define a [mathematical] function?http://ask.sagemath.org/question/49072/can-you-programmatically-define-a-mathematical-function/I want to take an array of coefficients and turn that into a function, a math function not a python function. for example take
[2, 0, 2, 7]
and turn this into
$$f(x) = 2x^3 + 2x + 7$$
something like
def createSym(coefficients, degree, x):
symbolicEqn = ''
for i in poly:
symbolicEqn += ' + ', (x**deg)*i
deg -= 1
return symbolicEqn
pass
then call my definition in the script like
x = var('x')
coeffArray = [2, 0, 2, 7]
degree = 3
polynomialEqn = createSym(coeffArray, degree, x)
But symbolicEqn is just a string and not an expression. Is there a sage/python way to do this?alienfetuseaterWed, 18 Dec 2019 15:33:25 -0600http://ask.sagemath.org/question/49072/How to convert string to sage expression?http://ask.sagemath.org/question/42692/how-to-convert-string-to-sage-expression/In python one can use `parse_expr` to convert a string which contains valid sympy expression to an sympy expression.
I can't find how to do the same in sage. I tried `eval` but this works sometimes and not other times. Here is an example
var('x')
sage: expr=sin(x)
sage: expr=str(expr)
sage: expr=eval(expr)
sin(x)
But not on this one
sage: expr=-1/2*x/(x^2 + 1)
sage: expr=str(expr)
sage: expr=eval(expr)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-16-599d1fa18625> in <module>()
----> 1 expr=eval(expr)
<string> in <module>()
/usr/lib/python2.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__xor__ (build/cythonized/sage/structure/element.c:8986)()
951
952 def __xor__(self, right):
--> 953 raise RuntimeError("Use ** for exponentiation, not '^', which means xor\n"+\
954 "in Python, and has the wrong precedence.")
955
RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.
I do not know before hand the "type" of the expression, other than it is valid sage expression, but in a string.
I found I could do this
sage: var('x')
sage: expr=-1/2*x/(x^2 + 1)
sage: expr=str(expr)
sage: expr=sage_eval(expr,locals={'x':x})
sage: expr
-1/2*x/(x^2 + 1)
and it works. But this means I need to know before hand that 'x' symbol was there. Which is not possible for me.
I am looking for something similar to Mathematica's `ToExpression` described in
http://reference.wolfram.com/language/ref/ToExpression.html or python `parse_expr`
I found answer here https://ask.sagemath.org/question/41135/converting-strings-into-expressions/ using something called `SR` which I still do not understand, but it does not work for me.
sage: expr=integrate(x^2*(sqrt(x^2 + 1) - 2)/((x^3 - (x^2 + 1)^(3/2) - 1)*sqrt(x^2 + 1)), x)
sage: expr=str(expr)
sage: expr=SR(expr)
TypeError Traceback (most recent call last)
So, how would one convert string that contains arbitrary sage valid expression to sage expression?NasserWed, 20 Jun 2018 11:15:49 -0500http://ask.sagemath.org/question/42692/Lazy evaluation of derivatives of an unknown functionhttp://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/Hi,
I am using Sage to check some solutions to partial differential equations. I am wondering if a have an unknown function f, can I somehow form the PDE in terms of its derivatives and then substitute in the assumed solution and evaluate the derivatives after the fact?
Here is what I tried so far:
var('x y')
f = function('f', x, y)
g = derivative(f, x, y)
print(g)
D[0, 1](f)(x, y)
h = D[0, 1](f)(x, y)
print(h)
Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.symbolic.expression.Expression' object has no
attribute '__getitem__'
I figured out that D[0, 1] represents the derivatives with respect to the ith indepent variable of the function (is this a Maxima expression?), but I'm not sure then how to use these types of expressions when I finally want to substitute in the known form of f. I.e., since the output of the expression for g is in terms of D[], and when I try to reuse that expression as h, I get an error (since D is actually some other type of object). Any help would be appreciated. Let me know if my question is not clear.
Many thanks!nosnerosMon, 10 Jun 2013 05:27:40 -0500http://ask.sagemath.org/question/10215/weird behavior of set and uniqhttp://ask.sagemath.org/question/37142/weird-behavior-of-set-and-uniq/Hi all,
I have a list of coefficients (which are variables) and I want to remove duplicates. In my original file (from May 2015) I could use set and or unique now both of them give me the same error
TypeError: <class 'sage.manifolds.coord_func_symb.CoordFunctionSymbRing_with_category.element_class'> is not hashable
Whenever I try to use the same commands with another list I don't have any error. I'm still scratching my head.
here is the notebook. I don't have enough karma to publish a link
cloud.sagemath.com/projects/263f082f-dc10-4817-a88d-d87700640552/files/Ask+sage.htmlrobertoFri, 31 Mar 2017 12:15:11 -0500http://ask.sagemath.org/question/37142/Evaluating symbolic expression, when some variables are finally fixedhttp://ask.sagemath.org/question/37057/evaluating-symbolic-expression-when-some-variables-are-finally-fixed/Hello
I'm looking for solution when bigger number of variables is in the picture.
Simple example:
There is relationship I = V/R
then we know that R = 3, with that knowledge let's plot I(V)
<br> in sage it should look something like:
V = var('V')
R = var('R')
I = V/R
R = 3
plot(I)
but above is not working, because in plot(I) I is still V/R not V/3 (knowledge of R=3 is not used), so I'm looking for some operation that turns V/R into V/3, something like
I = evaluate(I)
but such evaluate() apparently does not exist
there are however two different ways <br>
(1) repeat I=V/R after R=3 and in such situation I becomes V/3 as needed <br>
(2) I = I(R=R) and with earlier R=3, I also becomes V/3
<br>both however seems for me be solution(s) for simple expression with just few variables, and not good (not as straightforward as hypothetical I = evaluate(I) for more complex expression with several variables.
Is such evaluate() or so, available already, and I failed to find it?<br>
Is there a (easy) way to implement that proposed evaluate() ?wjurFri, 24 Mar 2017 05:43:58 -0500http://ask.sagemath.org/question/37057/Converting expression to list of terms and backhttp://ask.sagemath.org/question/31843/converting-expression-to-list-of-terms-and-back/ Given an expression how would I convert it to a list, and how would I convert a list to an expression?
For example:
> x * b * cos(x^3) * sin(b)+b * cos(x)+h * sin(x)
Would convert to:
> [x * b * cos(x^3) * sin(b),b * cos(x),h * sin(x)]pircksFri, 25 Dec 2015 22:22:09 -0600http://ask.sagemath.org/question/31843/Using matrix elements as argumentshttp://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/I have a rather easy question, or so it would seem. I have looked for an answer but was unable to find one anywhere so I'm asking it here.
I am making a very simple iterative algorithm for which the input as well as the output at the end of every iteration is a vector (or matrix for that matter). What I want to do is use these elements as arguments for several functions during each of the iteration. So for example
x=var('x')
y=var('y')
z=matrix(2,1,[ [1],[1] ]
f=x^2+y^3
H=f.hessian()
Then what I would like to do is say
H(z[0],z[1])
or
H(z)
But no matter what I try I can't seem to get it to work. Ideas?DisneySageFri, 26 Nov 2010 03:44:53 -0600http://ask.sagemath.org/question/7774/Numerical values VS symbolic values ?http://ask.sagemath.org/question/23678/numerical-values-vs-symbolic-values/ This question might be related with [link text](http://ask.sagemath.org/question/8588/pi-and-e-not-evaluated-when-i-use-my-own-classes/?comment=23671#comment-23671)
I have to perform some numerical calculation using constants like pi and e. What happens is that computing a simple expression of pi is evaluated numerically (eg, cos(pi) returns -1) but when I use a random function (eg, random()*pi), I have a symbolic expression like "0.123456789*pi". In a for loop with this expression I obtain at the end something like "0.123456789*pi + 0.987654321*pi + ..." and so on.
My question aims to clarify the way to use symbolic expression (SE) and/or numerical values (NV) within a code (either in sage shell or script file). I think we have different cases to think about :
1. I want to use only NV in my code, how can I specify once for all that constants I will use will be evaluated numerically ?
2. I want to use only SE in my code, this one seems straightforward as Sage uses a preparser structure with symbolic expression.
3. I want to use both in my code, a function using constants need to return NV but also SE. Of course calculation will use NV from this function and analysis will use SE (eg, derivation, integration, series expansion, etc...).
I hope this thread will be useful. I think I know how to use case 1, for example with NV of pi as PI=RDF.pi() or PI=pi.n(). In case of random()*PI, we have indeed a numerical result, as wanted.
Case 3 is more interesting, I remember having a lots of problem with python.sympy with SE and NV. I struggled to use SE for analysis then trying to obtain NV. I'd like to see what you think about this, Sage seems more powerful than sympy about that. I read documentation but maybe I missed something. I'm not working on this case for now (so no code example...) but if needed for clarity I can dig one of my old sympy code.
(sorry for my english, it is sometimes "random" )
bigdukeSun, 03 Aug 2014 06:05:57 -0500http://ask.sagemath.org/question/23678/[style] Choose between multiple inheritancehttp://ask.sagemath.org/question/10692/style-choose-between-multiple-inheritance/Dear Sage people,
I want to create a new (mathematical) object that sometimes is an Expression and sometimes a SymbolicFunction, depending on the arguments. You can think of this for example like $f(a, b, t) = \int_0^t a^b e^{-x^2} dx$. For special values of $t$ I would like to see it as an Expression ($t=0$ or $t=\infty$), but in all other cases I want it to be a BuiltinFunction (or something alike).
In Sage I can do something like:
class MyObjectExpression(Expression):
def __init__(self, a, b, t):
Expression.__init__(self, integral(a**b*e**(-x**2), x, 0, t))
# More (override) stuff below
class MyObjectFunction(BuiltinFunction):
def __init__(self, a, b, t):
BuiltinFunction.__init__(self, 'f(a,b,t)', nargs=1)
# More (override) stuff below
def MyObject(a, b, t):
if t == 0 or t == infty:
return MyObjectExpression(a, b, t)
else:
return MyObjectFunction(a, b, t)
Is it possible to combine these three things into one class? So I want to create a class which is sometimes an Expression and sometimes an much more abstract class, is this possible?
Best,
Noud
**Edit:**
What I actually want to do is programming Askey-Wilson polynomials and give them extra options, like a three term recurrence relation. But this depends on $n$. I already programmed this.
class Askey_Wilson(SageObject):
def __init__(self, SR, n, z, a, b, c, d, q):
self.n = n
self.z = z
self.q = q
self.a = a
self.b = b
self.c = c
self.d = d
self.param = [a, b, c, d]
if self.n in ZZ:
self.I = self.evaluate()
else:
self.I = var('askey_wilson')
def __repr__(self):
return 'p_%i(%s;%s,%s,%s,%s|%s)' % (
self.n, self.z, self.a, self.b, self.c, self.d, self.q
)
def evaluate(self):
n, q, z, a, b, c, d = [self.n, self.q, self.z] + self.param
lc = qPochhammerSymbol(SR, [a*b, a*c, a*d], q, n) / a**n
poly = BasicHypergeometricSeries(SR,
[q**(-n), a*b*c*d*q**(n-1), a*z, a*z**(-1)],
[a*b, a*c, a*d], q, q)
return lc*poly
def three_term_recurrence(self):
A, B, C = 0, 0, 0
# compute three term recurrence relation
return A, B, C
But now every time I want to know the explicit value of the Askey-Wilson polynomials I have to call askey_wilson.I. I want to get rid of the I.NoudSun, 03 Nov 2013 07:22:42 -0600http://ask.sagemath.org/question/10692/conversions from/to FunctionField(SR) and symbolic expressionhttp://ask.sagemath.org/question/10633/conversions-fromto-functionfieldsr-and-symbolic-expression/Hello,
read the following session OR if you won't please go directly to the question below
$ sage
----------------------------------------------------------------------
| Sage Version 5.6, Release Date: 2013-01-21 |
----------------------------------------------------------------------
sage: a,b,s = var('a b s')
sage: expr1 = (a^2*s + 2)/(s^3 + s + 3) + s
sage: expr1.denominator()
s^3 + s + 3
sage: type(s)
<type 'sage.symbolic.expression.Expression'>
sage: FF.<s> = FunctionField(SR)
sage: FF(expr1)
s + (a^2*s + 2)/(s^3 + s + 3)
sage: FF(expr1).denominator()
1
# s in expr1 is NOT recognized as the s in the definition of the
# function field.
sage: type(s)
<type 'sage.rings.function_field.function_field_element.FunctionFieldElement_rational'>
# BUT:
sage: x = var('x')
sage: expr2 = x + (45^2 + 2)/(x^3 + x + 3)
sage: FF2.<x> = FunctionField(RR)
# now RR instead of SR and x as the variable.
sage: FF2(expr2)
(x^4 + x^2 + 3.00000000000000*x + 2027.00000000000)/(x^3 + x + 3.00000000000000)
sage: FF2(expr2).denominator()
x^3 + x + 3.00000000000000
# x is correctly recognized in expr2 but not in expr1 !
sage: type(x)
<type 'sage.rings.function_field.function_field_element.FunctionFieldElement_rational'>
QUESTION: a FunctionField over RR with the variable x correctly recognizes
expressions where x=var('x') appears in the expression (see above), and the computation
of denominator is correct; FunctionField over SR with the variable s do not
recognizes expressions with s=var('s'); instead in this case the s is treated
like a coefficient (denominator=1 in example above in the first part).
How can i adjust this behavior, so that I obtain the same answer in both following cases:
sage: expr1
s + (a^2*s + 2)/(s^3 + s + 3)
# here s = var('s') is symbolic expression
sage: FF(expr1).denominator()
1
# I DO NOT WANT THIS ANSWER
sage: FF(s + (a^2*s+2)/(s^3 + s + 3)).denominator()
s^3 + s + 3
# but this answer (that works if the expression is constructed by hand) with
sage: type(s)
<type 'sage.rings.function_field.function_field_element.FunctionFieldElement_rational'>
Any suggestion ?
THANK YOU VERY MUCH !
alessandroSun, 20 Oct 2013 00:16:23 -0500http://ask.sagemath.org/question/10633/Getting a rooted graph from a nested list of listshttp://ask.sagemath.org/question/9614/getting-a-rooted-graph-from-a-nested-list-of-lists/At [Trac 9329](http://trac.sagemath.org/sage_trac/ticket/9329), a very simple expression tree walker is defined.
def tree(expr):
if expr.operator() is None:
return expr
else:
return [expr.operator()]+map(tree,expr.operands())
This gives a nested list of lists, like
sage: tree(2*(x+2^x))
[<built-in function add>, [<built-in function mul>, [<built-in function pow>, 2, x], 2],
[<built-in function mul>, x, 2]]
I'm trying to build a similar walker which would (roughly speaking) give the following dictionary which I could plot as a tree.
Graph({'add':['mul-lev1-1','mul-lev1-2'],'mul-lev1-1':['pow-lev2-1','2-lev2-1'],
'pow-lev2-1':['2-lev3','x-lev3'],'mul-lev1-2':['x-lev2','2-lev2-2']}).show(
layout='tree',tree_root='add')
![Binary Tree of this expression](/upfiles/13548511202509835.png)
Naturally, the same element could appear as many times as one liked in any level of the tree, so the naming scheme probably would be overly ponderous... but I'm wondering whether anyone who understands `Converter` better than I do (which isn't saying much) can get around the problem that dictionaries of dictionaries don't work here in any case to build a nice tree walker that can make such graph-able dictionaries. 'Cause doing it by hand is not particularly fun.kcrismanThu, 06 Dec 2012 15:36:07 -0600http://ask.sagemath.org/question/9614/How to return a list from callable symbolic expressionhttp://ask.sagemath.org/question/10449/how-to-return-a-list-from-callable-symbolic-expression/I was (wrongfully) expecting the return type of all of the following function calls to be the same, namely a list. Why does the callable symbolic expression (1) return a vector, and how to rewrite (1) such that it does return a list?
# (1)
sage: f(x) = [x,x]
# (2)
sage: g = lambda x: [x,x]
# (3)
sage: def h(x):
....: return [x,x]
sage: type(f(x))
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
sage: type(g(x))
<type 'list'>
sage: type(h(x))
<type 'list'>
MarkSat, 17 Aug 2013 06:51:14 -0500http://ask.sagemath.org/question/10449/Long expression in printed-out documentshttp://ask.sagemath.org/question/10278/long-expression-in-printed-out-documents/Hi all!
Could you give me a hint how to display a long symbolic result (Latex-like) in such a manner that it would be acceptable for printing, e.g. into pdf document (i.e. how to split automatically the output into several rows).
The command view(eq1, tiny=True), proposed in a similar topic, apparently does not work.
Thank you all in advance!hellerkopfTue, 25 Jun 2013 06:09:53 -0500http://ask.sagemath.org/question/10278/Turning an expression into a multivariate polynomial?http://ask.sagemath.org/question/10198/turning-an-expression-into-a-multivariate-polynomial/Suppose I have the expression
-a^3x^2 - a^2xy^2 + axy + bx^2 + 2bxy + xy^2
I want to turn this into a polynomial in x and y:
(1-a^2)xy^2+(a+2*b)xy+(b-a^3)x^2
so that I can then extract the coefficients (by setting x=1, y=1 in the list of operands of the new expression). How do I tell Sage which of the four variables will be the polynomial variables?AlasdairWed, 05 Jun 2013 17:57:36 -0500http://ask.sagemath.org/question/10198/How to recursively substitute from global name space?http://ask.sagemath.org/question/9533/how-to-recursively-substitute-from-global-name-space/In sage 3.x, I used the below function to automatically substitute into an expression all assignments I have made previously. How can I get it to work in Sage 5.x or is there a better way now?
def subsg(variable, *args):
"""
Substitutes all definitions in the global name space for the variables contained in 'variable' and returns the result.
INPUT:
variable -- a symbolic variable
*args -- an optional dictionary containing additional parameter definitions
EXAMPLE:
sage: var ('a b c d')
a b c
sage: f = a*x
sage: a = 2*b
sage: b = c^2
sage: cons1 = dict(c = d^3)
sage: subsg(f,cons1)
2*d^6*x
Or, if cons1 is not to be used:
sage: subsg(f)
2*c^2*x
"""
expr1 = variable
expr2 = variable.subs(globals()).subs(*args)
while expr1 <> expr2:
expr1 = expr2
expr2 = expr1.subs(globals()).subs(*args)
return expr1
stanWed, 14 Nov 2012 02:56:27 -0600http://ask.sagemath.org/question/9533/Compare elements of a recursive defined sequencehttp://ask.sagemath.org/question/10163/compare-elements-of-a-recursive-defined-sequence/I define the recursive sequence as:
A, b, c = var('A, b, c')
def Sequence_rec(k):
x = 0
for i in range(1,k+1):
x = x + (A - x)/((c-i+2)^b)
return x
For the parameters the assumptions are:
assume(A>0,c>0,b>0)
assume(c, 'integer')
I'm interested in the elements of Sequence_rec(k) with k<=c. The following relation has to be true for the defined sequence considering the given assumptions:
assume(c>2)
bool(Sequence_rec(4) > Sequence_rec(3))
But Sage computes it is false! The following plot shows the difference is positive:
plot((Sequence_rec(4) - Sequence_rec(3))(A=1,c=3),b,(0,100))
How can I force Sage to compare the elements of the sequence `bool(Sequence_rec(n+1) > Sequence_rec(n)) = true` for any positive integer n correctly? Thank you for your advice!
Kurt
KurtMWed, 29 May 2013 04:02:26 -0500http://ask.sagemath.org/question/10163/case distinction in symbolic expressionhttp://ask.sagemath.org/question/9907/case-distinction-in-symbolic-expression/Is it possible to include a case distinction in a symbolic expression?
More precisely, I'm currently trying to understand how to include a [truncated power function](http://en.wikipedia.org/wiki/Truncated_power_function) into a symbolic expression, if that is at all possible.MvGWed, 13 Mar 2013 06:25:38 -0500http://ask.sagemath.org/question/9907/Extracting numerical value from a symbolic expressionhttp://ask.sagemath.org/question/9825/extracting-numerical-value-from-a-symbolic-expression/Hi,
First of sorry for the title as I am not sure what should be the title of this question.
I used solve() command to solve a system of equations and got a result like this.
[[x1 == (4/3), x2 == (-1/6), x3 == (-1/6)]]
We can say that it is some vertex and I want to have a result as (4/3, -1/6, -1/6) or [4/3, -1/6, -1/6]. At the moment I am doing it manually. Is there any sage command that can automatically extract (4/3, -1/6, -1/6) from the solution [[x1 == (4/3), x2 == (-1/6), x3 == (-1/6)]].
Thanks in advance!
assadabbasiWed, 20 Feb 2013 12:26:55 -0600http://ask.sagemath.org/question/9825/Evaluating a symbolic expression for a Graphhttp://ask.sagemath.org/question/9768/evaluating-a-symbolic-expression-for-a-graph/I'm able to do this:
sage: f = function('radius', nargs=1, evalf_func=Graph.radius)
sage: f(graphs.HouseGraph())
2
But not this:
sage: var('G')
sage: expr = f(G)
sage: expr.subs(G=graphs.HouseGraph())
...
TypeError: no canonical coercion from <class 'sage.graphs.graph.Graph'> to Symbolic Ring
What am I missing? Is it not possible to use symbolic expressions like this?
patronicsThu, 14 Feb 2013 07:50:33 -0600http://ask.sagemath.org/question/9768/solve an equation in terms of an expression?http://ask.sagemath.org/question/9749/solve-an-equation-in-terms-of-an-expression/Hi,
Not entirely sure if I worded the subject line of this question right,
Essentially what I am trying to do is rearrange an equation based on what I want on the left hand side of it, for example:
I have defined my variables:
var('v_o, v_i, delta, T, v_d, v_c')
Entered my equation:
eqn1= v_i * delta * T == v_o + v_d + v_c - v_i * (1-delta) * T
Made a substitution:
eqn2 = eqn1.substitute(v_c=v_i)
and now, with minimum possible effort I would like sage to put it in the form of:
delta/(1-delta)=...
Many thanks to anyone who's even read this far down,
Any help would be much appreciated
penfoldMon, 28 Jan 2013 07:12:07 -0600http://ask.sagemath.org/question/9749/Traversing sage's symbolic expression trees in pythonhttp://ask.sagemath.org/question/9503/traversing-sages-symbolic-expression-trees-in-python/I'm writing some python code around sage and I need to build an expression tree of the following basic form:
- each node represents an operation
- each child tree represents an expression tree to which the operation applies
Can anyone point me in the right direction as to how to traverse an instance of `sage.symbolic.expression.Expression`, so as to extract this kind of semantic information? rmp251Mon, 12 Nov 2012 11:16:40 -0600http://ask.sagemath.org/question/9503/cannot evaluate symbolic expression numericallyhttp://ask.sagemath.org/question/9286/cannot-evaluate-symbolic-expression-numerically/I'm probably doing something wrong on a fundamental level, so: sorry for my ignorance. Yet i'd very much appreciate any suggestions, how to make this work:
I have me a function f, defined in somewhat lenghty way:
var('a,b,c,d')
i=matrix(SR,3,3, [1,0,0, 0,1,0, 0,0,1])
e_1=matrix(SR,3,3, [1,0,-1, 0,0,0, -1,0,1])
e_2=matrix(SR,3,3, [0,1,-1, 0,0,0, 0,-1,1])
e_3=matrix(SR,3,3, [0,0,0, 1,0,-1, -1,0,1])
e_4=matrix(SR,3,3, [0,0,0, 0,1,-1, 0,-1,1])
M=matrix(SR,3,3, [a,b,1-a-b, c,d,1-c-d, 1-a-c, 1-b-d,a+b+c+d-1])
M_1=e_1*M+M*e_1
M_2=e_2*M+M*e_2
M_3=e_3*M+M*e_3
M_4=e_4*M+M*e_4
A=matrix(SR,4,4, [ M_1[0,0], M_1[0,1],M_1[1,0],M_1[1,1], M_2[0,0], M_2[0,1],M_2[1,0],M_2[1,1], M_3[0,0], M_3[0,1],M_3[1,0],M_3[1,1], M_4[0,0], M_4[0,1],M_4[1,0],M_4[1,1] ])
f(a,b,c,d)=A.determinant()
And then I want a solution to f==0, after giving some random arguments to it
var('k,l,m,n,t,y')
for i in range(5):
k=random(); n=random()
if k>n: y=k
else: y=n
l=random()*(1-y)
m=random()*(1-y)
if k+l+m+n>1:
sols=solve([f(k*t+1-t,l*t,m*t,n*t+1-t)==0], t);
sols[1]
A wild string of numbers pops out. But how do i get to see how much is it? Changing to sols[1].n() gives "TypeError: cannot evaluate symbolic expression numerically".
Thanks in advance!
ozikSat, 01 Sep 2012 21:39:48 -0500http://ask.sagemath.org/question/9286/Visual Simplificationhttp://ask.sagemath.org/question/9234/visual-simplification/I want to take:
(adc*omP1*omP2)/omZ1)*(-I*2*Pi+
omZ1)/((-I*2*Pi + omP1)*(-I*2*Pi + omP2))
and have it shown as:
adc omP1 omP2 * (-I 2 Pi + omZ1)
------------- --------------
omZ1 (-I 2 Pi + omP1)(-I 2 Pi + omP2)
The * between, for example, -I and 2 or 2 and Pi can be present, but I would like to see the equation in a more simplified form like the second case. I would even like to enter it in this way or at least have a way to convert.
gjmTue, 14 Aug 2012 04:21:03 -0500http://ask.sagemath.org/question/9234/How to "recover" real number from expression?http://ask.sagemath.org/question/9066/how-to-recover-real-number-from-expression/How do I separate a `RealNumber` from an `Expression`, once it is constructed?
sage: b = RealField(200)(5)
sage: type(b)
<type 'sage.rings.real_mpfr.RealNumber'>
sage: type((b^x).operands()[0])
<type 'sage.symbolic.expression.Expression'>Eviatar BachTue, 12 Jun 2012 17:55:45 -0500http://ask.sagemath.org/question/9066/convert expression to functionhttp://ask.sagemath.org/question/8985/convert-expression-to-function/Hola,
is there a way to convert symbolic expression to proper functions?
E. g. `s = sin(x)` into `x |--> sin(x)`
So far I've been using f(x) = s(x), however, deprection warnings occur:
`DeprecationWarning: Substitution using function-call syntax and unnamed
arguments is deprecated and will be removed from a future release of Sage; you
can use named arguments instead, like EXPR(x=..., y=...)`MathemageSat, 19 May 2012 23:52:44 -0500http://ask.sagemath.org/question/8985/Getting a function from a symbolic expression (i.e. "y = x+2")http://ask.sagemath.org/question/8858/getting-a-function-from-a-symbolic-expression-ie-y-x2/Hi all,
I'm having trouble understanding how to solve the following problem. Let's say, for instance, that I have an expression
a = y - 2 == x
If I wish to solve this for y,
b = solve(a, y)
This returns another object of type Expression that looks like
"y = x + 2"
My question is, is it possible to obtain a callable symbolic expression x --> x + 2 from this result, b?
I'm trying to solve an implicit equation for a variable and obtain a plottable/differentiable etc. resultchasemeadorsWed, 04 Apr 2012 21:06:26 -0500http://ask.sagemath.org/question/8858/Strange result of a relational expressionhttp://ask.sagemath.org/question/8725/strange-result-of-a-relational-expression/Tried the following in sage. The answer is False, which is apparently incorrect. Anyone ever encountered this before? Any idea on what happened here?
---------------
forget()
assume(x >= 0)
expr = sin(x) <= x
print(bool(expr))
---------------
seflyerMon, 20 Feb 2012 20:43:00 -0600http://ask.sagemath.org/question/8725/Rewriting an expression in terms of other expressions or functionshttp://ask.sagemath.org/question/8677/rewriting-an-expression-in-terms-of-other-expressions-or-functions/I'd like to know if there is a way to ask Sage to write a given expression in terms of other expressions or functions. The user could have an insight about the latter while the CAS would be stuck. What I want is something like
write(a^2 - b^2, a-b)
write(cos(x+y), (cos(x), sin(x), cos(y), sin(y)))
write(gamma(p+1), gamma(p)) # or any other recurrence relation the user wants to check
Something like directed simplification, but not wholesale.
Green diodSat, 28 Jan 2012 08:42:32 -0600http://ask.sagemath.org/question/8677/Is it possible to edit the content of an expression by hand?http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/Hi,
Is it possible to edit an expression by hand? I know some terms in my calculation are not important and want to delete them by hand before the next step. However, I didn't find a way to do that.
To be explicit, is there a function to print the sage output in terms of an input form (like the Mathematica InputForm[] function), so I can edit and evaluate it again? For example,
sage: f = function('f', var('t'))
sage: expand((diff(f,t)+1)**2)
D[0](f)(t)^2 + 2*D[0](f)(t) + 1
I want to delete the "+1" in the output and save the "D\[0](f)(t)^2 + 2*D\[0](f)(t)" in another variable. Is it possible to do that? Directly write
g = D[0](f)(t)^2 + 2*D[0](f)(t)
doesn't make sense. Sage says 'D' is not defined.
tririverWed, 21 Dec 2011 15:33:54 -0600http://ask.sagemath.org/question/8586/Substitute list of expressionshttp://ask.sagemath.org/question/8518/substitute-list-of-expressions/How is it possible to substitute list of expressions?
For example:
sage: y=[4.0,2.0,5.0,1.0]
sage: x=[1.0,2.0,3.0,4.0]
sage: xy=zip(x,y)
sage: var('a,b,c,d,x')
(a, b, c, d, x)
sage: model(x)=a*x^3+b*x^2+c*x+d
sage: sol=find_fit(xy,model)
sage: sol
[a == -2.0000000000000031, b == 14.500000000000021, c == -31.500000000000046, d == 23.000000000000028]
Now sol is a list of expressions. How can I substitute it into my model? The only way I found is:
sage: func=model.substitute_expression(sol[0],sol[1],sol[2],sol[3])(x)
sage: func
-2.0000000000000031*x^3 + 14.500000000000021*x^2 - 31.500000000000046*x + 23.000000000000028
However, the expression like "sol[0],...,sol[3]" seems to be not a good style...
Best regards, Aleksey.Aleksey_RTue, 29 Nov 2011 19:50:02 -0600http://ask.sagemath.org/question/8518/