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, 18 Nov 2019 06:45:46 -0600Possible memory leak in polynomial evaluationhttp://ask.sagemath.org/question/48793/possible-memory-leak-in-polynomial-evaluation/ Hello,
if I execute this code in SageMath 8.9
RRR = RealField(prec = 165)
P = PolynomialRing(RRR,'x')
g = P([1..55])
gpi = RRR.pi()
ME = 32
c = 1.5
gamma = 0.5
disk = [cos(2*gpi*i/ME)+I*sin(2*gpi*i/ME) for i in range(ME)]
ellipse = [gamma*(w+c^2/(4*gamma^2)/w) for w in disk]
while (true):
max([abs(g(x=z)) for z in ellipse])
print "memory usage: " + str(get_memory_usage())
I see that the memory usage increases as time goes by. On the other hand, if I substitute the expression for `ellipse` with something like `[1..ME]`, I don't see an increment in memory anymore. Is it a normal behaviour?fabio_fcMon, 18 Nov 2019 06:45:46 -0600http://ask.sagemath.org/question/48793/Get output of a .py file in a browserhttp://ask.sagemath.org/question/46004/get-output-of-a-py-file-in-a-browser/It seems like basic question, but I already spent about 2 hours, tried jupyter, command line, different combinations, different commands, lots of stuff…
Basically, I have file `test.py` with one line of content, taken straight from docs:
```
plot(graphs.HeawoodGraph())
```
I want to evaluate it, and get a picture of the plot in a browser.
What I don't want:
* to open some external app for PNG files because in future I gonna need interactivity
* to open the file in some text editor inside a browser because I already have it opened in Emacs
* to get errors about `graphs` being undefined because when I execute in `sage` terminal, it's all correctly defined.
* to search for `import`s to run every particular sage example from docs, because typing those examples directly in `sage` interactive interpreter works just fine *(the examples don't include `import`s)*, so I see no reason to put an unnecessary effort over that.Hi-AngelWed, 03 Apr 2019 09:21:06 -0500http://ask.sagemath.org/question/46004/Evaluate sage pyhttp://ask.sagemath.org/question/46003/evaluate-sage-py/ It seems like basic question, but I already spent about 2 hours, tried jupyter, command line, different combinations, different commands, lots of stuff…
Basically, I have file `test.py` with one line of content, taken straight from docs:
```
plot(graphs.HeawoodGraph())
```
I want to evaluate it in a browser.
What I don't want:
* to open some external app for PNG files because in future I gonna need interactivity
* to open the file in some text editor inside a browser because I already have it opened in Emacs
* to get errors about `graphs` being undefined because when I execute in `sage` terminal, it's all correctly defined.Hi-AngelWed, 03 Apr 2019 09:18:45 -0500http://ask.sagemath.org/question/46003/Evaluating a derivative of an unknown functionhttp://ask.sagemath.org/question/45758/evaluating-a-derivative-of-an-unknown-function/I need to evaluate the derivative of a certain unknown function, but instead of doing this what I get is the expression with a change in the name of the variable.
My example
z,l = var('z,l')
g = function('g', nargs=1)(z)
g_prime = g.derivative(z)(z)
g_prime(l)
So the output for that code is \frac{\partial}{\partial l} g (l) .
Instead, I would like \frac{\partial}{\partial z} g(l)Luis EscuderoMon, 11 Mar 2019 10:15:59 -0500http://ask.sagemath.org/question/45758/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/Evaluating a list of multivariate equations at random values.http://ask.sagemath.org/question/41662/evaluating-a-list-of-multivariate-equations-at-random-values/I have a list of multivariate equations, there are `o` multivariate equations in the list where `o` is an integer and `v = 2o`, the variables that make up the equations are the generators of `P = PolynomialRing(K,'x',n)` where `n = v + o` so for example if `o = 2` then `v = 4` and `n = 6` so the variables that make up the multivariate equations in the list would be `P.gens() = (x0, x1, x2, x3, x4, x5)`. Now i need to substitute in random values from `GF(q)` for the first `v` variables in each of the multivariate equations in the list so in the example above where `v = 4` random values from `GF(q)`would be substituted in for the variables `x0, x1, x2, x3`in each of the multivariate equations in the list, leaving equations in only two variables `x4 and x5`. I have tried to implement this but i don't really know where to start as i know i am doing something wrong but what i have is the following. If the list of multivariate equations is called `eqn` for example.
for i in range(v):
xi = var('x'+str(i))
eqnsub0 = eqn[i].subs({x[i]:GF(q).random_element()})
I already know this is very wrong as the indexing will already go out of bounds since `v > o` but i don't really know where to start with this. Any help would be great, thanks. DalvirTue, 20 Mar 2018 16:51:34 -0500http://ask.sagemath.org/question/41662/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/Plotting 2d vector fields – how to delay function evaluationhttp://ask.sagemath.org/question/36127/plotting-2d-vector-fields-how-to-delay-function-evaluation/I want to plot, for example, the following 2d vector field:
$$\vec{F}(x,y)= \begin{cases}
(0.1,0.1), & \text{for }r\le d_{min}\newline
\frac{\vec{r}}{r^3}, & \text{otherwise}.
\end{cases}$$
In one SageMath cell I have defined corresponding Python function:
reset()
dmin = 0.03
def my_vector_field_2D(x,y):
vector_field = vector([0.1,0.1])
r = vector([x,y])
if r.norm()>dmin:
vector_field = 1/ r.norm()^3 * r
return vector_field
In another cell, I use function `plot_vector_field` to plot the field:
var('x','y')
plot_vector_field(my_vector_field_2D(x,y),(x,-0.3,0.3),(y,-0.3,0.3))
However, function my_vector_field_2D(x,y) is evaluated before it is given to the function `plot_vector_field` and because in this evaluation my_vector_field_2D receives instead of numbers symbolic variables `x` and `y`, expression `r.norm()>dmin` is always `False`. As a result, my_vector_field_2D(x,y) is evalueted to: (0.100000000000000, 0.100000000000000). So the plotted filed is actually $\vec{F}(x,y)=(0.1,0.1)$ everywhere and that is not what I want.
How can I prevent this function evaluation?
I tried something that works with the `plot` function (provide the `plot` function with just the name of a function that you want to plot (without parameters) in order to prevent function evaluation):
plot_vector_field(my_vector_field_2D,(-0.3,0.3),(-0.3,0.3))
But here this method does not work: **TypeError**: 'function' object is not iterable.
**My first why question**: Why this does not work? It works for `plot` function and also for `contour_plot` function and probably for some other plot functions.
Then I found in the SageMath documentation that “when plotting non-symbolic functions, they should be wrapped in `lambda`”. So I tried:
plot_vector_field(lambda x,y:my_vector_field_2D(x,y),(-0.3,0.3),(-0.3,0.3))
And the same error popped up: **TypeError**: 'function' object is not iterable.
But this works:
plot_vector_field((lambda x,y:my_vector_field_2D(x,y)[0],lambda x,y:my_vector_field_2D(x,y)[1]),(-0.3,0.3),(-0.3,0.3))
**My second why question**: Why do I need two `lambda` functions or why do I need to manually decompose vector returned by my_vector_field_2D function into its two components?
vkFri, 30 Dec 2016 11:54:07 -0600http://ask.sagemath.org/question/36127/how to turn off symbolic sum evaluationhttp://ask.sagemath.org/question/9649/how-to-turn-off-symbolic-sum-evaluation/I made some symbolic manipulation and finally... I want to save a 'sum()' in a input form (a evaluated version is horrible big). It is possible to turn off the sum evaluation in SAGE? F_RanekMon, 04 Nov 2013 03:15:35 -0600http://ask.sagemath.org/question/9649/Displaying mathematical notation as "strings"http://ask.sagemath.org/question/34784/displaying-mathematical-notation-as-strings/ Hello!
I can't seem to find if there is a command (or if sage is even capable) to display mathematical notation as a string to be printed, rather than evaluated.
For example, if I try;
foo = exp(x)
foo2 = str(foo)
Then using "foo2" displays e^x, rather than the actual superscript x. Although this isn't terribly surprising to me, I'm curious if there is a way to get the actual correct notation?
An even more important one would be limit notation. I can't figure out how to display the text "Lim" with "x -> 0" underneath the text "Lim" at all in sage.
If these features aren't possible that would be good to know, but it seems like a common and straight forward issue. Unfortunately googling gets drown in irrelevant hits and I don't know the appropriate term to search for. A pointer in the right direction would be much appreciated.Jason021Fri, 09 Sep 2016 09:24:17 -0500http://ask.sagemath.org/question/34784/Convert a symmetric function into a polynomial on elementary symmetric functionshttp://ask.sagemath.org/question/33378/convert-a-symmetric-function-into-a-polynomial-on-elementary-symmetric-functions/The following is a variant of a previous question (32569). What command fills in the blank?
sage: e = SymmetricFunctions(QQ).e()
sage: u = e[3](e[2])
sage: u
e[3, 3] + e[4, 1, 1] - 2*e[4, 2] - e[5, 1] + e[6]
sage: R.<e1,e2,e3,e4,e5,e6> = PolynomialRing(QQ, 6)
sage: p = _the_blank_() # What command goes here?
sage: p
e1^2*e4 + e3^2 - 2*e2*e4 - e1*e5 + e6kedlayaWed, 11 May 2016 18:22:07 -0500http://ask.sagemath.org/question/33378/Evaluating a polynomial on a matriceshttp://ask.sagemath.org/question/33073/evaluating-a-polynomial-on-a-matrices/ Hi,
I would like to evaluate a polynomial f(x) with coefficients as integers by letting x to be a matrix T. How do I do this on sage? The context is this:
I have a matrix A. sage can tell me the characteristic polynomial of A. It will even factorize it for me, say, as f_1(x)*f_2(x)*f_3(x). I know how to extract each irreducible factor. Is there a way that I can use f_i(x) to evaluate it on a matrix, using sage?
(other than manually writing out the polynomial with x replaced by the matrix I wish to evaluate on)
I am new to sage so apologies if this is a silly question.
nehaTue, 12 Apr 2016 01:02:52 -0500http://ask.sagemath.org/question/33073/Evaluate whatever rsolve returns. Recurrence relationshttp://ask.sagemath.org/question/33009/evaluate-whatever-rsolve-returns-recurrence-relations/ sage: from sympy import *
sage: x = Function('x')
sage: n = var('n')
sage: q = rsolve(x(n+1)-(1+(3/(100)))*x(n),x(n),{x(0):10000})
sage: q
10000*(103/100)**n
I want to evaluate the result , say q(5) , but i get the following: ''TypeError: 'Mul' object is not callable''.
Can someone help me
brayn92298Tue, 05 Apr 2016 12:32:34 -0500http://ask.sagemath.org/question/33009/Fast evaluation of big polynomialshttp://ask.sagemath.org/question/30971/fast-evaluation-of-big-polynomials/ Hello.
I am working on my Master thesis in cryptography where i need evaluate 256 really big boolean polynomials of 256 variables of degree 8. I use this kind of evaluation:
values = [0, 1, 1, 0, ... , 1]
polynomial = x1x45x12x47x125x47x214x255 + ... + x3x25x32x47x122x47x234x250
tmp = polynomial(*values)
The problem is that it lasts too long.
Is there some faster way?
Thank YouProSun, 22 Nov 2015 16:15:59 -0600http://ask.sagemath.org/question/30971/Sage fails to evaluate this expression.http://ask.sagemath.org/question/26372/sage-fails-to-evaluate-this-expression/Sage doesn't seem to compute correctly `8*sin(pi/9)^3 - 6*sin(pi/9) + 3^(1/2)` which is plain zero.xiver77Sun, 29 Mar 2015 04:12:06 -0500http://ask.sagemath.org/question/26372/How to generate normal noise for a functionhttp://ask.sagemath.org/question/25933/how-to-generate-normal-noise-for-a-function/
sage: func(x) = x^2
sage: func_noise = func + normalvariate(0,1); func_noise
x |--> x^2 - 0.4436993396832335
How can one force the `normalvariate` to generate the number only on function evaluation?
fehuWed, 25 Feb 2015 15:45:44 -0600http://ask.sagemath.org/question/25933/Evaluating a symbolic expressionhttp://ask.sagemath.org/question/25228/evaluating-a-symbolic-expression/I am trying to evaluate a symbolical expression, but I don't know to ask Sage to compute the final expression:
phi = var('phi')
a=.25
beta=1
g=.2
ce=.5
alpha=.1
gamma=10
V=.5
rho=.9
sigma=2
sigmaeps=1
K=2
R=2
K2=2
avginv=K+R+K2
Sinv=phi*avginv
Swinv=K2+R-Sinv
psinv=(-1+(1+4*rho^(-2)*sigma^(-2)*K)^(1/2))/(2*rho^(-2)*sigma^(-2))
phipriv=K/avginv
phipub=Swinv/avginv
phimax=1-phipriv
phimin=alpha/(1+alpha)
prob=a-a*e^(-Swinv)
kappa=gamma*prob*(1-prob)*g^2
N=beta^(-1)*(1+alpha)*phi - beta^(-1)*alpha
L=beta^(-2)*(V+(1-phipriv)*avginv^(-1)+sigmaeps - 2*sigmaeps*phi + rho^2*sigma^2*psinv^(-2)*phipriv^2)
q=(beta*ce^(-1)-alpha*kappa^(-1)*N)*(gamma*L + ce^(-1)+kappa^(-1)*N^2)^(-1)
qN=q*N
kg=prob*g
t=-rho^(-1)+(gamma/2)*q^2*beta^(-2)
t2=t(phi=1/3)
print t2
sage: 5.00000000000000*(-0.666666666666667/((0.250000000000000*e^(-2) + 0.750000000000000)*(-2.50000000000000*e^(-2) + 2.50000000000000)) + 2.00000000000000)^2/(-1.77777777777778/((0.250000000000000*e^(-2) + 0.750000000000000)*(-2.50000000000000*e^(-2) + 2.50000000000000)) - 13.2881527307120)^2 - 1.11111111111111
Why Sage hasn't computed the final expression?
JorgeThu, 11 Dec 2014 14:22:32 -0600http://ask.sagemath.org/question/25228/Evaluation questionhttp://ask.sagemath.org/question/24732/evaluation-question/Please explain to me the difference between the two computations:
lin=2*x - y + 2.0;lin(x=-11/9,y=-4/9)
-2.22044604925031e-16
and
2*(-11/9)-(-4/9)+2.000000000000000000
0.000000000000000000
It has to be a floating point computation, and I'm interested only in the sign of the outcome, which is why the first example gives me trouble.
Justin HynesFri, 31 Oct 2014 14:56:22 -0500http://ask.sagemath.org/question/24732/Plot evaluation different than function evalutionhttp://ask.sagemath.org/question/11063/plot-evaluation-different-than-function-evalution/My sage input is
sage: eta, chi, omega, omega_n = var('eta chi omega omega_n')
sage: exp1=integrate(e**(-eta**2*chi**2/2)*e**(I*(omega_n - omega)*chi),(chi,-1,1))
sage: exp2 = e**(-(omega - omega_n)**2/(2*eta**2))
It turns out that exp1 is
$\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{\sqrt{2} \sqrt{\pi} \text{ erf}\left(-\frac{\sqrt{2} \eta^{2} + i \sqrt{2} \omega - i \sqrt{2} \omega_{n}}{2 \eta}\right) e^{\left(-\frac{\omega^{2}}{2 \eta^{2}} + \frac{\omega \omega_{n}}{\eta^{2}} - \frac{\omega_{n}^{2}}{2 \eta^{2}}\right)}}{2 \eta} + \frac{\sqrt{2} \sqrt{\pi} \text{ erf}\left(\frac{\sqrt{2} \eta^{2} - i \sqrt{2} \omega + i \sqrt{2} \omega_{n}}{2 \eta}\right) e^{\left(-\frac{\omega^{2}}{2 \eta^{2}} + \frac{\omega \omega_{n}}{\eta^{2}} - \frac{\omega_{n}^{2}}{2 \eta^{2}}\right)}}{2 \eta}$
I wanted to check if exp2 can be used as an approximation of exp1.
Now suppose I choose to evaluate the absolute error
sage: N(abs((exp1-exp2).subs(omega=2**4,omega_n=0,eta=0.4)))
0.0321314939737044
This is what it should be. However if I plot it (choosing five points for simplicity)
sage: p1 = plot(abs((exp1-exp2).subs(omega=2**4,omega_n=0)),(eta,0,1),plot_points=5)
sage: list(p1[0])
[(0.0025,0.0),(0.148624441929,0.0),(0.347230931288,0.0),(0.396882553627,0.0),(0.446534175967,0.031240245935),(0.545837420646,0.0291057729397),(0.741443657239,0.0242243187304),(1.0,0.0172533234321)]
For some reason below about 0.41 the plot function evaluates it to 0, but a call like
sage: N(abs((exp1-exp2).subs(omega=2**4,omega_n=0,eta=0.0025)))
0.0359877559973356
shows that this is incorrect behavior.
Any idea why plot fails to evaluate this correctly for "small" values?
The larger omega is, the worse the cutoff is. That is, large omega means that the plot goes to 0 for even if eta is larger than 0.41 in general. For example choosing omega=2**5 gives a cutoff at about eta = 0.84, so that plot just puts zeros for eta < .84 in this case.KyleThu, 20 Feb 2014 12:33:02 -0600http://ask.sagemath.org/question/11063/Two questions about parameters in solutionshttp://ask.sagemath.org/question/10997/two-questions-about-parameters-in-solutions/In the Sage documentation, this example is given:
sols = solve([x+y == 3, 2*x+2*y == 6],x,y); sols
of a solution which includes an extra parameter:
[[x=?r1+3,y=r1]]
1. If I run this comand again, the solution is given with r2, then again with r3 etc. Is it possible to "reset" the parameter number?
2. How does one substitute a particular value for this parameter? I can do it by iterating through the list and using rhs():
[i.rhs().subs(r1=1/2) for i in sols]
or by setting solution_dict=True and fiddling another way:
[sols[i].subs(r1=1/2) for i in [x,y]]
Neither of these seem particularly elegant - is there a more "natural" way?AlasdairWed, 05 Feb 2014 17:04:20 -0600http://ask.sagemath.org/question/10997/Why Sage cannot pass a value of variable from one function to another nested function?http://ask.sagemath.org/question/10677/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-function/The first i ran this:
sage: f(x)=(2/n)*(sin(n*x)*(-1)^(n+1))
sage: sum(f, n, 1, 2) #using summation function
-sin(2*x) + 2*sin(x)
So, In this case the result was evaluated correctly.
But if i tried to combine the first line and the second line together:
sage: f(x,k) = sum((2/n)*(sin(n*x)*(-1)^(n+1)), n, 1, k)
#where n = 1,2,3 ... k
sage: f(x,2)
-2*sum((-1)^n*sin(n*x)/n, n, 1, 2)
The result wasn't finished!
Why sage cannot evaluate mathematical expression in this case?
Another tried to prove that Sage can pass its variable from left function to right function even though the right function was a nested function:
sage: f(x) = sin(arcsin(x))
sage: f(0.5)
0.500000000000000
Edit:
(See [the same question on SO](http://stackoverflow.com/questions/19711247/why-sage-cannot-pass-a-value-of-variable-from-one-function-to-another-nested-fun).)
terces907Thu, 31 Oct 2013 07:09:56 -0500http://ask.sagemath.org/question/10677/polynomial evaluationhttp://ask.sagemath.org/question/9769/polynomial-evaluation/If I have a polynomial p in variables $x_0,...,x_n$, how do I specialize the algebra appropriately to substitute values for $x_i$'s? For example, how do I compute $p(1,1,...,1)$? Or replace $x_i$ by $q^i$ ($q$ a parameter) so to compute $p(1,q,...,q^n)$? In Mathematica, if the variables were x[[i]], one could do "./x[[i]] -> q^i //Simplify" and it is the equivalent of this replace and simplify that I am looking for.
This is coming from symmetric polynomials/functions theory and I know some of the specializations are built in, but at the end of the day I want to try small examples with different specializations than what is already built in.dbTue, 05 Feb 2013 03:27:47 -0600http://ask.sagemath.org/question/9769/Infinite graphs in sagehttp://ask.sagemath.org/question/8728/infinite-graphs-in-sage/I wonder if there is some sort of lazy evaluation in sage. Assume I want to answer a finite question about some infinite structure that can be represented using finite data. As a contrived example: Consider the graph that has as its vertices the natural numbers and an edge (i,j) whenever i divides j. Is there an elegant (lazy evaluation) way to represent this? For instance I could think of a function that investigates the sizes of connected components below the first 500 primes or such.ThomasTue, 21 Feb 2012 02:46:11 -0600http://ask.sagemath.org/question/8728/evaluating an expressionhttp://ask.sagemath.org/question/8470/evaluating-an-expression/Hello,
started to use open source mathematical software. And i have some questions since i can not find in google what i am searching for (maybe i am a bad searcher :S).
The thing is if i write in maple (with predefined F, M, q and Q):
Sum('F[ix]', 'i' = 1 .. n) = 0; '-2*F*cos(45*deg)-3*Q+4*RB*cos(45*deg)-RB*sin(45*deg)-M = 0'; evalf[4](solve(%, RB))
I get
RB := 19.51
And in Sage:
solve(-2*F*cos(45*deg)-3*Q+4*RB*cos(45*deg)-RB*sin(45*deg)-M,RB)
i get:
[23/3*sqrt(2) + 26/3 == 23/3*sqrt(2) + 26/3]
Now i do not find any equivalent to evalf in sage.
Could someone help me, please?JustisSun, 13 Nov 2011 23:59:50 -0600http://ask.sagemath.org/question/8470/Delayed evaluation without all the parenthesis?http://ask.sagemath.org/question/8459/delayed-evaluation-without-all-the-parenthesis/Is there a way to have delayed evaluation without having unnecessary parenthesis.
For example,
(x.add(x, hold = True)).add(x, hold = True)
gives
(x + x) + x
not
x + x + x
Thanks
EDIT:
Great answer burcin!
I was hoping to be able to it in more of a binary way though, so that I could use the Infix hack from http://code.activestate.com/recipes/384122-infix-operators/.
I wanted to make a binary operator which is still delayed. Defined here
class Infix(object):
def __init__(self, function):
self.function = function
def __ror__(self, other):
return Infix(lambda x: self.function(other, x))
def __or__(self, other):
return self.function(other)
p=Infix(lambda x,y: x.add(y, hold=True))
Usage is
x |p| x |p| x
AlisterWed, 09 Nov 2011 21:28:18 -0600http://ask.sagemath.org/question/8459/Idea behind not evaluating symbolic equationshttp://ask.sagemath.org/question/8431/idea-behind-not-evaluating-symbolic-equations/Can anyone explain me the following behavior of Sage: What is the idea behind not evaluating symbolic equations which are (obviously) true? For example:
sage: m = var('m')
sage: m == m
m == m
Evaluation works with
sage: bool(m == m)
True
Daniel KrennWed, 02 Nov 2011 23:44:22 -0500http://ask.sagemath.org/question/8431/Combination of lambda function and plot3d(?)http://ask.sagemath.org/question/7807/combination-of-lambda-function-and-plot3d/I'm having trouble understanding why the following code doesn't work like I'd hoped:
<pre>
def test1():
base_frame = point3d((.5,.5,1), color='red', size=10)
def f(x,y,t): return(sin(pi*x)*sin(pi*y)*e^(-t))
frames = []
for t in [0,1]:
pic = base_frame
pic += plot3d(lambda x,y:f(x,y,t), (x, 0, 1), (y, 0, 1))
#It works if you uncomment the next line:
#pic.save('/tmp/foo.png')
frames += [pic,]
return(frames)
</pre>
What happens is that both the frames (or all of them if I create more than two) end up containing the last plot. It appears that the plot doesn't get "evaluated" when I want it to. So far, the only mechanism I've found that can force it to be evaluated is inserting a .show() or a .save(), which won't work to well in a larger loop.
Now, I understand that the function "f" I've got here could have been rendered as an expression rather than a function. Then there's no problem. But in the real program, things are more complicated, and (afict) I really need the lambda.
Can anyone help me understand what's happening here, and how I might get this to work?
---------- UPDATE -----------
I realize I haven't been all that clear (in my own mind, as well as in my explanation) of what the problem is here. I think there are two issues (1) Getting the "lambda" thing to work, and (2) getting the plot to "evaluate." Here are three updated test cases, which contain a function that actually *requires* the lambda: test1() does what I want, but at the price of having to save every frame to a file. test2() and test3() do get the plot3d function to evaluate, but in both of those cases, I lose the "lambda" functionality and so the function being plotted isn't right.
<pre>
#
# Test file
#
# Make sure I'm using the same function in all three tests.
# I need a function that actually requires the use of lambda
var('x,y,t')
def f(x,y,t):
if x < .5: return(sin(pi*x)*sin(pi*y))
else: return(sin(pi*x)*sin(pi*y)*e^(-t))
# Might as well define the base frame here too
base_frame = point3d((.5,.5,1), color='red', size=10)
def test1():
frames = []
for t in [0,1]:
pic = base_frame
pic += plot3d(lambda x,y:f(x,y,t), (x, 0, 1), (y, 0, 1))
# This "save" appears to force the line above to be "evaluated"
pic.save('/tmp/foo.png')
frames += [pic,]
return(frames)
def test2():
frames = []
for t in [0,1]:
pic = base_frame
def g(x,y): return(f(x,y,t))
pic += plot3d(g(x,y), (x, 0, 1), (y, 0, 1))
frames += [pic,]
return(frames)
def test3():
frames = []
for t in [0,1]:
pic = base_frame
ff = lambda t:lambda x,y:f(x,y,t)
pic += plot3d(ff(t)(x,y), (x, 0, 1), (y, 0, 1))
frames += [pic,]
return(frames)
</pre>Mike WittSat, 11 Dec 2010 09:56:06 -0600http://ask.sagemath.org/question/7807/Is the expression (pi > 0) lazy evaluated?http://ask.sagemath.org/question/7743/is-the-expression-pi-0-lazy-evaluated/[Absolute beginner here.]
The expression $pi > 0$ has the type `sage.symbolic.expression.Expression`. How can I force the evaluation and get the result `True`.
Also, as per the question title, can you please point me to a reference on the evaluation model in Sage?Jennifer DylanTue, 26 Oct 2010 09:31:39 -0500http://ask.sagemath.org/question/7743/problems with symbolic integration and then numerical evaluatinghttp://ask.sagemath.org/question/7629/problems-with-symbolic-integration-and-then-numerical-evaluating/can anyone explain this:
sage: integrate(legendre_P(64,x)*sin((1+x)*pi/2),x,-1,1).n()
1.16508247725542e79
from approximation one know's that the legendre coefficients converge exponentially to zero and not to infinity!
and indeed with mpmath I get a better answer:
sage: import sage.libs.mpmath.all as mpmath
sage: mpmath.call(mpmath.quad,lambda x: mpmath.legendre(64,x)*mpmath.sin(pi/2*(x+1)),[-1,1])
-5.04684703543649e-25
Is there an overhead happening, when I numerically evaluate large rationals or something???
Thanks in advance,
maldun
maldunTue, 24 Aug 2010 12:08:05 -0500http://ask.sagemath.org/question/7629/