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.Sat, 08 Jan 2022 23:13:57 +0100mpmath 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))
Sat, 08 Jan 2022 09:14:05 +0100https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/Comment by jaia for <p>I'm trying to get a numerical solution to a problem similar to the one discussed in <a href="https://ask.sagemath.org/question/8546/numerical-solution-of-a-system-of-non-linear-equations/">this old post</a> 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 <code>import mpmath as mp</code> 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.</p>
<p>Here's my code:</p>
<pre><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))
</code></pre>
https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60583#post-id-60583At least that's a mathematical one. Can't believe I missed that typo!Sat, 08 Jan 2022 19:26:44 +0100https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60583#post-id-60583Comment by rburing for <p>I'm trying to get a numerical solution to a problem similar to the one discussed in <a href="https://ask.sagemath.org/question/8546/numerical-solution-of-a-system-of-non-linear-equations/">this old post</a> 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 <code>import mpmath as mp</code> 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.</p>
<p>Here's my code:</p>
<pre><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))
</code></pre>
https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60579#post-id-60579You have a typo: `b=RR(Km)` should be `Km=RR(Km)`. If you fix it, you get a different error :)Sat, 08 Jan 2022 10:26:02 +0100https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60579#post-id-60579Answer by Emmanuel Charpentier for <p>I'm trying to get a numerical solution to a problem similar to the one discussed in <a href="https://ask.sagemath.org/question/8546/numerical-solution-of-a-system-of-non-linear-equations/">this old post</a> 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 <code>import mpmath as mp</code> 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.</p>
<p>Here's my code:</p>
<pre><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))
</code></pre>
https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?answer=60580#post-id-60580Your system seems to have no real solutions :
sage: var("Vmax, Km")
(Vmax, Km)
sage: eq0 = 101/4563863823**(32.4*Vmax/Km) - 71.85 == 0
sage: eq1 = 96.3/85080567**(2.4*Vmax/Km) - 74.25 == 0
Solve `eq0` for `Vmax`
sage: %time S0=solve(eq0, Vmax)
CPU times: user 5.5 s, sys: 7.6 ms, total: 5.5 s
Wall time: 5.15 s
sage: len(S0)
162
Of these, only one is real :
sage: time with assuming(Km,"real"): S0r = [u for u in S0 if u.rhs().is_real()]
CPU times: user 6.44 ms, sys: 0 ns, total: 6.44 ms
Wall time: 6.42 ms
sage: len(S0r)
1
sage: S0r
[Vmax == 5*Km*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)]
Substituting this real solution in `eq1` yelds an equation with no variables :
sage: eq1.subs(S0r[0])
96.3000000000000/85080567^(12.0000000000000*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)) - 74.2500000000000 == 0
And since the left hand is not numerically null :
sage: eq1.subs(S0r[0]).lhs().n()
20.0762677199469
`eq1` isn't satisfied and the system has no solutions with real `Vmax`.
BTW, your system seems to have no solution at all :
sage: time S01 = [eq1.subs(s).solve(Km) for s in S0]
CPU times: user 677 ms, sys: 3.96 ms, total: 681 ms
Wall time: 576 ms
All these solutions are empty :
sage: all(len(u)==0 for u in S01)
True
To understand this, let's rewrite this system *symbolically* (easier to follow) :
Eq0 = c0/c1^(c2*Vmax/Km) + c3
Eq1 = c4/c5^(c6*Vmax/Km) + c7
Again, solving `Eq1` for `Vmax` yelds a solution :
sage: %time Ss0=solve(Eq0, Vmax) ; Ss0
CPU times: user 6.77 ms, sys: 0 ns, total: 6.77 ms
Wall time: 6.75 ms
[Vmax == Km*log(-c0/c3)/(c2*log(c1))]
which, substituted in `Eq1`, yields an equation with no variable :
sage: Eq1.subs(Ss0[0])
c7 + c4/c5^(c6*log(-c0/c3)/(c2*log(c1))) == 0
The satisfaction of this equation do not depend on `Km`, but only on your numerical constants.
HTH,
Sat, 08 Jan 2022 16:04:15 +0100https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?answer=60580#post-id-60580Comment by Emmanuel Charpentier for <p>Your system seems to have no real solutions :</p>
<pre><code>sage: var("Vmax, Km")
(Vmax, Km)
sage: eq0 = 101/4563863823**(32.4*Vmax/Km) - 71.85 == 0
sage: eq1 = 96.3/85080567**(2.4*Vmax/Km) - 74.25 == 0
</code></pre>
<p>Solve <code>eq0</code> for <code>Vmax</code></p>
<pre><code>sage: %time S0=solve(eq0, Vmax)
CPU times: user 5.5 s, sys: 7.6 ms, total: 5.5 s
Wall time: 5.15 s
sage: len(S0)
162
</code></pre>
<p>Of these, only one is real :</p>
<pre><code>sage: time with assuming(Km,"real"): S0r = [u for u in S0 if u.rhs().is_real()]
CPU times: user 6.44 ms, sys: 0 ns, total: 6.44 ms
Wall time: 6.42 ms
sage: len(S0r)
1
sage: S0r
[Vmax == 5*Km*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)]
</code></pre>
<p>Substituting this real solution in <code>eq1</code> yelds an equation with no variables :</p>
<pre><code>sage: eq1.subs(S0r[0])
96.3000000000000/85080567^(12.0000000000000*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)) - 74.2500000000000 == 0
</code></pre>
<p>And since the left hand is not numerically null :</p>
<pre><code>sage: eq1.subs(S0r[0]).lhs().n()
20.0762677199469
</code></pre>
<p><code>eq1</code> isn't satisfied and the system has no solutions with real <code>Vmax</code>.</p>
<p>BTW, your system seems to have no solution at all :</p>
<pre><code>sage: time S01 = [eq1.subs(s).solve(Km) for s in S0]
CPU times: user 677 ms, sys: 3.96 ms, total: 681 ms
Wall time: 576 ms
</code></pre>
<p>All these solutions are empty :</p>
<pre><code>sage: all(len(u)==0 for u in S01)
True
</code></pre>
<p>To understand this, let's rewrite this system <em>symbolically</em> (easier to follow) :</p>
<pre><code>Eq0 = c0/c1^(c2*Vmax/Km) + c3
Eq1 = c4/c5^(c6*Vmax/Km) + c7
</code></pre>
<p>Again, solving <code>Eq1</code> for <code>Vmax</code> yelds a solution : </p>
<pre><code>sage: %time Ss0=solve(Eq0, Vmax) ; Ss0
CPU times: user 6.77 ms, sys: 0 ns, total: 6.77 ms
Wall time: 6.75 ms
[Vmax == Km*log(-c0/c3)/(c2*log(c1))]
</code></pre>
<p>which, substituted in <code>Eq1</code>, yields an equation with no variable :</p>
<pre><code>sage: Eq1.subs(Ss0[0])
c7 + c4/c5^(c6*log(-c0/c3)/(c2*log(c1))) == 0
</code></pre>
<p>The satisfaction of this equation do not depend on <code>Km</code>, but only on your numerical constants.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60587#post-id-60587Yiu may have noted that I timed the calls to `solve`. 5.5 seconds to solve with Python is no small potatoes ; add to that the overhead of going to `mpmath` and solve with (potentially very high precision) multiple-precision float arithmetic can be very high, and you may prepare for a wait of minutes, if not hours.
Brute force is expensive. Especially when trying to find a nonexistent solution (as learned a lot of politicians in human history...).Sat, 08 Jan 2022 23:13:57 +0100https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60587#post-id-60587Comment by jaia for <p>Your system seems to have no real solutions :</p>
<pre><code>sage: var("Vmax, Km")
(Vmax, Km)
sage: eq0 = 101/4563863823**(32.4*Vmax/Km) - 71.85 == 0
sage: eq1 = 96.3/85080567**(2.4*Vmax/Km) - 74.25 == 0
</code></pre>
<p>Solve <code>eq0</code> for <code>Vmax</code></p>
<pre><code>sage: %time S0=solve(eq0, Vmax)
CPU times: user 5.5 s, sys: 7.6 ms, total: 5.5 s
Wall time: 5.15 s
sage: len(S0)
162
</code></pre>
<p>Of these, only one is real :</p>
<pre><code>sage: time with assuming(Km,"real"): S0r = [u for u in S0 if u.rhs().is_real()]
CPU times: user 6.44 ms, sys: 0 ns, total: 6.44 ms
Wall time: 6.42 ms
sage: len(S0r)
1
sage: S0r
[Vmax == 5*Km*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)]
</code></pre>
<p>Substituting this real solution in <code>eq1</code> yelds an equation with no variables :</p>
<pre><code>sage: eq1.subs(S0r[0])
96.3000000000000/85080567^(12.0000000000000*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)) - 74.2500000000000 == 0
</code></pre>
<p>And since the left hand is not numerically null :</p>
<pre><code>sage: eq1.subs(S0r[0]).lhs().n()
20.0762677199469
</code></pre>
<p><code>eq1</code> isn't satisfied and the system has no solutions with real <code>Vmax</code>.</p>
<p>BTW, your system seems to have no solution at all :</p>
<pre><code>sage: time S01 = [eq1.subs(s).solve(Km) for s in S0]
CPU times: user 677 ms, sys: 3.96 ms, total: 681 ms
Wall time: 576 ms
</code></pre>
<p>All these solutions are empty :</p>
<pre><code>sage: all(len(u)==0 for u in S01)
True
</code></pre>
<p>To understand this, let's rewrite this system <em>symbolically</em> (easier to follow) :</p>
<pre><code>Eq0 = c0/c1^(c2*Vmax/Km) + c3
Eq1 = c4/c5^(c6*Vmax/Km) + c7
</code></pre>
<p>Again, solving <code>Eq1</code> for <code>Vmax</code> yelds a solution : </p>
<pre><code>sage: %time Ss0=solve(Eq0, Vmax) ; Ss0
CPU times: user 6.77 ms, sys: 0 ns, total: 6.77 ms
Wall time: 6.75 ms
[Vmax == Km*log(-c0/c3)/(c2*log(c1))]
</code></pre>
<p>which, substituted in <code>Eq1</code>, yields an equation with no variable :</p>
<pre><code>sage: Eq1.subs(Ss0[0])
c7 + c4/c5^(c6*log(-c0/c3)/(c2*log(c1))) == 0
</code></pre>
<p>The satisfaction of this equation do not depend on <code>Km</code>, but only on your numerical constants.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60585#post-id-60585When I ran `solve`, it wouldn't halt. That's why I was trying mpmath.Sat, 08 Jan 2022 20:50:52 +0100https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60585#post-id-60585Comment by Emmanuel Charpentier for <p>Your system seems to have no real solutions :</p>
<pre><code>sage: var("Vmax, Km")
(Vmax, Km)
sage: eq0 = 101/4563863823**(32.4*Vmax/Km) - 71.85 == 0
sage: eq1 = 96.3/85080567**(2.4*Vmax/Km) - 74.25 == 0
</code></pre>
<p>Solve <code>eq0</code> for <code>Vmax</code></p>
<pre><code>sage: %time S0=solve(eq0, Vmax)
CPU times: user 5.5 s, sys: 7.6 ms, total: 5.5 s
Wall time: 5.15 s
sage: len(S0)
162
</code></pre>
<p>Of these, only one is real :</p>
<pre><code>sage: time with assuming(Km,"real"): S0r = [u for u in S0 if u.rhs().is_real()]
CPU times: user 6.44 ms, sys: 0 ns, total: 6.44 ms
Wall time: 6.42 ms
sage: len(S0r)
1
sage: S0r
[Vmax == 5*Km*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)]
</code></pre>
<p>Substituting this real solution in <code>eq1</code> yelds an equation with no variables :</p>
<pre><code>sage: eq1.subs(S0r[0])
96.3000000000000/85080567^(12.0000000000000*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)) - 74.2500000000000 == 0
</code></pre>
<p>And since the left hand is not numerically null :</p>
<pre><code>sage: eq1.subs(S0r[0]).lhs().n()
20.0762677199469
</code></pre>
<p><code>eq1</code> isn't satisfied and the system has no solutions with real <code>Vmax</code>.</p>
<p>BTW, your system seems to have no solution at all :</p>
<pre><code>sage: time S01 = [eq1.subs(s).solve(Km) for s in S0]
CPU times: user 677 ms, sys: 3.96 ms, total: 681 ms
Wall time: 576 ms
</code></pre>
<p>All these solutions are empty :</p>
<pre><code>sage: all(len(u)==0 for u in S01)
True
</code></pre>
<p>To understand this, let's rewrite this system <em>symbolically</em> (easier to follow) :</p>
<pre><code>Eq0 = c0/c1^(c2*Vmax/Km) + c3
Eq1 = c4/c5^(c6*Vmax/Km) + c7
</code></pre>
<p>Again, solving <code>Eq1</code> for <code>Vmax</code> yelds a solution : </p>
<pre><code>sage: %time Ss0=solve(Eq0, Vmax) ; Ss0
CPU times: user 6.77 ms, sys: 0 ns, total: 6.77 ms
Wall time: 6.75 ms
[Vmax == Km*log(-c0/c3)/(c2*log(c1))]
</code></pre>
<p>which, substituted in <code>Eq1</code>, yields an equation with no variable :</p>
<pre><code>sage: Eq1.subs(Ss0[0])
c7 + c4/c5^(c6*log(-c0/c3)/(c2*log(c1))) == 0
</code></pre>
<p>The satisfaction of this equation do not depend on <code>Km</code>, but only on your numerical constants.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60584#post-id-60584> I couldn't get that result after trying the same things you did
What do you mean ?
Wht did you try, and what was the result ?Sat, 08 Jan 2022 20:31:49 +0100https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60584#post-id-60584Comment by jaia for <p>Your system seems to have no real solutions :</p>
<pre><code>sage: var("Vmax, Km")
(Vmax, Km)
sage: eq0 = 101/4563863823**(32.4*Vmax/Km) - 71.85 == 0
sage: eq1 = 96.3/85080567**(2.4*Vmax/Km) - 74.25 == 0
</code></pre>
<p>Solve <code>eq0</code> for <code>Vmax</code></p>
<pre><code>sage: %time S0=solve(eq0, Vmax)
CPU times: user 5.5 s, sys: 7.6 ms, total: 5.5 s
Wall time: 5.15 s
sage: len(S0)
162
</code></pre>
<p>Of these, only one is real :</p>
<pre><code>sage: time with assuming(Km,"real"): S0r = [u for u in S0 if u.rhs().is_real()]
CPU times: user 6.44 ms, sys: 0 ns, total: 6.44 ms
Wall time: 6.42 ms
sage: len(S0r)
1
sage: S0r
[Vmax == 5*Km*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)]
</code></pre>
<p>Substituting this real solution in <code>eq1</code> yelds an equation with no variables :</p>
<pre><code>sage: eq1.subs(S0r[0])
96.3000000000000/85080567^(12.0000000000000*log(1/1437*1437^(161/162)*505^(1/162)*2^(1/81))/log(4563863823)) - 74.2500000000000 == 0
</code></pre>
<p>And since the left hand is not numerically null :</p>
<pre><code>sage: eq1.subs(S0r[0]).lhs().n()
20.0762677199469
</code></pre>
<p><code>eq1</code> isn't satisfied and the system has no solutions with real <code>Vmax</code>.</p>
<p>BTW, your system seems to have no solution at all :</p>
<pre><code>sage: time S01 = [eq1.subs(s).solve(Km) for s in S0]
CPU times: user 677 ms, sys: 3.96 ms, total: 681 ms
Wall time: 576 ms
</code></pre>
<p>All these solutions are empty :</p>
<pre><code>sage: all(len(u)==0 for u in S01)
True
</code></pre>
<p>To understand this, let's rewrite this system <em>symbolically</em> (easier to follow) :</p>
<pre><code>Eq0 = c0/c1^(c2*Vmax/Km) + c3
Eq1 = c4/c5^(c6*Vmax/Km) + c7
</code></pre>
<p>Again, solving <code>Eq1</code> for <code>Vmax</code> yelds a solution : </p>
<pre><code>sage: %time Ss0=solve(Eq0, Vmax) ; Ss0
CPU times: user 6.77 ms, sys: 0 ns, total: 6.77 ms
Wall time: 6.75 ms
[Vmax == Km*log(-c0/c3)/(c2*log(c1))]
</code></pre>
<p>which, substituted in <code>Eq1</code>, yields an equation with no variable :</p>
<pre><code>sage: Eq1.subs(Ss0[0])
c7 + c4/c5^(c6*log(-c0/c3)/(c2*log(c1))) == 0
</code></pre>
<p>The satisfaction of this equation do not depend on <code>Km</code>, but only on your numerical constants.</p>
<p>HTH,</p>
https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60582#post-id-60582That's good to know, but I wonder why I couldn't get that result after trying the same things you did. I'm running Sage 9.0.Sat, 08 Jan 2022 19:25:25 +0100https://ask.sagemath.org/question/60577/mpmath-not-working-with-sage-equations/?comment=60582#post-id-60582