I’m trying to use the function “solve” from sympy to solve for the values that satisfies a complicated equation. I am receiving the following error message: TypeError(“invalid NaN comparison”)
Does anyone know how to resolve this issue or understand where it is coming from? Any advice would be much appreciated!
x_hat = [symbols(“x_hat$i”,real=true) for i=1:n].reshape(n,1)
term2 = find_max(find_min(Q*x_hat,d),zeros(n,1))
n is an integer, Q is a nxn matrix of floats, d is also an nx1 vector
term2 is in vague terms. In the real code it’s actually more complicated But, hopefully this gets the main point across
find_max and find_min are functions I made myself - the continuous form of minimum and maximum functions. (the reason why I wrote my own vs. the built-in max and min functions is because I originally wrote this in matlab and in matlab, it couldn’t take the minimum or maximum of symbolic terms)
also in matlab, I used the function vpasolve() and it worked there. I am translating my matlab code into Julia to speed the process up
reference for find_min and find_max functions:
function find_min(A,B)
val = A - (abs.(A-B)+A-B)./2;
return val
end
function find_max(A,B)
val = B + (abs.(A-B)+A-B)./2;
return val
end
hmm, got the same error. this is the full code to reproduce that error
import Sympy
n = 10
Q = rand(n,n)
d = rand(n,1)
function find_min(A,B)
return A - (abs.(A-B)+A-B)./2;
end
function find_max(A,B)
return B + (abs.(A-B)+A-B)./2;
end
x_hat = [Sympy.symbols("x_hat$i",real=true) for i=1:n].reshape(n,1)}
term2 = find_max(find_min(Q*x_hat,d),zeros(n,1))
Sympy.solve(term2-x_hat,x_hat)
@Grace_Chuan i think that the error lies in the find_min and find_max errors. you are creating an expression with Abs(sym) on term2-x_hat. changing those functions to:
function find_min(A,B)
res = zeros(eltype(A),size(A)) #create an Array of the same size and element type
for i in eachindex(A)
if A[i] < B[i] #compare an store the correct branch instead of the abs function
res[i] = A[i]
else
res[i] = B[i]
end
end
return res
end
function find_max(A,B)
res = zeros(eltype(A),size(A))
for i in eachindex(A)
if A[i] > B[i]
res[i] = A[i]
else
res[i] = B[i]
end
end
return res
end
of course, there is the corner case of A[i] == B[i] but that is outside the scope of this problem, and more of the specific domain that are you working on