discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Recursive Binary Chop

G
GregL
Sat, May 15, 2021 5:09 PM

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific area

  • an inverse function was created.
    Its arguments are the input value to be looked up
    lowest start value and its function
    highest start value and its function
    required accuracy

InvFF performs a binary chop until the upper and lower values are within the
required accuracy

//----------------------------------------------------------------------------------------
// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle 0 :
90
//-------------------------------------------------------------------------------
function FF(A) = sin(A);
//-------------------------------------------------------------------------------
function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with best
answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    // too
high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);  // too
low
//------------------------------------------------------------------------------
LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999

--
Sent from: http://forum.openscad.org/

I had a complicated function to calculate an area according to its one argument. The function was monotonic. There was then a need to find the argument value which gave a specific area - an inverse function was created. Its arguments are the input value to be looked up lowest start value and its function highest start value and its function required accuracy InvFF performs a binary chop until the upper and lower values are within the required accuracy //---------------------------------------------------------------------------------------- // test out with a simple inverse sin // sin(T) goes between 0 and 1 for angles 0 : 90 // if we give the recursive function a number 0 : 1, we want the angle 0 : 90 //------------------------------------------------------------------------------- function FF(A) = sin(A); //------------------------------------------------------------------------------- function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) = let(MidArg = (HighArg + LowArg)/2) abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy ? MidArg // Stop here with best answer : FF(MidArg) > Value ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // too high : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); // too low //------------------------------------------------------------------------------ LowestValue = 0; // for this test HighestValue = 90; // for this test // lookup the angle which gives a sine of 0.5 - ie 30 degrees echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, FF(HighestValue), 0.00001)); returns ECHO: 29.9999 -- Sent from: http://forum.openscad.org/
LM
Leonard Martin Struttmann
Sat, May 15, 2021 7:36 PM

Nice!

On Sat, May 15, 2021 at 12:10 PM GregL greg@leonardresearch.co.uk wrote:

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are within
the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle 0 :
90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with best
answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    // too
high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);  //
too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Nice! On Sat, May 15, 2021 at 12:10 PM GregL <greg@leonardresearch.co.uk> wrote: > I had a complicated function to calculate an area according to its one > argument. The function was monotonic. > There was then a need to find the argument value which gave a specific > area - an inverse function was created. > Its arguments are the input value to be looked up > lowest start value and its function > highest start value and its function > required accuracy > > InvFF performs a binary chop until the upper and lower values are within > the required accuracy > > > //---------------------------------------------------------------------------------------- > > // test out with a simple inverse sin > // sin(T) goes between 0 and 1 for angles 0 : 90 > // if we give the recursive function a number 0 : 1, we want the angle 0 : > 90 > //------------------------------------------------------------------------------- > > function FF(A) = sin(A); > //------------------------------------------------------------------------------- > > function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) > = let(MidArg = (HighArg + LowArg)/2) > abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy > ? MidArg // Stop here with best > answer > : FF(MidArg) > Value > ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // too > high > : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); // > too low > //------------------------------------------------------------------------------ > > LowestValue = 0; // for this test > HighestValue = 90; // for this test > // lookup the angle which gives a sine of 0.5 - ie 30 degrees > echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, > FF(HighestValue), 0.00001)); > > returns > ECHO: 29.9999 > > > ------------------------------ > Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> > at Nabble.com. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
NH
nop head
Sat, May 15, 2021 8:02 PM

Yes, nice, I have written one or two binary chops to do things like space
two pulleys for a closed belt, which surprisingly has no algebraic formula,
like the circumference of an ellipse. But it was before function literals,
so not generic.

On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann <
lenstruttmann@gmail.com> wrote:

Nice!

On Sat, May 15, 2021 at 12:10 PM GregL greg@leonardresearch.co.uk wrote:

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are within
the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle 0
: 90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with best
answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    //
too high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);  //
too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Yes, nice, I have written one or two binary chops to do things like space two pulleys for a closed belt, which surprisingly has no algebraic formula, like the circumference of an ellipse. But it was before function literals, so not generic. On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann < lenstruttmann@gmail.com> wrote: > Nice! > > On Sat, May 15, 2021 at 12:10 PM GregL <greg@leonardresearch.co.uk> wrote: > >> I had a complicated function to calculate an area according to its one >> argument. The function was monotonic. >> There was then a need to find the argument value which gave a specific >> area - an inverse function was created. >> Its arguments are the input value to be looked up >> lowest start value and its function >> highest start value and its function >> required accuracy >> >> InvFF performs a binary chop until the upper and lower values are within >> the required accuracy >> >> >> //---------------------------------------------------------------------------------------- >> >> // test out with a simple inverse sin >> // sin(T) goes between 0 and 1 for angles 0 : 90 >> // if we give the recursive function a number 0 : 1, we want the angle 0 >> : 90 >> //------------------------------------------------------------------------------- >> >> function FF(A) = sin(A); >> //------------------------------------------------------------------------------- >> >> function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) >> = let(MidArg = (HighArg + LowArg)/2) >> abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy >> ? MidArg // Stop here with best >> answer >> : FF(MidArg) > Value >> ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // >> too high >> : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); // >> too low >> //------------------------------------------------------------------------------ >> >> LowestValue = 0; // for this test >> HighestValue = 90; // for this test >> // lookup the angle which gives a sine of 0.5 - ie 30 degrees >> echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, >> FF(HighestValue), 0.00001)); >> >> returns >> ECHO: 29.9999 >> >> >> ------------------------------ >> Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> >> at Nabble.com. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
CA
craig and heather
Sat, May 15, 2021 8:51 PM

Could someone define what a binary chop is?

On Sat, May 15, 2021 at 2:04 PM nop head nop.head@gmail.com wrote:

Yes, nice, I have written one or two binary chops to do things like space
two pulleys for a closed belt, which surprisingly has no algebraic formula,
like the circumference of an ellipse. But it was before function literals,
so not generic.

On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann <
lenstruttmann@gmail.com> wrote:

Nice!

On Sat, May 15, 2021 at 12:10 PM GregL greg@leonardresearch.co.uk
wrote:

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are within
the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle 0
: 90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with best
answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    //
too high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);  //
too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

--
Craig Lindley / Heather Hubbard

New Recordings are here http://craigandheather.net/spellbound.html

Personal Website is here http://craigandheather.net

Please call the Rockrimmon house. Our cell phones don't work at home.
Rockrimmon House: (719) 426-9873
Craig's Cell: (719) 502-7925
Heather's Cell: (719) 571-0944

If you’re one in a million, there are now more than seven thousand people
exactly like you.

Could someone define what a binary chop is? On Sat, May 15, 2021 at 2:04 PM nop head <nop.head@gmail.com> wrote: > Yes, nice, I have written one or two binary chops to do things like space > two pulleys for a closed belt, which surprisingly has no algebraic formula, > like the circumference of an ellipse. But it was before function literals, > so not generic. > > On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann < > lenstruttmann@gmail.com> wrote: > >> Nice! >> >> On Sat, May 15, 2021 at 12:10 PM GregL <greg@leonardresearch.co.uk> >> wrote: >> >>> I had a complicated function to calculate an area according to its one >>> argument. The function was monotonic. >>> There was then a need to find the argument value which gave a specific >>> area - an inverse function was created. >>> Its arguments are the input value to be looked up >>> lowest start value and its function >>> highest start value and its function >>> required accuracy >>> >>> InvFF performs a binary chop until the upper and lower values are within >>> the required accuracy >>> >>> >>> //---------------------------------------------------------------------------------------- >>> >>> // test out with a simple inverse sin >>> // sin(T) goes between 0 and 1 for angles 0 : 90 >>> // if we give the recursive function a number 0 : 1, we want the angle 0 >>> : 90 >>> //------------------------------------------------------------------------------- >>> >>> function FF(A) = sin(A); >>> //------------------------------------------------------------------------------- >>> >>> function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) >>> = let(MidArg = (HighArg + LowArg)/2) >>> abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy >>> ? MidArg // Stop here with best >>> answer >>> : FF(MidArg) > Value >>> ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // >>> too high >>> : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); // >>> too low >>> //------------------------------------------------------------------------------ >>> >>> LowestValue = 0; // for this test >>> HighestValue = 90; // for this test >>> // lookup the angle which gives a sine of 0.5 - ie 30 degrees >>> echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, >>> FF(HighestValue), 0.00001)); >>> >>> returns >>> ECHO: 29.9999 >>> >>> >>> ------------------------------ >>> Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> >>> at Nabble.com. >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org > -- Craig Lindley / Heather Hubbard New Recordings are here <http://craigandheather.net/spellbound.html> Personal Website is here <http://craigandheather.net> Please call the Rockrimmon house. Our cell phones don't work at home. Rockrimmon House: (719) 426-9873 Craig's Cell: (719) 502-7925 Heather's Cell: (719) 571-0944 If you’re one in a million, there are now more than seven thousand people exactly like you.
NH
nop head
Sat, May 15, 2021 9:57 PM

You start with two extreme values that you assume to be either side of the
solution. Then you pick the midpoint and decide if the solution is between
the first point and the mid or the mid and the second, so you get a smaller
range for the solution and then recurse until the ends points are close
enough for your desired accuracy.

On Sat, 15 May 2021 at 21:51, craig and heather calhjh@gmail.com wrote:

Could someone define what a binary chop is?

On Sat, May 15, 2021 at 2:04 PM nop head nop.head@gmail.com wrote:

Yes, nice, I have written one or two binary chops to do things like space
two pulleys for a closed belt, which surprisingly has no algebraic formula,
like the circumference of an ellipse. But it was before function literals,
so not generic.

On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann <
lenstruttmann@gmail.com> wrote:

Nice!

On Sat, May 15, 2021 at 12:10 PM GregL greg@leonardresearch.co.uk
wrote:

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are
within the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle
0 : 90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with
best answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    //
too high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);  //
too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive
http://forum.openscad.org/ at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

--
Craig Lindley / Heather Hubbard

New Recordings are here http://craigandheather.net/spellbound.html

Personal Website is here http://craigandheather.net

Please call the Rockrimmon house. Our cell phones don't work at home.
Rockrimmon House: (719) 426-9873
Craig's Cell: (719) 502-7925
Heather's Cell: (719) 571-0944

If you’re one in a million, there are now more than seven thousand people
exactly like you.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

You start with two extreme values that you assume to be either side of the solution. Then you pick the midpoint and decide if the solution is between the first point and the mid or the mid and the second, so you get a smaller range for the solution and then recurse until the ends points are close enough for your desired accuracy. On Sat, 15 May 2021 at 21:51, craig and heather <calhjh@gmail.com> wrote: > Could someone define what a binary chop is? > > On Sat, May 15, 2021 at 2:04 PM nop head <nop.head@gmail.com> wrote: > >> Yes, nice, I have written one or two binary chops to do things like space >> two pulleys for a closed belt, which surprisingly has no algebraic formula, >> like the circumference of an ellipse. But it was before function literals, >> so not generic. >> >> On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann < >> lenstruttmann@gmail.com> wrote: >> >>> Nice! >>> >>> On Sat, May 15, 2021 at 12:10 PM GregL <greg@leonardresearch.co.uk> >>> wrote: >>> >>>> I had a complicated function to calculate an area according to its one >>>> argument. The function was monotonic. >>>> There was then a need to find the argument value which gave a specific >>>> area - an inverse function was created. >>>> Its arguments are the input value to be looked up >>>> lowest start value and its function >>>> highest start value and its function >>>> required accuracy >>>> >>>> InvFF performs a binary chop until the upper and lower values are >>>> within the required accuracy >>>> >>>> >>>> //---------------------------------------------------------------------------------------- >>>> >>>> // test out with a simple inverse sin >>>> // sin(T) goes between 0 and 1 for angles 0 : 90 >>>> // if we give the recursive function a number 0 : 1, we want the angle >>>> 0 : 90 >>>> //------------------------------------------------------------------------------- >>>> >>>> function FF(A) = sin(A); >>>> //------------------------------------------------------------------------------- >>>> >>>> function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) >>>> = let(MidArg = (HighArg + LowArg)/2) >>>> abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy >>>> ? MidArg // Stop here with >>>> best answer >>>> : FF(MidArg) > Value >>>> ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // >>>> too high >>>> : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); // >>>> too low >>>> //------------------------------------------------------------------------------ >>>> >>>> LowestValue = 0; // for this test >>>> HighestValue = 90; // for this test >>>> // lookup the angle which gives a sine of 0.5 - ie 30 degrees >>>> echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, >>>> FF(HighestValue), 0.00001)); >>>> >>>> returns >>>> ECHO: 29.9999 >>>> >>>> >>>> ------------------------------ >>>> Sent from the OpenSCAD mailing list archive >>>> <http://forum.openscad.org/> at Nabble.com. >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > > > -- > Craig Lindley / Heather Hubbard > > New Recordings are here <http://craigandheather.net/spellbound.html> > > Personal Website is here <http://craigandheather.net> > > Please call the Rockrimmon house. Our cell phones don't work at home. > Rockrimmon House: (719) 426-9873 > Craig's Cell: (719) 502-7925 > Heather's Cell: (719) 571-0944 > > If you’re one in a million, there are now more than seven thousand people > exactly like you. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
CA
craig and heather
Sat, May 15, 2021 10:12 PM

OK I've always called this a binary search. The term binary chop is new to
me. Thanks for enlightening me.

On Sat, May 15, 2021 at 3:58 PM nop head nop.head@gmail.com wrote:

You start with two extreme values that you assume to be either side of the
solution. Then you pick the midpoint and decide if the solution is between
the first point and the mid or the mid and the second, so you get a smaller
range for the solution and then recurse until the ends points are close
enough for your desired accuracy.

On Sat, 15 May 2021 at 21:51, craig and heather calhjh@gmail.com wrote:

Could someone define what a binary chop is?

On Sat, May 15, 2021 at 2:04 PM nop head nop.head@gmail.com wrote:

Yes, nice, I have written one or two binary chops to do things like
space two pulleys for a closed belt, which surprisingly has no
algebraic formula, like the circumference of an ellipse. But it was before
function literals, so not generic.

On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann <
lenstruttmann@gmail.com> wrote:

Nice!

On Sat, May 15, 2021 at 12:10 PM GregL greg@leonardresearch.co.uk
wrote:

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are
within the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle
0 : 90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with
best answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    //
too high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);
// too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive
http://forum.openscad.org/ at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

--
Craig Lindley / Heather Hubbard

New Recordings are here http://craigandheather.net/spellbound.html

Personal Website is here http://craigandheather.net

Please call the Rockrimmon house. Our cell phones don't work at home.
Rockrimmon House: (719) 426-9873
Craig's Cell: (719) 502-7925
Heather's Cell: (719) 571-0944

If you’re one in a million, there are now more than seven thousand people
exactly like you.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

--
Craig Lindley / Heather Hubbard

New Recordings are here http://craigandheather.net/spellbound.html

Personal Website is here http://craigandheather.net

Please call the Rockrimmon house. Our cell phones don't work at home.
Rockrimmon House: (719) 426-9873
Craig's Cell: (719) 502-7925
Heather's Cell: (719) 571-0944

If you’re one in a million, there are now more than seven thousand people
exactly like you.

OK I've always called this a binary search. The term binary chop is new to me. Thanks for enlightening me. On Sat, May 15, 2021 at 3:58 PM nop head <nop.head@gmail.com> wrote: > You start with two extreme values that you assume to be either side of the > solution. Then you pick the midpoint and decide if the solution is between > the first point and the mid or the mid and the second, so you get a smaller > range for the solution and then recurse until the ends points are close > enough for your desired accuracy. > > On Sat, 15 May 2021 at 21:51, craig and heather <calhjh@gmail.com> wrote: > >> Could someone define what a binary chop is? >> >> On Sat, May 15, 2021 at 2:04 PM nop head <nop.head@gmail.com> wrote: >> >>> Yes, nice, I have written one or two binary chops to do things like >>> space two pulleys for a closed belt, which surprisingly has no >>> algebraic formula, like the circumference of an ellipse. But it was before >>> function literals, so not generic. >>> >>> On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann < >>> lenstruttmann@gmail.com> wrote: >>> >>>> Nice! >>>> >>>> On Sat, May 15, 2021 at 12:10 PM GregL <greg@leonardresearch.co.uk> >>>> wrote: >>>> >>>>> I had a complicated function to calculate an area according to its one >>>>> argument. The function was monotonic. >>>>> There was then a need to find the argument value which gave a specific >>>>> area - an inverse function was created. >>>>> Its arguments are the input value to be looked up >>>>> lowest start value and its function >>>>> highest start value and its function >>>>> required accuracy >>>>> >>>>> InvFF performs a binary chop until the upper and lower values are >>>>> within the required accuracy >>>>> >>>>> >>>>> //---------------------------------------------------------------------------------------- >>>>> >>>>> // test out with a simple inverse sin >>>>> // sin(T) goes between 0 and 1 for angles 0 : 90 >>>>> // if we give the recursive function a number 0 : 1, we want the angle >>>>> 0 : 90 >>>>> //------------------------------------------------------------------------------- >>>>> >>>>> function FF(A) = sin(A); >>>>> //------------------------------------------------------------------------------- >>>>> >>>>> function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) >>>>> = let(MidArg = (HighArg + LowArg)/2) >>>>> abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy >>>>> ? MidArg // Stop here with >>>>> best answer >>>>> : FF(MidArg) > Value >>>>> ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // >>>>> too high >>>>> : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); >>>>> // too low >>>>> //------------------------------------------------------------------------------ >>>>> >>>>> LowestValue = 0; // for this test >>>>> HighestValue = 90; // for this test >>>>> // lookup the angle which gives a sine of 0.5 - ie 30 degrees >>>>> echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, >>>>> FF(HighestValue), 0.00001)); >>>>> >>>>> returns >>>>> ECHO: 29.9999 >>>>> >>>>> >>>>> ------------------------------ >>>>> Sent from the OpenSCAD mailing list archive >>>>> <http://forum.openscad.org/> at Nabble.com. >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>>> >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> >> >> -- >> Craig Lindley / Heather Hubbard >> >> New Recordings are here <http://craigandheather.net/spellbound.html> >> >> Personal Website is here <http://craigandheather.net> >> >> Please call the Rockrimmon house. Our cell phones don't work at home. >> Rockrimmon House: (719) 426-9873 >> Craig's Cell: (719) 502-7925 >> Heather's Cell: (719) 571-0944 >> >> If you’re one in a million, there are now more than seven thousand people >> exactly like you. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org > -- Craig Lindley / Heather Hubbard New Recordings are here <http://craigandheather.net/spellbound.html> Personal Website is here <http://craigandheather.net> Please call the Rockrimmon house. Our cell phones don't work at home. Rockrimmon House: (719) 426-9873 Craig's Cell: (719) 502-7925 Heather's Cell: (719) 571-0944 If you’re one in a million, there are now more than seven thousand people exactly like you.
A
adrianv
Sat, May 15, 2021 11:28 PM

I think the standard name for this algorithm in the context of root finding
is Bisection.  I've never heard "binary chop" before and "binary search" to
me refers to searching a sorted list, not root finding.

https://en.wikipedia.org/wiki/Bisection_method

The spacing for two pulleys seems like a problem that does have a closed
form solution.  I worked out math to make models like the one below with
varying radii at each end and separation and it was all simple trig.

http://forum.openscad.org/file/t2477/belt.png

CraigLindley wrote

OK I've always called this a binary search. The term binary chop is new to
me. Thanks for enlightening me.

On Sat, May 15, 2021 at 3:58 PM nop head <

nop.head@

> wrote:

You start with two extreme values that you assume to be either side of
the
solution. Then you pick the midpoint and decide if the solution is
between
the first point and the mid or the mid and the second, so you get a
smaller
range for the solution and then recurse until the ends points are close
enough for your desired accuracy.

On Sat, 15 May 2021 at 21:51, craig and heather <

calhjh@

> wrote:

Could someone define what a binary chop is?

On Sat, May 15, 2021 at 2:04 PM nop head <

nop.head@

> wrote:

Yes, nice, I have written one or two binary chops to do things like
space two pulleys for a closed belt, which surprisingly has no
algebraic formula, like the circumference of an ellipse. But it was
before
function literals, so not generic.

On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann <

lenstruttmann@

wrote:

Nice!

On Sat, May 15, 2021 at 12:10 PM GregL <

greg@.co

>

wrote:

I had a complicated function to calculate an area according to its
one
argument. The function was monotonic.
There was then a need to find the argument value which gave a
specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are
within the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the
angle
0 : 90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with
best answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)
//
too high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);
// too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive
<http://forum.openscad.org/> at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to

discuss-leave@.openscad


OpenSCAD mailing list
To unsubscribe send an email to

discuss-leave@.openscad


OpenSCAD mailing list
To unsubscribe send an email to

discuss-leave@.openscad

--
Craig Lindley / Heather Hubbard

New Recordings are here
<http://craigandheather.net/spellbound.html>

Personal Website is here <http://craigandheather.net>

Please call the Rockrimmon house. Our cell phones don't work at home.
Rockrimmon House: (719) 426-9873
Craig's Cell: (719) 502-7925
Heather's Cell: (719) 571-0944

If you’re one in a million, there are now more than seven thousand
people
exactly like you.


OpenSCAD mailing list
To unsubscribe send an email to

discuss-leave@.openscad


OpenSCAD mailing list
To unsubscribe send an email to

discuss-leave@.openscad

--
Craig Lindley / Heather Hubbard

New Recordings are here <http://craigandheather.net/spellbound.html>

Personal Website is here <http://craigandheather.net>

Please call the Rockrimmon house. Our cell phones don't work at home.
Rockrimmon House: (719) 426-9873
Craig's Cell: (719) 502-7925
Heather's Cell: (719) 571-0944

If you’re one in a million, there are now more than seven thousand people
exactly like you.


OpenSCAD mailing list
To unsubscribe send an email to

discuss-leave@.openscad

I think the standard name for this algorithm in the context of root finding is Bisection. I've never heard "binary chop" before and "binary search" to me refers to searching a sorted list, not root finding. https://en.wikipedia.org/wiki/Bisection_method The spacing for two pulleys seems like a problem that does have a closed form solution. I worked out math to make models like the one below with varying radii at each end and separation and it was all simple trig. <http://forum.openscad.org/file/t2477/belt.png> CraigLindley wrote > OK I've always called this a binary search. The term binary chop is new to > me. Thanks for enlightening me. > > On Sat, May 15, 2021 at 3:58 PM nop head &lt; > nop.head@ > &gt; wrote: > >> You start with two extreme values that you assume to be either side of >> the >> solution. Then you pick the midpoint and decide if the solution is >> between >> the first point and the mid or the mid and the second, so you get a >> smaller >> range for the solution and then recurse until the ends points are close >> enough for your desired accuracy. >> >> On Sat, 15 May 2021 at 21:51, craig and heather &lt; > calhjh@ > &gt; wrote: >> >>> Could someone define what a binary chop is? >>> >>> On Sat, May 15, 2021 at 2:04 PM nop head &lt; > nop.head@ > &gt; wrote: >>> >>>> Yes, nice, I have written one or two binary chops to do things like >>>> space two pulleys for a closed belt, which surprisingly has no >>>> algebraic formula, like the circumference of an ellipse. But it was >>>> before >>>> function literals, so not generic. >>>> >>>> On Sat, 15 May 2021 at 20:36, Leonard Martin Struttmann < >>>> > lenstruttmann@ >> wrote: >>>> >>>>> Nice! >>>>> >>>>> On Sat, May 15, 2021 at 12:10 PM GregL &lt; > greg@.co > &gt; >>>>> wrote: >>>>> >>>>>> I had a complicated function to calculate an area according to its >>>>>> one >>>>>> argument. The function was monotonic. >>>>>> There was then a need to find the argument value which gave a >>>>>> specific >>>>>> area - an inverse function was created. >>>>>> Its arguments are the input value to be looked up >>>>>> lowest start value and its function >>>>>> highest start value and its function >>>>>> required accuracy >>>>>> >>>>>> InvFF performs a binary chop until the upper and lower values are >>>>>> within the required accuracy >>>>>> >>>>>> >>>>>> //---------------------------------------------------------------------------------------- >>>>>> >>>>>> // test out with a simple inverse sin >>>>>> // sin(T) goes between 0 and 1 for angles 0 : 90 >>>>>> // if we give the recursive function a number 0 : 1, we want the >>>>>> angle >>>>>> 0 : 90 >>>>>> //------------------------------------------------------------------------------- >>>>>> >>>>>> function FF(A) = sin(A); >>>>>> //------------------------------------------------------------------------------- >>>>>> >>>>>> function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) >>>>>> = let(MidArg = (HighArg + LowArg)/2) >>>>>> abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy >>>>>> ? MidArg // Stop here with >>>>>> best answer >>>>>> : FF(MidArg) > Value >>>>>> ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) >>>>>> // >>>>>> too high >>>>>> : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); >>>>>> // too low >>>>>> //------------------------------------------------------------------------------ >>>>>> >>>>>> LowestValue = 0; // for this test >>>>>> HighestValue = 90; // for this test >>>>>> // lookup the angle which gives a sine of 0.5 - ie 30 degrees >>>>>> echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, >>>>>> FF(HighestValue), 0.00001)); >>>>>> >>>>>> returns >>>>>> ECHO: 29.9999 >>>>>> >>>>>> >>>>>> ------------------------------ >>>>>> Sent from the OpenSCAD mailing list archive >>>>>> &lt;http://forum.openscad.org/&gt; at Nabble.com. >>>>>> _______________________________________________ >>>>>> OpenSCAD mailing list >>>>>> To unsubscribe send an email to > discuss-leave@.openscad >>>>>> >>>>> _______________________________________________ >>>>> OpenSCAD mailing list >>>>> To unsubscribe send an email to > discuss-leave@.openscad >>>>> >>>> _______________________________________________ >>>> OpenSCAD mailing list >>>> To unsubscribe send an email to > discuss-leave@.openscad >>>> >>> >>> >>> -- >>> Craig Lindley / Heather Hubbard >>> >>> New Recordings are here >>> &lt;http://craigandheather.net/spellbound.html&gt; >>> >>> Personal Website is here &lt;http://craigandheather.net&gt; >>> >>> Please call the Rockrimmon house. Our cell phones don't work at home. >>> Rockrimmon House: (719) 426-9873 >>> Craig's Cell: (719) 502-7925 >>> Heather's Cell: (719) 571-0944 >>> >>> If you’re one in a million, there are now more than seven thousand >>> people >>> exactly like you. >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to > discuss-leave@.openscad >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to > discuss-leave@.openscad >> > > > -- > Craig Lindley / Heather Hubbard > > New Recordings are here &lt;http://craigandheather.net/spellbound.html&gt; > > Personal Website is here &lt;http://craigandheather.net&gt; > > Please call the Rockrimmon house. Our cell phones don't work at home. > Rockrimmon House: (719) 426-9873 > Craig's Cell: (719) 502-7925 > Heather's Cell: (719) 571-0944 > > If you’re one in a million, there are now more than seven thousand people > exactly like you. > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to > discuss-leave@.openscad -- Sent from: http://forum.openscad.org/
CH
Colin Hercus
Mon, May 17, 2021 1:53 PM

Binary chop can work for this but Newton Raphson should do it in less
iterations  https://en.wikipedia.org/wiki/Newton%27s_method

On Sun, May 16, 2021 at 1:09 AM GregL greg@leonardresearch.co.uk wrote:

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are within
the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle 0 :
90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with best
answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    // too
high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);  //
too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Binary chop can work for this but Newton Raphson should do it in less iterations https://en.wikipedia.org/wiki/Newton%27s_method On Sun, May 16, 2021 at 1:09 AM GregL <greg@leonardresearch.co.uk> wrote: > I had a complicated function to calculate an area according to its one > argument. The function was monotonic. > There was then a need to find the argument value which gave a specific > area - an inverse function was created. > Its arguments are the input value to be looked up > lowest start value and its function > highest start value and its function > required accuracy > > InvFF performs a binary chop until the upper and lower values are within > the required accuracy > > > //---------------------------------------------------------------------------------------- > > // test out with a simple inverse sin > // sin(T) goes between 0 and 1 for angles 0 : 90 > // if we give the recursive function a number 0 : 1, we want the angle 0 : > 90 > //------------------------------------------------------------------------------- > > function FF(A) = sin(A); > //------------------------------------------------------------------------------- > > function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) > = let(MidArg = (HighArg + LowArg)/2) > abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy > ? MidArg // Stop here with best > answer > : FF(MidArg) > Value > ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // too > high > : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); // > too low > //------------------------------------------------------------------------------ > > LowestValue = 0; // for this test > HighestValue = 90; // for this test > // lookup the angle which gives a sine of 0.5 - ie 30 degrees > echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, > FF(HighestValue), 0.00001)); > > returns > ECHO: 29.9999 > > > ------------------------------ > Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> > at Nabble.com. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
NH
nop head
Mon, May 17, 2021 2:27 PM

But you need the derivative of the function for Newton Raphson.

On Mon, 17 May 2021 at 14:54, Colin Hercus colinhercus@gmail.com wrote:

Binary chop can work for this but Newton Raphson should do it in less
iterations  https://en.wikipedia.org/wiki/Newton%27s_method

On Sun, May 16, 2021 at 1:09 AM GregL greg@leonardresearch.co.uk wrote:

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are within
the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle 0
: 90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with best
answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    //
too high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);  //
too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

But you need the derivative of the function for Newton Raphson. On Mon, 17 May 2021 at 14:54, Colin Hercus <colinhercus@gmail.com> wrote: > Binary chop can work for this but Newton Raphson should do it in less > iterations https://en.wikipedia.org/wiki/Newton%27s_method > > On Sun, May 16, 2021 at 1:09 AM GregL <greg@leonardresearch.co.uk> wrote: > >> I had a complicated function to calculate an area according to its one >> argument. The function was monotonic. >> There was then a need to find the argument value which gave a specific >> area - an inverse function was created. >> Its arguments are the input value to be looked up >> lowest start value and its function >> highest start value and its function >> required accuracy >> >> InvFF performs a binary chop until the upper and lower values are within >> the required accuracy >> >> >> //---------------------------------------------------------------------------------------- >> >> // test out with a simple inverse sin >> // sin(T) goes between 0 and 1 for angles 0 : 90 >> // if we give the recursive function a number 0 : 1, we want the angle 0 >> : 90 >> //------------------------------------------------------------------------------- >> >> function FF(A) = sin(A); >> //------------------------------------------------------------------------------- >> >> function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) >> = let(MidArg = (HighArg + LowArg)/2) >> abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy >> ? MidArg // Stop here with best >> answer >> : FF(MidArg) > Value >> ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // >> too high >> : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); // >> too low >> //------------------------------------------------------------------------------ >> >> LowestValue = 0; // for this test >> HighestValue = 90; // for this test >> // lookup the angle which gives a sine of 0.5 - ie 30 degrees >> echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, >> FF(HighestValue), 0.00001)); >> >> returns >> ECHO: 29.9999 >> >> >> ------------------------------ >> Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> >> at Nabble.com. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
CH
Colin Hercus
Mon, May 17, 2021 2:33 PM

You don't. You work out the slope from your two initial values.

On Mon, May 17, 2021 at 10:27 PM nop head nop.head@gmail.com wrote:

But you need the derivative of the function for Newton Raphson.

On Mon, 17 May 2021 at 14:54, Colin Hercus colinhercus@gmail.com wrote:

Binary chop can work for this but Newton Raphson should do it in less
iterations  https://en.wikipedia.org/wiki/Newton%27s_method

On Sun, May 16, 2021 at 1:09 AM GregL greg@leonardresearch.co.uk wrote:

I had a complicated function to calculate an area according to its one
argument. The function was monotonic.
There was then a need to find the argument value which gave a specific
area - an inverse function was created.
Its arguments are the input value to be looked up
lowest start value and its function
highest start value and its function
required accuracy

InvFF performs a binary chop until the upper and lower values are within
the required accuracy

//----------------------------------------------------------------------------------------

// test out with a simple inverse sin
// sin(T) goes between 0 and 1 for angles 0 : 90
// if we give the recursive function a number 0 : 1, we want the angle 0
: 90
//-------------------------------------------------------------------------------

function FF(A) = sin(A);
//-------------------------------------------------------------------------------

function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy)
=  let(MidArg = (HighArg + LowArg)/2)
abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy
?  MidArg                                    // Stop here with best
answer
:  FF(MidArg) > Value
? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy)    //
too high
: InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy);  //
too low
//------------------------------------------------------------------------------

LowestValue    =  0;      // for this test
HighestValue    =  90;    // for this test
// lookup the angle which gives a sine of 0.5 - ie 30 degrees
echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue,
FF(HighestValue), 0.00001));

returns
ECHO: 29.9999


Sent from the OpenSCAD mailing list archive http://forum.openscad.org/
at Nabble.com.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

You don't. You work out the slope from your two initial values. On Mon, May 17, 2021 at 10:27 PM nop head <nop.head@gmail.com> wrote: > But you need the derivative of the function for Newton Raphson. > > On Mon, 17 May 2021 at 14:54, Colin Hercus <colinhercus@gmail.com> wrote: > >> Binary chop can work for this but Newton Raphson should do it in less >> iterations https://en.wikipedia.org/wiki/Newton%27s_method >> >> On Sun, May 16, 2021 at 1:09 AM GregL <greg@leonardresearch.co.uk> wrote: >> >>> I had a complicated function to calculate an area according to its one >>> argument. The function was monotonic. >>> There was then a need to find the argument value which gave a specific >>> area - an inverse function was created. >>> Its arguments are the input value to be looked up >>> lowest start value and its function >>> highest start value and its function >>> required accuracy >>> >>> InvFF performs a binary chop until the upper and lower values are within >>> the required accuracy >>> >>> >>> //---------------------------------------------------------------------------------------- >>> >>> // test out with a simple inverse sin >>> // sin(T) goes between 0 and 1 for angles 0 : 90 >>> // if we give the recursive function a number 0 : 1, we want the angle 0 >>> : 90 >>> //------------------------------------------------------------------------------- >>> >>> function FF(A) = sin(A); >>> //------------------------------------------------------------------------------- >>> >>> function InvFF(Value,LowArg,LowValue,HighArg,HighValue,Accuracy) >>> = let(MidArg = (HighArg + LowArg)/2) >>> abs(HighValue - LowValue)/(HighValue + LowValue) < 2*Accuracy >>> ? MidArg // Stop here with best >>> answer >>> : FF(MidArg) > Value >>> ? InvFF(Value,LowArg,LowValue,MidArg,FF(MidArg),Accuracy) // >>> too high >>> : InvFF(Value,MidArg,FF(MidArg),HighArg,HighValue,Accuracy); // >>> too low >>> //------------------------------------------------------------------------------ >>> >>> LowestValue = 0; // for this test >>> HighestValue = 90; // for this test >>> // lookup the angle which gives a sine of 0.5 - ie 30 degrees >>> echo(InvFF(0.5, LowestValue, FF(LowestValue), HighestValue, >>> FF(HighestValue), 0.00001)); >>> >>> returns >>> ECHO: 29.9999 >>> >>> >>> ------------------------------ >>> Sent from the OpenSCAD mailing list archive <http://forum.openscad.org/> >>> at Nabble.com. >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >