What does this TypeError("invalid NaN comparison") mean? And how do I fix it?

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!

Thanks

hi, can you post what are term2 and x_hat?

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

  1. term2 is in vague terms. In the real code it’s actually more complicated :frowning: But, hopefully this gets the main point across
  2. 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)
  3. 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

1 Like

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

results in:

julia> Sympy.solve(term2-x_hat,x_hat)
Dict{Any, Any} with 10 entries:
  x_hat6  => 0.926800682408201 
  x_hat10 => 0.962863463691171 
  x_hat7  => 0.497643557077236 
  x_hat3  => 0.211146695704730 
  x_hat2  => 0.568369033677095 
  x_hat5  => 0.187652949522494 
  x_hat4  => 0.392861232103788 
  x_hat9  => 0.559299830282101 
  x_hat1  => 0.431629987943204 
  x_hat8  => 0.320110979997330

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

1 Like