[Community Puzzle] Derivative time ! - part2

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @pluieciel,validated by @CarlWalsh,@Zorg1 and @Yggdrasil4484.
If you have any issues, feel free to ping them.

LOL, I feel like having implemented Wolfram Alpha myself from scratch… :slight_smile:

While not necessary for this puzzle, it is an even more interesting and harder problem to being able to 'simplify" a formula, could be “part-3”. (Although it is hard to expect a unique test response as “simplification” can be done multiple ways).

I added many formula simplification rules, but still far from perfect:
E.g. having (sin f(x))^2 + (cos (f(x))^2 as input now I simplify the formula to 1, instead of a complicated evaluation or derivation.
This is already my longest solo puzzle solution code, with 15+ classes… :slight_smile:

2 Likes

Much easier to solve it numerically… I got it in 21 lines.

Yeah, but now I have a full, generic, symbolic, formula parser / evaluator / simplifier / differentiator solution. Might come in handy in the future :slight_smile: Plus it was an interesting challenge. (Well I am over 1000 lines, not 21 :slight_smile: ) Like a “SymPy for php” v0.1

1 Like

There’s a domain problem with this puzzle.
The first testcase is:

ln(ln(ln(ln x)))
x
x 2.72

but ln(ln(ln(ln x))) is not defined for 2.72.
Sure you can use the formula and then evaluate but it doesn’t mean anything.

Another faulty example to understand the problem:

ln x
x
x -1.00

If you don’t think about it and just apply the formula you get 1/x and then evaluate to 1/(-1.00) = -1.00.
Except that ln x is not defined in -1.00, thus not derivable in -1.00.

Yeah, I had to define ln(x)=ln(abs(x)) to get around that. But that is just because python eval couldn’t handle it. There is nothing wrong with taking logs of negative numbers, you just have to delve into the complex domain.

The problem with logarithm of negative numbers, is that the value is not unique.

Sure, but if you are trying to calculate the derivative for this problem, the imaginary part cancels out so the solution is real and unique.

But the function might not be continue here, thus, not derivable.

I draft this puzzle mainly for playing with parse and recursion, and I solve it quite the similar way as yours.
Just realized that there is SymPy, really a hack for this puzzle. Good to know. :wink:

I think you are right.
I didn’t really think about this issue when I draft the puzzle. Initially, I want the output to be a formula, but the format/non-unique answer is a big issue for validation. So, I use a random value for checking. It is only for validation of the result, I didn’t really think about the meanings.
I hope this would not impact the solution.

If you replace 2.72 by a value higher than e^e it should be fine. 15.16 for example.

modified, thanks for that !

Great, now you both can study why that wasn’t necessary :wink:

It is not the main point in this puzzle.
I will learn more in advanced mathematics, haha. not familiar with complex numbers.

Nicola and I are both math teachers, we’re well awared of complex numbers.
The puzzle is implicitly about real anaysis without any mention of complex functions, hence the domain problem.
Plus, ln is not de facto defined on C*, you can define complex logs but you usually define them on C minus a chosen ray with initial point 0 for continuity (thus derivability). The canonical choice is C minus R-, which ironically is the worst choice if you want a log derivable for negative real numbers.
So yes, there is was a real (no pun intended) problem and it was necessary to fix it, thanks to the author for that.
It didn’t prevent from solving the puzzle with symbolic calculus + evaluation or numeric analysis with the workaround you mentionned (using ln(abs(x)), but it was mathematically incorrect.

4 Likes