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.Thu, 27 Aug 2020 23:50:02 +0200Why doesn't the SageMath function find_local_maximum work for me in Python?https://ask.sagemath.org/question/53203/why-doesnt-the-sagemath-function-find_local_maximum-work-for-me-in-python/The code below works in CoCalc, but I can't get it to run in Python in my IDE. I've imported numpy and sagemath, and referred to each as needed to define the math functions, but I can't get the find_local_maximum function to work. See below.
Code:
import numpy as np
import sagemath as sm
def mean_x(factor, values):
return sum([np.cos(2*np.pi*v/factor) for v in values])/len(values)
def mean_y(factor, values):
return sum([np.sin(2*np.pi*v/factor) for v in values])/len(values)
def calculatePeriodAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = np.sqrt(mx**2+my**2)
return appeal
def calculateBestLinear(factor, values):
mx = mean_x(factor, values).n()
my = mean_y(factor, values).n()
y0 = factor*np.atan2(my,mx)/(2*np.pi).n()
err = 1-np.sqrt(mx**2+my**2).n()
return [factor*x + y0, err]
def calculateGCDAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = 1 - np.sqrt((mx-1)**2+my**2)/2
return appeal
testSeq = [552,827,275,809,534]
gcd = calculateGCDAppeal(x, testSeq)
agcd = sm.find_local_maximum(gcd,20,30)
print(agcd)
np.plot(gcd,(x, 20,30))
Error:
Traceback (most recent call last):
File "<ipython-input-163-fecb5397afab>", line 31, in <module>
agcd = sm.find_local_maximum(gcd,20,30)
AttributeError: module 'sagemath' has no attribute 'find_local_maximum'user90664Thu, 27 Aug 2020 23:50:02 +0200https://ask.sagemath.org/question/53203/Solving simple utility maximization problems in sage.https://ask.sagemath.org/question/51240/solving-simple-utility-maximization-problems-in-sage/Hi all im a new user to sage as a software. Im a mod on the economics.se so I understand if this question does not meet site requirements.
Im interested in solving standard utility maximization problems in economics of the form:
$$u(x_1,x_2)=x_1^{0.7}x_2^{0.3}$$
subject to:
$$p_1x_1+p_2x_2=m$$
the goal of solving this problem would be to solve for the marshallian demand equations:
$$x_1(p_1,p_2,m)$$
$$x_2(p_1,p_2,m)$$
I know how to solve this sort of problem with pen and paper however the appropriate sage code im not sure of.
Any help would be appreciated.EconJohnSun, 03 May 2020 05:47:19 +0200https://ask.sagemath.org/question/51240/Problem with find_local_maximum when expression has an abs() functionhttps://ask.sagemath.org/question/42844/problem-with-find_local_maximum-when-expression-has-an-abs-function/Hello!
I need to find the local maximum of an expression in an interval. It seems that the find_local_maximum() function has a problem when the argument expression contains the abs() function. This problem was reported to the sage-support group and two tickets were opened (https://trac.sagemath.org/ticket/24536 and https://trac.sagemath.org/ticket/24537), but it seems that it will take a while to be solved.
As a workaround, I can plot the function in that interval and look for the point with the maximum value, but this is not precise and produces a slightly different value each time the code is run.
Another workaround is to interface with Mathematica, but this is something I would like to avoid.
So, does anyone knows an alternative way to find the local maximum in an interval for an expression containing abs() functions? A piece of code is shown below as an example.
s = polygen(CC, "s")
omega = polygen(CC, "omega")
pi = CC.pi()
I = CC.0
p_order = 6
ePoly = s^6 + 2.52349407705767*s^5 + 4.57149144245730*s^4 + 4.95095921397024*s^3 + 3.70209076479638*s^2 + 1.64521458323101*s + 0.361999139300727
##### Generate P(s) polynomial #####
pMod(omega) = sqrt(ePoly(s=I*omega)*ePoly(s=-I*omega))
integrand = []
for a in range(p_order+1):
integrand.append(pMod*chebyshev_T(a, omega)/sqrt(1-omega^2))
c = []
for a in range(p_order+1):
if a == 0:
c.append((1/pi)*Rational(integrand[a].nintegral(omega, -1, 1)[0]))
else:
c.append((2/pi)*Rational(integrand[a].nintegral(omega, -1, 1)[0]))
up = 0
for a in range(p_order+1):
up = up + c[a]*chebyshev_T(a, omega)
eMod = ePoly(s=I*omega)
### First workaround: Find the plot point with maximum value
den = lambda omega: up/abs(eMod)
pl = plot(den(omega), -1, 1)
plPoints = []
for pair in list(pl[0]):
plPoints.append(pair[1])
# normFactor is multiplied by a constant to ensure that P(s) lies below the abscissa axis
normFactor = 1.00001*Rational(max(plPoints))
### Second workaround: Interfacing with Mathematica to calculate normFactor
# den = up/abs(eMod)
# mden = mathematica(den)
# mNormFactor = mden.NMaximize(-1 < omega < 1, omega)[1]
# normFactor = mNormFactor.sage()
pPoly = up/normFactor
pPoly = pPoly(omega=s/I)
pPoly = pPoly.polynomial(CC)
pPoly = pPoly.subs(x=s)
print(pPoly)
If I declare my expression as below, it works
den = lambda omega: (0.141162317194057*omega^6 + 0.173051768044343*omega^4 + 0.140157830888798*omega^2 + 0.359575336011182)/abs(-1.00000000000000*omega^6 + 2.52349407705767*I*omega^5 + 4.57149144245730*omega^4 - 4.95095921397024*I*omega^3 - 3.70209076479638*omega^2 + 1.64521458323101*I*omega + 0.361999139300727)
print(find_local_maximum(den,-1,1))
the result is
(1.0053729181238868, -0.32744025779801017)
If I declare my expression as
den = lambda omega: up/abs(eMod)
print(find_local_maximum(den,-1,1))
it returns
((0.141162317194057*omega^6 + 0.173051768044343*omega^4 + 0.140157830888798*omega^2 + 0.359575336011182)/abs(-1.00000000000000*omega^6 + 2.52349407705767*I*omega^5 + 4.57149144245730*omega^4 - 4.95095921397024*I*omega^3 - 3.70209076479638*omega^2 + 1.64521458323101*I*omega + 0.361999139300727), 0.99999996297566163)joaoffFri, 06 Jul 2018 12:27:02 +0200https://ask.sagemath.org/question/42844/