Skip to content

Add findLocalMin#3559

Merged
DmitryOlshansky merged 2 commits intodlang:masterfrom
9il:patch-2
Apr 29, 2016
Merged

Add findLocalMin#3559
DmitryOlshansky merged 2 commits intodlang:masterfrom
9il:patch-2

Conversation

@9il
Copy link
Member

@9il 9il commented Aug 16, 2015

It is fixed version of R's optimise.

fBlocked by https://issues.dlang.org/show_bug.cgi?id=15204

@kyllingstad
Copy link
Contributor

Is this implementation based on R's source code? If so, it probably cannot be included in Phobos, as R is licensed under GPL.

@9il
Copy link
Member Author

9il commented Aug 16, 2015

@kyllingstad R's implementation is translation from Brent's book (source code is in book!). I have checked each line and rewrite algorithm from scratch. Both R and book's algorithms look identical and have the same bug (infinity loop), that was fixed in this PR.

@9il
Copy link
Member Author

9il commented Aug 16, 2015

... "look identical" includes cycle structures and variable names.

@DmitryOlshansky
Copy link
Member

This is cute) I think we had at least one Brent's algorithm in std.numeric ?

@9il
Copy link
Member Author

9il commented Aug 17, 2015

This is cute) I think we had at least one Brent's algorithm in std.numeric ?

std.numeric.findRoot is a modification of Brent's algorithm.

@kyllingstad
Copy link
Contributor

R's implementation is translation from Brent's book (source code is in book!).

Ok, then this should be fine. I just had to make sure. (D has had its share of licence issues in the past, and we don't want to get into that mess again.)

std/numeric.d Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think assert messages should describe the error, not the expected state. E.g., this could be:

assert(isFinite(ax) && isFinite(bx), "ax and/or bx are not finite");

But this is a matter of taste, I guess. I don't know what is the Phobos convention (if there is any) here. Also, this should be two asserts, so the user immediately knows which argument is bad.

@kyllingstad
Copy link
Contributor

Two general comments:

  • You should add tests for all floating-point types, not just double.
  • There are a bit too many empty lines in here for my taste, both inside the function and between some of the unittests. The whole thing could be a lot more compact without affecting readability.

@9il
Copy link
Member Author

9il commented Aug 17, 2015

You should add tests for all floating-point types, not just double.
There are a bit too many empty lines in here for my taste, both inside the function and between some of the unittests. The whole thing could be a lot more compact without affecting readability.

done

std/numeric.d Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment about assert messages holds for the other two as well.

@kyllingstad
Copy link
Contributor

Well, I don't have any further comments. I guess some of the more active Phobos devs should comment on any remaining style issues, and whether this functionality is general enough to warrant inclusion in Phobos. (I certainly think it is!)

@9il
Copy link
Member Author

9il commented Aug 18, 2015

Thanks!

On 18 Aug 2015, at 11:19, Lars T. Kyllingstad notifications@github.com wrote:

Well, I don't have any further comments. I guess some of the more active Phobos devs should comment on any remaining style issues, and whether this functionality is general enough to warrant inclusion in Phobos. (I certainly think it is!)


Reply to this email directly or view it on GitHub #3559 (comment).

@9il
Copy link
Member Author

9il commented Aug 27, 2015

Fails because magic in 32 bit release mode:

writeln("ttt ", [i, a, b, m, (a + b) / 2, (a + b) / 2.0, ], a <= m, m <= b);

produce:

ttt [8, -5.50533e+307, 4.49423e+307, -8.08875e+307, -8.08875e+307, -8.08875e+307]truetrue

O_O So, (-5.50533e+307 + 4.49423e+307) / 2.0 = -8.08875e+307 :-(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.

static assert(approxEqual((2*c - 3)^^2, 5.0);

? Probably total overkill, but I get nervous with these hexadecimal float constants obtained from external sources that basically have to be taken on faith.

@quickfur
Copy link
Member

Also, please squash commits, or at least put readable commit messages for them. :-)

@9il
Copy link
Member Author

9il commented Aug 28, 2015

Also, please squash commits, or at least put readable commit messages for them. :-)

Please read my last comment about 32 bit release mode bug. I have no idea what I can do with this.

@DmitryOlshansky
Copy link
Member

Oops, wrong button.

Fails because magic in 32 bit release mode:

File bug report, it looks like codegen bug. Have you tried LDC/GDC?

@9il
Copy link
Member Author

9il commented Sep 1, 2015

I will try with LDC and file it.

@9il 9il changed the title Add findLocalMin [blocked, wrong-code] Add findLocalMin Oct 15, 2015
@9il
Copy link
Member Author

9il commented Oct 15, 2015

OSX works fine both with LDC and DMD. But FreeBSD/Linux fails with DMD. ISSUE https://issues.dlang.org/show_bug.cgi?id=15204

@DmitryOlshansky
Copy link
Member

DmitryOlshansky commented Apr 19, 2016

FreeBSD32 passed recently

@9il 9il changed the title [blocked, wrong-code] Add findLocalMin Add findLocalMin Apr 19, 2016
@9il
Copy link
Member Author

9il commented Apr 19, 2016

Cleaned and rebased

@DmitryOlshansky
Copy link
Member

As this is a name addition so CC @andralex

std/numeric.d Outdated

/++
Find a real minimum of a real function $(D f(x)) via bracketing.
Given a function $(D f) and a range $(D (ax..bx)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The range notation does not exist in D or in math. Gotta choose one :o).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just follow findRoot naming: Given a function f and a range [a..b] ...

@andralex
Copy link
Member

I approve the name addition, please mind the nits before pulling. Thx!

License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
Authors: $(WEB erdani.org, Andrei Alexandrescu),
Don Clugston, Robert Jacques
Don Clugston, Robert Jacques, Ilya Yaroshenko
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3559 (findLocalMin), #2500 (sumOfLog2s), and findRoot fixes and optimizations: #3028, #2639, #2524, #2266

@9il 9il removed the @andralex label Apr 28, 2016
@9il
Copy link
Member Author

9il commented Apr 28, 2016

I didn't squash commit because the last contains style fixes for all std.numeric.
Ready to merge

@DmitryOlshansky
Copy link
Member

Auto-merge toggled on

@9il
Copy link
Member Author

9il commented Apr 29, 2016

just removed the link to wikipedia, because this refers to root-finding algorithm instead of optimization algorithm

@wilzbach
Copy link
Contributor

just removed the link to wikipedia, because this refers to root-finding algorithm instead of optimization algorithm

You are lucky that @braddr hasn't forbidden/fixed this yet braddr/d-tester#49 ;-)

@DmitryOlshansky DmitryOlshansky merged commit c110eac into dlang:master Apr 29, 2016
@9il 9il deleted the patch-2 branch April 29, 2016 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants