Haskell floating point error - math

So I have finished creating my own complex number data type in haskell.
I've also, thanks to another question on here, got a function that will solve a quadratic equation.
The only problem now is that the code generates a parsing error in hugs, when trying to solve a quadratic with complex roots.
i.e. In hugs...
Main> solve (Q 1 2 1)
(-1.0,-1.0)
Main> solve (Q 1 2 0)
(0.0,-2.0)
Main> solve (Q 1 2 2)
(
Program error: pattern match failure: v1618_v1655 (C -1.#IND -1.#IND)
It looks to my like its a problem after the square-root has been applied, but I'm really not sure. Any help trying to pick up what is going wrong or any indications as to what this error means would be brilliant.
Thanks,
Thomas
The Code:
-- A complex number z = (re +im.i) is represented as a pair of Floats
data Complex = C {
re :: Float,
im :: Float
} deriving Eq
-- Display complex numbers in the normal way
instance Show Complex where
show (C r i)
| i == 0 = show r
| r == 0 = show i++"i"
| r < 0 && i < 0 = show r ++ " - "++ show (C 0 (i*(-1)))
| r < 0 && i > 0 = show r ++ " + "++ show (C 0 i)
| r > 0 && i < 0 = show r ++ " - "++ show (C 0 (i*(-1)))
| r > 0 && i > 0 = show r ++ " + "++ show (C 0 i)
-- Define algebraic operations on complex numbers
instance Num Complex where
fromInteger n = C (fromInteger n) 0 -- tech reasons
(C a b) + (C x y) = C (a+x) (b+y)
(C a b) * (C x y) = C (a*x - b*y) (b*x + b*y)
negate (C a b) = C (-a) (-b)
instance Fractional Complex where
fromRational r = C (fromRational r) 0 -- tech reasons
recip (C a b) = C (a/((a^2)+(b^2))) (b/((a^2)+(b^2)))
root :: Complex -> Complex
root (C x y)
| y == 0 && x == 0 = C 0 0
| y == 0 && x > 0 = C (sqrt ( ( x + sqrt ( (x^2) + 0 ) ) / 2 ) ) 0
| otherwise = C (sqrt ( ( x + sqrt ( (x^2) + (y^2) ) ) / 2 ) ) ((y/(2*(sqrt ( ( x + sqrt ( (x^2) + (y^2) ) ) / 2 ) ) ) ) )
-- quadratic polynomial : a.x^2 + b.x + c
data Quad = Q {
aCoeff, bCoeff, cCoeff :: Complex
} deriving Eq
instance Show Quad where
show (Q a b c) = show a ++ "x^2 + " ++ show b ++ "x + " ++ show c
solve :: Quad -> (Complex, Complex)
solve (Q a b c) = ( sol (+), sol (-) )
where sol op = (op (negate b) $ root $ b*b - 4*a*c) / (2 * a)

Your numbers seem denormalized in your error :
(C -1.#IND -1.#IND)
In this case, you can't assume that any comparison on float are valid anymore. This is in the definition of floating point numbers. Then your definition of show
show (C r i)
| i == 0 = show r
| r == 0 = show i++"i"
| r < 0 && i < 0 = show r ++ " - "++ show (C 0 (i*(-1)))
| r < 0 && i > 0 = show r ++ " + "++ show (C 0 i)
| r > 0 && i < 0 = show r ++ " - "++ show (C 0 (i*(-1)))
| r > 0 && i > 0 = show r ++ " + "++ show (C 0 i)
leave opportunity for a pattern failure, because of denormalized numbers. You can add the following condition
| otherwise = show r ++ "i" ++ show i"
Now for the why is it like that, when you evaluate
b * b - 4 * a * c
with Q 1 2 2, you obtain -4, and then in root, you fall in your last case, and in the second equation :
y
-----------------------------
________________
/ _______
/ / 2 2
/ x + \/ x + y
2 * \ / ----------------
\/ 2
-4 + sqrt( (-4) ^2) == 0, from there, you're doomed, division by 0, followed by a "NaN" (not a number), screwing everything else

Dave hit the nail on the head.
With the original code in GHCi, I get:
*Main> solve (Q 1 2 2)
(*** Exception: c.hs:(11,4)-(17,63): Non-exhaustive patterns in function show
If we update the show block:
instance Show Complex where
show (C r i)
| i == 0 = show r
| r == 0 = show i++"i"
| r < 0 && i < 0 = show r ++ " - "++ show (C 0 (i*(-1)))
| r < 0 && i > 0 = show r ++ " + "++ show (C 0 i)
| r > 0 && i < 0 = show r ++ " - "++ show (C 0 (i*(-1)))
| r > 0 && i > 0 = show r ++ " + "++ show (C 0 i)
| otherwise = "???(" ++ show r ++ " " ++ show i ++ ")"
then we get this information in GHCi:
*Main> :l c.hs
[1 of 1] Compiling Main ( c.hs, interpreted )
c.hs:22:0:
Warning: No explicit method nor default method for `abs'
In the instance declaration for `Num Complex'
c.hs:22:0:
Warning: No explicit method nor default method for `signum'
In the instance declaration for `Num Complex'
Ok, modules loaded: Main.
*Main> solve (Q 1 2 2)
(???(NaN NaN),???(NaN NaN))
I was "born and raised" on GHCi, so I don't know exactly how Hugs compares in verbosity of warnings and errors; but it looks like GHCi is a clear winner in telling you what went wrong.

Off the top of my head: It could be a problem with your definition of show for Complex.
I notice you don't have default case like this:
| otherwise = ...
Therefore if your conditions with r and i are non exhaustive you'll get a pattern match failure.

Related

/(::VariableRef,::QuadExpr) is not defined

I'm trying to solve a problem with JuMp and it's a non linear problem. So I have the error bellow :
/(::VariableRef,::QuadExpr) is not defined. Are you trying to build a nonlinear problem? Make sure you use #NLconstraint/#NLobjective.
And I'm using #NLobjective and #NLconstraint.
I have the problem at the second line of this function when I call it in my function bvpsolve:
function hamiltonien(z,eps)
u = z[:,8]./(2*eps*Cd2*z[:,6].*z[:,2])
h = z[:,5] .* z[:,1] .* [sin(z[i,4]) for i in size(z[:,4])] + z[:,6] .* (T(z[:,1])./z[:,3] - phi(z[:,1]) * S * z[:,2].^2 /(2*z[:,3])* (Cd1+Cd2*u^2) - g *sin(z[:,4])) - z[:,7] *Cs(z[:,2])*T(z[:,1]) + 1/eps * z[:,8] *(phi(z[:,1])*S*z[:,2] *u /(2*z[:,3]) - g/z[:,2] *cos(z[:,4]))
return 1
end
function bvpsolve(eps,N)
sys = Model(optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 5))
set_optimizer_attribute(sys,"tol",1e-8)
set_optimizer_attribute(sys,"constr_viol_tol",1e-6)
#variables(sys, begin
tf
x[1:N+1 , 1:n]
y[1:N+1 , 1:n]
0. ≤ h[1:N] ≤ 10
end)
Δt = (tf-t0)/N
# Objective
#NLobjective(sys, Min, sum(sum((x[i,j]-y[i,j])^2 for i in 1:N+1) for j in 1:n )/N + α*sum((h[i]-Δt)^2 for i in 1:N))
hx = hamiltonien(x,eps)
hy = hamiltonien(y,eps)
xpoint , ppointx = hvfun(hx,x)
ypoint , ppointy = hvfun(hy,y)
#NLconstraints(sys, begin
con_h0, x[1,1] - 3480. == 0
con_hf, x[N+1,1] - 9144. == 0
con_v0, x[1,2] - 151.67 == 0
con_vf, x[N+1,2] - 191. == 0
con_m0, x[1,3] - 69000. == 0
con_mf, x[N+1,3] - 68100. == 0
con_g0, x[1,4] - 69000. == 0
con_gf, x[N+1,4] - 68100. == 0
end)
...
end)
You can't construct nonlinear expressions outside the macros.
See the documentation: https://jump.dev/JuMP.jl/stable/manual/nlp/
You can use a user-defined function, but I don't understand your example. It hard-codes return 1, so I don't know what u and h are for.
Your example is also non-reproducible, because I don't know what hvfun is.
p.s., If you want to have a longer discussion, please post on the community forum: https://discourse.julialang.org/c/domain/opt/13. It's a bit easier to have a back-and-forward than on stack overflow.

F# recursive function alternating between plus and minus

I'm asked to remove one clause in below F# function, while maintaining it's functionality, that's alternating between plus and minus.
let rec altsum =
function
| [] -> 0
| [ x ] -> x
| x0 :: x1 :: xr -> x0 - x1 + altsum xr
I stumbled upon below solution, but I don't quite understand how it evaluates to the correct result since it only contain a minus symbol and not a plus symbol. Can someone please explain how below function altsum1 evaluates?
let rec altsum1 =
function
| [] -> 0
| x0 :: xr -> x0 - altsum1 xr
It works because -1 * -1 = 1. We can then use the distributive property to show that:
a - (b - c) = a - b + c
So:
altsum([1; 2; 3; 4]) =
1 - altsum([2; 3; 4]) =
1 - (2 - altsum([3; 4])) =
1 - (2 - (3 - altsum[4])) =
1 - (2 - (3 - 4)) =
1 - 2 + 3 - 4 =
-2

Ocaml nested if without else

Is it possible to have nested if without else statements. I wrote the following useless program to demonstrate nested ifs. How do I fix this so it's correct in terms of syntax. lines 5 and 6 gives errors.
let rec move_helper b sz r = match b with
[] -> r
|(h :: t) ->
if h = 0 then
if h - 1 = sz then h - 1 ::r
if h + 1 = sz then h + 1 ::r
else move_helper t sz r
;;
let move_pos b =
move_helper b 3 r
;;
let g = move_pos [0;8;7;6;5;4;3;2;1]
You can't have if without else unless the result of the expression is of type unit. This isn't the case for your code, so it's not possible.
Here's an example where the result is unit:
let f x =
if x land 1 <> 0 then print_string "1";
if x land 2 <> 0 then print_string "2";
if x land 4 <> 0 then print_string "4"
You must understand that if ... then is an expression like any other. If no else is present, it must be understood as if ... then ... else () and thus has type unit. To emphasize the fact that it is an expression, suppose you have two functions f and g of type, say, int → int. You can write
(if test then f else g) 1
You must also understand that x :: r does not change r at all, it constructs a new list putting x in front of r (the tail of this list is shared with the list r). In your case, the logic is not clear: what is the result when h=0 but the two if fail?
let rec move_helper b sz r = match b with
| [] -> r
| h :: t ->
if h = 0 then
if h - 1 = sz then (h - 1) :: r
else if h + 1 = sz then (h + 1) :: r
else (* What do you want to return here? *)
else move_helper t sz r
When you have a if, always put an else. Because when you don't put an else, Java will not know if the case is true or false.

reverse recursion in F#

(I am not sure about that whether reverse recursion is a correct name..)
I'd like to define a recursive function with initial value at k=n and terminate at k = 0
So I tried to write it in this way(here n = 10):
let rec f k =
match k with
|_ when k > 10 -> 0
| 10 -> 1
|_ -> (f n+1)-1
and the VS crashed. Could anyone help me?
dividing line------------------
I think my question is a little like this:
Consider a sequence with
a(10) = 1
a(k) = [a(k+1)*a(k+1)] + 1
a(k) = 0 if (k < 0 or k > 10)
How could I implement it in the F# ?
let rec f k =
match k with
|_ when k > 10 || k < 0 -> 0
| 10 -> 1
|_ -> f (k+1) * f (k+1) + 1

Haskell and Quadratics

I have to write a program to solve quadratics, returning a complex number result.
I've gotten so far, with defining a complex number, declaring it to be part of num, so +,- and * - ing can take place.
I've also defined a data type for a quadratic equation, but im now stuck with the actual solving of the quadratic. My math is quite poor, so any help would be greatly appreciated...
data Complex = C {
re :: Float,
im :: Float
} deriving Eq
-- Display complex numbers in the normal way
instance Show Complex where
show (C r i)
| i == 0 = show r
| r == 0 = show i++"i"
| r < 0 && i < 0 = show r ++ " - "++ show (C 0 (i*(-1)))
| r < 0 && i > 0 = show r ++ " + "++ show (C 0 i)
| r > 0 && i < 0 = show r ++ " - "++ show (C 0 (i*(-1)))
| r > 0 && i > 0 = show r ++ " + "++ show (C 0 i)
-- Define algebraic operations on complex numbers
instance Num Complex where
fromInteger n = C (fromInteger n) 0 -- tech reasons
(C a b) + (C x y) = C (a+x) (b+y)
(C a b) * (C x y) = C (a*x - b*y) (b*x + b*y)
negate (C a b) = C (-a) (-b)
instance Fractional Complex where
fromRational r = C (fromRational r) 0 -- tech reasons
recip (C a b) = C (a/((a^2)+(b^2))) (b/((a^2)+(b^2)))
root :: Complex -> Complex
root (C x y)
| y == 0 && x == 0 = C 0 0
| y == 0 && x > 0 = C (sqrt ( ( x + sqrt ( (x^2) + 0 ) ) / 2 ) ) 0
| otherwise = C (sqrt ( ( x + sqrt ( (x^2) + (y^2) ) ) / 2 ) ) ((y/(2*(sqrt ( ( x + sqrt ( (x^2) + (y^2) ) ) / 2 ) ) ) ) )
-- quadratic polynomial : a.x^2 + b.x + c
data Quad = Q {
aCoeff, bCoeff, cCoeff :: Complex
} deriving Eq
instance Show Quad where
show (Q a b c) = show a ++ "x^2 + " ++ show b ++ "x + " ++ show c
solve :: Quad -> (Complex, Complex)
solve (Q a b c) = STUCK!
EDIT: I seem to have missed out the whole point of using my own complex number datatype is to learn about custom datatypes. I'm well aware that i could use complex.data. Any help that could be given using my solution so far would be greatly appreciated.\
EDIT 2: It seems that my initial question was worded horribly. I'm aware that the quadratic formula will return both (or just the one) root to me. Where I am having trouble is returning these roots as a (complex, complex) tuple with the code above.
I'm well aware that I could use the built in quadratic functions as have been displayed below, but this is not the exercise. The idea behind the exercise, and creating ones own complex number data type, is to learn about custom data types.
Like newacct said, it's just the quadratic equation:
(-b +- sqrt(b^2 - 4ac)) / 2a
module QuadraticSolver where
import Data.Complex
data Quadratic a = Quadratic a a a deriving (Show, Eq)
roots :: (RealFloat a) => Quadratic a -> [ Complex a ]
roots (Quadratic a b c) =
if discriminant == 0
then [ numer / denom ]
else [ (numer + root_discriminant) / denom,
(numer - root_discriminant) / denom ]
where discriminant = (b*b - 4*a*c)
root_discriminant = if (discriminant < 0)
then 0 :+ (sqrt $ -discriminant)
else (sqrt discriminant) :+ 0
denom = 2*a :+ 0
numer = (negate b) :+ 0
in practice:
ghci> :l QuadraticSolver
Ok, modules loaded: QuadraticSolver.
ghci> roots (Quadratic 1 2 1)
[(-1.0) :+ 0.0]
ghci> roots (Quadratic 1 0 1)
[0.0 :+ 1.0,(-0.0) :+ (-1.0)]
And adapting to use your terms:
solve :: Quad -> (Complex, Complex)
solve (Q a b c) = ( sol (+), sol (-) )
where sol op = (op (negate b) $ root $ b*b - 4*a*c) / (2 * a)
Although I haven't tested that code
Since Haskell's sqrt can also handle complex numbers, rampion's solution can even be further simplified:
import Data.Complex
-- roots for quadratic equations with complex coefficients
croots :: (RealFloat a) =>
(Complex a) -> (Complex a) -> (Complex a) -> [Complex a]
croots a b c
| disc == 0 = [solution (+)]
| otherwise = [solution (+), solution (-)]
where disc = b*b - 4*a*c
solution plmi = plmi (-b) (sqrt disc) / (2*a)
-- roots for quadratic equations with real coefficients
roots :: (RealFloat a) => a -> a -> a -> [Complex a]
roots a b c = croots (a :+ 0) (b :+ 0) (c :+ 0)
You can also use this croots function with your own datatype, if you change the types to fit your implementation (and call your root function instead of sqrt).

Resources