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, 03 Mar 2022 17:02:43 +0100Error from integration using mpmath quadhttps://ask.sagemath.org/question/61377/error-from-integration-using-mpmath-quad/Hello all,
I am trying to solve an integral with one variable `kx` and then I need the result from that integral to be evaluated in a point defined by `x,y` coordinates.
The code I am using is the following:
import numpy as np
from numpy.lib.scimath import sqrt
from mpmath import *
import matplotlib.pyplot as plt
mp.dps = 15
f = 8500
rho = 1.225
c0 = 343
Omega = 2*np.pi*f
k = Omega/c0
Z = -426
integrandReal = lambda kx, x, y: np.real(((2*sqrt(k**2 - kx**2)*Z)/(sqrt(k**2 - kx**2)*Z + Omega*rho))*((np.exp(1j*(kx*x + sqrt(k**2 - kx**2)*y)))/(sqrt(k**2 - kx**2))))
integrandImag = lambda kx, x, y: np.imag(((2*sqrt(k**2 - kx**2)*Z)/(sqrt(k**2 - kx**2)*Z + Omega*rho))*((np.exp(1j*(kx*x + sqrt(k**2 - kx**2)*y)))/(sqrt(k**2 - kx**2))))
integral = lambda x, y: quad(integrandReal, [-100*k, 100*k], [x], [y]) + 1j*quad(integrandImag, [-100*k, 100*k], [x], [y])
G = integral(0,0)
My problem is that for any value of `x` and `y`that I use I get:
mpc(real='0.0', imag='0.0')
I appreciate any help to solve this!samosontoThu, 03 Mar 2022 17:02:43 +0100https://ask.sagemath.org/question/61377/mpmath not working with sage equationshttps://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/I'm trying to get a numerical solution to a problem similar to the one discussed in [this old post](https://ask.sagemath.org/question/8546/numerical-solution-of-a-system-of-non-linear-equations/) but somewhat messier -- there are decimals and the variables to solve for are in an exponent. I ran the example given in the old post, with som syntactical updating and using `import mpmath as mp` and everything works exactly as shown. But when I switch to my problem, I get a "TypeError: cannot evaluate symbolic expression numerically" error. What's the difference between the two cases and what should I do? I've tried changing ^ to ** and restarting the kernel.
Here's my code:
import mpmath as mp
var("Vmax,Km")
eq0 = 101/4563863823**(32.4*Vmax/Km) - 71.85 == 0
eq1 = 96.3/85080567**(2.4*Vmax/Km) - 74.25 == 0
f = [lambda Vmax,Km: eq0.lhs().subs(Vmax=RR(Vmax), Km=RR(Km)),
lambda Vmax,Km: eq1.lhs().subs(Vmax=RR(Vmax), b=RR(Km))]
found_root = mp.findroot(f, (2, 2))
found_root = Matrix(RR, found_root.tolist())
print(found_root)
fa,fb = found_root.list()
#Check results
print(eq0.subs(Vmax=fa,Km=fb))
print(eq1.subs(Vmax=fa,Km=fb))
jaiaSat, 08 Jan 2022 09:14:05 +0100https://ask.sagemath.org/question/60577/How can I get mpmath to work with sage variables?https://ask.sagemath.org/question/39921/how-can-i-get-mpmath-to-work-with-sage-variables/ In Sage, the following code produces error messages ending with "TypeError: cannot evaluate symbolic expression numerically":
from mpmath import *
var('a')
print sqrt(a)
If I remove the line "from mpmath import *", it prints "sqrt(a)" as expected.
How can I get mpmath to work with sage variables?
millermjSun, 03 Dec 2017 00:10:43 +0100https://ask.sagemath.org/question/39921/Solving a system of equations using mpmath.findroothttps://ask.sagemath.org/question/32678/solving-a-system-of-equations-using-mpmathfindroot/ Hi All,
A few weeks ago, the code below, which was designed to compute some points was working perfectly.
After compiling it a few days ago, I got :
ValueError: Could not find root within given tolerance. (7235.55 > 2.1684e-19)
Try another starting point or tweak arguments.
Please I will be very pleased if someone could help me solve this problem. Thanks for your understanding
import mpmath
theta=pi/12
p1=vector([0,0,0])
p2=vector([-12,0,0])
p3=vector([1/24,-17/24*sqrt(287),0])
#Calcul du point p4
x4,r4=var('x4,r4')
T4=solve([(p1[0]-x4)^2+(p1[1]-r4*cos(theta))^2+(p1[2]-r4*sin(theta))^2==25,(p2[0]-x4)^2+(p2[1]-r4*cos(theta))^2+(p2[2]-r4*sin(theta))^2==100],x4,r4,solution_dict=True)
a=T4[0].values()
b=T4[1].values()
p4=vector([a[1],0,0])
if b[0]<0:
p4[1]=a[0]*cos(theta)
p4[2]=a[0]*sin(theta)
else:
p4[1]=b[0]*cos(theta)
p4[2]=b[0]*sin(theta)
var(" x5 y5 z5 x6 y6 z6 x7 y7 z7")
eq25 = (p2[0]-x5)^2+(p2[1]-y5)^2+(p2[2]-z5)^2-100 == 0
eq46 = (p4[0]-x6)^2+(p4[1]-y6)^2+(p4[2]-z6)^2-144 == 0
eq45 = (p4[0]-x5)^2+(p4[1]-y5)^2+(p4[2]-z5)^2-121 == 0
eq16 = (p1[0]-x6)^2+(p1[1]-y6)^2+(p1[2]-z6)^2-100 == 0
eq27 = (p2[0]-x7)^2+(p2[1]-y7)^2+(p2[2]-z7)^2-144 == 0
eq37 = (p3[0]-x7)^2+(p3[1]-y7)^2+(p3[2]-z7)^2-144 == 0
eq56 = (x6-x5)^2+(y6-y5)^2+(z6-z5)^2-144 == 0
eq67 = (x6-x7)^2+(y6-y7)^2+(z6-z7)^2-100 == 0
eq57 = (x7-x5)^2+(y7-y5)^2+(z7-z5)^2-25 == 0
# Calcul des points p5, p6, et p7
f= [lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq25.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7)),
lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq46.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7)),
lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq45.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7)),
lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq16.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7)),
lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq27.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7)),
lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq37.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7)),
lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq56.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7)),
lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq67.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7)),
lambda x5,y5,z5,x6,y6,z6,x7,y7,z7: eq57.lhs().subs(x5=RR(x5), y5=RR(y5), z5=RR(z5), x6=RR(x6), y6=RR(y6), z6=RR(z6), x7=RR(x7), y7=RR(y7), z7=RR(z7))]
found_root = mpmath.findroot(f, (-5,-6,-3.5, 3.5, 0.1,-9,-5,-5,-8))
found_root = Matrix(RR, found_root.tolist())
p5=vector([found_root[0][0], found_root[1][0], found_root[2][0]])
p6=vector([found_root[3][0], found_root[4][0], found_root[5][0]])
p7=vector([found_root[6][0], found_root[7][0], found_root[8][0]])
print p5
cyrilleSat, 27 Feb 2016 08:55:33 +0100https://ask.sagemath.org/question/32678/Why do mpmath mpf values compare strangely to Integers?https://ask.sagemath.org/question/32642/why-do-mpmath-mpf-values-compare-strangely-to-integers/With both sage 6.8 and 7.1.beta3 on OSX 10.10.5 I get this odd result:
sage: import mpmath
sage: max(Integer(0), mpmath.mpf(-1))
0
sage: max(mpmath.mpf(-1), Integer(0))
mpf('-1.0')
sage: Integer(0) < mpmath.mpf(-1)
True
The maximum of 0 and -1 should be 0, although I'm not sure if it should be Integer(0) or mpf(0).
Am I missing something about how sage expects me to handle arbitrary-precision floating-point numbers?w4Kn0ADbGU9iW6PqTue, 23 Feb 2016 15:53:21 +0100https://ask.sagemath.org/question/32642/def type of mpmath matrices in cythonhttps://ask.sagemath.org/question/31210/def-type-of-mpmath-matrices-in-cython/ I'm trying to speed up the exact *iterative* calculation of the 1E9 th Fibonacci number, which has 208987640 digits.
Working with Python and mpmath I've reached an acceptable time, but I'm looking to further reduce it using Cython.
In this case the overload is due to the huge numbers, stored in Knuth's 2 x 2 matrices, that have been defined using Numpy and mpmath.
For Cython to be helpful in this subject, I should define those matrices as static, but I don't know the type I should use to define it. Is there a special type for this case?
Cython curiousMon, 30 Nov 2015 19:43:17 +0100https://ask.sagemath.org/question/31210/Precision plots - How do I do those?https://ask.sagemath.org/question/29231/precision-plots-how-do-i-do-those/Hi.
I have a function which varies so slowly, that in the range I'm interested only the 17th decimal changes.
The sws is this:
sage: mp = 139570180.
sage: mm = 105658371.5
sage: mn = 1.
sage: dmp = 350.
sage: dmm = 3.8
sage: dmn = 0.1
sage: l(a,b,c) = a^2 + b^2 + c^2 - 2*( a*b + b*c + c*a )
sage: f(x) = sqrt(l(mp^2, mm^2, x^2))/(2*mp)
sage: R=RealField(100)
sage: g = fast_callable(f, vars=[x], domain=R)
sage: plot( g, (x,0,2))
but I got the following plot, which is unsatisfactory in several ways!
![image description](/upfiles/14400980865379363.png)
How could I possible improve it? (ticks on vertical axis, a "continuous" line, ...)
Thank you!DoxThu, 20 Aug 2015 21:17:53 +0200https://ask.sagemath.org/question/29231/Matrices and mpmathhttps://ask.sagemath.org/question/9709/matrices-and-mpmath/Hello, I'm trying to work with matrices. I type `matrix(4)` and it gives me a 4 by 4 matrix of zeros
which is fine. But when I use the command `from mpmath import *` then try to create matrices with the same command (`matrix(4)`) it gives an error message as below:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_10.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dD1tYXRyaXgoNSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpdJ1MFV/___code___.py", line 3, in <module>
exec compile(u't=matrix(_sage_const_5 )
File "", line 1, in <module>
File "/sagenb/sage_install/sage-5.4-sage.math.washington.edu-x86_64-Linux/local/lib/python2.7/site-packages/mpmath/matrices/matrices.py", line 332, in __init__
raise TypeError('could not interpret given arguments')
TypeError: could not interpret given arguments
Since I need this command (`from mpmath import *`) for other operations, I have to use it. On the
other hand it disrupts my matrix calculations. Is there anyway which I can have both working together in Sage similar to ipython?
Any help would be appreciated. Thank you.
ErfanTue, 15 Jan 2013 16:08:51 +0100https://ask.sagemath.org/question/9709/Write function for use by both mpmath and sage typeshttps://ask.sagemath.org/question/9489/write-function-for-use-by-both-mpmath-and-sage-types/I've got the following function:
def area2(d, r):
x = (sqrt(2*r^2 - d^2) - d)/2
a2 = 2*arccos((d + x)/r)
a2 = (a2 - sin(a2))/2*r^2
return ((2*x)^2 + 4*a2) - d.parent().pi()/9
I'd like to use this function with various argument typs, among them `RealField`, `RealIntervalField` and the `mpf` type from `mpmath`. The latter appears to be a requirement for using `mpmath.findroot`, as suggested by [this answer](http://ask.sagemath.org/question/1934/find-roots-with-arbitrary-precision?answer=2812#2812).
However, the code currently fails to work with `mpf`, as these numbers don't interact nicely with sage constants:
sage: area2(mpmath.mpf(4.44), mpmath.mpf(4.74))
[ ]
a2 = _sage_const_2 *arccos((d + x)/r)
[ ]
TypeError: cannot coerce arguments: no canonical coercion from <type 'sage.libs.mpmath.ext_main.mpf'> to Symbolic Ring
So my question is this: is there a way to write this function so that it will allow operation on all the mentioned number types, without case distinctions to cater for the different types, and without loosing precision to casts?
If this is not possible, do you know of any existing bug report asking for such a feature, should I open a new ticket, or is there any reason why such an interoperability is not technically possible?MvGWed, 31 Oct 2012 13:03:05 +0100https://ask.sagemath.org/question/9489/implement and plot meijerghttps://ask.sagemath.org/question/8634/implement-and-plot-meijerg/Very new to Sage. Think it is installed correctly and I am using the Notebook interface. Trying to run the meijerg that is supposedly in mpmath but it complains that it can't find the function.
I did
import mpmath
but did not get a response to that command
Do I need to download something else? What can I do to get meijerg working (and need to plot)?
rballTue, 17 Jan 2012 15:38:56 +0100https://ask.sagemath.org/question/8634/convert mpmath function to cythonhttps://ask.sagemath.org/question/7983/convert-mpmath-function-to-cython/I think I could figure this out eventually, but I'm hoping it will be a very easy question for someone out there. I would like to convert the following function to cython to be as fast as possible. I am not sure exactly what I need to import from mpmath to do that.
Here is the function:
from mpmath import *
mp.dps = 25; mp.pretty = True
def hyp_mp(a1,a2,b1,b2):
num = gamma(a1+a2+1)*gamma(b1+b2+1)*gamma(a1+b1+1)*gamma(a2+b2+1)
denom = gamma(a1+1)*gamma(a2+1)*gamma(b1+1)*gamma(b2+1)*gamma(a1+a2+b1+b2+1)
return num/denom
def myfisher_mp(a1,a2,b1,b2):
p = hyp_mp(a1,a2,b1,b2)
outp = p
for i in range(0,a1):
temp = hyp_mp(i,a2+a1-i,b1-i+a1,b2-a1+i)
if temp > p:
break
else:
outp += temp
for i in range(0,a2):
temp = hyp_mp(a1+a2-i,i,b1-a2+i,b2+a2-i)
if temp > p:
break
else:
outp += temp
return outp
A good test case would be myfisher_mp(1286, 9548, 133437, 148905), which takes about 1 second on my desktop. The inputs a1,a2,b1,b2 can be assumed to be positive ints.mhamptonFri, 04 Mar 2011 15:39:58 +0100https://ask.sagemath.org/question/7983/