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.Tue, 09 Apr 2019 14:33:04 -0500Direct substitution vs "subs" methodhttp://ask.sagemath.org/question/46068/direct-substitution-vs-subs-method/Suppose I define
var('a b c')
formula = a^2 + 5*b + c
Then I would like to substitute some values, like a=2, b=2 and c=4. As far as I'm aware, there are two options to do this:
formula(a=2, b=2, c=4)
and
formula.subs(a=2, b=2, c=4)
In this particular case, is there any difference between this two options? Is there any difference in some other case(s)?
Thanks in advance for you answers!dsejasTue, 09 Apr 2019 14:33:04 -0500http://ask.sagemath.org/question/46068/Sage subs() function include product conditionhttp://ask.sagemath.org/question/44578/sage-subs-function-include-product-condition/Code:
from brial import *
katan=declare_ring([Block('x',32),Block('k',80)],globals())
r1=x(3)*x(8) + x(7) + x(10)*x(12) + x(18) + x(23) + k(1) + k(16)
r2 = r1.subs({x(3)*x(8)+x(7)+ x(10)*x(12)+ x(18) + x(23) + k(1) + k(16) : 0})
print(r2)
this is a sage code, make r1 equals a polynomial include some variables, then use the subs() function to fix some variable, in this case, let the polynomial equals 0, so the expected result of r2 is 0, but the sage gives the result is
x(7) + x(10)*x(12) + x(18) + x(23) + k(1) + k(16)
i know , some thing wrong with the x(3)*x(8), but i don't know how to make it right.linuxCipherTue, 04 Dec 2018 04:41:19 -0600http://ask.sagemath.org/question/44578/How can I get back an expression for free variables in solve function.http://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution.
$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$
$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$
$solution=solve(eqns,A,B,C,D,E,F,G)$
Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$
Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?TheBeiramMon, 06 Nov 2017 04:29:40 -0600http://ask.sagemath.org/question/39396/subs() function gives KeyError when keyword is a list memberhttp://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/ I have a multivariate Boolean polynomial my_poly in xi (x0,x1,...,etc) and want to substitute one of the variables (say e.g. x0=0). my_poly subs(x0=0) works but I need to determine the exact variable (left hand side of '=') and value (right hand side of '=') at run-time depending on some conditions. The problem is subs() function do not accept expression on the left hand side of '=' and I have many xi variables, so if else is not practical. How can I solve this issue?adnanbaysalWed, 03 Feb 2016 03:25:06 -0600http://ask.sagemath.org/question/32426/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/Substitution using Dictionary with Matrix as Valuehttp://ask.sagemath.org/question/9075/substitution-using-dictionary-with-matrix-as-value/As a newcomer to SAGE, trying to use it to do symbolic linear algebra, I am wondering why substitution of a variable using a dictionary doesn't work in this case:
sage: aMatrix = matrix(SR,1,1)
sage: var('aVariable')
aVariable
sage: aDict = {}
sage: aDict[aVariable] = aMatrix
sage: aDict[aVariable]
[0]
but:
sage: aVariable.subs(aDict)
....
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:16025)()
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:11265)()
/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent_old.so in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3369)()
/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.coerce (sage/structure/parent.c:8912)()
TypeError: no canonical coercion from Full MatrixSpace of 1 by 1 dense matrices over Symbolic Ring to Symbolic Ring
Functionality to substitute matrices for variables seems to be indispensable to doing symbolic linear algebra, so I am sure there is a proper way to do this.
heatkernelThu, 14 Jun 2012 10:58:48 -0500http://ask.sagemath.org/question/9075/