1. Minimum & Maximum

I'm in need of a function that accepts objects
containing reals and/or ints, and returns
minimums/maximums of each element such that:
if a={5.2,3.4,   7,  55,24} and
   b={  6,  2,12.7,15.9, 5}
then maximum(a,b) should return
     {6.2,3.4,12.7,  55,24}

Obviously, you cannot simply do:
if a<b then return a else return b end if
since that only does atoms.

So, my best solutions thus far, that are safe, are:
-------------------------
function minimum(object a, object b)
object z
integer len
   if atom(a) and atom(b) then
        if a<b then return a else return b end if
   else
        len = length(a)
        if len != length(b) then
          --print an error, the objects, and abort
          Error("Unmatched objects passed to minimum.",a,b)
        end if
        z = repeat(0,len)
        for i = 1 to len do
           z[i] = minimum(a,b)
        end for
   end if
   return z
end function
-----------------------------
with, naturally, function maximum being the opposite of above.

Certainly, there is an easier, faster, more sequence
all-at-once approach? (I'd swear I saw one in the listserv,
but I looked and didn't see it...)
I had thought of the find(1,{expression}) trick, but
I couldn't think of a way to actually make that work
like it would need to.

*hoping that today's brain-fart he's having doesn't offend*
--Hawke'

new topic     » topic index » view message » categorize

2. Re: Minimum & Maximum

function minimum(object a, object b)
    object c

    c = repeat(length(a), 0)
    for i = 1 to length(a) do
        if a[i] > b[i] then
            c[i] = b[i]
        else
            c[i] = a[i]
        end if
    end for
    return c
end function

I have no idea if this is faster (or works), but it seems less confusin,
and ain't recursive which cuts down on function call time.  Potential
prolems:  how fast is repeat?  and repeat also has a max, sumthin like
175,000.

Plus maximum just involves switchin the "<" to a ">", which is entirely
lazy enough to suit me. : )

snortboy

new topic     » goto parent     » topic index » view message » categorize

3. Re: Minimum & Maximum

Noah Smith wrote:
> function minimum(object a, object b)
snip
> end function

> I have no idea if this is faster (or works),
ummm... it doesn't handle nested sequences, sumfin i
have learned the very hard way, cuz ralf keeps
beating it into my head to make sure i write code
that fully enters all dimensions of a sequence. :)
(hush ralf, i'm kidding... well... okay...
so i'm not... ;)

>but it seems less confusin,
it is...

>and ain't recursive which cuts down on
>function call time.
actually, after the thread on abs, a few things
were gleaned from rob about recursion... that
EU is actually the only language i have come across
(as i think most of us) that can really handle
recursion.  jiri once mentioned an interview held
by the father of programming, and how he was going
to code a language that was kinder and gentler
to recursion... rob has gone far towards that...

>Potential prolems:  how fast is repeat?
repeat is faster than successive appending...
(for larger sequences or ones you think may get big)
extremely fast in fact... not that appending is
slow... it can be faster if you aren't doing
that much appending (like a few dozen) since
rob always allocates a wee bit extra at the tail
of a sequence.

>and repeat also has a max, sumthin like 175,000.
i hadn't noticed that, is that confirmable?
cuz that is one of those things that needs
documenting if it's true...


thanks for the time you spent and the help...
--Hawke'

new topic     » goto parent     » topic index » view message » categorize

4. Re: Minimum & Maximum

Hrmph.  Well, its not reproducable.  Although, both me and my friend
were hitting the same number on different computers at the time.  Repeat
seems to bottom out at limit of your RAM.  Distressingly, however, it
appears that Euphoria forgot to deallocate 126 megs of RAM...Anyone kno
how to undo that w/out restarting?

snortboy

new topic     » goto parent     » topic index » view message » categorize

5. Re: Minimum & Maximum

>function minimum(object a, object b)
>    object c
>
>    c = repeat(length(a), 0)

-- POSSIBLE CRASH 1: a is an atom!
-- Cannot tell the length of an atom

>    for i = 1 to length(a) do

-- POSSIBLE CRASH 2: a is an atom
-- Cannot tell the length of an atom

>        if a[i] > b[i] then

-- Possible CRASH 3: a is an atom
-- Cannot subscript atoms

-- Possible CRASH 4: a[i] is an sequence,
-- Cannot use sequences inside if's

>            c[i] = b[i]

-- Possible crash 5 & 6: b[i] is an sequence or b is an atom.


>        else
>            c[i] = a[i]

-- POssible crash 7 & 8: a[i[ is an sequence or a is an atom

>        end if
>    end for
>    return c
>end function

-- ANd what with sequences like htis ? { 3,4 , { 45, 12.343, {34,3}, {},
34}}
-- See why recursing is needed, emulated or just by calling your own
function again..

Ralf

new topic     » goto parent     » topic index » view message » categorize

6. Re: Minimum & Maximum

How's this..

function max(sequence a, sequence b)
        return a *(a>=b) + b *(b>a)
        end function


Yours Truly
Michael Palffy

michael at igrin.co.nz

----------
> I'm in need of a function that accepts objects
> containing reals and/or ints, and returns
> minimums/maximums of each element such that:
> if a={5.2,3.4,   7,  55,24} and
>    b={  6,  2,12.7,15.9, 5}
> then maximum(a,b) should return
>      {6.2,3.4,12.7,  55,24}
>

new topic     » goto parent     » topic index » view message » categorize

7. Re: Minimum & Maximum

On Thu, 24 Sep 1998, Noah Smith wrote:

> function minimum(object a, object b)
>     object c
>
>     c = repeat(length(a), 0)
>     for i = 1 to length(a) do
>         if a[i] > b[i] then
>             c[i] = b[i]
>         else
>             c[i] = a[i]
>         end if
>     end for
>     return c
> end function

function minimum(object a, object b)
    return a * (a<b) + b * (b<a)
end function

function maximum(object a, object b)
    return a * (a>b) + b * (b>a)
end function

I'm at it again :)

--
Carl R White
E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
Url......: http://www.bigfoot.com/~cyrek/
"Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr." - B.Q.Vgesa

new topic     » goto parent     » topic index » view message » categorize

8. Re: Minimum & Maximum

Michael Palffy wrote:

>How's this..
>
>function max(sequence a, sequence b)
>        return a *(a>=b) + b *(b>a)
>        end function

Now we are really cooking, echos of c.r.white...

First of all, just a minor thing, Mike, you probably want to change
the parameter type from a sequence to an object, to cover plain atoms
as well as sequences. So your min and max functions become

    function min(object a, object b)
        return a*(a<=b) + b*(b<a)
    end function

    function max(object a, object b)
        return a*(a>=b) + b*(b>a)
    end function


Nice and short, and it works! (I does not happen all that often on
this list blink)

But (there is almost always a 'but' lurking somewhere in everything!)
these function have no explicit type checking that all the previous
suggestions had. So it gives me a chance to ask a question I wanted to
ask anyway: do we really need those explicit checks? And I think, we
do not! The compiler itself will eventually tell us, anyway, if we try
something stupid, illegal. The error message may be a bit more cryptic
('an attempt to subscribe an atom', or whatever...), but we will not
get away with it. And these superfluous checks always slow things down,
sometimes considerably!


Now a treat for the benchmark freaks. I stripped my minimum function
down to

function minimum(object a, object b)
    if atom(a) then
        if b<a then a=b end if
    else
        for i=1 to length(a) do
            a[i]=minimum(a[i],b[i])
        end for
    end if
    return a
end function

to make it more directly comparable with Mike's.

The times are in seconds for 1,000,000 iterations on my 233 MHz
Pentium II, 64 Mb ram, under Win95, the same simple set as in my
previous post, tick_rate(1000):

                                           Mike's          Mine
================================================================
min(5,3)                                    1.14           0.91
min({5,3,1},{-5,2,6})                       6.69           6.89
min({5,3,{4,5},1},{-5,2,{-7,7},6})         11.82          13.27

Not much in it!  jiri

new topic     » goto parent     » topic index » view message » categorize

9. Re: Minimum & Maximum

Carl,

I think you have to include <= and >= comparisons respectively in your
functions, as it is done in Mike's. Your functions return zeros for equal
elements. jiri


-----Original Message-----
From: Carl R. White <C.R.White at SCM.BRAD.AC.UK>
To: EUPHORIA at cwisserver1.mcs.muohio.edu
<EUPHORIA at cwisserver1.mcs.muohio.edu>
Date: Friday, September 25, 1998 9:30 PM
Subject: Re: Minimum & Maximum


>On Thu, 24 Sep 1998, Noah Smith wrote:
>
>> function minimum(object a, object b)
>>     object c
>>
>>     c = repeat(length(a), 0)
>>     for i = 1 to length(a) do
>>         if a[i] > b[i] then
>>             c[i] = b[i]
>>         else
>>             c[i] = a[i]
>>         end if
>>     end for
>>     return c
>> end function
>
>function minimum(object a, object b)
>    return a * (a<b) + b * (b<a)
>end function
>
>function maximum(object a, object b)
>    return a * (a>b) + b * (b>a)
>end function
>
>I'm at it again :)
>
>--
>Carl R White
>E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
>Url......: http://www.bigfoot.com/~cyrek/
>"Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr." - B.Q.Vgesa
>

new topic     » goto parent     » topic index » view message » categorize

10. Re: Minimum & Maximum

On Fri, 25 Sep 1998, jiri babor wrote:

> I think you have to include <= and >= comparisons respectively in your
> functions, as it is done in Mike's. Your functions return zeros for equal
> elements. jiri

I make a mistake every now and again (coding on the fly and all that) :)

Your recursive solution seems to handle *some* type mismatches, but not
all. What if length(a) != length(b)?

> function minimum(object a, object b)
>     if atom(a) then
>         if b<a then a=b end if
>     else
>         for i=1 to length(a) do
>             a[i]=minimum(a[i],b[i])
>         end for
>     end if
>     return a
> end function

how about:?

function typeof(object x)
    if sequence(x) then
        return length(x)
    else
        return 0
        -- Integers may as well be considered atoms for this exercise.
    end if
end function

function minimum(object a, object b)
    integer tya, tyb
    tya = typeof(a)
    tyb = typeof(b)

    if tya = tyb then
        if tya = 0 then
            if b < a then a = b end if
        else
            for i = 1 to tya do
                a[i] = minimum(a[i], b[i])
            end for
        end if
    elsif tyb < tya then
        a = b -- assume atom is less than sequence
    end if

    return a
end function

For such a simple problem, this is quite a monster! :)

--
Carl R White
E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
Url......: http://www.bigfoot.com/~cyrek/
"Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr." - B.Q.Vgesa

new topic     » goto parent     » topic index » view message » categorize

11. Re: Minimum & Maximum

Carl just wrote:

>Your recursive solution seems to handle *some* type mismatches, but not
>all. What if length(a) != length(b)?


Bugger! I knew about that, but I thought it was too subtle for the rest of
you blink.
Serves me right! jiri

new topic     » goto parent     » topic index » view message » categorize

12. Re: Minimum & Maximum

jiri babor wrote:
> >Michael Palffy wrote:
> >How's this..
> >function max(sequence a, sequence b)
>Now we are really cooking, echos of c.r.white...
>Nice and short, and it works! (It does not happen
>all that often on this list blink)
very sweet :)
makes me wonder how you and carl (as per his post)
come up with these darned line... i racked my poor wittle
head trying to come up with that line :)


>But (there is almost always a 'but' lurking somewhere in everything!)
>these function have no explicit type checking that all the previous
>suggestions had.
to me, bad form... bad bad bad form...

>So it gives me a chance to ask a question I wanted toask anyway:
>do we really need those explicit checks? And I think, we
>do not!
actually, yes, in this case (and in perhaps other cases--see below)

>The compiler itself will eventually tell us, anyway, if we try
>something stupid, illegal. The error message may be a bit more cryptic
>('an attempt to subscribe an atom', or whatever...), but we will not
>get away with it.
you are correct here. the compiler will look at those things.
but, think of david and things he has mentioned about the
compiler's "error messages" and how it behaves when an error occurs.

it *DIES*. *BAM*.

the program aborts in a poor manner.  you lose your data, and
the possibility to see what values caused the problem.

and if it is a program distributed to the masses, you *CANNOT*
distribute with the registered version, and if that program
is over 300lines then the end user cannot give you any debug info.

so, i say, intercept before the crash, and my Error() routine
accepts not only a message to print, but the *data* that caused
the problem, and it prints the *full* value of that data,
which even ex.err does not capture (it truncates long sequences).

this data can then be saved to a file or printed by the
Error() routine. the end user is happy too, cuz their data
is preserved...

to me, leaving it to the compiler is just not an option any
longer, and that philosophy is also "bad form" in the first place.

>And these superfluous checks always slow things down,
>sometimes considerably!
what is the price to pay for safe data?
what is the price to pay to prevent crashing?
and
what is the priced to pay for end-user peace of mind?

> Now a treat for the benchmark freaks. I stripped my minimum function
> down to
> function minimum(object a, object b)
> to make it more directly comparable with Mike's.
                                      Mike's          Mine
==========================================================
> min(5,3)                             1.14           0.91
                won by ~80%
> min({5,3,1},{-5,2,6})                6.69           6.89
                call this tied
> min({5,3,{4,5},1},{-5,2,{-7,7},6})  11.82          13.27
                lost by ~10%
in most cases i would go with jiri's, as we generally use
min/max on lists of atoms...
but for this particular library, i need sequence speed...
very very close race tho, kudos to jiri,carl and mike.

thanks one and all--Hawke'

p.s.
1    if atom(a) then            --jne 4 (gotta do at least this)
2        if b<a then a=b end if --jmp 8 after 'end if'
3    else
4        for i=1 to length(a) do
5            a[i]=minimum(a[i],b[i])
6        end for
7    end if
8    return a
if you wanna speed yours up, by maybe enough to get you
back in the running with sequences... its a small
improvement, but it could save up to like 20-30
clockticks per loop...
(lets say it saves 10 ticks, over a million iterations,
thats 10 million clockticks... 25MHZ machine = halfsecond)
itsa trick from the days when computers were a bit
slower and you would notice these things
try:
1   if atom(a) then             --jne 4 (gotta do at least this)
2      if b<a then a=b end if   --
3      return a                 --no jmp here, we exit
4   else
5      for...endfor
6   end if
7   return a
what this does (i think) is save a jmp statement to the
"return a" after the "end if" at the end of the function...
rule is: get out while the getting is good...
try rerunning the benchmarks with/without this in place...
try with 10,000,000 iterations...
lemme know???

pps... i checked my borland turbo assembler documentation and
it says that a jmp can use between 3 and 45 ticks on a 486...
it depends (for 1) on how much code is between the if and the
end if... some long if/endif blocks might see real performance
boosts, at least the way i read it...

new topic     » goto parent     » topic index » view message » categorize

13. Re: Minimum & Maximum

Carl R. White wrote:
>I make a mistake every now and again
>(coding on the fly and all that) :)
tis a'ight :)

>Your recursive solution seems to handle *some* type
>mismatches, but not all. What if length(a) != length(b)?
ummmmm, i do not understand.
this is the original function posted by jiri:
---------------------
function Min(object a, object b)
    if atom(a) and atom(b) then
        if b<a then a=b end if
    elsif sequence(a) and sequence(b) then
        if length(a)=length(b) then        --LOOK HERE
            for i=1 to length(a) do a[i]=Min(a[i],b[i]) end for
        else
            puts(1,"Error: unequal sequence lengths!") abort(0)
        end if
    else
        puts(1,"Error: unmatched objects!") abort(0)
    end if
    return a
end function
-------------------(slight changes to shorten)
the line "if length(a)=length(b) then" will address
if len(a)!=len(b) as well, since the equality condition
will not be met if they are unequal in length. the
flow will shift to the else "error" line if lenA!=lenB,
so the condition that you are pointing out is already
caught...
so i ask, carl, what are you pointing out? i don't
understand, and i don't see.

> For such a simple problem, this is quite a monster! :)
yeah, no kidding, tis why i asked fer a wee bit'o'assitance...
my poor wittle head started hurtin'...
ever notice how it's *always* the simple problems that
are the least simplistic????
or is it just me? ;>   --Hawke'

new topic     » goto parent     » topic index » view message » categorize

14. Re: Minimum & Maximum

On Fri, 25 Sep 1998, Hawke wrote:

> > For such a simple problem, this is quite a monster! :)
> yeah, no kidding, tis why i asked fer a wee bit'o'assitance...
> my poor wittle head started hurtin'...
> ever notice how it's *always* the simple problems that
> are the least simplistic????
> or is it just me? ;>   --Hawke'

Sorree :)

I have a tendency to write code that is useful even out of the context
I've presented it in. For instance, the typeof() function could possibly
be used somewhere else.

I also have a habit of over-formatting code. Rather than:

if condition then statement end if

if condition then -- I'll do this :)
    statement
end if

I do what I do 'cause I'm always writing libraries (like the mathematical
ones in projects.zip). Stuck in my ways I guess.

Carl

--
Carl R White
E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
Url......: http://www.bigfoot.com/~cyrek/
"Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr." - B.Q.Vgesa

new topic     » goto parent     » topic index » view message » categorize

15. Re: Minimum & Maximum

Hawke wrote:

>try:
>1   if atom(a) then             --jne 4 (gotta do at least this)
>2      if b<a then a=b end if   --
>3      return a                 --no jmp here, we exit
>4   else
>5      for...endfor
>6   end if
>7   return a
>what this does (i think) is save a jmp statement to the
>"return a" after the "end if" at the end of the function...
>rule is: get out while the getting is good...
>try rerunning the benchmarks with/without this in place...
>try with 10,000,000 iterations...
>lemme know???


Ok, but one million only, after all it's 1.15 am. Good night. jiri


                                      Mike's      Mine   Modified
=================================================================
5,3                                    1.14       0.92       0.91
{5,3,1},{-5,2,6}                       6.69       6.90       6.83
{5,3,{4,5},1},{-5,2,{-7,7},6}         11.89      13.33      13.10

new topic     » goto parent     » topic index » view message » categorize

16. Re: Minimum & Maximum

jiri babor wrote:
> Ok, but one million only, after all it's 1.15 am. Good night. jiri
thankee :)
'nite
>                                       Mike's      Mine   Modified
> =================================================================
> 5,3                                    1.14       0.92       0.91
> {5,3,1},{-5,2,6}                       6.69       6.90       6.83
> {5,3,{4,5},1},{-5,2,{-7,7},6}         11.89      13.33      13.10
13.33 versus 13.10 may seem insignificant to some...
remember, this is a 233PII system...
.23 seconds over 1mil iterations on this system is
~55 clock ticks for that simple jmp in a short
if..endif statement... for a total of
~55MILLION saved clockticks.
.23seconds seems small... but this is a 233PII.
on a 486.66 it is almost a full second saved.
on a 486.25 that is over TWO SECONDS.
this is for only 1million iterations of a small sequence.
10million iterations gets you TWENTY SECONDS saved on a 486.25.
i know i'm sounding a trifle picky, but those are ticks
that could be used by windoze, it's slow enuff anyway,
and why write 'sloppy' code?
would you rather go thru a whole sequence to find sumfin,
or exit the loop once found?
the nice thing about getting out while the getting is
good, it costs you nothing in terms of readability,
maintainability, or safety, but gives potentially
large returns in speed.

and no, i'm not trying to beleager a point... :)
--Hawke'

new topic     » goto parent     » topic index » view message » categorize

17. Re: Minimum & Maximum

Thanks for spotting my *ahem* deliberate mistake Jiri.


Yours Truly
Michael Palffy

michael at igrin.co.nz
----------
> From: jiri babor <jbabor at PARADISE.NET.NZ>
> To: EUPHORIA at cwisserver1.mcs.muohio.edu
> Subject: Re: Minimum & Maximum
> Date: Friday, 25 September 1998 21:47
>
> Michael Palffy wrote:
>
> >How's this..
> >
> >function max(sequence a, sequence b)
> >        return a *(a>=b) + b *(b>a)
> >        end function
>
> Now we are really cooking, echos of c.r.white...
>
> First of all, just a minor thing, Mike, you probably want to change
> the parameter type from a sequence to an object, to cover plain atoms
> as well as sequences. So your min and max functions become
>
>     function min(object a, object b)
>         return a*(a<=b) + b*(b<a)
>     end function
>
>     function max(object a, object b)
>         return a*(a>=b) + b*(b>a)
>     end function
>

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu