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/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/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/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/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/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/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/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/How to make special functions/orthogonal polynomials as callable symbolic expression.http://ask.sagemath.org/question/8446/how-to-make-special-functionsorthogonal-polynomials-as-callable-symbolic-expression/Hello,
I'd like to make some special functions/orthogonal polynomials as callable symbolic
expression. However, those functions always remind me the argument is not an integer.
var('n a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
, and
var('n x')
g(x) = spherical_bessel_J(n, x)
TypeError: unable to convert x (=n) to an integer
Even if I tried the "domain" keyword, there's still the same problem:
var('n', domain=ZZ)
var('a x')
f(x) = gen_laguerre(n,a,x)
TypeError: unable to convert x (=n) to an integer
How do I reassure those functions that I will give integers to n later in each calculation?minihairSat, 05 Nov 2011 21:18:21 -0500http://ask.sagemath.org/question/8446/Substituting multiple valueshttp://ask.sagemath.org/question/8292/substituting-multiple-values/What is the best way of substituting a list (or vector or whatever) of values into an expression? For example, suppose I have
z = var('x y')
zvals = (1, 2)
w = x^2 + y^2
and want to substitute `zvals` for `z` in the expression `w`. I have tried the following commands:
w.subs(z=zvals) #doesn't work
w.subs({z:zvals}) #doesn't work
w.subs(x=1,y=2) #fine, but cumbersome if z has many elements
w.subs(z[0]=zvals[0],z[1]=zvals[1]) #doesn't work
w.subs({z[0]:zvals[0],z[1]:zvals[1]}) #fine, and could turn this into a loop
#if there are many variables, but ugly
w.subs(dict(zip(z,zvals))) #best I can come up with
As far as I can see, none of this behaviour changes if `z` and `zvals` are vectors or lists instead of tuples.
Is there a simpler way of doing this? Also, why doesn't the fourth attempt work when the fifth one does - is this down to a limitation of Python?
EDIT: I also realised that you can do `w.subs(z[0]==zvals[0])` but `w.subs(z[0]==zvals[0],z[1]==zvals[1])` won't work - why is this?SagenoobThu, 25 Aug 2011 03:09:33 -0500http://ask.sagemath.org/question/8292/Making my own special type of variablehttp://ask.sagemath.org/question/8109/making-my-own-special-type-of-variable/I want to create an object that I can take formal products and sums of, and also that remembers some extra data. I'd like to use sage's symbolic tools to do it without reimplementing multiplication, etc. For example, I'd like to make a class `myVar` that behaves something like this:
sage: a = myVar("some data about a")
sage: b = myVar("other important stuff")
sage: p = ((a + b)^2).expand()
a^2 + 2*a*b + b^2
sage: p.operands()
[a^2, 2a*b, b^2]
sage: p.operands()[0].operands[0]
a
sage: p.operands()[0].operands[0].get_data()
"some data about a"
I've tried subclassing Expression, but it seems like whenever I try to do some sums or products it just turns my thing into an Expression and forgets that it was a myVar.
Is something like this possible in sage?paragonWed, 11 May 2011 06:35:18 -0500http://ask.sagemath.org/question/8109/ValueError: free variable x |--> x when plotting the function xhttp://ask.sagemath.org/question/8001/valueerror-free-variable-x-x-when-plotting-the-function-x/Hi all.
I got the following problem:
sage: plot(symbolic_expression(x).function(x))
this raises
ValueError: free variable: x |--> x
If I replace `x` by anything else (but `1*x`) it works fine.
How can I do ?
My rationale behind my question is that I have a class which takes a function as argument and can perform many thinks on it, among other the plot. I made the following :
class MyFunction(object):
def __init__(self,f):
self.f=symbolic_expression(f).function(x)
def plot(self):
return plot(self.f)
My point in doing so is that I have to accept, as input `f` expressions like `x**2`, `2`, `g.diff(x)` (where `g` is an other function) and so on. In these cases, it turns out that I need to use the `symbolic_expression` trick in order to be sure that what I have is a function (need for numerical integration for example)
My questions :
1. Can I do otherwise in `__init__` in order to be sure to be able to use numerical integration, derivative, ... on `self.f` ?
2. If not, how can I plot when the input is simply "x" ?Laurent ClaessensTue, 15 Mar 2011 04:35:34 -0500http://ask.sagemath.org/question/8001/Symbolic expressions and simplifyinghttp://ask.sagemath.org/question/7852/symbolic-expressions-and-simplifying/I asked this on IRC, but no-one seemed to be on there.
I had a quick question concerning simplifying a symbolic expression in sage.
Basically, when i do a Laplace transform, i end up with the RHS of the following equation, but I would like to see the LHS of the equation
http://upload.wikimedia.org/math/a/1/7/a1752ef3bbff0288217159ca274c5bc8.png
Would anyone know how to change from one side to the other side please?
CoolHanDrewSat, 08 Jan 2011 00:22:02 -0600http://ask.sagemath.org/question/7852/bool returns false with arcsin(x) and 2*arctan(x/(1+sqrt(1-x^2)))http://ask.sagemath.org/question/7762/bool-returns-false-with-arcsinx-and-2arctanx1sqrt1-x2/The two expression should be equal
but when I write
bool(arcsin(x) == 2*arctan(x/(1+sqrt(1-x^2))))
it returns false.
Any clue - Why it does that? Any get around?ShuFri, 12 Nov 2010 07:40:25 -0600http://ask.sagemath.org/question/7762/