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.Mon, 28 Aug 2023 23:51:26 +0200Define 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 f(x,y)=x/y also plots the plane y=0 instead of being undefinedhttps://ask.sagemath.org/question/54072/plotting-fxyxy-also-plots-the-plane-y0-instead-of-being-undefined/when i plot f(x,y)=x/y plot3D additionally draws the plane y=0... thus the level curves become crosses instead of lines ... Here is my code :
var('x','y','z')
f(x,y)=x/y
from sage.plot.plot3d.plot3d import axes
P = axes(2, color='black')
P=plot3d(f(x,y),(x,-4,4),(y,-4,4),color='khaki',opacity=0.7)
levels=[-3,-2,-1,0,1,2,3]
epsilon=0.1
for h in levels:
P+=implicit_plot3d(f(x,y)==h,(x,-4,4),(y,-4,4),(z,h,h+epsilon),color='red')
P.show()
Additionally i would like to specify the range of z (i.e. between -10 and 10), and i cannot find how to do that in the manuel...QdemourguesThu, 29 Oct 2020 13:02:14 +0100https://ask.sagemath.org/question/54072/Roots of multivariable polynomials with respect to one variable?https://ask.sagemath.org/question/44686/roots-of-multivariable-polynomials-with-respect-to-one-variable/This question was previously titled "Finding residues of a huge multivariable rational function."
From my understanding, when computing with huge rational functions, we shouldn't use symbolic variables. However, I don't see how to find roots and residues without symbolic variables. Here is a small scale example of the issue. I have a rational function that looks like below:
$f(u_1,x_1,u_2,x_2,u_3,x_3) = \frac{1}{{\left(u_{1} u_{2} u_{3} - x_{1} x_{2} x_{3}\right)} {\left(u_{1} u_{2} u_{3} - 1\right)} {\left(u_{1} u_{2} - x_{1} x_{2}\right)} {\left(u_{1} u_{2} - 1\right)} {\left(u_{1} - x_{1}\right)} {\left(u_{1} - 1\right)}}$
First I want to solve for $u_1$ in the denominator and find those roots (poles) that have $x_1$ as below.
Then I will loop through the roots and compute the residue of $f$ w.r.t. $u_1$ at those poles.
u1,u2,u3,x1,x2,x3 = var('u1,u2,u3,x1,x2,x3') #symbolic variables
f=1/((u1*u2*u3 - x1*x2*x3)*(u1*u2*u3 - 1)*(u1*u2 - x1*x2)*(u1*u2 - 1)*(u1 - x1)*(u1 - 1))
fden=f.denominator() #denominator of f
list1=fden.roots(u1) #poles of u1
[root for (root, multiplicity) in list1] #list of roots
The output is
[x1*x2*x3/(u2*u3), x1*x2/u2, x1, 1/(u2*u3), 1/u2, 1]
Then we choose those roots that have $x1$
poles1 = [x1*x2*x3/(u2*u3), x1*x2/u2, x1] #choose those that have x1
Finally, we find the residue of $f$ w.r.t $u1$ of the rational function at the poles containing $x1.$
ans1=0
for ff in poles1:
tmp=f.residue(u1==ff)
ans1+=tmp #ans1 is the residue of f w.r.t u1 at all the poles containing x1
ans1
The output is then
1/((u2*u3*x1 - x1*x2*x3)*(u2*u3*x1 - 1)*(u2*x1 - x1*x2)*(u2*x1 - 1)*(x1 - 1)) - 1/((u3*x1*x2 - x1*x2*x3)*(u3*x1*x2 - 1)* (x1*x2 - 1)*u2*(x1 - x1*x2/u2)*(x1*x2/u2 - 1)) + 1/((x1*x2*x3 - 1)*(x1*x2 - x1*x2*x3/u3)*(x1*x2*x3/u3 - 1)*u2*u3*(x1 x1*x2*x3/(u2*u3))*(x1*x2*x3/(u2*u3) - 1))
Then I replace $f$ with $ans1$ to continue to do the same process w.r.t $u2$ and poles containing $x2$ and finally w.r.t $u3$ and poles containing $x3.$ However, this consumes about 800GB of memory on an HPC when I feed it a larger rational function.
Is there a way to find
1. roots of multivariable polynomials with respect to one variable?
2. residue of a rational function avoiding symbolic variables?
Both .roots() and .residue() are not defined for rational functions that are not defined in terms of symbolic variables.HoldenSun, 16 Dec 2018 23:32:23 +0100https://ask.sagemath.org/question/44686/A way of checking that a multivariate system of equations has a solution?https://ask.sagemath.org/question/41913/a-way-of-checking-that-a-multivariate-system-of-equations-has-a-solution/Is there a way to check that a multivariate system of polynomial equations does indeed have a solution(or not)? The context i am using this in is as follows: say i have a system of multivariate polynomial equations over a finite field, i then put values in for some of the variables, and then i need to check if the resulting system in the remaining unassigned variables has a solution or not? If not, then i will reassign different values to the other variables until a system with a solution is yielded. Of course because the assigning of these values is random there is a high chance the system has a solution anyway, but in the event that it does not is where i need a way to check whether a solution exists or not.DalvirSun, 08 Apr 2018 20:40:45 +0200https://ask.sagemath.org/question/41913/Evaluating a list of multivariate equations at random values.https://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 22:51:34 +0100https://ask.sagemath.org/question/41662/Different syntax to solvehttps://ask.sagemath.org/question/33361/different-syntax-to-solve/ I've below equation( j.mp/sage_emi ) involving 4 variables. I've fixed 3 variables' values, but can't get the 4th variables' value (both, including numerical). Can someone fix the code? Manually, I can get the answer for emi as 1210/21.
P, emi, rate, duration = var("P emi rate duration")
eq1 = P = emi * (( 1 + rate / 100)^ duration - 1) / (rate / 100) / (1 + rate /100)^ duration
solve([eq1, P == 100, duration == 2, rate == 10], emi)
It would be helpful if someone writes 2,3 different sage code to achieve the same above objective.
I'm just curious can we do this:
solve([eq1], rate)
Sukrit-GuptaWed, 11 May 2016 07:32:54 +0200https://ask.sagemath.org/question/33361/How to declare another variable for a polynomialhttps://ask.sagemath.org/question/31696/how-to-declare-another-variable-for-a-polynomial/ n=var('n")
def T(n):
a=1
b=x
if n==0:
return a
if n==1:
return b
else:
for i in [2..n]:
b=2*x*b-a
a=(a+b)/(2*x)
return b
**Could you help me to compute T(i) at x=1 or anything ?Mean that I want to declare x is a variable of T(i), but I can't.
Thank you so much**MinhminhSat, 19 Dec 2015 12:19:25 +0100https://ask.sagemath.org/question/31696/problem with numerical integration and differentiation with scipyhttps://ask.sagemath.org/question/9272/problem-with-numerical-integration-and-differentiation-with-scipy/Hi,
I was trying to learn Scipy, using it for integration and differentiation, but at the very initial step I encontered the following problems.
First, the following integration has a complex value as its numerical result when is solved analytically, but the numerical solution only gives its real part without any note for caution:
reset()
forget()
import scipy as sp
from scipy import integrate
var('y')
f=lambda x: 10^10*sin(x)
g=lambda x,y: f(x+y^2)
I=integrate.dblquad(g(x,y), 0, pi, lambda y:0,lambda y:1)[0]; show(I)
show( integral(integral(g(x,y),x,0,1),y,0,pi) )
show( integral(integral(g(x,y),x,0,1),y,0,pi).n() )
is this natural or I am doing something wrong that may cause my even **Real calculations** erratic?
Next, about numerical differentiation, it seems that the only function that works for callable functions is scipy.derivative() if I'm right!? However, I couldn't work with it:
**1st)** when I am not going to specify the point at which the differentiation is to be taken, e.g. when the differentiation is under an integral so that it is the integral that should assign the numerical values to its integrand's variable, not me. As a simple example I tried this code:
import scipy as sp
from scipy import integrate, derivative
I2=integrate.quad( sp.derivative(f(y),y, dx=0.00001, n=1, order=7) , 0, pi,)[0]; show(I2)
show( integral(diff(f(y),y),y,0,1).n() )
also it gives the warning that "Warning: The ocurrence of roundoff error is detected, which prevents the requested tolerance from being achieved. The error may be underestimated."
**2nd)** when I want to find the derivative of a multivariable function like g(x,y) in the above example, and something like sp.derivative(g(x,y),(x,0.5), dx=0.01, n=1, order=3) gives error as was easily expected.
Looking forward to hearing from you if that integration result is natural and how to resolve the above cited problems with numerical differentiation.
Best RegardsowariTue, 28 Aug 2012 12:15:47 +0200https://ask.sagemath.org/question/9272/