Why I get 1-element Vector{Sym} instead of just number as answer from SymPy?

Hi all,

it is just a curious question. I am calculating k from this

\int_{0}^{180} kx^{2} (180 - x) dx = 1

solving for k I should obtain \frac{1}{87,480,000} but why the answer contains 1-element Vector? instead of just the number?

using SymPy
@syms x, k

a,b = 0, 180
k1 = solve(integrate(k*(x^2)*(180-x),(x,a,b)) ~ 1, k)

1-element Vector{Sym}:
 1/87480000

that’s just how the interface works. My guess is to make type stable – i.e. you don’t get Sym vs. Vector{Sym} depending on how many variables you have, which is a runtime property

4 Likes

There is this problem I try:

k1::Vector

1-element Vector{Sym}:
95802719.0924019

k1::Int64

TypeError: in typeassert, expected Int64, got a value of type Vector{Sym}
Stacktrace:

I want to turn k1 into integer or float… how?

I got it when I type

k1[1]
1 Like

I’m a big fan of only(k1) in this situation. It’s the same thing as k1[1] except it will throw an error if length(k1) != 1.

3 Likes