ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 17 May 2024 00:20:50 +0200Matrix functionhttps://ask.sagemath.org/question/77468/matrix-function/How do you calculate a matrix function in Sage? For example, exp is easily calculated as exp(A) or A.exp(). And how do you calculate sin(A), сos(A), ln(A)?Roman1988Fri, 17 May 2024 00:20:50 +0200https://ask.sagemath.org/question/77468/contour_plot behaves differently when I include the functional dependencehttps://ask.sagemath.org/question/77204/contour_plot-behaves-differently-when-i-include-the-functional-dependence/ If I include the functional dependence of my function, the contour_plot routine behaves differently. MWE:
var('x y')
def check(x,y):
val=-1. # negative by default,
for j in range(1,10):
for i in range(1,10):
if ( ( (x>0.9*i) and (x<1.1*i) ) and ( (y>0.9*j) and (y<1.1*j) ) ):
val=1.
return val
So this is kind of a strange function, but basically if (x,y) are near any of the 2d grid points (1-10), the function is 1 instead of zero. If I call this function without specifying the functional dependence, it looks right:
contour_plot(check,(x,-1.,8.),(y,-1.,8.))
![image description](/upfiles/17146635468964111.png)
However, if I call them and specify them explicitly, I get the unexpected (to me):
contour_plot(check(x,y),(x,-1.,8.),(y,-1.,8.))
![image description](/upfiles/17146636743397668.png)
(I grabbed the depreciation warning too, although I don't see how it matters here).
This is not just academic, because what I really want to do is have a four-variable funcftion, a la
def h(x,y,z,w):
...
and do a contour plot on two variables, setting the other two constant, like
contour_plot(h(x,y,0,1),....)
I'm having trouble, and trying to debug. Any thoughts?
thethinkerThu, 02 May 2024 17:34:30 +0200https://ask.sagemath.org/question/77204/Is there a SageMath version of Mathematica's '//' shorthand?https://ask.sagemath.org/question/77035/is-there-a-sagemath-version-of-mathematicas-shorthand/ If I had some expression `x^2 + x^3 - 31` and I want to apply a previously defined function `f` to it, is there a quicker way of doing `f(x^2 + x^3 - 31)`?
I'm thinking along the lines of Mathematica's `x^2 + x^3 - 31 // f`, where `//` applies the function to the expression that precedes it.reynormalizerMon, 22 Apr 2024 12:06:01 +0200https://ask.sagemath.org/question/77035/Defining a cocyclehttps://ask.sagemath.org/question/73895/defining-a-cocycle/ Hello everyone,
I would like to define an arbitrary group cocycle. That is, I want to define a function $f:A\times A\to \mathbb{T}$, where $A$ is my group and $\mathbb{T}$ is the unit circle, such that $f$ satisfies the following rule:
> $f(a,b)f(ab,c) = f(b,c)f(a,bc) \quad\forall a,b,c\in A.$
Any help will be appreciated it.asdfgSun, 15 Oct 2023 20:23:25 +0200https://ask.sagemath.org/question/73895/Define truncated power series as two variable function?https://ask.sagemath.org/question/72981/define-truncated-power-series-as-two-variable-function/I'd like to be able to define a function of two variables that involves a sum. My attempt is
x,n,k=var('x n k')
term(k,x) = (-1)^k*1/(2*k+1)*cos((2*k+1)*pi*x/2)
f(m,x) = 4/pi*sum(term(k,x),k,0,m)
show(f(2,x))
When I evaluate the function at any value of m, as above, sagemath doesn't expand the sum, or even evaluate to a number if I enter a value of x. Somehow the sum becomes atomic. Is there a way to make this sort of definition work?
My students naturally constructed truncated power series this way to experiment with in Desmos, and I'd like to bridge that gap into sagemath without Big-O notation or power series rings or anything like that.barremaMon, 28 Aug 2023 23:51:26 +0200https://ask.sagemath.org/question/72981/Plotting a Probability distribution with varying parameterhttps://ask.sagemath.org/question/69787/plotting-a-probability-distribution-with-varying-parameter/Suppose I wish to plot the function
import numpy as np
var('a,y')
A=np.linspace(-1,1,10)
for a in A:
z=a+x-(e^(-y^2/2))/sqrt((2*pi))
plot3d(z,(x,-1,1),(y,-1,1))
That is, I wish to plot the function `z` over a range of `a`. Actually the original function was `z=a+x`. But, in order to introduce randomization, I changed the variable `x` to `x-y'`, where `y'` is the standard normal variable. I wish to obtain the plot of `z` as a kind of variable probability distribution over the grid of `x` and `y` with the variation of `a` (so kind of series of probability distributions overlapping each other). How do I do this? Any hints? Thanks beforehand.vidyarthiFri, 07 Jul 2023 12:32:51 +0200https://ask.sagemath.org/question/69787/How can I calculate limit(cos(x+I*y),y=infinity) ?https://ask.sagemath.org/question/65551/how-can-i-calculate-limitcosxiyyinfinity/ How can I calculate limit(cos(x+I*y),y=infinity) ?hichemeMon, 26 Dec 2022 12:17:32 +0100https://ask.sagemath.org/question/65551/Displaying The Christoffel Symbols in Terms of a Function "F"https://ask.sagemath.org/question/65492/displaying-the-christoffel-symbols-in-terms-of-a-function-f/ When dealing with a metric-tensor that has a function "f" of the coordinates in it's terms, is it possible to express the christoffel symbols in terms of, say, df/dx, df/dy, etc, rather than displaying the entire output?Jack ZuffanteSat, 24 Dec 2022 06:42:40 +0100https://ask.sagemath.org/question/65492/how to import a function in another filehttps://ask.sagemath.org/question/63770/how-to-import-a-function-in-another-file/In python it's easy to find how can I do:
from file import func1, func2, func3
But I couldn't do in jupyter notebook. It didn't read the file.
For example dag is an function in sample_code ;
import ipynb
from ipynb.fs.full.sample_code import dag
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-cb32ddc8df9f> in <module>
----> 1 import ipynb
2 from ipynb.fs.full.sample_code import dag
ModuleNotFoundError: No module named 'ipynb'
Thanks for advance.SuleThu, 25 Aug 2022 13:26:40 +0200https://ask.sagemath.org/question/63770/compose (non-)symbolic functionshttps://ask.sagemath.org/question/63574/compose-non-symbolic-functions/I wonder how to compose with a non-symbolic function, or, more generally, how to compose several functions, if some of them are non-symbolic.
In my concrete case, I wish to plot a transformed Gaussian distribution function (concretely, the Camp-Paulson approximation), comparing with a plot of probabilities of the binomial distribution this approximates.
What works, but is not flexible enough is:
import scipy.stats
n=20
p=0.05
binom_dist = scipy.stats.binom(n,p)
Bin=bar_chart([binom_dist.pmf(x) for x in range(n+1)],width=1)
T = RealDistribution('gaussian', 1)
CP=plot(lambda k:T.distribution_function(-1/3*(((0.95*k + 0.95)/(-0.05*k + 1.0))^(1/3)*(1/(k + 1) - 9) + 1/(k - 20) + 9)/sqrt(((0.95*k + 0.95)/(-0.05*k + 1.0))^(2/3)/(k + 1) - 1/(k - 20))),(k,-1,21) , rgbcolor=(0.8,0,0))
show(Bin+CP)
This option has the argument of T.distribution_function hard coded. I rather would have this computed from n and p, using another function. I have two approaches so far, that both don't work.
The first one has a function K computing the input to T.distribution_function like so:
import scipy.stats
n=20
p=0.05
binom_dist = scipy.stats.binom(n,p)
Bin=bar_chart([binom_dist.pmf(x) for x in range(n+1)],width=1)
T = RealDistribution('gaussian', 1)
#
a = 1/(9*n-9*k)
b = 1/(9*k+9)
r = (k+1)*(1-p)/(n*p-k*p)
c = (1-b)*r^(1/3)
μ = 1 - a
σ = sqrt(b*r^(2/3) + a)
def K(k): return (c - μ)/σ
#
CP=plot(lambda k:T.distribution_function(K(k)),(k,-1,21), rgbcolor=(0.8,0,0))
show(Bin+CP)
another alternative tries to set up the composed function before entering plot like so:
import scipy.stats
n=20
p=0.05
binom_dist = scipy.stats.binom(n,p)
Bin=bar_chart([binom_dist.pmf(x) for x in range(n+1)],width=1)
T = RealDistribution('gaussian', 1)
#
a = 1/(9*n-9*k)
b = 1/(9*k+9)
r = (k+1)*(1-p)/(n*p-k*p)
c = (1-b)*r^(1/3)
μ = 1 - a
σ = sqrt(b*r^(2/3) + a)
def distCPapprox(k): return T.distribution_function((c - μ)/σ)
#
CP=plot(lambda k:distCPapprox(k),(k,-1,21), rgbcolor=(0.8,0,0))
show(Bin+CP)
Both approaches don't work. I searched extensively for advice for function composition but couldn't find anything helpful. Your advice is greatly appreciated.oloidSun, 07 Aug 2022 17:36:19 +0200https://ask.sagemath.org/question/63574/How to define a matrix with variables in SageMath?https://ask.sagemath.org/question/62878/how-to-define-a-matrix-with-variables-in-sagemath/I would like to define a function which produce matrices with variables. It will be something like the following.
def MatrixG(x,n,m):
r=Matrix(n,m)
for i in [0..n-1]:
for j in [0..m-1]:
r[i,j]=x[i,j]
return r
Here x[i,j]'s are variables. How to write the above correctly in Sage?
I also would like to have LU decomposition of some matrix like r1=MatrixG(3,3), r1 is a symbolic matrix.
Thank you very much.lijr07Sat, 18 Jun 2022 12:36:29 +0200https://ask.sagemath.org/question/62878/limit within a python functionhttps://ask.sagemath.org/question/61618/limit-within-a-python-function/Hi there,
total beginner in Sage, and this drives me mad:
def mylimit(f,x,a):
return f.limit(x=a)
var('y')
mylimit(1/y,y,2).show()
which always returns: 1/y...
How shall I specify that x should be a var?
Thanks in advance.
eatdustFri, 25 Mar 2022 14:17:15 +0100https://ask.sagemath.org/question/61618/Assume a function is real-valuedhttps://ask.sagemath.org/question/61315/assume-a-function-is-real-valued/ In the following expression:
var('z')
u = function('u')(z)
v = function('v')(z)
f = u + i * v
show(f.diff(z).conjugate().expand().simplify_full())
# conjugate(diff(u(z), z)) - I*conjugate(diff(v(z), z))
I would like Sage to make the simplification `conjugate(diff(u(z), z)) = diff(u(z), z)` because $u, v$ are intended to be real-valued. But my first attempt:
assume(u, 'real')
didn't work:
> TypeError: self (=u(z)) must be a relational expression
What is the right way to write this assumption?ripple_carrySun, 27 Feb 2022 20:45:00 +0100https://ask.sagemath.org/question/61315/How can I substitute an unknown function in an expression with a known function?https://ask.sagemath.org/question/60468/how-can-i-substitute-an-unknown-function-in-an-expression-with-a-known-function/I have defined an unknown function "p" with command
p=function("p")
Then I deduced another function, which contains "p":
aws(time)=921600*(-1333.33340000000*time + 2000)/(-3.36000004800000e6*p(time) + 3840000)
Now I would like to plot the function aws(time), by subtituting p(time) with a known function, like:
p(time)=time^0.54
I discovered I can't use .subs() as with free variables, so this doesn't work:
aws.subs(p(time)==time^0.54)
Then how can I substitute the known function, to be able to plot aws(time) finally?
KonstantinTue, 28 Dec 2021 03:11:56 +0100https://ask.sagemath.org/question/60468/How can I plot two parametric functions in a common coordinate system?https://ask.sagemath.org/question/60387/how-can-i-plot-two-parametric-functions-in-a-common-coordinate-system/ I can easily use parametric_plot() to plot 1 parametric function at once. For example
parametric_plot((sin(2*t),cos(3*t)),(t,0.0,9),aspect_ratio=1)
plots a nice Lissajous curve.
I also can plot a circle:
parametric_plot((sin(t),cos(t)),(t,0.0,9),aspect_ratio=1)
But how can I plot both of them in a common coordinate system?
I tried:
parametric_plot([(sin(2*t),cos(3*t)),(sin(t),cos(t))],(t,0.0,9),aspect_ratio=1)
Which doesn't work -> Last error message: ''tuple' object is not callable'
I also tried:
pl1=parametric_plot((sin(t),cos(t)),(t,0.0,9),aspect_ratio=1)
pl2=parametric_plot((sin(2*t),cos(3*t)),(t,0.0,9),aspect_ratio=1)
pl1.add_primitive(pl2)
pl1
Error: 'Graphics' object has no attribute 'options'
Then how can I use parametric_plot() to plot two parametric functions at once, in a common coordinate system?
KonstantinWed, 22 Dec 2021 23:45:43 +0100https://ask.sagemath.org/question/60387/Redefine symbolic function even in derivativeshttps://ask.sagemath.org/question/60324/redefine-symbolic-function-even-in-derivatives/As a simple example, I have the following variables and functions:
r = var('r')
th = var('th', latex_name = '\\theta')
g = function('g')(r, th)
f = g + diff(g,r)
**From now on** I want to decompose `g` into `g0`and `g2` as:
g0 = function('g0', latex_name = 'g_0')(r)
g2 = function('g2', latex_name = 'g_2')(r)
g = g0 + cos(th)*g2
If I print `f` I still get:
$ f = g(r, \theta) + dg(r,\theta)/dr $
So instead, I apply a substitution on `g`:
f = f.subs(g==g0+cos(th)*g2)
This changes `g`, but not `diff(g,r)`:
$f = g_0(r) + g_2(r)*\cos(\theta) + dg(r,\theta)/dr$
In order to change also `diff(g,r)` I have to substitute explicitly the derivative:
f = f.subs(diff(g,r) == diff(g0+cos(th)*g2, r))
My question is the following:
Is there a way I can redefine a function without having to redefine also every single derivative? This way, I would avoid having to write all these substitutions:
diff(g,r) == diff(g0+g2*cos(th), r),
diff(g,r,r) == diff(g0+g2*cos(th), r, r),
diff(g,th) == diff(g0+g2*cos(th), th),
diff(g,th,th) == diff(g0+g2*cos(th), th, th),
diff(g,th,r) == diff(g0+g2*cos(th), th, r),
diff(g,r,th) == diff(g0+g2*cos(th), r, th)kekoFri, 17 Dec 2021 12:34:35 +0100https://ask.sagemath.org/question/60324/Globally redefine symbolic function in a tensor fieldhttps://ask.sagemath.org/question/59686/globally-redefine-symbolic-function-in-a-tensor-field/As a simple example, I have the following manifold and chart:
M = Manifold(2, 'M', structure='Lorentzian')
X.<t,r> = M.chart(r"t r:(0,+oo)")
with these functions:
h = function('h')(r)
h0 = function('h0')(r)
Now, I define the following tensor:
A = M.tensor_field(0,2)
A[0,0] = h
If I print `A`, I get:
[h(r) 0]
[ 0 0]
as I expected. However, from now on I want `h` to be:
h = 2*h0
After setting `h=2*h0`, if I print `A[:]` I get the same tensor I had before, instead of `A[0,0] = 2*h0`.
How can I redefine a symbolic function inside a tensor? I have tried with `A.subs` and `A.apply_map`, but none of them did the job.kekoThu, 11 Nov 2021 16:51:30 +0100https://ask.sagemath.org/question/59686/Substitute multiplication of sine and cosine for a symbolic functionhttps://ask.sagemath.org/question/59881/substitute-multiplication-of-sine-and-cosine-for-a-symbolic-function/I have one variable and two functions:
th = var('th')
f = function('f')(th)
omega = function('omega')(th)
and the following equation (this is an example, my actual equation has more terms such as +cos(th)+sin(th)):
eq = 2*pi*cos(th)*sin(th)*diff(omega,th,th)
Now, I want cos(th)*sin(th) to be substituted for f. I have tried the following:
eq = eq.substitute(cos(th)*sin(th)==f)
and also,
expression = cos(th)*sin(th)
eq = eq.substitute_function(expression,f)
but when I print eq, I still get:
> 2 * pi * cos(th) * sin(th) * diff(omega(th), th)
How can I do it? It should also work for different permutations of the individual components of the equation. For example:
eq = 2*pi*sin(th)*diff(omega,th,th)*cos(th)
I have found that a [similar question](https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/) was asked a long time ago, where the author commented that maxima.fullratsubst did the job. However, I am not capable of working with it; I have run
eq = maxima.fullratsubst(f,cos(th)*sin(th),eq)
directly on SageMath, but I guess I am doing something wrong since it does not work.kekoMon, 22 Nov 2021 16:00:40 +0100https://ask.sagemath.org/question/59881/User defined functionhttps://ask.sagemath.org/question/59838/user-defined-function/I'm working with beta distribution and its integral. A straightforward integral of beta distribution would return the hypergeometric form of the incomplete beta function. This makes my results very messy and difficult to work with. I have been able to define a function class IncBeta as
def D_t(self, t, z, w, **kwds):
return t^(z-1)*(1-t)^(w-1)#/beta(z, w)
IncBeta=function('IncBeta', nargs=3, derivative_func=D_t, latex_name='I_x')
Is there a way that I can define an inverse function class of the above and tell Sage the integral of that inverse function leads to IncBeta?LiangFri, 19 Nov 2021 17:39:20 +0100https://ask.sagemath.org/question/59838/monte_carlo_integral using already defined variablehttps://ask.sagemath.org/question/59601/monte_carlo_integral-using-already-defined-variable/ hello, i have some function that i already defined, and i want to do monte carlo integral on in:
a,b,c = var('a,b,c')
c = 1
f = c*a*b
monte_carlo_integral(lambda a,b: f, [0,0], [3,3], 1000)
but i get many errors like "unable to simplify to float approximation".
i understand that when using the lambda its different a,b than my previous a,b.
and that this works:
a,b,c = var('a,b,c')
c = 1
monte_carlo_integral(lambda a,b: c*a*b, [0,0], [3,3], 1000)
but i have many calculations on f before doing the integral, so is there any way to make it work?zaharFri, 05 Nov 2021 21:19:52 +0100https://ask.sagemath.org/question/59601/Assign value to symbolic function?https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/I have a symbolic function $g(x,y)$, which depends on the variables $x$ and $y$. Using this, I define the function $f(x,y)$ as:
\begin{equation}
f(x,y) = g(x,y) + 2x.
\end{equation}
If I calculate the derivative of $f(x,y)$ with respect to $x$:
\begin{equation}
\frac{df(x,y)}{dx}=\frac{dg(x,y)}{dx}+2.
\end{equation}
Now, I need to evaluate this at $x=0$, knowing that $\frac{dg(x,y)}{dx}\bigg\rvert_{x=0}=10$. This should give me:
\begin{equation}
\frac{df(x,y)}{dx}\bigg\rvert_{x = 0} = \frac{dg(x,y)}{dx}\bigg\rvert_{x = 0} +2=12
\end{equation}
The code I have written to achieve this is the following:
x = var('x')
y = var('y')
g = function('g')(x,y) #symbolic function
f = g + 2*x
der_f = diff(f,x); der_f
and this is what I get:
diff(g(x, y), x) + 2
as I expected. However, I don't know how to follow. In particular, I need to know how to:
1) assign $\frac{dg(x,y)}{dx}\bigg\rvert_{x=0}=10$,
2) evaluate $\frac{df(x,y)}{dx}$ at $x=0$, so that I obtain $\frac{df(x,y)}{dx}\bigg\rvert_{x = 0} = 12$.kekoFri, 29 Oct 2021 10:56:21 +0200https://ask.sagemath.org/question/59524/function changing input valuehttps://ask.sagemath.org/question/59336/function-changing-input-value/The following code is supposed to implement a basic percolation process on a matrix of 0's and 1's. For some reason, it's changing the value of the matrix I input, and I can't figure out why. I'm sure it's a basic error on my part, but if someone can set me straight, I'd appreciate it!
Here's the function I defined:
<pre><code>
n = 4;
def percolate(B):
update = B;
for i in [0..n-1]:
for j in [0..n-1]:
nbr_sum = 0;
if (i==0):
nbr_sum += B[i+1,j];
elif (i==n-1):
nbr_sum += B[i-1,j];
else:
nbr_sum += B[i-1,j]+B[i+1,j];
if (j==0):
nbr_sum += B[i,j+1];
elif (j==n-1):
nbr_sum += B[i,j-1];
else:
nbr_sum += B[i,j-1]+B[i,j+1];
if (nbr_sum > 1):
update[i,j] = 1;
return update;
</code></pre>
When I execute
<pre><code>
Bid = matrix.identity(n);
percolate(Bid)
</code></pre>
I get the expected output
<pre><code>
[1 1 0 0]
[1 1 1 0]
[0 1 1 1]
[0 0 1 1]
</code></pre>
but the value of <code>Bid</code> also changes to the same value (as opposed to continuing to be the n x n identity matrix, as desired).Kyle OrmsbyTue, 12 Oct 2021 19:31:54 +0200https://ask.sagemath.org/question/59336/function vs expressionhttps://ask.sagemath.org/question/58733/function-vs-expression/ Hi there,
Is there a difference between, e.g.,
g=x^2
and
f(x)=x^2
?
In the literature the first is considered to be an expression while the other is a function, but checking their types gives
<class 'sage.symbolic.expression.Expression'>
in both cases.eyalSun, 29 Aug 2021 14:55:38 +0200https://ask.sagemath.org/question/58733/Evaluation of Boolean function at a point.https://ask.sagemath.org/question/57285/evaluation-of-boolean-function-at-a-point/Hello! I am struggling with the following code. I have a random Boolean function, f, in n variables (n varies) and also a set of n tuples which is generated from some other code. Now I want to evaluate the function f at one of the points, i.e., I want to determine f(a). But a, being of the form say a = [random.randit(0,1) for i in range(n)], is not being taken as an input for f. Moreover changing a to tuple(a) or list(a) neither works. I am writing down the problem in the following code:
`n = 4`
`R = BooleanPolynomialRing(n,['x%d'%i for i in range(n)])`
`f = R.random_element()`
`print(f)`
`import random`
`a = [random.randint(0,1) for i in range(n)]`
`print(f(a))`
Upon running this shows the error:
`"Number of arguments is different from the number of variables of parent ring."`DodulThu, 27 May 2021 00:37:22 +0200https://ask.sagemath.org/question/57285/How to define functions with varying number of variableshttps://ask.sagemath.org/question/56908/how-to-define-functions-with-varying-number-of-variables/I want to compute the Frechet derivative for a vector field, and that code works perfectly for functions with two variables *x* and *t*.
var ("t x")
v = function ("v")
u = function ("u")
w1 = function ("w1")
w2 = function ("w2")
eqsys = [diff(v(x,t), x) - u(x,t), diff(v(x,t), t) - diff(u(x,t), x)/(u(x,t)**2)]
def FrechetD (support, dependVar, independVar, testfunction):
frechet = []
eps = var ("eps")
v = independVar[:] + [eps]
for j in range (len(support)):
deriv = []
for i in range (len(support)):
r0 (x, t, eps) = dependVar[i](*independVar)+ testfunction[i](*independVar) * eps
s = support[j].substitute_function (dependVar[i], r0)
deriv.append (diff(s, eps).subs ({eps: 0}))
frechet.append (deriv)
return frechet
FrechetD (eqsys, [u,v], [x,t], [w1,w2])
[[-w1(x, t), diff(w2(x, t), x)],
[2*w1(x, t)*diff(u(x, t), x)/u(x, t)^3 - diff(w1(x, t), x)/u(x, t)^2,
diff(w2(x, t), t)]]
but my problem is the line
r0 (x, t, eps) = dependVar[i](*independVar)+ testfunction[i](*independVar) * eps
because this depends on the hardcoded *x* and *t*. Without it the following derivation for *eps* always is 0. Even when I add someting like
_r0 = function('ro')(*v)
doesn't work.
What i want to do is to something like
r0 (*v) = dependVar[i](*independVar)+ testfunction[i](*independVar) * eps
to get rid of the hardcoded variables and/or the number of variables. Is that possible ?
tamanduaSun, 02 May 2021 21:54:48 +0200https://ask.sagemath.org/question/56908/A simple sum causes division by zero exceptionhttps://ask.sagemath.org/question/55766/a-simple-sum-causes-division-by-zero-exception/I'm very new to SageMath and did some small experiments yesterday. This is my Code:
x, n, r, i = var('x, n, r, i')
f(x, n, r) = x * sum((1 + r) ^ i, i, 1, n)
f(1, 30, 0.0)
It works well if `r != 0`, but will raise a exception of "division by zero" if equal.
My ipynb file: nbviewer.jupyter.org/gist/7sDream/5db3cfd153269fd1a1cacaf0b60f69bb
It seems some optimizations of sum did not consider the range of variable `r`.
But there is also no change if I added `assume(r >= 0)`before define `f`.
So how can I disable this sum optimizations? Or what is wrong with the way I use it?7sDreamFri, 19 Feb 2021 12:53:02 +0100https://ask.sagemath.org/question/55766/Mapping isomorphism for posets or graphshttps://ask.sagemath.org/question/55675/mapping-isomorphism-for-posets-or-graphs/I know that we can check if two posets/graphs are isomorphic using is_isomorphic(), but is there any way that I can get Sage to output a possible mapping between the two posets/graphs that are isomorphic?sunflowerThu, 11 Feb 2021 18:59:45 +0100https://ask.sagemath.org/question/55675/Quickly recalling the definition of a temporary function to make minor edits?https://ask.sagemath.org/question/54736/quickly-recalling-the-definition-of-a-temporary-function-to-make-minor-edits/Suppose you're working with the sage console primarily as a calculator, and define some temporary shorthand function, such as,
sage: R = GF(751)
sage: def f(x):
....: return R(x^3-x+188)
After I've used this `f` several times, I may want to change the output to something similar, like `x^3-x+376`. Is there a quick way to recall the definition of `f` within the console so that I can edit in the minor change, instead of having to define a new function?vikahdv@gmail.comFri, 18 Dec 2020 03:46:59 +0100https://ask.sagemath.org/question/54736/Is there a shortcut command to iterate an endomorphism?https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/Suppose $f$ is a function from some set to itself. Is there a shortcut command in sage to compute an iterated composition of $f$, such as $f^3(x)=f(f(f(x)))$ where I could type something like `f^3(x)` into the console, and more generally for computing $f^n(x)$ for a given $n$?vikahdv@gmail.comTue, 01 Dec 2020 05:08:08 +0100https://ask.sagemath.org/question/54479/Plot ratio of Bessel functionshttps://ask.sagemath.org/question/54361/plot-ratio-of-bessel-functions/I am new to SageMath and to this page. I'm running this version:
`SageMath version 9.2, Release Date: 2020-10-24 Using Python 3.8.5.`
I tried to plot the ratio of two functions related to Bessel functions.
First I defined `f` as the first derivative of `bessel_J(1, x)`
and `g`as `x*bessel_J(1, x)`:
f(x) = bessel_J(1, x)
g = derivative(f, x)
Then I defined `h` as their ratio:
h = g / (x*f)
Then, I tried to plot `h` with:
plot(h, (x, 0, 10))
The result is an empty plot, showing only the x, y axes, and this is not correct.
Am I doing something wrong? Is it possible to plot such a function and, if yes, how?wmaddoxSun, 22 Nov 2020 23:49:14 +0100https://ask.sagemath.org/question/54361/