[Scala] Finding the intersect of functions

Ok,before someone reminds me that this is JavaGaming.org and not ScalaGaming.org Scala still runs on the JAVA virtual machine, so take that!

Lets say we have two anonymous functions (or lambdas) :

val f1 = (x:Int) => x-1
val f2 = (x:Int) => -x+1

How do we find the value of x that will result in the same output?

So the method signature would look somthing like this :

type Function = Int => Int
def intercept(a:Function, b:Function):Int = ????????

This is math problem.

Each function results in a straight line. Just find the intersection-point, if any.

{x->1}…can scala do exp rewritting?

Let’s say that you don’t know anything about these functions (linear, quadratic whatever). You can than do a numerical approximation, most of the time in an iterative fashion.
The most common method was invented by Newton.

So you would subtract one of the function from the over and then with the help of the above method approximated the intersection point.

How do you automatically get the derivate? (Or can scala replace types so you can use dual numbers?)

dunno, calculate the tangent yourself with a small sample size.

ps: you can probably write a solver macro to do all the hard work at compile time :smiley:

The example isn’t really clear…like is the field of integers only of interest? Wait scratch that…what do you really want to do?

This is what I meant sorry.
Ill look into this method.