# FW: [stack] More annoying quadratic...

## Manfred Von Thun — 2005-12-12 06:44:57

    ------ Forwarded Message

    From: Manfred Von Thun <m.vonthun@...>

    Date: Mon, 12 Dec 2005 17:37:15 +1100

    To: Manfred Von Thun <m.vonthun@...>

    Subject: Re: [stack] More annoying quadratic...

     On 12/12/05 5:14 PM, "Manfred Von Thun" <m.vonthun@...> wrote:

    >

    >

    >

    > On 7/12/05 2:06 PM, "William Tanksley, Jr" <wtanksleyjr@...> wrote:

    >

    >> [..]

    >>

    >> The challenge was to use a single-variable polynomial (18th order) to

    >> approximate a function simulating something in the real world. The

    >> equation was something like

    >>

    >> y = 1.5 * (1.2 - 0.03x^2 - 0.02x^4 - 0.0076x^6 ... );

    >>

    >> [..]

    I'm sorry about the extra >> which this eudora abomination put in

    specifically to annoy me. The text below is what I'm trying to send now

    >> I can¹t access our much loved Alpha under VMS any more, and I have

    >> not yet been fully transplanted onto the linux machine where I shall

    >> live in future. So I can¹t really test my ideas, but here we go:

    >>

    >> The idea of producing a list of coefficient/powers list and then

    >> taking the sum is obviously correct, at least as a first draft.

    >> (An optimisation might improve on this by using an accumulator

    >> to avoid creating the list in the first place).

    >>

    >> So my first idea was to use the same technique as for the quadratic formula:

    >> assume that the value of x is second on the stack, and on top is a list of

    >> quotations,  where each quotation computes a term of the polynomial:

    >>

    >>     x   [ ... [coefficient  power  foo] ...]   [i] map sum popd

    >>

    >> The [i] map should produce the list as for the quadratic, take the sum, and

    >> finally pop the 3 which is still sitting there, inspected but unchanged by

    >> each call of foo. We still need to define foo, which takes a power of

    >> x and multiplies by the coefficient.

    >>

    >> But then we have to write foo as many times as there are terms in the

    >> polynomial. Much nicer if we could define a different foo and write

    >> the details as a list of coefficient/power pairs, in the form [c p]

    >>

    >>     DEFINE  poly  ==  [foo] map sum popd

    >>

    >>  Then a call will look like this:

    >>

    >>     x  [ ... [c p] ... ]  poly

    >>

    >> for example

    >>

    >>     x  [[1.1 7] [2.2 18]]  poly

    >>

    >> should compute  y = 1.1 * x^7  + 2.2 * x^18

    >>

    >> We stiil have to define this foo function. Every time it is called by map

    >> it will find a  pair [c p] and below that the x which it can consume

    >> for itself but which will be restored by map for the next pair. So foo must

    >> put the two numbers in the pair on top of the stack, like this

    >>     x  c p

    >> and that can be done by either uncons uncons pop, or more simply

    >> by just the i combinator. Now swap the x and the c by the swapd

    >> operator:

    >>     c  x  p

    >> and take the p-th power of x with the power operator pow. Finally, to

    >> multiply with the coefficient, just use the * operator. That makes

    >>     foo  ==  i swapd pow *

    >> However, this is such an idiosyncratic operator that one would not want to

    >> define it at all, but just inline it into the definition of poly:

    >>

    >>    DEFINE  poly  == [i swapd pow *] map sum popd

    >>

    >> The intermediate list could probably be avoided by using the step

    >> combinator instead of the map combinator, and  use an accumulator

    >> during the stepping. But I suspect the extra dips that would be needed

    >> will cost as much as the construction of the  list.

    >>

    >> So, that is my design, untested unfortunately.

    >> I quite like the notation of a list of [c p] pairs, which is as close as

    >> one can get to the ordinary mathematical notation.

    >>

    >>   - Manfred

    >>

    >>

    >>

    >>

    >>

    >>  YAHOO! GROUPS LINKS

    >>

    >> *  Visit your group "concatenative

    >> <http://groups.yahoo.com/group/concatenative> " on the web.

    >> *

    >> *  To unsubscribe from this group, send an email to:

    >> *  concatenative-unsubscribe@yahoogroups.com

    >> <mailto:concatenative-unsubscribe@yahoogroups.com?subject=Unsubscribe>

    >> *

    >> *  Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service

    >> <http://docs.yahoo.com/info/terms/> .

    >>

    >>

    >>

    >>

    >

    ------ End of Forwarded Message